From 4036142c8fbff5abb079cd9da52b011cd70e2b31 Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Mon, 13 Sep 2021 14:05:10 -0400 Subject: [PATCH] [Fix #10066] Fix how `MinDigits` is calculated for `Style/NumericLiterals` when generating a configuration file. --- ...fix_fix_how_mindigits_is_calculated_for.md | 1 + lib/rubocop/cop/style/numeric_literals.rb | 15 +++-- lib/rubocop/rspec/cop_helper.rb | 2 +- spec/rubocop/cop/layout/line_length_spec.rb | 2 +- .../cop/style/numeric_literals_spec.rb | 55 +++++++++++++++++++ 5 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 changelog/fix_fix_how_mindigits_is_calculated_for.md diff --git a/changelog/fix_fix_how_mindigits_is_calculated_for.md b/changelog/fix_fix_how_mindigits_is_calculated_for.md new file mode 100644 index 00000000000..7ff549fe40c --- /dev/null +++ b/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][]) diff --git a/lib/rubocop/cop/style/numeric_literals.rb b/lib/rubocop/cop/style/numeric_literals.rb index ed9e205a2f9..4a34bbcc0d2 100644 --- a/lib/rubocop/cop/style/numeric_literals.rb +++ b/lib/rubocop/cop/style/numeric_literals.rb @@ -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 diff --git a/lib/rubocop/rspec/cop_helper.rb b/lib/rubocop/rspec/cop_helper.rb index e6a7611f6e8..05a4fa18314 100644 --- a/lib/rubocop/rspec/cop_helper.rb +++ b/lib/rubocop/rspec/cop_helper.rb @@ -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 diff --git a/spec/rubocop/cop/layout/line_length_spec.rb b/spec/rubocop/cop/layout/line_length_spec.rb index 42dcaa6e5e9..a14c9ab1e6a 100644 --- a/spec/rubocop/cop/layout/line_length_spec.rb +++ b/spec/rubocop/cop/layout/line_length_spec.rb @@ -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 diff --git a/spec/rubocop/cop/style/numeric_literals_spec.rb b/spec/rubocop/cop/style/numeric_literals_spec.rb index 0c2d6b4d76e..35090be04ab 100644 --- a/spec/rubocop/cop/style/numeric_literals_spec.rb +++ b/spec/rubocop/cop/style/numeric_literals_spec.rb @@ -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