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

Rubocop converting single-line methods to Endless methods on unsupported Rubies #9466

Closed
searls opened this issue Jan 29, 2021 · 5 comments · Fixed by #9468
Closed

Rubocop converting single-line methods to Endless methods on unsupported Rubies #9466

searls opened this issue Jan 29, 2021 · 5 comments · Fixed by #9468
Assignees
Labels

Comments

@searls
Copy link

searls commented Jan 29, 2021

Noticed this while updating Standard to support 1.8.1, but verified in this repro repo that it persists in 1.9.0 as well

Expected behavior

When running on Rubies older than 3.0, I'd expect Rubocop's defaults to not change code into endless methods, as they won't be valid Ruby.

Given:

echo "def Foo;'hi'end" | bundle exec rubocop -a -s wat.rb

I'd expect it to correct to:

====================
def Foo
  'hi'
end

Actual behavior

In fact, it looks like Rubocop is doing a first-pass and converting it to an endless method, then blowing up because it's not parseable. Full output follows:

For /Users/justin/tmp/wat: Default configuration from /Users/justin/.rbenv/versions/2.4.10/lib/ruby/gems/2.4.0/gems/rubocop-1.9.0/config/default.yml
Inspecting 1 file
Scanning /Users/justin/tmp/wat/wat.rb
E

Offenses:

wat.rb:1:1: C: [Corrected] Style/SingleLineMethods: Avoid single-line method definitions.
def Foo;'hi'end
^^^^^^^^^^^^^^^
wat.rb:1:8: C: [Corrected] Layout/SpaceAfterSemicolon: Space missing after semicolon.
def Foo;'hi'end
       ^
wat.rb:1:11: E: Lint/Syntax: unexpected token tEQL
(Using Ruby 2.4 parser; configure using TargetRubyVersion parameter, under AllCops)
def Foo() = 'hi'
          ^

1 file inspected, 3 offenses detected, 2 offenses corrected
====================
def Foo() = 'hi'
Finished in 0.14265999989584088 seconds

Steps to reproduce the problem

Here's a repro repo: https://github.com/searls/rubocop-endless-method-repro#rubocop-and-endless-methods

RuboCop version

Tested under Ruby 2.4 and 2.7 FWIW

$ [bundle exec] rubocop -V
1.9.0 (using Parser 3.0.0.0, rubocop-ast 1.4.1, running on ruby 2.7.2 x86_64-darwin20)
@rrosenblum
Copy link
Contributor

Sounds like we need to add a check for the Ruby version before handling anything with endless methods. A quick look into the code, Style/EndlessMethod appears to have a check for Ruby 3.0. This issue seems to be coming from Style/SingleLineMethods because that cop supports auto-correct to endless methods. Style/SingleLineMethods is checking the config from Style/EndlessMethod, however it doesn't seem to pickup on the minimum Ruby version.

As a quick workaround, it looks like you can either disable Style/EndlessMethod or set its EnforcedStyle to disallow to prevent Style/SingleLineMethods from correcting the code to an endless method

@searls
Copy link
Author

searls commented Jan 29, 2021

Thanks for digging in @rrosenblum

@dvandersluis
Copy link
Member

Thanks @searls I overlooked this will fix shortly.

@dvandersluis dvandersluis self-assigned this Jan 29, 2021
dvandersluis added a commit to dvandersluis/rubocop that referenced this issue Jan 29, 2021
…ess methods if the target ruby is < 3.0.
dvandersluis added a commit that referenced this issue Jan 29, 2021
[Fix #9466] Don't correct `Style/SingleLineMethods` using endless methods if the target ruby is < 3.0
@searls
Copy link
Author

searls commented Jan 29, 2021

Holy cow, @dvandersluis -- what incredible turnaround time. Thank you!! ✨

@dvandersluis
Copy link
Member

Happy to, thanks for the bug report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants