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 #10825] Fix an incorrect autocorrect for Style/ClassAndModuleChildren #10826

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 @@
* [#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