Skip to content

Commit 10ec66a

Browse files
committed
Fix an incorrect autocorrect for Lint/AmbiguousBlockAssociation
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 }) ```
1 parent ab4b73c commit 10ec66a

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#11928](https://github.com/rubocop/rubocop/pull/11928): Fix an incorrect autocorrect for `Lint/AmbiguousBlockAssociation`. ([@koic][])

lib/rubocop/cop/lint/ambiguous_block_association.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ def message(send_node)
9797
def wrap_in_parentheses(corrector, node)
9898
range = node.loc.selector.end.join(node.first_argument.source_range.begin)
9999

100-
corrector.replace(range, '(')
100+
corrector.remove(range)
101+
corrector.insert_before(range, '(')
101102
corrector.insert_after(node.last_argument, ')')
102103
end
103104
end

spec/rubocop/cli/autocorrect_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,28 @@ def batch
341341
RUBY
342342
end
343343

344+
it 'corrects `EnforcedStyle: require_parentheses` of `Style/MethodCallWithArgsParentheses` with ' \
345+
'`Lint/AmbiguousBlockAssociation`' do
346+
create_file('.rubocop.yml', <<~YAML)
347+
Style/MethodCallWithArgsParentheses:
348+
EnforcedStyle: require_parentheses
349+
YAML
350+
create_file('example.rb', <<~RUBY)
351+
expect { foo }.not_to change { bar }
352+
RUBY
353+
expect(
354+
cli.run(
355+
[
356+
'--autocorrect',
357+
'--only', 'Style/MethodCallWithArgsParentheses,Lint/AmbiguousBlockAssociation'
358+
]
359+
)
360+
).to eq(0)
361+
expect(File.read('example.rb')).to eq(<<~RUBY)
362+
expect { foo }.not_to(change { bar })
363+
RUBY
364+
end
365+
344366
it 'corrects `EnforcedStyle: require_parentheses` of `Style/MethodCallWithArgsParentheses` with ' \
345367
'`Lint/AmbiguousOperator`' do
346368
create_file('.rubocop.yml', <<~YAML)

0 commit comments

Comments
 (0)