Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an incorrect auto-correct for Style/AndOr #9646

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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