Skip to content

Commit

Permalink
[Fix rubocop#10700] Update Style/EmptyMethod to not correct if the …
Browse files Browse the repository at this point in the history
…correction would exceed the configuration for `Layout/LineLength`.
  • Loading branch information
dvandersluis committed Jun 22, 2022
1 parent 980fb94 commit c6537b8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions 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][])
13 changes: 12 additions & 1 deletion lib/rubocop/cop/style/empty_method.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
63 changes: 63 additions & 0 deletions spec/rubocop/cop/style/empty_method_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit c6537b8

Please sign in to comment.