From f6cbc012359412c86031d8671364f6264358a39e Mon Sep 17 00:00:00 2001 From: Genadi Samokovarov Date: Wed, 17 Mar 2021 16:30:50 +0200 Subject: [PATCH] Allow parentheses in safe navigation calls for `Style/MethodCallWithArgsParentheses` 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. --- CHANGELOG.md | 4 ++++ .../method_call_with_args_parentheses/omit_parentheses.rb | 8 ++++---- .../cop/style/method_call_with_args_parentheses_spec.rb | 4 ++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1988fe9c0c3..e026ac56623 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb b/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb index ce15f324d48..8564ccebd3c 100644 --- a/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +++ b/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb @@ -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) || @@ -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) diff --git a/spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb b/spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb index 2236e3b68b1..58f587ae484 100644 --- a/spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb +++ b/spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb @@ -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 {