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)
      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

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

    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

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)
      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

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

    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

Fix Rubocop lints in ColorKeyword linter
Fix Rubocop lints in ColorKeyword linter

Change-Id: I9015955323b37eb087829e01c45c80d8c8c6bb7e
Reviewed-on: http://gerrit.causes.com/36709
Tested-by: jenkins <d95b56ce41a2e1ac4cecdd398defd7414407cc08@causes.com>
Reviewed-by: Shane da Silva <6f6e68d1df92f30cb4b3ce35260ddf94a402f33d@causes.com>
