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+){0,2})\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[1] && values[2].nil? && values[3].nil?
          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

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+){0,2})\Z/
          return create_lint(prop_node) unless valid_shorthand?($1)
        end
      end

      def valid_shorthand?(shorthand)
        values = shorthand.split(/\s+/)
        top, right, bottom, left = values

        if top == right && right == bottom && bottom == left
          false
        elsif top == right && bottom.nil? && left.nil?
          false
        elsif top == bottom && right == left
          false
        elsif top == bottom && left.nil?
          false
        elsif right == left
          false
        else
          true
        end
      end
    end
  end
end

Improve clarity of shorthand linter through naming
Improve clarity of shorthand linter through naming

This commit improves the clarity of the shorthand linter by assigning
the values of the array that contains a property's shorthand values to
named variables that correspond to their placement (top, right, bottom,
and left).

Related Story:
  http://www.pivotaltracker.com/story/show/43148129
  ("Teach scss-lint about missed shorthand case")

Change-Id: Idc8833d36f2857eb56f7bada40674d5ba88ead14
Reviewed-on: https://gerrit.causes.com/17754
Reviewed-by: Lann Martin <564fa7a717c51b612962ea128f146981a0e99d90@causes.com>
Tested-by: Joe Lencioni <1dd72cf724deaab5db236f616560fc7067b734e1@causes.com>
