Skip to content

Commit

Permalink
[Fix rubocop#9766] Fix a clobbering error for `Style/ClassAndModuleCh…
Browse files Browse the repository at this point in the history
…ildren` cop with `compact` style

Closes rubocop#9766
  • Loading branch information
tejasbubane committed May 7, 2021
1 parent 7be423a commit 83d360e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
@@ -0,0 +1 @@
* [#9766](https://github.com/rubocop/rubocop/pull/9766): Fix a clobbering error for `Style/ClassAndModuleChildren` cop with compact style. ([@tejasbubane][])
3 changes: 3 additions & 0 deletions lib/rubocop/cop/style/class_and_module_children.rb
Expand Up @@ -132,6 +132,9 @@ def check_nested_style(node)
end

def check_compact_style(node, body)
parent = node.parent
return if parent&.class_type? || parent&.module_type?

return unless needs_compacting?(body)

add_offense(node.loc.name, message: COMPACT_MSG) do |corrector|
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/style/class_and_module_children_spec.rb
Expand Up @@ -227,6 +227,23 @@ class Foo::Bar::Baz
RUBY
end

it 'registers and offense for deeply nested children' do
expect_offense(<<~RUBY)
class Foo
^^^ Use compact module/class definition instead of nested style.
class Bar
class Baz
end
end
end
RUBY

expect_correction(<<~RUBY)
class Foo::Bar::Baz
end
RUBY
end

it 'registers an offense for modules with partially nested children' do
expect_offense(<<~RUBY)
module Foo::Bar
Expand Down

0 comments on commit 83d360e

Please sign in to comment.