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 #8524] Fix Layout/EmptyLinesAroundClassBody and Layout/EmptyLinesAroundModuleBody to handle access modifier #8551

Merged
merged 1 commit into from Aug 17, 2020
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 @@ -15,6 +15,7 @@
* [#8537](https://github.com/rubocop-hq/rubocop/pull/8537): Allow a trailing comment as a description comment for `Bundler/GemComment`. ([@pocke][])
* [#8507](https://github.com/rubocop-hq/rubocop/issues/8507): Fix `Style/RescueModifier` to handle parentheses around rescue modifiers. ([@dsavochkin][])
* [#8527](https://github.com/rubocop-hq/rubocop/pull/8527): Prevent an incorrect auto-correction for `Style/CaseEquality` cop when comparing with `===` against a regular expression receiver. ([@koic][])
* [#8524](https://github.com/rubocop-hq/rubocop/issues/8524): Fix `Layout/EmptyLinesAroundClassBody` and `Layout/EmptyLinesAroundModuleBody` to correctly handle an access modifier as a first child. ([@dsavochkin][])

### Changes

Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/mixin/empty_lines_around_body.rb
Expand Up @@ -19,7 +19,8 @@ module EmptyLinesAroundBody
private

def_node_matcher :constant_definition?, '{class module}'
def_node_matcher :empty_line_required?, '{def defs class module}'
def_node_matcher :empty_line_required?,
'{def defs class module (send nil? {:private :protected :public})}'

def check(node, body, adjusted_first_line: nil)
return if valid_body_style?(body)
Expand Down
39 changes: 38 additions & 1 deletion spec/support/empty_lines_around_body_shared_examples.rb
Expand Up @@ -4,7 +4,7 @@
context 'when EnforcedStyle is empty_lines_special' do
let(:cop_config) { { 'EnforcedStyle' => 'empty_lines_special' } }

context 'when first child is method' do
context 'when first child is a method' do
it "requires blank line at the beginning and ending of #{type} body" do
expect_no_offenses(<<~RUBY)
#{type} SomeObject
Expand Down Expand Up @@ -109,6 +109,43 @@ def do_something
end
end

context 'when first child is an access modifier' do
context "with blank lines at the beginning and ending of #{type} body" do
it 'registers no offense' do
expect_no_offenses(<<~RUBY)
#{type} SomeObject

private
def do_something; end

end
RUBY
end
end

context "with no blank lines at the beginning and ending of #{type} body" do
it 'registers and corrects an offense' do
expect_offense(<<~RUBY)
#{type} SomeObject
private
^ #{missing_begin}
def do_something; end
end
^ #{missing_end}
RUBY

expect_correction(<<~RUBY)
#{type} SomeObject

private
def do_something; end

end
RUBY
end
end
end

context 'when first child is NOT a method' do
it "does not require blank line at the beginning of #{type} body "\
'but requires blank line before first def definition '\
Expand Down