Skip to content

Conversation

@dvandersluis
Copy link
Member

@dvandersluis dvandersluis commented Oct 23, 2024

When the condition to a ternary is a send node with arguments, and Style/TernaryParentheses removes 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:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • 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.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.


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

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.

Copy link
Member Author

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!

Comment on lines 236 to 242
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))
Copy link
Member

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?

Suggested change
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
Copy link
Member

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

Suggested change
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

@dvandersluis
Copy link
Member Author

@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?
Copy link
Member

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?

Copy link
Member Author

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.


def condition_send_node(condition)
send_node = condition.child_nodes.last
send_node if node_with_args?(send_node)
Copy link
Member

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.

Copy link
Member Author

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.
@koic koic merged commit ca82587 into rubocop:master Oct 23, 2024
22 checks passed
@koic
Copy link
Member

koic commented Oct 23, 2024

Thanks!

@dvandersluis dvandersluis deleted the issue/13378 branch October 23, 2024 22:57
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.

Style/TernaryParentheses reported incorrectly

2 participants