Skip to content

Commit f70f4f0

Browse files
helixbassGeoffreyBooth
authored andcommitted
conditional else as simple expression (#5171)
1 parent 3f5abb3 commit f70f4f0

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

lib/coffeescript/nodes.js

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

src/nodes.coffee

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4868,6 +4868,8 @@ exports.If = class If extends Base
48684868
alternate:
48694869
if @isChain
48704870
@elseBody.unwrap().ast o, if isStatement then LEVEL_TOP else LEVEL_COND
4871+
else if not isStatement and @elseBody?.expressions?.length is 1
4872+
@elseBody.expressions[0].ast o, LEVEL_TOP
48714873
else
48724874
@elseBody?.ast(o, LEVEL_TOP) ? null
48734875
postfix: !!@postfix

test/abstract_syntax_tree.coffee

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,31 +2534,32 @@ test "AST as expected for If node", ->
25342534
postfix: no
25352535
inverted: no
25362536

2537-
testExpression '''
2538-
f(
2539-
if b
2540-
c
2541-
d
2542-
)
2543-
''',
2544-
type: 'CallExpression'
2545-
arguments: [
2546-
type: 'ConditionalExpression'
2547-
test: ID 'b'
2548-
consequent:
2549-
type: 'BlockStatement'
2550-
body: [
2551-
type: 'ExpressionStatement'
2552-
expression:
2553-
ID 'c'
2554-
,
2555-
type: 'ExpressionStatement'
2556-
expression:
2557-
ID 'd'
2558-
]
2559-
postfix: no
2560-
inverted: no
2561-
]
2537+
testExpression '''
2538+
f(
2539+
if b
2540+
c
2541+
d
2542+
)
2543+
''',
2544+
type: 'CallExpression'
2545+
arguments: [
2546+
type: 'ConditionalExpression'
2547+
test: ID 'b'
2548+
consequent:
2549+
type: 'BlockStatement'
2550+
body: [
2551+
type: 'ExpressionStatement'
2552+
expression:
2553+
ID 'c'
2554+
,
2555+
type: 'ExpressionStatement'
2556+
expression:
2557+
ID 'd'
2558+
]
2559+
alternate: null
2560+
postfix: no
2561+
inverted: no
2562+
]
25622563

25632564
testStatement 'a unless b',
25642565
type: 'IfStatement'
@@ -2573,6 +2574,24 @@ test "AST as expected for If node", ->
25732574
postfix: yes
25742575
inverted: yes
25752576

2577+
testExpression '''
2578+
f(
2579+
if b
2580+
c
2581+
else
2582+
d
2583+
)
2584+
''',
2585+
type: 'CallExpression'
2586+
arguments: [
2587+
type: 'ConditionalExpression'
2588+
test: ID 'b'
2589+
consequent: ID 'c'
2590+
alternate: ID 'd'
2591+
postfix: no
2592+
inverted: no
2593+
]
2594+
25762595
test "AST as expected for MetaProperty node", ->
25772596
testExpression '''
25782597
-> new.target

0 commit comments

Comments
 (0)