Sha256: d9617d01e2f951b654543399d65cc1b7e1a84006830fbdf1609108e376fb3963

Contents?: true

Size: 863 Bytes

Versions: 51

Compression:

Stored size: 863 Bytes

Contents

module SCSSLint
  # Checks if hexadecimal colors are written lowercase / uppercase.
  class Linter::HexNotation < Linter
    include LinterRegistry

    HEX_REGEX = /(#(\h{3}|\h{6}))(?!\h)/

    def visit_script_color(node)
      return unless hex = source_from_range(node.source_range)[HEX_REGEX, 1]
      check_hex(hex, node)
    end

    def visit_script_string(node)
      return unless node.type == :identifier

      node.value.scan(HEX_REGEX) do |match|
        check_hex(match.first, node)
      end
    end

  private

    def check_hex(hex, node)
      return if expected(hex) == hex

      add_lint(node, "Color `#{hex}` should be written as `#{expected(hex)}`")
    end

    def expected(color)
      return color.downcase if lowercase_style?
      color.upcase
    end

    def lowercase_style?
      config['style'] == 'lowercase'
    end
  end
end

Version data entries

51 entries across 51 versions & 3 rubygems

Version Path
scss_lint-0.57.1 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.57.0 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.56.0 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.55.0 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.54.0 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.53.0 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.52.0 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.51.0 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.50.3 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.50.2 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.50.1 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.50.0 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.49.0 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.48.0 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.47.1 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.47.0 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.46.0 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.45.0 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.44.0 lib/scss_lint/linter/hex_notation.rb
scss_lint-0.43.2 lib/scss_lint/linter/hex_notation.rb