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 #5250] Fix incorrect autocorrection for Rails/Presence #5258

Conversation

wata727
Copy link
Contributor

@wata727 wata727 commented Dec 14, 2017

Fixes #5250

When an else block is multiline, the syntax is broken by autocorrection, so it must be ignored.

Original

if a.present?
  a
else
  something
  something
  something
end

Auto Fixed

a.presence || something # This is obviously broken... :(
  something
  something

Also, note postfix if and postfix rescue. If it applies autocorrection to these, the return value may change.

Original

if 1.present?
  1
else
  2 if false
end
# => 1

Auto Fixed

1.presence || 2 if false
# => nil

Unfortunately, this cop still includes another bug... But this will be fixed in #5238.


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 rake default or rake parallel. It executes all tests and RuboCop for itself, and generates the documentation.

end
RUBY
[1, 2, 3].map { |num| num + 1 }
.map { |num| num + 2 }.presence || b
Copy link
Contributor Author

@wata727 wata727 Dec 14, 2017

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...

@wata727 wata727 force-pushed the fix_incorrect_autocorrection_for_rails_presence branch from 588c40e to 3199ea9 Compare December 14, 2017 16:16
@wata727 wata727 changed the title [Fix #5250] Fix incorrect autocorrect for Rails/Presence [Fix #5250] Fix incorrect autocorrection for Rails/Presence Dec 14, 2017
@@ -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?
Copy link
Collaborator

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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also b while a?

@wata727 wata727 force-pushed the fix_incorrect_autocorrection_for_rails_presence branch 2 times, most recently from 3102533 to ee06733 Compare December 15, 2017 14:51
@bbatsov
Copy link
Collaborator

bbatsov commented Dec 21, 2017

This should be rebased on top of master.

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.
@wata727 wata727 force-pushed the fix_incorrect_autocorrection_for_rails_presence branch from ee06733 to 77820d9 Compare December 21, 2017 14:48
@wata727
Copy link
Contributor Author

wata727 commented Dec 21, 2017

@bbatsov Rebased!

@bbatsov bbatsov merged commit b219b39 into rubocop:master Dec 22, 2017
@wata727 wata727 deleted the fix_incorrect_autocorrection_for_rails_presence branch December 22, 2017 08:04
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.

Autocorrect for Rails/Presence corrupts code when there's an elsif block
3 participants