Skip to content

Commit

Permalink
[Fix #9425] Fix error in Layout/ClassStructure when initializer com…
Browse files Browse the repository at this point in the history
…es after private attribute macro

Closes #9425
  • Loading branch information
tejasbubane committed Jan 27, 2021
1 parent 4356728 commit 39b9f82
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_error_in_layout_class_structure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9425](https://github.com/rubocop-hq/rubocop/issues/9425): Fix error in `Layout/ClassStructure` when initializer comes after private attribute macro. ([@tejasbubane][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/class_structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def humanize_node(node)

def source_range_with_comment(node)
begin_pos, end_pos =
if node.def_type? || node.send_type? && node.def_modifier?
if node.def_type? && !node.method?(:initialize) || node.send_type? && node.def_modifier?
start_node = find_visibility_start(node) || node
end_node = find_visibility_end(node) || node
[begin_pos_with_comment(start_node),
Expand Down
27 changes: 27 additions & 0 deletions spec/rubocop/cop/layout/class_structure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,31 @@ def bar
RUBY
end
end

context 'initializer is private and comes after attribute macro' do
it 'registers offense and auto-corrects' do
expect_offense(<<~RUBY)
class A
private
attr_accessor :foo
def initialize
^^^^^^^^^^^^^^ `initializer` is supposed to appear before `private_attribute_macros`.
end
end
RUBY

expect_correction(<<~RUBY)
class A
private
def initialize
end
attr_accessor :foo
end
RUBY
end
end
end

0 comments on commit 39b9f82

Please sign in to comment.