Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #9766] Fix a clobbering error for Style/ClassAndModuleChildren with compact style #9783

Merged
merged 1 commit into from May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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