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

Style/ClassAndModuleChildren auto correction makes invalid code #10825

Closed
takayamaki opened this issue Jul 20, 2022 · 0 comments · Fixed by #10826
Closed

Style/ClassAndModuleChildren auto correction makes invalid code #10825

takayamaki opened this issue Jul 20, 2022 · 0 comments · Fixed by #10826
Labels

Comments

@takayamaki
Copy link

Hi.
I found a problem about auto correction with Style/ClassAndModuleChildren cop.

Thank you!

Expected behavior

module Hoge
  module Fuga; end
end

module Hoge::Fuga; end
    it 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

Actual behavior

module Hoge
  module Fuga; end
end

module Hoge::Fugaend
Failures:

  1) RuboCop::Cop::Style::ClassAndModuleChildren compact style is expected to eq "class FooClass::BarClass; end\n"
     Failure/Error: expect(new_source).to eq(correction)
     
       expected: "class FooClass::BarClass; end\n"
            got: "class FooClass::BarClassend\n"
     
       (compared using ==)
     
       Diff:
       @@ -1 +1 @@
       -class FooClass::BarClass; end
       +class FooClass::BarClassend
       
     # ./lib/rubocop/rspec/expect_offense.rb:162:in `expect_correction'
     # ./spec/rubocop/cop/style/class_and_module_children_spec.rb:396:in `block (3 levels) in <top (required)>'

Finished in 0.07287 seconds (files took 0.78103 seconds to load)
1 example, 1 failure

Steps to reproduce the problem

fusagiko@yuika22:~/src/hoge$ cat .rubocop.yml 
AllCops:
  NewCops: enable
Style/ClassAndModuleChildren:
  EnforcedStyle: compact
fusagiko@yuika22:~/src/hoge$ cat hoge.rb 
# frozen_string_literal: true

module Hoge
  module Fuga; end
end
fusagiko@yuika22:~/src/hoge$ bundle ex rubocop -A hoge.rb 
Inspecting 1 file
E

Offenses:

hoge.rb:3:8: C: [Corrected] Style/ClassAndModuleChildren: Use compact module/class definition instead of nested style.
module Hoge
       ^^^^
hoge.rb:4:1: E: Lint/Syntax: unexpected token $end
(Using Ruby 2.6 parser; configure using TargetRubyVersion parameter, under AllCops)

1 file inspected, 2 offenses detected, 1 offense corrected
fusagiko@yuika22:~/src/hoge$ cat hoge.rb 
# frozen_string_literal: true

module Hoge::Fugaend
fusagiko@yuika22:~/src/hoge$

RuboCop version

fusagiko@yuika22:~/src/hoge$ bundle ex rubocop -V
1.31.2 (using Parser 3.1.2.0, rubocop-ast 1.19.1, running on ruby 3.0.3 x86_64-linux)
@takayamaki takayamaki changed the title Style/ClassAndModuleChildren auro correction makes invalid code Style/ClassAndModuleChildren auto correction makes invalid code Jul 20, 2022
@koic koic added the bug label Jul 21, 2022
koic added a commit to koic/rubocop that referenced this issue Jul 21, 2022
…oduleChildren`

Fixes rubocop#10825.

This PR fixes an incorrect autocorrect for `Style/ClassAndModuleChildren`
when using nested one-liner class.
bbatsov pushed a commit that referenced this issue Jul 27, 2022
…ildren`

Fixes #10825.

This PR fixes an incorrect autocorrect for `Style/ClassAndModuleChildren`
when using nested one-liner class.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants