Skip to content

Commit

Permalink
[Fix rubocop#10066] Fix how MinDigits is calculated for `Style/Nume…
Browse files Browse the repository at this point in the history
…ricLiterals` when generating a configuration file.
  • Loading branch information
dvandersluis committed Sep 14, 2021
1 parent 35d3540 commit 4036142
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 10 deletions.
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?)
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

0 comments on commit 4036142

Please sign in to comment.