Sha256: 7a8ce0074841424232d00c1f0e6134d43b9cf1b558ba5a4ba8e2a0f4d838b075

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module SCSSLint
  class Linter::HexFormat < Linter
    include LinterRegistry

    def visit_prop(node)
      if node.value.is_a?(Sass::Script::String) &&
         node.value.type == :identifier

        node.value.value.scan(HEX_REGEX) do |match|
          add_hex_lint(node, match.first) unless valid_hex_format?(match.first)
        end
      end

      yield # Continue visiting children
    end

    def visit_script_color(node)
      unless valid_hex_format?(node.original[HEX_REGEX, 1])
        add_hex_lint(node, node.original)
      end
    end

  private

    HEX_REGEX = /(#\h{3,6})/

    def add_hex_lint(node, hex)
      add_lint(node, "Color `#{hex}` should be written as `#{shortest_form(hex)}`")
    end

    def valid_hex_format?(hex)
      hex == shortest_form(hex)
    end

    def shortest_form(hex)
      (can_be_condensed?(hex) ? (hex[0..1] + hex[3] + hex[5]) : hex).downcase
    end

    def can_be_condensed?(hex)
      hex.length == 7 &&
        hex[1] == hex[2] &&
        hex[3] == hex[4] &&
        hex[5] == hex[6]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scss-lint-0.10.0 lib/scss_lint/linter/hex_format.rb