Sha256: 0156083750fbe266e220e982b03fac78fbaa1e2c57e78014a37d9f316c248dd0

Contents?: true

Size: 1 KB

Versions: 8

Compression:

Stored size: 1 KB

Contents

require 'sass'

module SCSSLint
  class Linter::HexLinter < Linter
    include LinterRegistry

    class << self
      def run(engine)
        lints = []
        engine.tree.each do |node|
          if node.is_a?(Sass::Tree::PropNode)
            lints << check_valid_hex_value(node)
          end
        end
        lints.compact
      end

      def description
        'Hexadecimal color codes should be lowercase and in 3-digit form where possible'
      end

    private

      def check_valid_hex_value(prop_node)
        if prop_node.value.is_a?(Sass::Script::String) &&
           prop_node.value.to_s =~ /#(\h{3,6})/
          return create_lint(prop_node) unless valid_hex?($1)
        end
      end

      def valid_hex?(hex)
        [3,6].include?(hex.length) &&
          hex.downcase == hex &&
          !can_be_condensed(hex)
      end

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
scss-lint-0.6 lib/scss_lint/linter/hex_linter.rb
scss-lint-0.5.2 lib/scss_lint/linter/hex_linter.rb
scss-lint-0.5.1 lib/scss_lint/linter/hex_linter.rb
scss-lint-0.5 lib/scss_lint/linter/hex_linter.rb
scss-lint-0.4 lib/scss_lint/linter/hex_linter.rb
scss-lint-0.3 lib/scss_lint/linter/hex_linter.rb
scss-lint-0.2 lib/scss_lint/linter/hex_linter.rb
scss-lint-0.1 lib/scss_lint/linter/hex_linter.rb