Skip to content

Commit

Permalink
[Fix #9415] Change Layout/ClassStructure to detect inline modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Eres authored and bbatsov committed Jan 27, 2021
1 parent 9a06e84 commit 1f7d2d2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9415](https://github.com/rubocop-hq/rubocop/issues/9415): Change `Layout/ClassStructure` to detect inline modifiers. ([@AndreiEres][])
9 changes: 7 additions & 2 deletions lib/rubocop/cop/layout/class_structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,12 @@ def find_category(node)
name = node.method_name.to_s
category, = categories.find { |_, names| names.include?(name) }
key = category || name
visibility_key = "#{node_visibility(node)}_#{key}"
visibility_key =
if node.def_modifier?
"#{name}_methods"
else
"#{node_visibility(node)}_#{key}"
end
expected_order.include?(visibility_key) ? visibility_key : key
end

Expand Down Expand Up @@ -264,7 +269,7 @@ def humanize_node(node)

def source_range_with_comment(node)
begin_pos, end_pos =
if node.def_type?
if node.def_type? || 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
51 changes: 48 additions & 3 deletions spec/rubocop/cop/layout/class_structure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,6 @@ def other_public_method
private :other_public_method
private def something_inline
end
def yet_other_public_method
end
Expand Down Expand Up @@ -321,4 +318,52 @@ def do_something
end
RUBY
end

context 'when def modifier is used' do
it 'registers an offense and corrects public method with modifier declared after private method with modifier' do
expect_offense(<<~RUBY)
class A
private def foo
end
public def bar
^^^^^^^^^^^^^^ `public_methods` is supposed to appear before `private_methods`.
end
end
RUBY

expect_correction(<<~RUBY)
class A
public def bar
end
private def foo
end
end
RUBY
end

it 'registers an offense and corrects public method without modifier declared after private method with modifier' do
expect_offense(<<~RUBY)
class A
private def foo
end
def bar
^^^^^^^ `public_methods` is supposed to appear before `private_methods`.
end
end
RUBY

expect_correction(<<~RUBY)
class A
def bar
end
private def foo
end
end
RUBY
end
end
end

0 comments on commit 1f7d2d2

Please sign in to comment.