Sha256: f2a6ab6b405030b64307fe6abd089ed4b2f1375094197a32694819d0e9e7ab06

Contents?: true

Size: 863 Bytes

Versions: 10

Compression:

Stored size: 863 Bytes

Contents

module SCSSLint
  # Checks for hexadecimal colors that can be shortened.
  class Linter::HexFormat < Linter
    include LinterRegistry

    def visit_script_color(node)
      return unless node.original_string && node.original_string.match(HEX_REGEX)

      unless valid_hex_format?(node.original_string[HEX_REGEX, 1])
        add_hex_lint(node, node.original_string)
      end
    end

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

      node.value.scan(HEX_REGEX) do |match|
        add_hex_lint(node, match.first) unless valid_hex_format?(match.first)
      end
    end

  private

    HEX_REGEX = /(#\h{3,6})/

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

    def valid_hex_format?(hex)
      hex == shortest_hex_form(hex)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
scss-lint-0.19.0 lib/scss_lint/linter/hex_format.rb
scss-lint-0.18.0 lib/scss_lint/linter/hex_format.rb
scss-lint-0.17.3 lib/scss_lint/linter/hex_format.rb
scss-lint-0.17.2 lib/scss_lint/linter/hex_format.rb
scss-lint-0.17.1 lib/scss_lint/linter/hex_format.rb
scss-lint-0.17.0 lib/scss_lint/linter/hex_format.rb
scss-lint-0.16.1 lib/scss_lint/linter/hex_format.rb
scss-lint-0.16.0 lib/scss_lint/linter/hex_format.rb
scss-lint-0.15.0 lib/scss_lint/linter/hex_format.rb
scss-lint-0.14.0 lib/scss_lint/linter/hex_format.rb