Skip to content

Commit

Permalink
Allow parentheses in yield arguments for `Style/MethodCallWithArgsPar…
Browse files Browse the repository at this point in the history
…entheses`

When using the `omit_parentheses` style, we don't recognize `yield`
argument calls and require parentheses omission. If we yield multiple
values, we cannot omit the parentheses in method calls as this won't be
valid Ruby. We currently register offenses for:

```ruby
# Example 1
yield File.basename(path)
      ^^^^^^^^^^^^^^^^^^^ Omit parentheses for method calls with arguments.

# Correction...
yield File.basename path # 👈 This compiles.

# Example 2
yield path, File.basename(path)
            ^^^^^^^^^^^^^^^^^^^ Omit parentheses for method calls with arguments.

# Correction...
yield path, File.basename path # 👈 This DOES NOT!!!
```

If we treat yield arguments as any other method call arguments, we'll
allow the parentheses and let people write valid Ruby in the cases
above.
  • Loading branch information
gsamokovarov committed Mar 22, 2021
1 parent 6746f15 commit 770beb1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
@@ -0,0 +1 @@
* [#9625](https://github.com/rubocop/rubocop/pull/9625): Allow parentheses in yield arguments with `Style/MethodCallWithArgsParentheses` `EnforcedStyle: omit_parentheses` to fix invalid Ruby auto-correction. ([@gsamokovarov][])
Expand Up @@ -114,7 +114,7 @@ def call_with_braced_block?(node)
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.csend_type? || node.parent.super_type? || node.parent.yield_type?)
end

def hash_literal_in_arguments?(node)
Expand Down
Expand Up @@ -658,6 +658,11 @@ def seatle_style arg: default(42)
expect_no_offenses('foo &block')
end

it 'accepts parens in yield argument method calls' do
expect_no_offenses('yield File.basepath(path)')
expect_no_offenses('yield path, File.basepath(path)')
end

it 'accepts parens in super without args' do
expect_no_offenses('super()')
end
Expand Down

0 comments on commit 770beb1

Please sign in to comment.