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 incorrectly tries to transform one line conditional to ternary operator #7705

Closed
Lykos opened this issue Feb 12, 2020 · 2 comments · Fixed by #8573
Closed

Rubocop incorrectly tries to transform one line conditional to ternary operator #7705

Lykos opened this issue Feb 12, 2020 · 2 comments · Fixed by #8573
Labels
bug stale Issues that haven't been active in a while

Comments

@Lykos
Copy link

Lykos commented Feb 12, 2020

Rubocop tries to correct a one line conditional with elsif clauses to a ternary operator, but the way it does it is broken and leads to a syntax error.

Expected behavior

The actual expected behavior is a bit of a product decision that I don't dare to make. It could either leave this line unchanged or transform it to multiple ternary operators.

Actual behavior

Rubocop messes up lines with a one line conditional that contains an elsif clause and the result has a syntax error.

Steps to reproduce the problem

Create a file with the following silly bit of code in an otherwise empty directory:

# frozen_string_literal: true
digit = gets.to_i % 10
nice_digit = if digit >= 5 then 5 elsif digit >= 2 then 2 else 1 end
puts nice_digit

Double check that it works by running it and seeing that you can enter an integer and it produces some output.

Run rubocop -a which will try to correct the one line conditional.

See that the line with the one line conditional is now messed up. Running the file produces a syntax error.

RuboCop version

0.79.0 (using Parser 2.7.0.2, running on ruby 2.5.1 x86_64-linux-gnu)

@Drenmi Drenmi added the bug label Feb 12, 2020
@Lykos
Copy link
Author

Lykos commented Feb 12, 2020

I am actually somewhat interested in fixing this myself now. But I need a bit of guidance. I have two questions:

I think the right solution here is to transform it to a multiline conditional if there's an elsif clause. I.e.

if digit >= 5 then 5 elsif digit >= 2 then 2 else 1 end

becomes

if digit >= 5
  5
elsif digit >= 2
  2
else
  1
end

Do you agree?

If not, I guess the solution would be to not autocorrect this and let the user figure out how to refactor. How would something like that be implemented? Can the autocorrect function just do nothing in some cases or does that cause problems?

Lykos added a commit to Lykos/rubocop that referenced this issue Feb 12, 2020
One line conditionals with elsif branches are now transformed into
multiline versions. Before this change, they were broken and autocorrect
produced code that resulted in a syntax error. See Issue rubocop#7705 for more
details.
@stale
Copy link

stale bot commented Aug 11, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

@stale stale bot added the stale Issues that haven't been active in a while label Aug 11, 2020
dmytro-savochkin pushed a commit to dmytro-savochkin/rubocop that referenced this issue Aug 24, 2020
…lsif/then/else and add AlwaysCorrectToMultiline config option
bbatsov pushed a commit that referenced this issue Aug 24, 2020
…en/else and add AlwaysCorrectToMultiline config option
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug stale Issues that haven't been active in a while
Projects
None yet
2 participants