Skip to content

Commit

Permalink
multiple 'else' in line
Browse files Browse the repository at this point in the history
  • Loading branch information
zdenko committed Jan 19, 2018
1 parent ea6f46a commit e9c4cb6
Show file tree
Hide file tree
Showing 3 changed files with 297 additions and 1 deletion.
36 changes: 35 additions & 1 deletion lib/coffeescript/rewriter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions src/rewriter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,8 @@ exports.Rewriter = class Rewriter
starter = indent = outdent = null
leading_switch_when = null
leading_if_then = null
# Count `THEN` tags
ifThens = []

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

closeElseTag = (tokens, i) =>
tlen = ifThens.length
lastThen = ifThens.pop()
[, outdentElse] = @indentation tokens[lastThen]
if tlen >= 2
outdentElse[1] = tlen*2
# Insert `OUTDENT` tag and close inner `IF`.
tokens.splice(i, 0, outdentElse)
# Remove outdents from the end.
@detectEnd i + 1,
(token, i) -> token[0] in ['OUTDENT', 'TERMINATOR']
(token, i) ->
if @tag(i) is 'OUTDENT' and @tag(i + 1) is 'OUTDENT'
tokens.splice i, 2
outdentElse[1] = 2
i += 1
# Insert `OUTDENT` tag and close outer `IF`.
tokens.splice(i, 0, outdentElse)
i += 1

@scanTokens (token, i, tokens) ->
[tag] = token
conditionTag = tag in ['->', '=>'] and
Expand Down Expand Up @@ -591,6 +613,10 @@ exports.Rewriter = class Rewriter
if tag is 'THEN'
leading_switch_when = @findTagsBackwards(i, ['LEADING_WHEN']) and @tag(i + 1) is 'IF'
leading_if_then = @findTagsBackwards(i, ['IF']) and @tag(i + 1) is 'IF'
ifThens.push i if tag is 'THEN' and @findTagsBackwards(i, ['IF'])
# `ELSE` tag is not closed.
if tag is 'ELSE' and @tag(i - 1) isnt 'OUTDENT' and ifThens.length > 0
i = closeElseTag tokens, i
tokens.splice i + 1, 0, indent
@detectEnd i + 2, condition, action
tokens.splice i, 1 if tag is 'THEN'
Expand Down
236 changes: 236 additions & 0 deletions test/control_flow.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,118 @@ test "#2343: if / then / if / then / else", ->
eq undefined, x()
eq undefined, y()

test "#2343: if / then / if / then / else / else", ->
a = b = yes
c = e = g = no
d = 1
f = 2
h = 3
i = 4
j = 5
k = 6

s = ->
if a
if b
if c
d
else
e
if e
f
else
if g
h
else
i
else
j
else
k

t = ->
if a
if b
if c then d
else if e
f
else if g
h
else
i
else
j
else
k

u = ->
if a
if b
if c then d else if e
f
else if g
h
else i
else j
else k

v = ->
if a
if b
if c then d else if e then f
else if g then h
else i
else j else k

w = ->
if a then if b
if c then d
else if e
f
else
if g then h
else i
else j else k

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

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

eq 4, s()
eq 4, t()
eq 4, u()
eq 4, v()
eq 4, w()
eq 4, x()
eq 4, y()

c = yes
eq 1, s()
eq 1, t()
eq 1, u()
eq 1, v()
eq 1, w()
eq 1, x()
eq 1, y()

b = no
eq 5, s()
eq 5, t()
eq 5, u()
eq 5, v()
eq 5, w()
eq 5, x()
eq 5, y()

a = no
eq 6, s()
eq 6, t()
eq 6, u()
eq 6, v()
eq 6, w()
eq 6, x()
eq 6, y()


test "#2343: switch / when / then / if / then / else", ->
a = b = yes
Expand Down Expand Up @@ -772,6 +884,130 @@ test "#2343: switch / when / then / if / then / else / else", ->
eq 0, x()
eq 0, y()

test "#2343: switch / when / then / if / then / else / else / else", ->
a = b = yes
c = e = g = no
d = 1
f = 2
h = 3
i = 4
j = 5

s = ->
switch
when a
if b
if c
d
else if e
f
else if g
h
else
i
else
j
else
0

t = ->
switch
when a
if b
if c then d
else if e
f
else if g
h
else i
else
j
else 0

u = ->
switch
when a
if b
if c
d
else if e
f
else if g
h
else i
else j
else 0

v = ->
switch
when a
if b
if c then d
else if e
f
else if g then h
else i
else j
else 0

w = ->
switch
when a
if b
if c then d
else if e then f
else if g then h
else i
else j
else 0

x = ->
switch
when a
if b then if c then d else if e then f else if g then h else i else j
else 0

y = -> switch
when a
if b then (if c then d else (if e then f else (if g then h else i))) else j
else 0

eq 4, s()
eq 4, t()
eq 4, u()
eq 4, v()
eq 4, w()
eq 4, x()
eq 4, y()

c = yes
eq 1, s()
eq 1, t()
eq 1, u()
eq 1, v()
eq 1, w()
eq 1, x()
eq 1, y()

b = no
eq 5, s()
eq 5, t()
eq 5, u()
eq 5, v()
eq 5, w()
eq 5, x()
eq 5, y()

b = yes
a = no
eq 0, s()
eq 0, t()
eq 0, u()
eq 0, v()
eq 0, w()
eq 0, x()
eq 0, y()

# Test for issue #3921: Inline function without parentheses used in condition fails to compile
test "#3921: `if` & `unless`", ->
a = {}
Expand Down

0 comments on commit e9c4cb6

Please sign in to comment.