Skip to content

Commit

Permalink
Accept parentheses in argument calls with blocks
Browse files Browse the repository at this point in the history
In `Style/MethodCallWithArgsParentheses` with `omit_parentheses` enforced
style we need to allow parentheses in method calls with blocks, when
their value is used as an arguments in method dispatch calls. We
have syntax errors when we enforce their omissions:

```ruby
foo(
  Class.new(Base) do
    # valid Ruby
  end
)
```

Should be allowed as the example below is invalid Ruby:

```ruby
foo(
  Class.new Base do
    # invalid Ruby
  end
)
```
  • Loading branch information
gsamokovarov committed Jan 11, 2024
1 parent 5cd23f8 commit 64fb437
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
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?
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

0 comments on commit 64fb437

Please sign in to comment.