Skip to content

Commit

Permalink
[Fix rubocop#7962] Fix a false positive for `Lint/ParenthesesAsGroupe…
Browse files Browse the repository at this point in the history
…dExpression`

Fixes rubocop#7962.

This PR fixes the following false positive for `Lint/ParenthesesAsGroupedExpression`
when heredoc has a space between the same string as the method name and `(`.

```ruby
foo(
  <<~EOS
    foo (
    )
  EOS
)
```

This PR adds a reproduction test and reverts a required logic removed by rubocop#7909.
  • Loading branch information
koic committed May 13, 2020
1 parent 9731d67 commit 56fe59e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#7953](https://github.com/rubocop-hq/rubocop/issues/7953): Fix an error for `Lint/AmbiguousOperator` when a method with no arguments is used in advance. ([@koic][])
* [#7962](https://github.com/rubocop-hq/rubocop/issues/7962): Fix a false positive for `Lint/ParenthesesAsGroupedExpression` when heredoc has a space between the same string as the method name and `(`. ([@koic][])

### Changes

Expand Down
14 changes: 11 additions & 3 deletions lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb
Expand Up @@ -21,9 +21,7 @@ class ParenthesesAsGroupedExpression < Cop
MSG = '`(...)` interpreted as grouped expression.'

def on_send(node)
return unless node.arguments.one?
return if node.operator_method? || node.setter_method?
return if grouped_parentheses?(node)
return if valid_context?(node)

space_length = spaces_before_left_parenthesis(node)
return unless space_length.positive?
Expand All @@ -45,6 +43,16 @@ def autocorrect(node)

private

def valid_context?(node)
return true unless node.arguments.one? && first_arugment_starts_with_left_parenthesis?(node)

node.operator_method? || node.setter_method? || grouped_parentheses?(node)
end

def first_arugment_starts_with_left_parenthesis?(node)
node.first_argument.source.start_with?('(')
end

def grouped_parentheses?(node)
first_argument = node.first_argument

Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb
Expand Up @@ -74,6 +74,17 @@
expect_no_offenses('assert_equal (0..1.9), acceleration.domain')
end

it 'does not register an offesne when heredoc has a space between the same string as the method name and `(`' do
expect_no_offenses(<<~RUBY)
foo(
<<~EOS
foo (
)
EOS
)
RUBY
end

context 'when using safe navigation operator' do
it 'registers an offense and corrects for method call with space before ' \
'the parenthesis' do
Expand Down

0 comments on commit 56fe59e

Please sign in to comment.