Minor refactors in Style/LargeNumbers rule

This commit is contained in:
Sijawusz Pur Rahnama 2022-11-28 03:19:13 +01:00
parent 134963ece7
commit b79f3e6e07

View file

@ -39,11 +39,12 @@ module Ameba::Rule::Style
Tokenizer.new(source).run do |token| Tokenizer.new(source).run do |token|
next unless token.type.number? && decimal?(token.raw) next unless token.type.number? && decimal?(token.raw)
parsed = parse_number token.raw parsed = parse_number(token.raw)
if allowed?(*parsed) && (expected = underscored *parsed) != token.raw if allowed?(*parsed) && (expected = underscored *parsed) != token.raw
location = token.location location = token.location
end_location = location.adjust(column_number: token.raw.size - 1) end_location = location.adjust(column_number: token.raw.size - 1)
issue_for location, end_location, MSG % expected do |corrector| issue_for location, end_location, MSG % expected do |corrector|
corrector.replace(location, end_location, expected) corrector.replace(location, end_location, expected)
end end
@ -58,21 +59,21 @@ module Ameba::Rule::Style
private def allowed?(_sign, value, fraction, _suffix) private def allowed?(_sign, value, fraction, _suffix)
return true if fraction && fraction.size > 3 return true if fraction && fraction.size > 3
digits = value.chars.select &.to_s.=~ /[0-9]/ digits = value.chars.select(&.number?)
digits.size >= int_min_digits digits.size >= int_min_digits
end end
private def underscored(sign, value, fraction, suffix) private def underscored(sign, value, fraction, suffix)
value = slice_digits(value.reverse) { |slice| slice }.reverse value = slice_digits(value.reverse).reverse
fraction = "." + slice_digits(fraction) { |slice| slice } if fraction fraction = "." + slice_digits(fraction) if fraction
"#{sign}#{value}#{fraction}#{suffix}" "#{sign}#{value}#{fraction}#{suffix}"
end end
private def slice_digits(value, by = 3) private def slice_digits(value, by = 3)
([] of String).tap do |slices| %w[].tap do |slices|
value.chars.reject(&.== '_').each_slice(by) do |slice| value.chars.reject(&.== '_').each_slice(by) do |slice|
slices << (yield slice).join slices << slice.join
end end
end.join('_') end.join('_')
end end