Skip to content

Commit

Permalink
Allow parentheses in safe navigation calls for `Style/MethodCallWithA…
Browse files Browse the repository at this point in the history
…rgsParentheses`

While calls like `foo&.[] bar` do compile, I won't blame people if they
want to leave the parentheses so they know what's going on.

I'm proposing we legitimize the parens in `foo&.[](bar)` and any other
safe navigation calls. I don't think this deserves extra configuration
either.
  • Loading branch information
gsamokovarov committed Mar 17, 2021
1 parent 6746f15 commit f6cbc01
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#9612](https://github.com/rubocop/rubocop/pull/9612): Allow parentheses in safe navigation calls for `Style/MethodCallWithArgsParentheses` `EnforcedStyle: omit_parentheses`. ([@gsamokovarov][])

## 1.11.0 (2021-03-01)

### New features
Expand Down
Expand Up @@ -98,7 +98,7 @@ def call_in_single_line_inheritance?(node)

def call_with_ambiguous_arguments?(node)
call_with_braced_block?(node) ||
call_as_argument_or_chain?(node) ||
call_in_safe_navigation_argument_or_chain?(node) ||
hash_literal_in_arguments?(node) ||
node.descendants.any? do |n|
ambigious_literal?(n) || logical_operator?(n) ||
Expand All @@ -111,10 +111,10 @@ def call_with_braced_block?(node)
node.block_node && node.block_node.braces?
end

def call_as_argument_or_chain?(node)
node.parent &&
def call_in_safe_navigation_argument_or_chain?(node)
node.csend_type? || node.parent &&
(node.parent.send_type? && !assigned_before?(node.parent, node) ||
node.parent.csend_type? || node.parent.super_type?)
node.parent.csend_type? || node.parent.super_type?)
end

def hash_literal_in_arguments?(node)
Expand Down
Expand Up @@ -753,6 +753,10 @@ def seatle_style arg: default(42)
expect_no_offenses('Something.find(criteria: given)&.field')
end

it 'accepts parens in safe operator calls' do
expect_no_offenses('foo&.[](bar)')
end

context 'allowing parenthesis in chaining' do
let(:cop_config) do
{
Expand Down

0 comments on commit f6cbc01

Please sign in to comment.