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

Fix an incorrect auto-correct for Style/MultilineTernaryOperator #8661

Conversation

koic
Copy link
Member

@koic koic commented Sep 7, 2020

This PR fixes the following incorrect auto-correct for Style/MultilineTernaryOperator when returning a multiline ternary operator expression.

% cat example.rb
return distance_in_minutes == 0 ?
       locale.t(:less_than_x_minutes, count: 1) :
       locale.t(:x_minutes, count: distance_in_minutes)

% bundle exec rubocop -a --only Style/MultilineTernaryOperator
(snip)

Inspecting 2 files
C.

Offenses:

example.rb:1:8: C: [Corrected] Style/MultilineTernaryOperator: Avoid
multi-line ternary operators, use if or unless instead.
return distance_in_minutes == 0 ? ...
       ^^^^^^^^^^^^^^^^^^^^^^^^^^

2 files inspected, 1 offense detected, 1 offense corrected

% cat example.rb
return if distance_in_minutes == 0
  locale.t(:less_than_x_minutes, count: 1)
else
  locale.t(:x_minutes, count: distance_in_minutes)
end

% ruby -c example.rb
example.rb:3: syntax error, unexpected `else', expecting end-of-input

I found it with the following code.
https://github.com/rails/rails/blob/v6.0.3.2/actionview/lib/action_view/helpers/date_helper.rb#L109-L111


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@@ -5777,6 +5777,9 @@ a = if cond
else
c
end
return cond ?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we shouldn't auto-correct this, but I think we should still flag this, as it looks quite weird.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I get it and updated this PR!

@koic koic changed the title Fix a false positive for Style/MultilineTernaryOperator Fix an incorrect auto-correct for Style/MultilineTernaryOperator Sep 8, 2020
@koic koic force-pushed the fix_false_positive_for_style_multiline_ternary_operator branch from c7ce426 to 838bb4e Compare September 8, 2020 15:16
Copy link
Collaborator

@bbatsov bbatsov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add in the docs some explanation why won't auto-correct a ternary op in return.


add_offense(node) do |corrector|
next unless offense?(node) && !node.parent.return_type?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add a comment here explaining why this is needed for future reference.

@koic koic force-pushed the fix_false_positive_for_style_multiline_ternary_operator branch from 838bb4e to 6794f3f Compare September 10, 2020 17:24
@koic
Copy link
Member Author

koic commented Sep 10, 2020

Thank you for your advice. I added a note to the doc.

@koic koic force-pushed the fix_false_positive_for_style_multiline_ternary_operator branch 2 times, most recently from 9abfb7a to 4c4c50e Compare September 11, 2020 10:49
This PR fixes the following incorrect auto-correct for
`Style/MultilineTernaryOperator` when returning
a multiline ternary operator expression.

```console
% cat example.rb
return distance_in_minutes == 0 ?
       locale.t(:less_than_x_minutes, count: 1) :
       locale.t(:x_minutes, count: distance_in_minutes)

% bundle exec rubocop -a --only Style/MultilineTernaryOperator
(snip)

Inspecting 2 files
C.

Offenses:

example.rb:1:8: C: [Corrected] Style/MultilineTernaryOperator: Avoid
multi-line ternary operators, use if or unless instead.
return distance_in_minutes == 0 ? ...
       ^^^^^^^^^^^^^^^^^^^^^^^^^^

2 files inspected, 1 offense detected, 1 offense corrected

% cat example.rb
return if distance_in_minutes == 0
  locale.t(:less_than_x_minutes, count: 1)
else
  locale.t(:x_minutes, count: distance_in_minutes)
end

% ruby -c example.rb
example.rb:3: syntax error, unexpected `else', expecting end-of-input
```

I found it with the following code.
https://github.com/rails/rails/blob/v6.0.3.2/actionview/lib/action_view/helpers/date_helper.rb#L109-L111
@koic koic force-pushed the fix_false_positive_for_style_multiline_ternary_operator branch from 4c4c50e to 8a1776f Compare September 13, 2020 06:10
@koic koic merged commit dca548f into rubocop:master Sep 13, 2020
@koic koic deleted the fix_false_positive_for_style_multiline_ternary_operator branch September 13, 2020 08:43
koic added a commit to koic/rubocop that referenced this pull request Apr 15, 2022
Follow up rubocop#8661.

This PR fixes an incorrect auto-correct for `Style/MultilineTernaryOperator`
when returning a multiline ternary operator expression.
Using `break`, `next`, or method call also causes the same syntax error as `return`.

And this PR will change these auto-correct to single-line ternary operator as valid syntax.
bbatsov pushed a commit that referenced this pull request Apr 16, 2022
Follow up #8661.

This PR fixes an incorrect auto-correct for `Style/MultilineTernaryOperator`
when returning a multiline ternary operator expression.
Using `break`, `next`, or method call also causes the same syntax error as `return`.

And this PR will change these auto-correct to single-line ternary operator as valid syntax.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants