Skip to content

Commit 14585a7

Browse files
koicbbatsov
authored andcommitted
[Fix #9128] Fix an incorrect auto-correct for Style/ClassAndModuleChildren
Fixes #9128. This PR fixes an incorrect auto-correct for `Style/ClassAndModuleChildren` when namespace is defined as a class in the same file. It is still undetectable when class definition exists in the different file, but it can be detected in the same file. This PR solves the latter.
1 parent 1d75e8b commit 14585a7

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#9128](https://github.com/rubocop-hq/rubocop/issues/9128): Fix an incorrect auto-correct for `Style/ClassAndModuleChildren` when namespace is defined as a class in the same file. ([@koic][])

lib/rubocop/cop/style/class_and_module_children.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,18 @@ def nest_definition(corrector, node)
5555
padding = ((' ' * indent_width) + leading_spaces(node)).to_s
5656
padding_for_trailing_end = padding.sub(' ' * node.loc.end.column, '')
5757

58-
replace_keyword_with_module(corrector, node)
58+
replace_namespace_keyword(corrector, node)
5959
split_on_double_colon(corrector, node, padding)
6060
add_trailing_end(corrector, node, padding_for_trailing_end)
6161
end
6262

63-
def replace_keyword_with_module(corrector, node)
64-
corrector.replace(node.loc.keyword, 'module')
63+
def replace_namespace_keyword(corrector, node)
64+
class_definition = node.left_sibling&.each_node(:class)&.find do |class_node|
65+
class_node.identifier == node.identifier.namespace
66+
end
67+
namespace_keyword = class_definition ? 'class' : 'module'
68+
69+
corrector.replace(node.loc.keyword, namespace_keyword)
6570
end
6671

6772
def split_on_double_colon(corrector, node, padding)

spec/rubocop/cop/style/class_and_module_children_spec.rb

+42
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,48 @@ class BarClass
1818
RUBY
1919
end
2020

21+
it 'registers an offense for not nested classes when namespace is defined as a class' do
22+
expect_offense(<<~RUBY)
23+
class FooClass
24+
end
25+
26+
class FooClass::BarClass
27+
^^^^^^^^^^^^^^^^^^ Use nested module/class definitions instead of compact style.
28+
end
29+
RUBY
30+
31+
expect_correction(<<~RUBY)
32+
class FooClass
33+
end
34+
35+
class FooClass
36+
class BarClass
37+
end
38+
end
39+
RUBY
40+
end
41+
42+
it 'registers an offense for not nested classes when namespace is defined as a module' do
43+
expect_offense(<<~RUBY)
44+
module FooClass
45+
end
46+
47+
class FooClass::BarClass
48+
^^^^^^^^^^^^^^^^^^ Use nested module/class definitions instead of compact style.
49+
end
50+
RUBY
51+
52+
expect_correction(<<~RUBY)
53+
module FooClass
54+
end
55+
56+
module FooClass
57+
class BarClass
58+
end
59+
end
60+
RUBY
61+
end
62+
2163
it 'registers an offense for not nested classes with explicit superclass' do
2264
expect_offense(<<~RUBY)
2365
class FooClass::BarClass < Super

0 commit comments

Comments
 (0)