lib/scss_lint/linter/leading_zero.rb in scss-lint-0.19.0 vs lib/scss_lint/linter/leading_zero.rb in scss-lint-0.20.0

- old
+ new

@@ -7,27 +7,46 @@ return unless node.type == :identifier non_string_values = remove_quoted_strings(node.value).split non_string_values.each do |value| - if number = value[/\b(0\.\d+)/, 1] - add_leading_zero_lint(node, number) + if number = value[FRACTIONAL_DIGIT_REGEX, 1] + check_number(node, number) end end end def visit_script_number(node) - if node.original_string =~ /^0\./ - add_leading_zero_lint(node, node.original_string) - end + return unless number = source_from_range(node.source_range)[FRACTIONAL_DIGIT_REGEX, 1] + + check_number(node, number) end private - def add_leading_zero_lint(node, number) - trimmed_number = number[/^[^\.]+(.*)$/, 1] + FRACTIONAL_DIGIT_REGEX = /^-?(0?\.\d+)/ - add_lint(node, "`#{number}` should be written without a leading zero " << - "as `#{trimmed_number}`") + CONVENTIONS = { + 'exclude_zero' => { + explanation: '`%s` should be written without a leading zero as `%s`', + validator: ->(original) { original =~ /^\.\d+$/ }, + converter: ->(original) { original[1..-1] }, + }, + 'include_zero' => { + explanation: '`%s` should be written with a leading zero as `%s`', + validator: ->(original) { original =~ /^0\.\d+$/ }, + converter: ->(original) { "0#{original}" } + }, + } + + def check_number(node, original_number) + style = config.fetch('style', 'exclude_zero') + convention = CONVENTIONS[style] + + unless convention[:validator].call(original_number) + corrected = convention[:converter].call(original_number) + + add_lint(node, convention[:explanation] % [original_number, corrected]) + end end end end