Skip to content

Commit

Permalink
[Fix #9128] Fix an incorrect auto-correct for `Style/ClassAndModuleCh…
Browse files Browse the repository at this point in the history
…ildren`

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.
  • Loading branch information
koic authored and bbatsov committed Dec 1, 2020
1 parent 1d75e8b commit 14585a7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +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][])
11 changes: 8 additions & 3 deletions lib/rubocop/cop/style/class_and_module_children.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,18 @@ def nest_definition(corrector, node)
padding = ((' ' * indent_width) + leading_spaces(node)).to_s
padding_for_trailing_end = padding.sub(' ' * node.loc.end.column, '')

replace_keyword_with_module(corrector, node)
replace_namespace_keyword(corrector, node)
split_on_double_colon(corrector, node, padding)
add_trailing_end(corrector, node, padding_for_trailing_end)
end

def replace_keyword_with_module(corrector, node)
corrector.replace(node.loc.keyword, 'module')
def replace_namespace_keyword(corrector, node)
class_definition = node.left_sibling&.each_node(:class)&.find do |class_node|
class_node.identifier == node.identifier.namespace
end
namespace_keyword = class_definition ? 'class' : 'module'

corrector.replace(node.loc.keyword, namespace_keyword)
end

def split_on_double_colon(corrector, node, padding)
Expand Down
42 changes: 42 additions & 0 deletions spec/rubocop/cop/style/class_and_module_children_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,48 @@ class BarClass
RUBY
end

it 'registers an offense for not nested classes when namespace is defined as a class' do
expect_offense(<<~RUBY)
class FooClass
end
class FooClass::BarClass
^^^^^^^^^^^^^^^^^^ Use nested module/class definitions instead of compact style.
end
RUBY

expect_correction(<<~RUBY)
class FooClass
end
class FooClass
class BarClass
end
end
RUBY
end

it 'registers an offense for not nested classes when namespace is defined as a module' do
expect_offense(<<~RUBY)
module FooClass
end
class FooClass::BarClass
^^^^^^^^^^^^^^^^^^ Use nested module/class definitions instead of compact style.
end
RUBY

expect_correction(<<~RUBY)
module FooClass
end
module FooClass
class BarClass
end
end
RUBY
end

it 'registers an offense for not nested classes with explicit superclass' do
expect_offense(<<~RUBY)
class FooClass::BarClass < Super
Expand Down

0 comments on commit 14585a7

Please sign in to comment.