Skip to content

Commit

Permalink
Merge pull request #9671 from koic/fix_incorrect_autocorrect_for_ambi…
Browse files Browse the repository at this point in the history
…guous_operator

Fix an incorrect auto-correct for `Lint/AmbiguousOperator`
  • Loading branch information
koic committed Apr 3, 2021
2 parents 1cd90c7 + e0342b2 commit f4e72bc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
@@ -0,0 +1 @@
* [#9671](https://github.com/rubocop/rubocop/pull/9671): Fix an incorrect auto-correct for `Lint/AmbiguousOperator` with `Style/MethodCallWithArgsParentheses`. ([@koic][])
5 changes: 4 additions & 1 deletion lib/rubocop/cop/util.rb
Expand Up @@ -38,7 +38,10 @@ def add_parentheses(node, corrector)
elsif node.arguments.empty?
corrector.insert_after(node, '()')
else
corrector.replace(args_begin(node), '(')
args_begin = args_begin(node)

corrector.remove(args_begin)
corrector.insert_before(args_begin, '(')
corrector.insert_after(args_end(node), ')')
end
end
Expand Down
23 changes: 23 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Expand Up @@ -236,6 +236,29 @@ def batch
RUBY
end

it 'corrects `EnforcedStyle: require_parentheses` of `Style/MethodCallWithArgsParentheses` with ' \
'`Lint/AmbiguousOperator`' do
create_file('.rubocop.yml', <<~YAML)
Style/MethodCallWithArgsParentheses:
EnforcedStyle: require_parentheses
YAML
create_file('example.rb', <<~RUBY)
def foo(&block)
do_something &block
end
RUBY
expect(
cli.run(
['--auto-correct', '--only', 'Style/MethodCallWithArgsParentheses,Lint/AmbiguousOperator']
)
).to eq(0)
expect(IO.read('example.rb')).to eq(<<~RUBY)
def foo(&block)
do_something(&block)
end
RUBY
end

it 'corrects `Style/IfUnlessModifier` with `Style/SoleNestedConditional`' do
source = <<~RUBY
def foo
Expand Down

0 comments on commit f4e72bc

Please sign in to comment.