Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 34f77fe

Browse files
committed
Do not create single line conditional if conditional is a single line conditional
1 parent f537c00 commit 34f77fe

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

lib/syntax_tree/node.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4680,6 +4680,11 @@ def format(q)
46804680
return
46814681
end
46824682

4683+
if contains_conditional?
4684+
format_break(q, force: true)
4685+
return
4686+
end
4687+
46834688
if node.consequent || node.statements.empty?
46844689
q.group { format_break(q, force: true) }
46854690
else
@@ -4716,6 +4721,11 @@ def format_break(q, force:)
47164721
q.breakable(force: force)
47174722
q.text("end")
47184723
end
4724+
4725+
def contains_conditional?
4726+
node.statements.body.length == 1 &&
4727+
[If, IfMod, IfOp, Unless, UnlessMod].include?(node.statements.body.first.class)
4728+
end
47194729
end
47204730

47214731
# If represents the first clause in an +if+ chain.

test/fixtures/if.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@
2828
if (foo += 1)
2929
foo
3030
end
31+
%
32+
if foo
33+
a ? b : c
34+
end

test/fixtures/unless.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@
2828
unless (foo += 1)
2929
foo
3030
end
31+
%
32+
unless foo
33+
a ? b : c
34+
end

0 commit comments

Comments
 (0)