diff --git a/CHANGELOG.md b/CHANGELOG.md index f9da0d51cb6..b35a6dfb0a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * [#7967](https://github.com/rubocop-hq/rubocop/pull/7967): `Style/SlicingWithRange` cop now supports any expression as its first index. ([@zverok][]) * [#7972](https://github.com/rubocop-hq/rubocop/issues/7972): Fix an incorrect autocrrect for `Style/HashSyntax` when using a return value uses `return`. ([@koic][]) * [#7886](https://github.com/rubocop-hq/rubocop/issues/7886): Fix a bug in `AllowComments` logic in `Lint/SuppressedException`. ([@jonas054][]) +* [#7991](https://github.com/rubocop-hq/rubocop/issues/7991): Fix an error for `Layout/EmptyLinesAroundAttributeAccessor` when attribute method is method chained. ([@koic][]) ### Changes diff --git a/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb b/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb index dce9cc2b096..f8497cf4a62 100644 --- a/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +++ b/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb @@ -70,7 +70,7 @@ def on_send(node) return if next_line_empty?(node.last_line) next_line_node = next_line_node(node) - return if next_line_node.nil? || allow_alias?(next_line_node) || attribute_or_allowed_method?(next_line_node) + return unless require_empty_line?(next_line_node) add_offense(node) end @@ -89,6 +89,12 @@ def next_line_empty?(line) processed_source[line].blank? end + def require_empty_line?(node) + return false unless node&.respond_to?(:type) + + !allow_alias?(node) && !attribute_or_allowed_method?(node) + end + def next_line_node(node) node.parent.children[node.sibling_index + 1] end diff --git a/spec/rubocop/cop/layout/empty_lines_around_attribute_accessor_spec.rb b/spec/rubocop/cop/layout/empty_lines_around_attribute_accessor_spec.rb index 3bd47d4a929..5158f3c5bf4 100644 --- a/spec/rubocop/cop/layout/empty_lines_around_attribute_accessor_spec.rb +++ b/spec/rubocop/cop/layout/empty_lines_around_attribute_accessor_spec.rb @@ -67,6 +67,14 @@ class Foo RUBY end + it 'accepts code when attribute method is method chained' do + expect_no_offenses(<<~RUBY) + class Foo + attr.foo + end + RUBY + end + context 'when `AllowAliasSyntax: true`' do let(:cop_config) do { 'AllowAliasSyntax' => true }