Skip to content

Commit

Permalink
Fix an incorrect autocorrect for Style/SoleNestedConditional
Browse files Browse the repository at this point in the history
This PR fixes an incorrect autocorrect for `Style/SoleNestedConditional` with
`Style/MethodCallWithArgsParentheses`.

```ruby
if items.include? item
  if condition
    do_something
  end
end
```

## Before

```console
$ bundle exec rubocop -a --only Style/MethodCallWithArgsParentheses,Style/SoleNestedConditional
Inspecting 1 file
F

Offenses:

example.rb:1:4: C: [Corrected] Style/MethodCallWithArgsParentheses: Use parentheses for method calls with arguments.
if items.include? item
   ^^^^^^^^^^^^^^^^^^^
example.rb:1:24: F: Lint/Syntax: unexpected token tRPAREN
(Using Ruby 3.3 parser; configure using TargetRubyVersion parameter, under AllCops)
if items.include?(item)) && condition
                       ^
example.rb:2:3: C: [Corrected] Style/SoleNestedConditional: Consider merging nested conditions into outer if conditions.
  if condition
  ^^
example.rb:3:3: F: Lint/Syntax: unexpected token kEND
(Using Ruby 3.3 parser; configure using TargetRubyVersion parameter, under AllCops)
  end
  ^^^

1 file inspected, 4 offenses detected, 2 offenses corrected
```

Syntax error with redundant closing parentheses.

```console
if items.include?(item)) && condition
    do_something
  end
```

## After

Valid syntax.

```console
if items.include?(item) && condition
    do_something
  end
```
  • Loading branch information
koic committed Jun 12, 2023
1 parent 53ffe25 commit 809c1a8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
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

0 comments on commit 809c1a8

Please sign in to comment.