-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
[Fix #13378] When removing parens in Style/TernaryParentheses with a send node condition, ensure its arguments are parenthesized
#13381
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
Conversation
|
|
||
| def condition_send_node(condition) | ||
| return condition if condition.call_type? || condition.defined_type? | ||
| return nil unless condition.begin_type? && condition.child_nodes.length == 1 |
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.
Is this a necessary process? If so, please add a test for it.
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.
Not necessary, I'll simplify. Thank you!
| return false unless send_node.call_type? || send_node.defined_type? | ||
|
|
||
| send_node.arguments.any? && | ||
| !send_node.parenthesized? && | ||
| (send_node.dot? || | ||
| send_node.safe_navigation? || | ||
| unparenthesized_method_call?(send_node)) |
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.
Can you refactor to the following?
| return false unless send_node.call_type? || send_node.defined_type? | |
| send_node.arguments.any? && | |
| !send_node.parenthesized? && | |
| (send_node.dot? || | |
| send_node.safe_navigation? || | |
| unparenthesized_method_call?(send_node)) | |
| return false unless send_node.call_type? || send_node.defined_type? | |
| return false if send_node.arguments.none? || send_node.parenthesized? | |
| send_node.dot? || send_node.safe_navigation? || unparenthesized_method_call?(send_node) |
| RUBY | ||
| end | ||
|
|
||
| it 'registers an offense calling method with non-alphanumeric name' do |
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.
It seems we could use "operator method" instead of "non-alphanumeric name". e.g.:
| it 'registers an offense calling method with non-alphanumeric name' do | |
| it 'registers an offense calling an operator method with dot or safe navigation' do |
81046a9 to
e530531
Compare
|
@koic thanks for the review! ❤️ I've updated with your requested changes. |
|
|
||
| def condition_send_node(condition) | ||
| return condition if node_with_args?(condition) | ||
| return nil unless condition.begin_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.
Is this condition (return nil unless condition.begin_type?) necessary as well?
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.
You're right, since this is called from correct_parenthesized, condition is always going to be a begin node already, so this is unnecessary, as is the first check for a callable node. Updated again.
e530531 to
30fe3b1
Compare
|
|
||
| def condition_send_node(condition) | ||
| send_node = condition.child_nodes.last | ||
| send_node if node_with_args?(send_node) |
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.
Apologies for the repetition feedback, it seems the tests pass even without the if node_with_args? guard condition.
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.
Yup true, because send_node_args_need_parens? calls node_with_args? too. I refactored away the condition_send_node method now.
…` with a `send` node condition, ensure its arguments are parenthesized.
30fe3b1 to
0c5cf48
Compare
|
Thanks! |
When the condition to a ternary is a send node with arguments, and
Style/TernaryParenthesesremoves parentheses around that condition, ensure the arguments are themselves parenthesized, because otherwise removing the surrounding parens will change the semantics of the ternary.This also allows a number of test cases to be autocorrected which previously did not have autocorrect.
Fixes #13378.
Before submitting the PR make sure the following are checked:
[Fix #issue-number](if the related issue exists).master(if not - rebase it).bundle exec rake default. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.mdif the new code introduces user-observable changes. See changelog entry format for details.