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 autocorrect for Style/SoleNestedConditional #11944

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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11944](https://github.com/rubocop/rubocop/pull/11944): Fix an incorrect autocorrect for `Style/SoleNestedConditional` with `Style/MethodCallWithArgsParentheses`. ([@koic][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/style/sole_nested_conditional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ def correct_outer_condition(corrector, condition)
begin_pos = condition.first_argument.source_range.begin_pos
return if end_pos > begin_pos

corrector.replace(range_between(end_pos, begin_pos), '(')
range = range_between(end_pos, begin_pos)
corrector.remove(range)
corrector.insert_after(range, '(')
corrector.insert_after(condition.last_argument, ')')
end

Expand Down
28 changes: 28 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,34 @@ def foo(&block)
RUBY
end

it 'corrects `EnforcedStyle: require_parentheses` of `Style/MethodCallWithArgsParentheses` with ' \
'`Style/SoleNestedConditional`' do
create_file('.rubocop.yml', <<~YAML)
Style/MethodCallWithArgsParentheses:
EnforcedStyle: require_parentheses
YAML
create_file('example.rb', <<~RUBY)
if items.include? item
if condition
do_something
end
end
RUBY
expect(
cli.run(
[
'--autocorrect',
'--only', 'Style/MethodCallWithArgsParentheses,Style/SoleNestedConditional'
]
)
).to eq(0)
expect(File.read('example.rb')).to eq(<<~RUBY)
if items.include?(item) && condition
do_something
end
RUBY
end

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