lib/scss_lint/linter/zero_unit.rb in scss-lint-0.23.1 vs lib/scss_lint/linter/zero_unit.rb in scss-lint-0.24.0
- old
+ new
@@ -5,33 +5,35 @@
def visit_script_string(node)
return unless node.type == :identifier
node.value.scan(ZERO_UNIT_REGEX) do |match|
+ next unless zero_with_length_units?(match.first)
add_lint(node, MESSAGE_FORMAT % match.first)
end
end
def visit_script_number(node)
length = source_from_range(node.source_range)[ZERO_UNIT_REGEX, 1]
+ return unless zero_with_length_units?(length)
- if node.value == 0 && zero_with_units?(length)
- add_lint(node, MESSAGE_FORMAT % length)
- end
+ add_lint(node, MESSAGE_FORMAT % length)
end
private
- ZERO_UNIT_REGEX = %r{
+ ZERO_UNIT_REGEX = /
\b
- (?<!\.|\#) # Ignore zeroes following `#` or `.` (colors / decimals)
- (0[a-z]+) # Zero followed by letters (indicating some sort of unit)
+ (?<!\.|\#) # Ignore zeroes following `#` (colors) or `.` (decimals)
+ (0[a-z]+) # Zero followed by letters indicating some sort of unit
\b
- }ix
+ /ix
MESSAGE_FORMAT = '`%s` should be written without units as `0`'
- def zero_with_units?(string)
- string =~ /^0[a-z]+/
+ LENGTH_UNITS = %w[em ex ch rem vw vh vmin vmax cm mm in pt pc px].to_set
+
+ def zero_with_length_units?(string)
+ string =~ /^0([a-z]+)/ && LENGTH_UNITS.include?(Regexp.last_match(1))
end
end
end