class SCSSLint::Linter::HexLength

Checks that hexadecimal colors are written in the desired number of characters.

Constants

HEX_REGEX

Public Instance Methods

visit_script_color(node) click to toggle source
# File lib/scss_lint/linter/hex_length.rb, line 9
def visit_script_color(node)
  return unless hex = source_from_range(node.source_range)[HEX_REGEX, 1]
  check_hex(hex, node)
end
visit_script_string(node) click to toggle source
# File lib/scss_lint/linter/hex_length.rb, line 14
def visit_script_string(node)
  return unless node.type == :identifier

  node.value.scan(HEX_REGEX) do |match|
    check_hex(match.first, node)
  end
end

Private Instance Methods

can_be_shorter?(hex) click to toggle source
# File lib/scss_lint/linter/hex_length.rb, line 37
def can_be_shorter?(hex)
  hex.length == 7 &&
    hex[1] == hex[2] &&
    hex[3] == hex[4] &&
    hex[5] == hex[6]
end
check_hex(hex, node) click to toggle source
# File lib/scss_lint/linter/hex_length.rb, line 24
def check_hex(hex, node)
  return if expected(hex) == hex

  add_lint(node, "Color `#{hex}` should be written as `#{expected(hex)}`")
end
expected(hex) click to toggle source
# File lib/scss_lint/linter/hex_length.rb, line 30
def expected(hex)
  return short_hex_form(hex) if can_be_shorter?(hex) && short_style?
  return long_hex_form(hex) if hex.length == 4 && !short_style?

  hex
end
long_hex_form(hex) click to toggle source
# File lib/scss_lint/linter/hex_length.rb, line 48
def long_hex_form(hex)
  [hex[0..1], hex[1], hex[2], hex[2], hex[3], hex[3]].join
end
short_hex_form(hex) click to toggle source
# File lib/scss_lint/linter/hex_length.rb, line 44
def short_hex_form(hex)
  [hex[0..1], hex[3], hex[5]].join
end
short_style?() click to toggle source
# File lib/scss_lint/linter/hex_length.rb, line 52
def short_style?
  config['style'] == 'short'
end