Sha256: 0bb3e5a199a85bba37c2d32150e5316f16015ed9584432497191c540f2812923

Contents?: true

Size: 1.19 KB

Versions: 3

Compression:

Stored size: 1.19 KB

Contents

require 'sass'

module SCSSLint
  class Linter::ShorthandLinter < Linter
    include LinterRegistry

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

      def description
        'Property values should use the shortest shorthand syntax allowed'
      end

    private

      def check_valid_shorthand_value(prop_node)
        if prop_node.value.is_a?(Sass::Script::String) &&
           prop_node.value.to_s.strip =~ /\A(\S+\s+\S+\s+\S+(\s+\S+)?)\Z/
          return create_lint(prop_node) unless valid_shorthand?($1)
        end
      end

      def valid_shorthand?(shorthand)
        values = shorthand.split(/\s+/)

        if values[0] == values[1] &&
           values[1] == values[2] &&
           values[2] == values[3]
          false
        elsif values[0] == values[2] && values[1] == values[3]
          false
        elsif values[0] == values[2] && values[3].nil?
          false
        elsif values[1] == values[3]
          false
        else
          true
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
scss-lint-0.4 lib/scss_lint/linter/shorthand_linter.rb
scss-lint-0.3 lib/scss_lint/linter/shorthand_linter.rb
scss-lint-0.2 lib/scss_lint/linter/shorthand_linter.rb