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

Support auto-correction for Style/IfUnlessModifierOfIfUnless #8223

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.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