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/SoleNestedConditional #9114

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
@@ -0,0 +1 @@
* [#9114](https://github.com/rubocop-hq/rubocop/pull/9114): Support auto-correction for `Style/SoleNestedConditional`. ([@koic][])
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -4273,6 +4273,7 @@ Style/SoleNestedConditional:
which can be merged into outer conditional node.
Enabled: true
VersionAdded: '0.89'
VersionChanged: '<<next>>'
AllowModifier: false

Style/SpecialGlobalVars:
Expand Down
52 changes: 49 additions & 3 deletions lib/rubocop/cop/style/sole_nested_conditional.rb
Expand Up @@ -33,17 +33,22 @@ module Style
# end
#
class SoleNestedConditional < Base
include RangeHelp
extend AutoCorrector

MSG = 'Consider merging nested conditions into '\
'outer `%<conditional_type>s` conditions.'

def on_if(node)
return if node.ternary? || node.else? || node.elsif?

branch = node.if_branch
return unless offending_branch?(branch)
if_branch = node.if_branch
return unless offending_branch?(if_branch)

message = format(MSG, conditional_type: node.keyword)
add_offense(branch.loc.keyword, message: message)
add_offense(if_branch.loc.keyword, message: message) do |corrector|
autocorrect(corrector, node, if_branch)
end
end

private
Expand All @@ -57,6 +62,47 @@ def offending_branch?(branch)
!(branch.modifier_form? && allow_modifier?)
end

def autocorrect(corrector, node, if_branch)
if node.unless?
corrector.replace(node.loc.keyword, 'if')
corrector.insert_before(node.condition, '!')
end

and_operator = if_branch.unless? ? ' && !' : ' && '
if if_branch.modifier_form?
correct_for_gurad_condition_style(corrector, node, if_branch, and_operator)
else
correct_for_basic_condition_style(corrector, node, if_branch, and_operator)
end

correct_for_comment(corrector, node, if_branch)
end

def correct_for_gurad_condition_style(corrector, node, if_branch, and_operator)
corrector.insert_after(node.condition, "#{and_operator}#{if_branch.condition.source}")

range = range_between(
if_branch.loc.keyword.begin_pos, if_branch.condition.source_range.end_pos
)
corrector.remove(range_with_surrounding_space(range: range, newlines: false))
corrector.remove(if_branch.loc.keyword)
end

def correct_for_basic_condition_style(corrector, node, if_branch, and_operator)
range = range_between(
node.condition.source_range.end_pos, if_branch.condition.source_range.begin_pos
)
corrector.replace(range, and_operator)
corrector.remove(range_by_whole_lines(node.loc.end, include_final_newline: true))
end

def correct_for_comment(corrector, node, if_branch)
comments = processed_source.comments_before_line(if_branch.source_range.line)
comment_text = comments.map(&:text).join("\n") << "\n"

corrector.insert_before(node.loc.keyword, comment_text) unless comments.empty?
end

def allow_modifier?
cop_config['AllowModifier']
end
Expand Down
72 changes: 65 additions & 7 deletions spec/rubocop/cop/style/sole_nested_conditional_spec.rb
Expand Up @@ -5,7 +5,7 @@
{ 'AllowModifier' => false }
end

it 'registers an offense when using nested `if` within `if`' do
it 'registers an offense and corrects when using nested `if` within `if`' do
expect_offense(<<~RUBY)
if foo
if bar
Expand All @@ -14,9 +14,15 @@
end
end
RUBY

expect_correction(<<~RUBY)
if foo && bar
do_something
end
RUBY
end

it 'registers an offense when using nested `unless` within `if`' do
it 'registers an offense and corrects when using nested `unless` within `if`' do
expect_offense(<<~RUBY)
if foo
unless bar
Expand All @@ -25,9 +31,15 @@
end
end
RUBY

expect_correction(<<~RUBY)
if foo && !bar
do_something
end
RUBY
end

it 'registers an offense when using nested `if` within `unless`' do
it 'registers an offense and corrects when using nested `if` within `unless`' do
expect_offense(<<~RUBY)
unless foo
if bar
Expand All @@ -36,9 +48,15 @@
end
end
RUBY

expect_correction(<<~RUBY)
if !foo && bar
do_something
end
RUBY
end

it 'registers an offense when using nested `unless` within `unless`' do
it 'registers an offense and corrects when using nested `unless` within `unless`' do
expect_offense(<<~RUBY)
unless foo
unless bar
Expand All @@ -47,6 +65,12 @@
end
end
RUBY

expect_correction(<<~RUBY)
if !foo && !bar
do_something
end
RUBY
end

it 'does not register an offense when using nested conditional within `elsif`' do
Expand All @@ -59,16 +83,37 @@
RUBY
end

it 'registers an offense when using nested modifier conditional' do
it 'registers an offense and corrects when using nested `if` modifier conditional' do
expect_offense(<<~RUBY)
if foo
do_something if bar
^^ Consider merging nested conditions into outer `if` conditions.
end
RUBY

expect_correction(<<~RUBY)
if foo && bar
do_something
end
RUBY
end

it 'registers an offense for multiple nested conditionals' do
it 'registers an offense and corrects when using nested `unless` modifier conditional' do
expect_offense(<<~RUBY)
if foo
do_something unless bar
^^^^^^ Consider merging nested conditions into outer `if` conditions.
end
RUBY

expect_correction(<<~RUBY)
if foo && !bar
do_something
end
RUBY
end

it 'registers an offense and corrects for multiple nested conditionals' do
expect_offense(<<~RUBY)
if foo
if bar
Expand All @@ -80,9 +125,15 @@
end
end
RUBY

expect_correction(<<~RUBY)
if foo && bar && baz
do_something
end
RUBY
end

it 'registers an offense when using nested conditional and branch contains a comment' do
it 'registers an offense and corrects when using nested conditional and branch contains a comment' do
expect_offense(<<~RUBY)
if foo
# Comment.
Expand All @@ -92,6 +143,13 @@
end
end
RUBY

expect_correction(<<~RUBY)
# Comment.
if foo && bar
do_something
end
RUBY
end

it 'does not register an offense when using nested ternary within conditional' do
Expand Down