Skip to content

Commit

Permalink
[Fix #10825] Fix an incorrect autocorrect for `Style/ClassAndModuleCh…
Browse files Browse the repository at this point in the history
…ildren`

Fixes #10825.

This PR fixes an incorrect autocorrect for `Style/ClassAndModuleChildren`
when using nested one-liner class.
  • Loading branch information
koic authored and bbatsov committed Jul 27, 2022
1 parent b22c44c commit ac73f3b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
@@ -0,0 +1 @@
* [#10825](https://github.com/rubocop/rubocop/issues/10825): Fix an incorrect autocorrect for `Style/ClassAndModuleChildren` when using nested one-liner class. ([@koic][])
8 changes: 4 additions & 4 deletions lib/rubocop/cop/style/class_and_module_children.rb
Expand Up @@ -117,10 +117,10 @@ def compact_identifier_name(node)
end

def remove_end(corrector, body)
range = range_between(
body.loc.end.begin_pos - leading_spaces(body).size,
body.loc.end.end_pos + 1
)
remove_begin_pos = body.loc.end.begin_pos - leading_spaces(body).size
adjustment = processed_source.raw_source[remove_begin_pos] == ';' ? 0 : 1
range = range_between(remove_begin_pos, body.loc.end.end_pos + adjustment)

corrector.remove(range)
end

Expand Down
14 changes: 14 additions & 0 deletions spec/rubocop/cop/style/class_and_module_children_spec.rb
Expand Up @@ -367,6 +367,20 @@ class BazModule
RUBY
end

it 'registers a offense for classes with nested one-liner children' do
expect_offense(<<~RUBY)
class FooClass
^^^^^^^^ Use compact module/class definition instead of nested style.
class BarClass; end
end
RUBY

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

it 'accepts class/module with single method' do
expect_no_offenses(<<~RUBY)
class FooClass
Expand Down

0 comments on commit ac73f3b

Please sign in to comment.