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

Accept parentheses in argument calls with blocks #12610

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12610](https://github.com/rubocop/rubocop/pull/12610): Accept parentheses in argument calls with blocks for `Style/MethodCallWithArgsParentheses` `omit_parentheses` style. ([@gsamokovarov][])
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,31 @@ def call_in_single_line_inheritance?(node)

def call_with_ambiguous_arguments?(node) # rubocop:disable Metrics/PerceivedComplexity
call_with_braced_block?(node) ||
call_in_argument_with_block?(node) ||
call_as_argument_or_chain?(node) ||
call_in_match_pattern?(node) ||
hash_literal_in_arguments?(node) ||
node.descendants.any? do |n|
n.forwarded_args_type? || ambiguous_literal?(n) || logical_operator?(n) ||
call_with_braced_block?(n)
n.forwarded_args_type? || n.block_type? ||
ambiguous_literal?(n) || logical_operator?(n)
end
end

def call_with_braced_block?(node)
(node.send_type? || node.super_type?) && node.block_node&.braces?
(node.call_type? || node.super_type?) && node.block_node&.braces?
gsamokovarov marked this conversation as resolved.
Show resolved Hide resolved
end

def call_in_argument_with_block?(node)
parent = node.parent&.block_type? && node.parent&.parent
return false unless parent

parent.call_type? || parent.super_type? || parent.yield_type?
end

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

def call_in_match_pattern?(node)
Expand Down
42 changes: 41 additions & 1 deletion spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ def seatle_style arg: default(42)
expect_no_offenses('foo(1) { 2 }')
end

it 'accepts parens in array literal calls' do
it 'accepts parens in array literal calls with blocks' do
expect_no_offenses(<<~RUBY)
[
foo.bar.quux(:args) do
Expand Down Expand Up @@ -1058,6 +1058,46 @@ def seatle_style arg: default(42)
)
RUBY
end

it 'accepts parens in argument calls with blocks' do
expect_no_offenses(<<~RUBY)
foo(
bar.new(quux) do
pass
end
)
RUBY
end

it 'accepts parens in argument csend with blocks' do
expect_no_offenses(<<~RUBY)
foo(
bar&.new(quux) do
pass
end
)
RUBY
end

it 'accepts parens in super argument call with blocks' do
expect_no_offenses(<<~RUBY)
super(
bar.new(quux) do
pass
end
)
RUBY
end

it 'accepts parens in yield argument call with blocks' do
expect_no_offenses(<<~RUBY)
yield(
bar.new(quux) do
pass
end
)
RUBY
end
end

context 'allowing parens in camel-case methods' do
Expand Down