Skip to content

Commit

Permalink
Fix a false positive for Style/ClassAndModuleChildren
Browse files Browse the repository at this point in the history
This PR fixes the following incorrect auto-correct for
`Style/ClassAndModuleChildren` when using cbase class name.

```console
% cat example.rb
class ::Foo
end

% bundle exec rubocop -A --only Style/ClassAndModuleChildren
(snip)

Inspecting 1 file
C

Offenses:

example.rb:1:7: C: [Corrected] Style/ClassAndModuleChildren: Use nested
module/class definitions instead of compact style.
class ::Foo
      ^^^^^

1 file inspected, 1 offense detected, 1 offense corrected

% cat example.rb
module
  class Foo
  end
end

% ruby -c example.rb
example.rb:3: syntax error, unexpected '\n', expecting '.' or &. or ::
or '['
```
  • Loading branch information
koic authored and bbatsov committed Sep 6, 2020
1 parent b00ac49 commit 73cbce0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
* [#8627](https://github.com/rubocop-hq/rubocop/issues/8627): Fix a false positive for `Lint/DuplicateRequire` when same feature argument but different require method. ([@koic][])
* [#8572](https://github.com/rubocop-hq/rubocop/issues/8572): Fix a false positive for `Style/RedundantParentheses` when parentheses are used like method argument parentheses. ([@koic][])
* [#8653](https://github.com/rubocop-hq/rubocop/pull/8653): Fix a false positive for `Layout/DefEndAlignment` when using refinements and `private def`. ([@koic][])
* [#8655](https://github.com/rubocop-hq/rubocop/pull/8655): Fix a false positive for `Style/ClassAndModuleChildren` when using cbase class name. ([@koic][])

### Changes

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/style/class_and_module_children.rb
Expand Up @@ -32,6 +32,7 @@ class ClassAndModuleChildren < Base
'nested style.'

def on_class(node)
return if node.identifier.children[0]&.cbase_type?
return if node.parent_class && style != :nested

check_style(node, node.body)
Expand Down
7 changes: 7 additions & 0 deletions spec/rubocop/cop/style/class_and_module_children_spec.rb
Expand Up @@ -60,6 +60,13 @@ module BarModule
RUBY
end

it 'accepts cbase class name' do
expect_no_offenses(<<~RUBY)
class ::Foo
end
RUBY
end

it 'accepts :: in parent class on inheritance' do
expect_no_offenses(<<~RUBY)
class FooClass
Expand Down

0 comments on commit 73cbce0

Please sign in to comment.