lib/scss_lint/linter/shorthand.rb in scss_lint-0.49.0 vs lib/scss_lint/linter/shorthand.rb in scss_lint-0.50.0
- old
+ new
@@ -3,15 +3,27 @@
# Checks for the use of the shortest form for properties that can be written
# in shorthand.
class Linter::Shorthand < Linter
include LinterRegistry
+ def visit_root(*)
+ @shorthands_forbidden = @config['allowed_shorthands'] == []
+ yield # Continue linting children
+ end
+
# @param node [Sass::Tree::Node]
def visit_prop(node)
property_name = node.name.join
return unless SHORTHANDABLE_PROPERTIES.include?(property_name)
+ if @shorthands_forbidden
+ add_lint(node, "The `#{property_name}` shorthand property is " \
+ 'forbidden since the `allowed_shorthands` option ' \
+ 'is set to an empty list.')
+
+ end
+
case node.value
when Sass::Script::Tree::Literal
check_script_literal(property_name, node.value)
when Sass::Script::Tree::ListLiteral
check_script_list(property_name, node.value)
@@ -66,10 +78,12 @@
# @param prop [String]
# @param node [Sass::Script::Value::String]
# @param values [Array<String>]
def check_shorthand(prop, node, values)
+ values = shorthand_values(values)
+
add_lint(node, "Shorthands of length `#{values.count}` are not allowed. " \
"Value was `#{values.join(' ')}`") unless allowed?(values.count)
return unless (2..4).member?(values.count)
@@ -135,8 +149,12 @@
# @param size [Number]
# @return [Boolean]
def allowed?(size)
return false unless config['allowed_shorthands']
config['allowed_shorthands'].map(&:to_i).include?(size)
+ end
+
+ def shorthand_values(values)
+ values.take(4).take_while { |value| !value.to_s.start_with?('!') }
end
end
end