Skip to content

Commit

Permalink
Support auto-correction for Style/IfUnlessModifierOfIfUnless
Browse files Browse the repository at this point in the history
This PR supports auto-correction for `Style/IfUnlessModifierOfIfUnless`.

From:

```ruby
condition ? then_part : else_part unless external_condition
```

To:

```ruby
unless external_condition
condition ? then_part : else_part
end
```

Branch indentation is left to `Layout/IndentationWidth` cop to keep the code simple.
  • Loading branch information
koic authored and bbatsov committed Jun 29, 2020
1 parent e8269eb commit a26b716
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
* [#7868](https://github.com/rubocop-hq/rubocop/pull/7868): `Cop::Base` is the new recommended base class for cops. ([@marcandre][])
* [#8213](https://github.com/rubocop-hq/rubocop/pull/8213): Permit to specify TargetRubyVersion 2.8 (experimental). ([@koic][])
* [#8164](https://github.com/rubocop-hq/rubocop/pull/8164): Support auto-correction for `Lint/InterpolationCheck`. ([@koic][])
* [#8223](https://github.com/rubocop-hq/rubocop/pull/8223): Support auto-correction for `Style/IfUnlessModifierOfIfUnless`. ([@koic][])

### Bug fixes

Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -2997,6 +2997,7 @@ Style/IfUnlessModifierOfIfUnless:
Avoid modifier if/unless usage on conditionals.
Enabled: true
VersionAdded: '0.39'
VersionChanged: '0.87'

Style/IfWithSemicolon:
Description: 'Do not use if x; .... Use the ternary operator instead.'
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/ROOT/pages/cops_style.adoc
Expand Up @@ -3683,9 +3683,9 @@ end

| Enabled
| Yes
| No
| Yes
| 0.39
| -
| 0.87
|===

Checks for if and unless statements used as modifiers of other if or
Expand Down
12 changes: 12 additions & 0 deletions lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb
Expand Up @@ -33,6 +33,18 @@ def on_if(node)
add_offense(node, location: :keyword,
message: format(MSG, keyword: node.keyword))
end

def autocorrect(node)
lambda do |corrector|
keyword = node.if? ? 'if' : 'unless'

corrector.replace(node, <<~RUBY.chop)
#{keyword} #{node.condition.source}
#{node.if_branch.source}
end
RUBY
end
end
end
end
end
Expand Down
47 changes: 45 additions & 2 deletions spec/rubocop/cop/style/if_unless_modifier_of_if_unless_spec.rb
Expand Up @@ -10,25 +10,68 @@
condition ? then_part : else_part unless external_condition
^^^^^^ Avoid modifier `unless` after another conditional.
RUBY

expect_correction(<<~RUBY)
unless external_condition
condition ? then_part : else_part
end
RUBY
end

context 'ternary with modifier' do
it 'registers an offense' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
condition ? then_part : else_part unless external_condition
^^^^^^ Avoid modifier `unless` after another conditional.
RUBY

expect_correction(<<~RUBY)
unless external_condition
condition ? then_part : else_part
end
RUBY
end
end

context 'conditional with modifier' do
it 'registers an offense' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
unless condition
then_part
end if external_condition
^^ Avoid modifier `if` after another conditional.
RUBY

expect_correction(<<~RUBY)
if external_condition
unless condition
then_part
end
end
RUBY
end
end

context '`unless` / `else` with modifier' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
unless condition
then_part
else
else_part
end if external_condition
^^ Avoid modifier `if` after another conditional.
RUBY

expect_correction(<<~RUBY)
if external_condition
unless condition
then_part
else
else_part
end
end
RUBY
end
end

Expand Down

0 comments on commit a26b716

Please sign in to comment.