Sha256: 41648b516ec4f39b187b574c92f80a7162e468f9f70938eba124b1beba287c82

Contents?: true

Size: 1.15 KB

Versions: 14

Compression:

Stored size: 1.15 KB

Contents

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

    COLOR_REGEX = /(#?[a-f0-9]{3,6}|[a-z]+)/i

    def visit_script_color(node)
      color = source_from_range(node.source_range)[COLOR_REGEX, 1]
      add_color_lint(node, color) if color_keyword?(color)
    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 `#{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

14 entries across 14 versions & 1 rubygems

Version Path
scss-lint-0.33.0 lib/scss_lint/linter/color_keyword.rb
scss-lint-0.32.0 lib/scss_lint/linter/color_keyword.rb
scss-lint-0.31.0 lib/scss_lint/linter/color_keyword.rb
scss-lint-0.30.0 lib/scss_lint/linter/color_keyword.rb
scss-lint-0.29.0 lib/scss_lint/linter/color_keyword.rb
scss-lint-0.28.0 lib/scss_lint/linter/color_keyword.rb
scss-lint-0.27.0 lib/scss_lint/linter/color_keyword.rb
scss-lint-0.26.2 lib/scss_lint/linter/color_keyword.rb
scss-lint-0.26.1 lib/scss_lint/linter/color_keyword.rb
scss-lint-0.26.0 lib/scss_lint/linter/color_keyword.rb
scss-lint-0.25.1 lib/scss_lint/linter/color_keyword.rb
scss-lint-0.25.0 lib/scss_lint/linter/color_keyword.rb
scss-lint-0.24.1 lib/scss_lint/linter/color_keyword.rb
scss-lint-0.24.0 lib/scss_lint/linter/color_keyword.rb