Sha256: b8c1fa14487abe52fcc2e41d4c1ee6467a4c0e06e55ac672166d8411d6eff941

Contents?: true

Size: 1.09 KB

Versions: 10

Compression:

Stored size: 1.09 KB

Contents

module SCSSLint
  # Checks for uses of a color keyword instead of the preferred hexadecimal
  # form.
  class Linter::ColorKeyword < Linter
    include LinterRegistry

    def visit_script_color(node)
      add_color_lint(node, node.original_string) if color_keyword?(node.original_string)
    end

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

      remove_quoted_strings(node.value).scan(/(^|\s)([a-z]+)(?=\s|$)/i) do |_, word|
        add_color_lint(node, word) if color_keyword?(word)
      end
    end

  private

    def add_color_lint(node, original)
      hex_form = Sass::Script::Value::Color.new(color_rgb(original)).tap do |color|
        color.options = {} # `inspect` requires options to be set
      end.inspect

      add_lint(node,
               "Color `#{original}` should be written in hexadecimal form " <<
               "as `#{shortest_hex_form(hex_form)}`")
    end

    def color_keyword?(string)
      !!color_rgb(string) && string != 'transparent'
    end

    def color_rgb(string)
      Sass::Script::Value::Color::COLOR_NAMES[string]
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

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