Skip to content

Commit e9c4cb6

Browse files
committed
multiple 'else' in line
1 parent ea6f46a commit e9c4cb6

File tree

3 files changed

+297
-1
lines changed

3 files changed

+297
-1
lines changed

lib/coffeescript/rewriter.js

Lines changed: 35 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rewriter.coffee

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,8 @@ exports.Rewriter = class Rewriter
548548
starter = indent = outdent = null
549549
leading_switch_when = null
550550
leading_if_then = null
551+
# Count `THEN` tags
552+
ifThens = []
551553

552554
condition = (token, i) ->
553555
token[1] isnt ';' and token[0] in SINGLE_CLOSERS and
@@ -561,6 +563,26 @@ exports.Rewriter = class Rewriter
561563
action = (token, i) ->
562564
@tokens.splice (if @tag(i - 1) is ',' then i - 1 else i), 0, outdent
563565

566+
closeElseTag = (tokens, i) =>
567+
tlen = ifThens.length
568+
lastThen = ifThens.pop()
569+
[, outdentElse] = @indentation tokens[lastThen]
570+
if tlen >= 2
571+
outdentElse[1] = tlen*2
572+
# Insert `OUTDENT` tag and close inner `IF`.
573+
tokens.splice(i, 0, outdentElse)
574+
# Remove outdents from the end.
575+
@detectEnd i + 1,
576+
(token, i) -> token[0] in ['OUTDENT', 'TERMINATOR']
577+
(token, i) ->
578+
if @tag(i) is 'OUTDENT' and @tag(i + 1) is 'OUTDENT'
579+
tokens.splice i, 2
580+
outdentElse[1] = 2
581+
i += 1
582+
# Insert `OUTDENT` tag and close outer `IF`.
583+
tokens.splice(i, 0, outdentElse)
584+
i += 1
585+
564586
@scanTokens (token, i, tokens) ->
565587
[tag] = token
566588
conditionTag = tag in ['->', '=>'] and
@@ -591,6 +613,10 @@ exports.Rewriter = class Rewriter
591613
if tag is 'THEN'
592614
leading_switch_when = @findTagsBackwards(i, ['LEADING_WHEN']) and @tag(i + 1) is 'IF'
593615
leading_if_then = @findTagsBackwards(i, ['IF']) and @tag(i + 1) is 'IF'
616+
ifThens.push i if tag is 'THEN' and @findTagsBackwards(i, ['IF'])
617+
# `ELSE` tag is not closed.
618+
if tag is 'ELSE' and @tag(i - 1) isnt 'OUTDENT' and ifThens.length > 0
619+
i = closeElseTag tokens, i
594620
tokens.splice i + 1, 0, indent
595621
@detectEnd i + 2, condition, action
596622
tokens.splice i, 1 if tag is 'THEN'

test/control_flow.coffee

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,118 @@ test "#2343: if / then / if / then / else", ->
571571
eq undefined, x()
572572
eq undefined, y()
573573

574+
test "#2343: if / then / if / then / else / else", ->
575+
a = b = yes
576+
c = e = g = no
577+
d = 1
578+
f = 2
579+
h = 3
580+
i = 4
581+
j = 5
582+
k = 6
583+
584+
s = ->
585+
if a
586+
if b
587+
if c
588+
d
589+
else
590+
e
591+
if e
592+
f
593+
else
594+
if g
595+
h
596+
else
597+
i
598+
else
599+
j
600+
else
601+
k
602+
603+
t = ->
604+
if a
605+
if b
606+
if c then d
607+
else if e
608+
f
609+
else if g
610+
h
611+
else
612+
i
613+
else
614+
j
615+
else
616+
k
617+
618+
u = ->
619+
if a
620+
if b
621+
if c then d else if e
622+
f
623+
else if g
624+
h
625+
else i
626+
else j
627+
else k
628+
629+
v = ->
630+
if a
631+
if b
632+
if c then d else if e then f
633+
else if g then h
634+
else i
635+
else j else k
636+
637+
w = ->
638+
if a then if b
639+
if c then d
640+
else if e
641+
f
642+
else
643+
if g then h
644+
else i
645+
else j else k
646+
647+
x = -> if a then if b then if c then d else if e then f else if g then h else i else j else k
648+
649+
y = -> if a then (if b then (if c then d else (if e then f else (if g then h else i))) else j) else k
650+
651+
eq 4, s()
652+
eq 4, t()
653+
eq 4, u()
654+
eq 4, v()
655+
eq 4, w()
656+
eq 4, x()
657+
eq 4, y()
658+
659+
c = yes
660+
eq 1, s()
661+
eq 1, t()
662+
eq 1, u()
663+
eq 1, v()
664+
eq 1, w()
665+
eq 1, x()
666+
eq 1, y()
667+
668+
b = no
669+
eq 5, s()
670+
eq 5, t()
671+
eq 5, u()
672+
eq 5, v()
673+
eq 5, w()
674+
eq 5, x()
675+
eq 5, y()
676+
677+
a = no
678+
eq 6, s()
679+
eq 6, t()
680+
eq 6, u()
681+
eq 6, v()
682+
eq 6, w()
683+
eq 6, x()
684+
eq 6, y()
685+
574686

575687
test "#2343: switch / when / then / if / then / else", ->
576688
a = b = yes
@@ -772,6 +884,130 @@ test "#2343: switch / when / then / if / then / else / else", ->
772884
eq 0, x()
773885
eq 0, y()
774886

887+
test "#2343: switch / when / then / if / then / else / else / else", ->
888+
a = b = yes
889+
c = e = g = no
890+
d = 1
891+
f = 2
892+
h = 3
893+
i = 4
894+
j = 5
895+
896+
s = ->
897+
switch
898+
when a
899+
if b
900+
if c
901+
d
902+
else if e
903+
f
904+
else if g
905+
h
906+
else
907+
i
908+
else
909+
j
910+
else
911+
0
912+
913+
t = ->
914+
switch
915+
when a
916+
if b
917+
if c then d
918+
else if e
919+
f
920+
else if g
921+
h
922+
else i
923+
else
924+
j
925+
else 0
926+
927+
u = ->
928+
switch
929+
when a
930+
if b
931+
if c
932+
d
933+
else if e
934+
f
935+
else if g
936+
h
937+
else i
938+
else j
939+
else 0
940+
941+
v = ->
942+
switch
943+
when a
944+
if b
945+
if c then d
946+
else if e
947+
f
948+
else if g then h
949+
else i
950+
else j
951+
else 0
952+
953+
w = ->
954+
switch
955+
when a
956+
if b
957+
if c then d
958+
else if e then f
959+
else if g then h
960+
else i
961+
else j
962+
else 0
963+
964+
x = ->
965+
switch
966+
when a
967+
if b then if c then d else if e then f else if g then h else i else j
968+
else 0
969+
970+
y = -> switch
971+
when a
972+
if b then (if c then d else (if e then f else (if g then h else i))) else j
973+
else 0
974+
975+
eq 4, s()
976+
eq 4, t()
977+
eq 4, u()
978+
eq 4, v()
979+
eq 4, w()
980+
eq 4, x()
981+
eq 4, y()
982+
983+
c = yes
984+
eq 1, s()
985+
eq 1, t()
986+
eq 1, u()
987+
eq 1, v()
988+
eq 1, w()
989+
eq 1, x()
990+
eq 1, y()
991+
992+
b = no
993+
eq 5, s()
994+
eq 5, t()
995+
eq 5, u()
996+
eq 5, v()
997+
eq 5, w()
998+
eq 5, x()
999+
eq 5, y()
1000+
1001+
b = yes
1002+
a = no
1003+
eq 0, s()
1004+
eq 0, t()
1005+
eq 0, u()
1006+
eq 0, v()
1007+
eq 0, w()
1008+
eq 0, x()
1009+
eq 0, y()
1010+
7751011
# Test for issue #3921: Inline function without parentheses used in condition fails to compile
7761012
test "#3921: `if` & `unless`", ->
7771013
a = {}

0 commit comments

Comments
 (0)