Skip to content

Commit e916e9a

Browse files
authored
Merge pull request #9426 from tejasbubane/fix-9425
[Fix #9425] Fix error in `Layout/ClassStructure` when initializer comes after private attribute macro
2 parents 4356728 + 39b9f82 commit e916e9a

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#9425](https://github.com/rubocop-hq/rubocop/issues/9425): Fix error in `Layout/ClassStructure` when initializer comes after private attribute macro. ([@tejasbubane][])

lib/rubocop/cop/layout/class_structure.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def humanize_node(node)
269269

270270
def source_range_with_comment(node)
271271
begin_pos, end_pos =
272-
if node.def_type? || node.send_type? && node.def_modifier?
272+
if node.def_type? && !node.method?(:initialize) || node.send_type? && node.def_modifier?
273273
start_node = find_visibility_start(node) || node
274274
end_node = find_visibility_end(node) || node
275275
[begin_pos_with_comment(start_node),

spec/rubocop/cop/layout/class_structure_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,31 @@ def bar
366366
RUBY
367367
end
368368
end
369+
370+
context 'initializer is private and comes after attribute macro' do
371+
it 'registers offense and auto-corrects' do
372+
expect_offense(<<~RUBY)
373+
class A
374+
private
375+
376+
attr_accessor :foo
377+
378+
def initialize
379+
^^^^^^^^^^^^^^ `initializer` is supposed to appear before `private_attribute_macros`.
380+
end
381+
end
382+
RUBY
383+
384+
expect_correction(<<~RUBY)
385+
class A
386+
private
387+
388+
def initialize
389+
end
390+
attr_accessor :foo
391+
392+
end
393+
RUBY
394+
end
395+
end
369396
end

0 commit comments

Comments
 (0)