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

Autocorrect for Style/TrailingBodyOnClass breaks the code when body trails after singleton class definition #10618

Closed
j-miyake opened this issue May 10, 2022 · 1 comment · Fixed by #10638

Comments

@j-miyake
Copy link
Contributor

Related with #10617

The autocorrect for Style/TrailingBodyOnClass breaks this code

class Foo bar # <------------ `bar` is the actual first body line of this class.
  class << self; baz  # <------------ `baz` is the actual first body line of this singleton class.
    def func
      puts 1
    end
  end
end

to

class Foo 
  bar
  class << self baz # Syntax error
    def func
      puts 1
    end
  end
end

Expected behavior

class Foo 
  bar    
  class << self
    baz
    def func
      puts 1
    end
  end
end

Steps to reproduce the problem

Save the example code to example.rb, and run

exe/rubocop example.rb -a --only Style/TrailingBodyOnClass 

RuboCop version

$ bundle exec rubocop -V
1.29.0 (using Parser 3.1.2.0, rubocop-ast 1.17.0, running on ruby 2.7.5 x86_64-darwin20)
  - rubocop-performance 1.13.3
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.10.0
@j-miyake j-miyake changed the title The autocorrect for Style/TrailingBodyOnClass breaks the code when something is following an opening of singleton class Autocorrect for Style/TrailingBodyOnClass breaks the code when body trails after singleton class definition May 11, 2022
@j-miyake
Copy link
Contributor Author

There is another pattern.

This code

class Bar def bar; end
end

is corrected to

class Bar
  def bar end # Syntax error
end

j-miyake added a commit to j-miyake/rubocop that referenced this issue May 15, 2022
bbatsov pushed a commit that referenced this issue May 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant