diff --git a/CHANGELOG.md b/CHANGELOG.md index b1e2dcffec7..b0b8aff9efd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/mixin/empty_lines_around_body.rb b/lib/rubocop/cop/mixin/empty_lines_around_body.rb index 2bf34965e24..f04b4e3ba82 100644 --- a/lib/rubocop/cop/mixin/empty_lines_around_body.rb +++ b/lib/rubocop/cop/mixin/empty_lines_around_body.rb @@ -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) diff --git a/spec/support/empty_lines_around_body_shared_examples.rb b/spec/support/empty_lines_around_body_shared_examples.rb index 7ded520b025..0fd048b9ea2 100644 --- a/spec/support/empty_lines_around_body_shared_examples.rb +++ b/spec/support/empty_lines_around_body_shared_examples.rb @@ -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 @@ -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 '\