Skip to content

Commit

Permalink
Merge pull request #11928 from koic/fix_incorrect_autocorrect_for_lin…
Browse files Browse the repository at this point in the history
…t_ambiguous_block_association

Fix an incorrect autocorrect for `Lint/AmbiguousBlockAssociation`
  • Loading branch information
koic committed Jun 5, 2023
2 parents ab4b73c + 10ec66a commit a796b10
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 a796b10

Please sign in to comment.