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 a false positive for Layout/EmptyLinesAroundAccessModifier #8756

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.md
Expand Up @@ -13,6 +13,7 @@
* [#8742](https://github.com/rubocop-hq/rubocop/issues/8742): Fix some assignment counts for `Metrics/AbcSize`. ([@marcandre][])
* [#8750](https://github.com/rubocop-hq/rubocop/pull/8750): Fix an incorrect auto-correct for `Style/MultilineWhenThen` when line break for multiple condidate values of `when` statement. ([@koic][])
* [#8754](https://github.com/rubocop-hq/rubocop/pull/8754): Fix an error for `Style/RandomWithOffset` when using a range with non-integer bounds. ([@eugeneius][])
* [#8756](https://github.com/rubocop-hq/rubocop/issues/8756): Fix an infinite loop error for `Layout/EmptyLinesAroundAccessModifier` with `Layout/EmptyLinesAroundBlockBody` when using access modifier with block argument. ([@koic][])

### Changes

Expand Down
22 changes: 14 additions & 8 deletions lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb
Expand Up @@ -84,14 +84,7 @@ def on_block(node)
end

def on_send(node)
return unless node.bare_access_modifier?

case style
when :around
return if empty_lines_around?(node)
when :only_before
return if allowed_only_before_style?(node)
end
return unless register_offense?(node)

message = message(node)
add_offense(node, message: message) do |corrector|
Expand All @@ -105,6 +98,19 @@ def on_send(node)

private

def register_offense?(node)
return false unless node.bare_access_modifier? && !node.parent.block_type?

case style
when :around
return false if empty_lines_around?(node)
when :only_before
return false if allowed_only_before_style?(node)
end

true
end

def allowed_only_before_style?(node)
if node.special_modifier?
return true if processed_source[node.last_line] == 'end'
Expand Down
30 changes: 30 additions & 0 deletions spec/rubocop/cli/cli_autocorrect_spec.rb
Expand Up @@ -1599,6 +1599,36 @@ def self.some_method(foo, bar: 1)
RUBY
end

it 'does not crash when using Lint/SafeNavigationWithEmpty and Layout/EmptyLinesAroundBlockBody' do
create_file('example.rb', <<~RUBY)
FactoryBot.define do
factory :model do
name { 'value' }

private { value }
end
end
RUBY

expect(
cli.run(
[
'--auto-correct',
'--only', 'Layout/EmptyLinesAroundAccessModifier,Layout/EmptyLinesAroundBlockBody'
]
)
).to eq(0)
expect(IO.read('example.rb')).to eq(<<~RUBY)
FactoryBot.define do
factory :model do
name { 'value' }

private { value }
end
end
RUBY
end

it 'corrects TrailingCommaIn(Array|Hash)Literal and ' \
'Multiline(Array|Hash)BraceLayout offenses' do
create_file('.rubocop.yml', <<~YAML)
Expand Down
Expand Up @@ -105,6 +105,14 @@ def #{access_modifier}?
RUBY
end

it "ignores #{access_modifier} with block argument" do
expect_no_offenses(<<~RUBY)
def foo
#{access_modifier} { do_something }
end
RUBY
end

it 'autocorrects blank line after #{access_modifier} with comment' do
expect_offense(<<~RUBY)
class Test
Expand Down