Skip to content

Commit

Permalink
Merge pull request #9646 from koic/fix_incorrect_autocorrect_for_styl…
Browse files Browse the repository at this point in the history
…e_and_or

Fix an incorrect auto-correct for `Style/AndOr`
  • Loading branch information
koic committed Mar 31, 2021
2 parents 4711f1c + c6a6e7b commit 2d1fd7b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_incorrect_autocorrect_for_style_and_or.md
@@ -0,0 +1 @@
* [#9646](https://github.com/rubocop/rubocop/pull/9646): Fix an incorrect auto-correct for `EnforcedStyle: require_parentheses` of `Style/MethodCallWithArgsParentheses` with `EnforcedStyle: conditionals` of `Style/AndOr`. ([@koic][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/style/and_or.rb
Expand Up @@ -94,7 +94,9 @@ def correct_send(node, corrector)

return unless correctable_send?(node)

corrector.replace(whitespace_before_arg(node), '(')
whitespace_before_arg_range = whitespace_before_arg(node)
corrector.remove(whitespace_before_arg_range)
corrector.insert_before(whitespace_before_arg_range, '(')
corrector.insert_after(node.last_argument, ')')
end

Expand Down
21 changes: 21 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Expand Up @@ -215,6 +215,27 @@ def batch
RUBY
end

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

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

0 comments on commit 2d1fd7b

Please sign in to comment.