Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #10066] Fix how MinDigits is calculated for Style/NumericLiterals when generating a configuration file #10077

Merged
merged 1 commit into from Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/fix_fix_how_mindigits_is_calculated_for.md
@@ -0,0 +1 @@
* [#10066](https://github.com/rubocop/rubocop/issues/10066): Fix how `MinDigits` is calculated for `Style/NumericLiterals` when generating a configuration file. ([@dvandersluis][])
15 changes: 7 additions & 8 deletions lib/rubocop/cop/style/numeric_literals.rb
Expand Up @@ -58,18 +58,17 @@ def check(node)

case int
when /^\d+$/
return unless (self.min_digits = int.size + 1)

register_offense(node)
register_offense(node) { self.min_digits = int.size + 1 }
when /\d{4}/, short_group_regex
return unless (self.config_to_allow_offenses = { 'Enabled' => false })

register_offense(node)
register_offense(node) { self.config_to_allow_offenses = { 'Enabled' => false } }
end
end

def register_offense(node)
add_offense(node) { |corrector| corrector.replace(node, format_number(node)) }
def register_offense(node, &_block)
add_offense(node) do |corrector|
yield
corrector.replace(node, format_number(node))
end
end

def short_group_regex
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/rspec/cop_helper.rb
Expand Up @@ -49,7 +49,7 @@ def _investigate(cop, processed_source)
team = RuboCop::Cop::Team.new([cop], nil, raise_error: true)
report = team.investigate(processed_source)
@last_corrector = report.correctors.first || RuboCop::Cop::Corrector.new(processed_source)
report.offenses
report.offenses.reject(&:disabled?)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to test this, I needed to update expect_offense and expect_no_offenses to not consider disabled offenses. There was only one test (in spec/rubocop/cop/layout/line_length_spec.rb) that was affected by this change.

end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/layout/line_length_spec.rb
Expand Up @@ -361,7 +361,7 @@ def method_definition_that_is_just_under_the_line_length_limit(foo) # rubocop:di
context 'and the source contains non-directive #s as non-comment' do
it 'registers an offense for the line' do
expect_offense(<<-RUBY)
LARGE_DATA_STRING_PATTERN = %r{\A([A-Za-z0-9\+\/#]*\={0,2})#([A-Za-z0-9\+\/#]*\={0,2})#([A-Za-z0-9\+\/#]*\={0,2})\z} # rubocop:disable Layout/LineLength
LARGE_DATA_STRING_PATTERN = %r{\A([A-Za-z0-9\+\/#]*\={0,2})#([A-Za-z0-9\+\/#]*\={0,2})#([A-Za-z0-9\+\/#]*\={0,2})\z} # rubocop:disable Style/ClassVars
#{' ' * 68}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [117/80]
RUBY
end
Expand Down
55 changes: 55 additions & 0 deletions spec/rubocop/cop/style/numeric_literals_spec.rb
Expand Up @@ -164,4 +164,59 @@
RUBY
end
end

context 'for --auto-gen-config' do
let(:enabled) { cop.config_to_allow_offenses['Enabled'] }
let(:min_digits) { cop.config_to_allow_offenses.dig(:exclude_limit, 'MinDigits') }

context 'when the number is only digits' do
it 'detects right value of MinDigits based on the longest number' do
expect_offense(<<~RUBY)
1234567890
^^^^^^^^^^ [...]
12345678901234567890
^^^^^^^^^^^^^^^^^^^^ [...]
123456789012
^^^^^^^^^^^^ [...]
RUBY

expect(min_digits).to eq(21)
expect(enabled.nil?).to be(true)
end

it 'sets the right value if one is disabled inline' do
expect_offense(<<~RUBY)
1234567890
^^^^^^^^^^ [...]
12345678901234567890 # rubocop:disable Style/NumericLiterals
123456789012
^^^^^^^^^^^^ [...]
RUBY

expect(min_digits).to eq(13)
expect(enabled.nil?).to be(true)
end
end

context 'with separators' do
it 'disables the cop' do
expect_offense(<<~RUBY)
1234_5678_90
^^^^^^^^^^^^ [...]
RUBY

expect(enabled).to eq(false)
expect(min_digits.nil?).to be(true)
end

it 'does not disable the cop if the line is disabled' do
expect_no_offenses(<<~RUBY)
1234_5678_90 # rubocop:disable Style/NumericLiterals
RUBY

expect(enabled.nil?).to be(true)
expect(min_digits.nil?).to be(true)
end
end
end
end