Sha256: 12c718784d06338ca0828ca69ccecc2d6e1811cae8af67927657addb409408e9

Contents?: true

Size: 816 Bytes

Versions: 8

Compression:

Stored size: 816 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 color = source_from_range(node.source_range)[HEX_REGEX, 1]

      unless valid_hex_format?(color)
        add_hex_lint(node, color)
      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

8 entries across 8 versions & 1 rubygems

Version Path
scss-lint-0.23.1 lib/scss_lint/linter/hex_format.rb
scss-lint-0.23.0 lib/scss_lint/linter/hex_format.rb
scss-lint-0.22.0 lib/scss_lint/linter/hex_format.rb
scss-lint-0.21.0 lib/scss_lint/linter/hex_format.rb
scss-lint-0.20.3 lib/scss_lint/linter/hex_format.rb
scss-lint-0.20.2 lib/scss_lint/linter/hex_format.rb
scss-lint-0.20.1 lib/scss_lint/linter/hex_format.rb
scss-lint-0.20.0 lib/scss_lint/linter/hex_format.rb