Skip to content

Commit

Permalink
Fix an incorrect autocorrect for Lint/AmbiguousBlockAssociation
Browse files Browse the repository at this point in the history
This PR fixes the following incorrect autocorrect for
`Lint/AmbiguousBlockAssociation` with `Style/MethodCallWithArgsParentheses`.

```ruby
expect { foo }.not_to change { bar }
```

## Before

```console
$ bundle exec rubocop --only Lint/AmbiguousBlockAssociation,Style/MethodCallWithArgsParentheses -a
Inspecting 1 file
F

Offenses:

example.rb:1:1: W: [Corrected] Lint/AmbiguousBlockAssociation: Parenthesize the param change { bar } to make sure that the block will be associated with the change method call.
expect { foo }.not_to change { bar }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
example.rb:1:1: C: [Corrected] Style/MethodCallWithArgsParentheses: Use parentheses for method calls with arguments.
expect { foo }.not_to change { bar }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
example.rb:1:38: F: Lint/Syntax: unexpected token tRPAREN
(Using Ruby 3.2 parser; configure using TargetRubyVersion parameter, under AllCops)
expect { foo }.not_to(change { bar }))
                                     ^

1 file inspected, 3 offenses detected, 2 offenses corrected
```

Syntax error with redundant closing parentheses.

```diff
$ git diff .
diff --git a/x/example.rb b/x/example.rb
index 142ff55..6de7188 100644
--- a/x/example.rb
+++ b/x/example.rb
@@ -1 +1 @@
-expect { foo }.not_to change { bar }
+expect { foo }.not_to(change { bar }))
```

## After

Valid syntax.

```diff
$ git diff .
diff --git a/x/example.rb b/x/example.rb
index 142ff55..7808ed5 100644
--- a/x/example.rb
+++ b/x/example.rb
@@ -1 +1 @@
-expect { foo }.not_to change { bar }
+expect { foo }.not_to(change { bar })
```
  • Loading branch information
koic committed Jun 5, 2023
1 parent ab4b73c commit 10ec66a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11928](https://github.com/rubocop/rubocop/pull/11928): Fix an incorrect autocorrect for `Lint/AmbiguousBlockAssociation`. ([@koic][])
3 changes: 2 additions & 1 deletion lib/rubocop/cop/lint/ambiguous_block_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def message(send_node)
def wrap_in_parentheses(corrector, node)
range = node.loc.selector.end.join(node.first_argument.source_range.begin)

corrector.replace(range, '(')
corrector.remove(range)
corrector.insert_before(range, '(')
corrector.insert_after(node.last_argument, ')')
end
end
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,28 @@ def batch
RUBY
end

it 'corrects `EnforcedStyle: require_parentheses` of `Style/MethodCallWithArgsParentheses` with ' \
'`Lint/AmbiguousBlockAssociation`' do
create_file('.rubocop.yml', <<~YAML)
Style/MethodCallWithArgsParentheses:
EnforcedStyle: require_parentheses
YAML
create_file('example.rb', <<~RUBY)
expect { foo }.not_to change { bar }
RUBY
expect(
cli.run(
[
'--autocorrect',
'--only', 'Style/MethodCallWithArgsParentheses,Lint/AmbiguousBlockAssociation'
]
)
).to eq(0)
expect(File.read('example.rb')).to eq(<<~RUBY)
expect { foo }.not_to(change { bar })
RUBY
end

it 'corrects `EnforcedStyle: require_parentheses` of `Style/MethodCallWithArgsParentheses` with ' \
'`Lint/AmbiguousOperator`' do
create_file('.rubocop.yml', <<~YAML)
Expand Down

0 comments on commit 10ec66a

Please sign in to comment.