Sha256: 566421a4f9b9b7595f705495674dec9b851d0ab466e900321e6f034ccc80e24b

Contents?: true

Size: 1.06 KB

Versions: 17

Compression:

Stored size: 1.06 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)
      word = source_from_range(node.source_range)[/([a-z]+)/i, 1]
      add_color_lint(node, word) if color_keyword?(word)
    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)
      return if in_map?(node)

      hex_form = Sass::Script::Value::Color.new(color_keyword_to_code(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 `#{hex_form}`")
    end

    def in_map?(node)
      node_ancestor(node, 2).is_a?(Sass::Script::Tree::MapLiteral)
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
scss_lint-0.49.0 lib/scss_lint/linter/color_keyword.rb
scss_lint-0.48.0 lib/scss_lint/linter/color_keyword.rb
scss_lint-0.47.1 lib/scss_lint/linter/color_keyword.rb
scss_lint-0.47.0 lib/scss_lint/linter/color_keyword.rb
scss_lint-0.46.0 lib/scss_lint/linter/color_keyword.rb
scss_lint-0.45.0 lib/scss_lint/linter/color_keyword.rb
scss_lint-0.44.0 lib/scss_lint/linter/color_keyword.rb
scss_lint-0.43.2 lib/scss_lint/linter/color_keyword.rb
scss_lint-0.43.1 lib/scss_lint/linter/color_keyword.rb
scss_lint-0.43.0 lib/scss_lint/linter/color_keyword.rb
scss_lint-0.42.2 lib/scss_lint/linter/color_keyword.rb
scss_lint-0.42.1 lib/scss_lint/linter/color_keyword.rb
scss_lint-0.42.0 lib/scss_lint/linter/color_keyword.rb
scss_lint-0.41.0 lib/scss_lint/linter/color_keyword.rb
scss_lint-0.40.1 lib/scss_lint/linter/color_keyword.rb
scss_lint-0.40.0 lib/scss_lint/linter/color_keyword.rb
scss_lint-0.39.0 lib/scss_lint/linter/color_keyword.rb