From c6537b8f9a03a32f97c7815dee05624f5f1154d5 Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Wed, 22 Jun 2022 15:51:14 -0400 Subject: [PATCH] [Fix #10700] Update `Style/EmptyMethod` to not correct if the correction would exceed the configuration for `Layout/LineLength`. --- ..._update_styleemptymethod_to_not_correct.md | 1 + lib/rubocop/cop/style/empty_method.rb | 13 +++- spec/rubocop/cop/style/empty_method_spec.rb | 63 +++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_update_styleemptymethod_to_not_correct.md diff --git a/changelog/fix_update_styleemptymethod_to_not_correct.md b/changelog/fix_update_styleemptymethod_to_not_correct.md new file mode 100644 index 00000000000..180f468c7ba --- /dev/null +++ b/changelog/fix_update_styleemptymethod_to_not_correct.md @@ -0,0 +1 @@ +* [#10700](https://github.com/rubocop/rubocop/issues/10700): Update `Style/EmptyMethod` to not correct if the correction would exceed the configuration for `Layout/LineLength`. ([@dvandersluis][]) diff --git a/lib/rubocop/cop/style/empty_method.rb b/lib/rubocop/cop/style/empty_method.rb index c805881bf0d..958c1525bf9 100644 --- a/lib/rubocop/cop/style/empty_method.rb +++ b/lib/rubocop/cop/style/empty_method.rb @@ -51,7 +51,12 @@ def on_def(node) return if node.body || comment_lines?(node) return if correct_style?(node) - add_offense(node) { |corrector| corrector.replace(node, corrected(node)) } + add_offense(node) do |corrector| + correction = corrected(node) + next if compact_style? && max_line_length && correction.size > max_line_length + + corrector.replace(node, correction) + end end alias on_defs on_def @@ -98,6 +103,12 @@ def compact_style? def expanded_style? style == :expanded end + + def max_line_length + return unless config.for_cop('Layout/LineLength')['Enabled'] + + config.for_cop('Layout/LineLength')['Max'] + end end end end diff --git a/spec/rubocop/cop/style/empty_method_spec.rb b/spec/rubocop/cop/style/empty_method_spec.rb index 04a909f5e2d..33b0e2813b9 100644 --- a/spec/rubocop/cop/style/empty_method_spec.rb +++ b/spec/rubocop/cop/style/empty_method_spec.rb @@ -157,6 +157,46 @@ def self.foo RUBY end end + + context 'relation with Layout/LineLength' do + let(:other_cops) do + { + 'Layout/LineLength' => { + 'Enabled' => line_length_enabled, + 'Max' => 20 + } + } + end + let(:line_length_enabled) { true } + + context 'when that cop is disabled' do + let(:line_length_enabled) { false } + + it 'corrects to long lines' do + expect_offense(<<~RUBY) + def foo(abc: '10000', def: '20000', ghi: '30000') + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Put empty method definitions on a single line. + end + RUBY + + expect_correction(<<~RUBY) + def foo(abc: '10000', def: '20000', ghi: '30000'); end + RUBY + end + end + + context 'when the correction would exceed the configured maximum' do + it 'reports an offense but does not correct' do + expect_offense(<<~RUBY) + def foo(abc: '10000', def: '20000', ghi: '30000') + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Put empty method definitions on a single line. + end + RUBY + + expect_no_corrections + end + end + end end context 'when configured with expanded style' do @@ -281,5 +321,28 @@ def bar RUBY end end + + context 'relation with Layout/LineLength' do + let(:other_cops) do + { + 'Layout/LineLength' => { + 'Enabled' => true, + 'Max' => 20 + } + } + end + + it 'still corrects even if the method is longer than the configured Max' do + expect_offense(<<~RUBY) + def foo(abc: '10000', def: '20000', ghi: '30000'); end + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Put the `end` of empty method definitions on the next line. + RUBY + + expect_correction(<<~RUBY) + def foo(abc: '10000', def: '20000', ghi: '30000') + end + RUBY + end + end end end