Skip to content

Commit

Permalink
[Fix rubocop#8524] Fix Layout/EmptyLinesAroundClassBody and `Layout…
Browse files Browse the repository at this point in the history
…/EmptyLinesAroundModuleBody` to handle access modifier
  • Loading branch information
Dmytro Savochkin committed Aug 17, 2020
1 parent a2c6a94 commit 12774b6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
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

0 comments on commit 12774b6

Please sign in to comment.