-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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 #5250] Fix incorrect autocorrection for Rails/Presence
#5258
[Fix #5250] Fix incorrect autocorrection for Rails/Presence
#5258
Conversation
end | ||
RUBY | ||
[1, 2, 3].map { |num| num + 1 } | ||
.map { |num| num + 2 }.presence || b |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the original code can be fixable, but I don't think the fixed code is good...
588c40e
to
3199ea9
Compare
Rails/Presence
Rails/Presence
lib/rubocop/cop/rails/presence.rb
Outdated
@@ -76,6 +76,8 @@ def on_if(node) | |||
receiver, other = redundant_negative_receiver_and_other(node) | |||
end | |||
return unless receiver | |||
return if other.multiline? || other.if_type? || other.rescue_type? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not use other.multiline?
. Probably the cop breaks on multiple statement on one line. For example:
if a.present?
a
else
something; something; something
end
We can check begin
type for other node instead.
{
(if
(send $_recv :present?)
_recv
$!begin
)
...
Because if if
body has multiple statements, the body is a begin
node.
$ ruby-parse -e if cond then foo end
(if
(send nil :cond)
(send nil :foo) nil)
$ ruby-parse -e if cond then foo; foo end
(if
(send nil :cond)
(begin
(send nil :foo)
(send nil :foo)) nil)
if a.present? | ||
a | ||
else | ||
b rescue a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also b while a
?
3102533
to
ee06733
Compare
This should be rebased on top of |
When the else block is multiline, the syntax is broken by autocorrection, so it must be ignored. Also, note postfix if, postfix rescue and postfix while. If it applies autocorrection to these, the return value may change.
ee06733
to
77820d9
Compare
@bbatsov Rebased! |
Fixes #5250
When an else block is multiline, the syntax is broken by autocorrection, so it must be ignored.
Original
Auto Fixed
Also, note postfix if and postfix rescue. If it applies autocorrection to these, the return value may change.
Original
Auto Fixed
Unfortunately, this cop still includes another bug... But this will be fixed in #5238.
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).and description in grammatically correct, complete sentences.
rake default
orrake parallel
. It executes all tests and RuboCop for itself, and generates the documentation.