Skip to content

Commit 300e29b

Browse files
koicbbatsov
authored andcommitted
Fix an error for Style/SoleNestedConditional
This PR fixes the following error for `Style/SoleNestedConditional` when using nested single line `if`: ```console $ echo "if foo; if bar; end; end" | bundle exec rubocop --stdin example.rb --only Style/SoleNestedConditional -A -d (snip) Inspecting 1 file Scanning /Users/koic/src/github.com/rubocop/rubocop/example.rb An error occurred while Style/SoleNestedConditional cop was inspecting /Users/koic/src/github.com/rubocop/rubocop/example.rb:1:0. /Users/koic/.rbenv/versions/3.5-dev/lib/ruby/gems/3.5.0+4/gems/parser-3.3.10.0/lib/parser/source/tree_rewriter.rb:427: in 'Parser::Source::TreeRewriter#trigger_policy': Parser::Source::TreeRewriter detected clobbering (Parser::ClobberingError) ```
1 parent eb0b07a commit 300e29b

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#14631](https://github.com/rubocop/rubocop/pull/14631): Fix an error for `Style/SoleNestedConditional` when using nested single line `if`. ([@koic][])

lib/rubocop/cop/style/sole_nested_conditional.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def correct_for_guard_condition_style(corrector, node, if_branch)
129129
corrector.remove(range_with_surrounding_space(range, newlines: false))
130130
end
131131

132+
# rubocop:disable Metrics/AbcSize
132133
def correct_for_basic_condition_style(corrector, node, if_branch)
133134
range = range_between(
134135
node.condition.source_range.end_pos, if_branch.condition.source_range.begin_pos
@@ -137,8 +138,14 @@ def correct_for_basic_condition_style(corrector, node, if_branch)
137138

138139
corrector.replace(if_branch.condition, chainable_condition(if_branch))
139140

140-
corrector.remove(range_by_whole_lines(node.loc.end, include_final_newline: true))
141+
end_range = if same_line?(node.loc.end, node.if_branch.loc.end)
142+
node.loc.end
143+
else
144+
range_by_whole_lines(node.loc.end, include_final_newline: true)
145+
end
146+
corrector.remove(end_range)
141147
end
148+
# rubocop:enable Metrics/AbcSize
142149

143150
def autocorrect_outer_condition_modify_form(corrector, node, if_branch)
144151
correct_node(corrector, if_branch)

spec/rubocop/cop/style/sole_nested_conditional_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,17 @@ def foo
681681
RUBY
682682
end
683683

684+
it 'registers an offense and corrects when using nested single line `if`' do
685+
expect_offense(<<~RUBY)
686+
if foo; if bar; end; end
687+
^^ Consider merging nested conditions into outer `if` conditions.
688+
RUBY
689+
690+
expect_correction(<<~RUBY)
691+
if foo && bar; end;#{' '}
692+
RUBY
693+
end
694+
684695
context 'when disabling `Style/IfUnlessModifier`' do
685696
let(:config) { RuboCop::Config.new('Style/IfUnlessModifier' => { 'Enabled' => false }) }
686697

0 commit comments

Comments
 (0)