conditional else as simple expression (#5171)

This commit is contained in:
Julian Rosse
2019-03-17 03:01:46 -04:00
committed by Geoffrey Booth
parent 3f5abb3c60
commit f70f4f09b7
3 changed files with 48 additions and 27 deletions

View File

@@ -7348,12 +7348,12 @@
}
astProperties(o) {
var isStatement, ref1, ref2;
var isStatement, ref1, ref2, ref3, ref4;
isStatement = this.isStatementAst(o);
return {
test: this.condition.ast(o, isStatement ? LEVEL_PAREN : LEVEL_COND),
consequent: isStatement ? this.body.ast(o, LEVEL_TOP) : this.bodyNode().ast(o, LEVEL_TOP),
alternate: this.isChain ? this.elseBody.unwrap().ast(o, isStatement ? LEVEL_TOP : LEVEL_COND) : (ref1 = (ref2 = this.elseBody) != null ? ref2.ast(o, LEVEL_TOP) : void 0) != null ? ref1 : null,
alternate: this.isChain ? this.elseBody.unwrap().ast(o, isStatement ? LEVEL_TOP : LEVEL_COND) : !isStatement && ((ref1 = this.elseBody) != null ? (ref2 = ref1.expressions) != null ? ref2.length : void 0 : void 0) === 1 ? this.elseBody.expressions[0].ast(o, LEVEL_TOP) : (ref3 = (ref4 = this.elseBody) != null ? ref4.ast(o, LEVEL_TOP) : void 0) != null ? ref3 : null,
postfix: !!this.postfix,
inverted: this.type === 'unless'
};

View File

@@ -4868,6 +4868,8 @@ exports.If = class If extends Base
alternate:
if @isChain
@elseBody.unwrap().ast o, if isStatement then LEVEL_TOP else LEVEL_COND
else if not isStatement and @elseBody?.expressions?.length is 1
@elseBody.expressions[0].ast o, LEVEL_TOP
else
@elseBody?.ast(o, LEVEL_TOP) ? null
postfix: !!@postfix

View File

@@ -2534,31 +2534,32 @@ test "AST as expected for If node", ->
postfix: no
inverted: no
testExpression '''
f(
if b
c
d
)
''',
type: 'CallExpression'
arguments: [
type: 'ConditionalExpression'
test: ID 'b'
consequent:
type: 'BlockStatement'
body: [
type: 'ExpressionStatement'
expression:
ID 'c'
,
type: 'ExpressionStatement'
expression:
ID 'd'
]
postfix: no
inverted: no
]
testExpression '''
f(
if b
c
d
)
''',
type: 'CallExpression'
arguments: [
type: 'ConditionalExpression'
test: ID 'b'
consequent:
type: 'BlockStatement'
body: [
type: 'ExpressionStatement'
expression:
ID 'c'
,
type: 'ExpressionStatement'
expression:
ID 'd'
]
alternate: null
postfix: no
inverted: no
]
testStatement 'a unless b',
type: 'IfStatement'
@@ -2573,6 +2574,24 @@ test "AST as expected for If node", ->
postfix: yes
inverted: yes
testExpression '''
f(
if b
c
else
d
)
''',
type: 'CallExpression'
arguments: [
type: 'ConditionalExpression'
test: ID 'b'
consequent: ID 'c'
alternate: ID 'd'
postfix: no
inverted: no
]
test "AST as expected for MetaProperty node", ->
testExpression '''
-> new.target