Skip to content

Commit

Permalink
Fix an edge case in omit_parentheses style of `Style/MethodCallWith…
Browse files Browse the repository at this point in the history
…ArgsParentheses`

Before this change:

```ruby
foo(a) || bar(b) do
             ^^^ Omit parentheses for method calls with arguments.
  pass
end
```

This resulted in a `SyntaxError`, if corrected. The check for a call
with logical operator did not take into account that the parent of a send node with a block is the block node itself.

After this change, the code below is lawful:

```ruby
foo(a) || bar(b) do
  pass
end
```
  • Loading branch information
gsamokovarov committed Jan 9, 2020
1 parent 1a6ef08 commit c7d6d76
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#7639](https://github.com/rubocop-hq/rubocop/pull/7639): Fix logical operator edge case in `omit_parentheses` style of `Style/MethodCallWithArgsParentheses`. ([@gsamokovarov][])

### Changes

* [#7636](https://github.com/rubocop-hq/rubocop/issues/7636): Remove `console` from `Lint/Debugger` to prevent false positives. ([@gsamokovarov][])
Expand Down
Expand Up @@ -74,10 +74,11 @@ def call_in_literals?(node)
end

def call_in_logical_operators?(node)
node.parent &&
(logical_operator?(node.parent) ||
node.parent.send_type? &&
node.parent.arguments.any?(&method(:logical_operator?)))
parent = node.parent&.block_type? ? node.parent.parent : node.parent
parent &&
(logical_operator?(parent) ||
parent.send_type? &&
parent.arguments.any?(&method(:logical_operator?)))
end

def call_in_optional_arguments?(node)
Expand Down
Expand Up @@ -471,6 +471,11 @@ def seatle_style arg: default(42)
it 'accepts parens in calls with logical operators' do
expect_no_offenses('foo(a) && bar(b)')
expect_no_offenses('foo(a) || bar(b)')
expect_no_offenses(<<~RUBY)
foo(a) || bar(b) do
pass
end
RUBY
end

it 'accepts parens in calls with args with logical operators' do
Expand Down

0 comments on commit c7d6d76

Please sign in to comment.