Skip to content

Commit

Permalink
Merge pull request #8756 from koic/fix_false_positive_for_empty_lines…
Browse files Browse the repository at this point in the history
…_around_access_modifier

Fix a false positive for `Layout/EmptyLinesAroundAccessModifier`
  • Loading branch information
koic committed Sep 21, 2020
2 parents 058d541 + e753311 commit fa97782
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
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

0 comments on commit fa97782

Please sign in to comment.