Autocorrect Style/LargeNumbers

This commit is contained in:
fn ⌃ ⌥ 2021-10-23 02:09:04 -07:00
parent 99e7ccd23b
commit e5fb0526e0
2 changed files with 11 additions and 5 deletions

View file

@ -9,7 +9,11 @@ module Ameba
expect_issue rule, <<-CRYSTAL, number: number
number = %{number}
# ^{number} error: Large numbers should be written with underscores: #{expected}
# ^{number} error: Large numbers should be written with underscores.
CRYSTAL
expect_correction <<-CRYSTAL
number = #{expected}
CRYSTAL
end
end
@ -122,7 +126,7 @@ module Ameba
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:7"
issue.message.should match /1_200_000/
issue.message.should eq "Large numbers should be written with underscores."
end
context "properties" do

View file

@ -28,12 +28,12 @@ module Ameba::Rule::Style
# ```
class LargeNumbers < Base
properties do
enabled false
enabled true
description "Disallows usage of large numbers without underscore"
int_min_digits 5
end
MSG = "Large numbers should be written with underscores: %s"
MSG = "Large numbers should be written with underscores."
def test(source)
Tokenizer.new(source).run do |token|
@ -48,7 +48,9 @@ module Ameba::Rule::Style
location.line_number,
location.column_number + token.raw.size - 1
)
issue_for location, end_location, MSG % expected
issue_for location, end_location, MSG do |corrector|
corrector.replace(location, end_location, expected)
end
end
end
end