Clean up transformations after #714.
Since now we have runtime helper, we don't need expression -> statement conversions anymore.
This commit is contained in:
@@ -12,51 +12,21 @@ function transformExpression(node, scope, state) {
|
||||
return (function subTransform(node) {
|
||||
switch (node.type) {
|
||||
case "ConditionalExpression":
|
||||
var callConsequent = subTransform(node.consequent);
|
||||
var callAlternate = subTransform(node.alternate);
|
||||
if (!callConsequent && !callAlternate) {
|
||||
return;
|
||||
}
|
||||
|
||||
// if ternary operator had tail recursion in value, convert to optimized if-statement
|
||||
node.type = "IfStatement";
|
||||
node.consequent = callConsequent ? t.toBlock(callConsequent) : returnBlock(node.consequent);
|
||||
if (callAlternate) {
|
||||
node.alternate = t.isIfStatement(callAlternate) ? callAlternate : t.toBlock(callAlternate);
|
||||
} else {
|
||||
node.alternate = returnBlock(node.alternate);
|
||||
}
|
||||
return [node];
|
||||
// any value of ternary operator can be final one
|
||||
subTransform(node.consequent);
|
||||
subTransform(node.alternate);
|
||||
break;
|
||||
|
||||
case "LogicalExpression":
|
||||
// only call in right-value of can be optimized
|
||||
var callRight = subTransform(node.right);
|
||||
if (!callRight) {
|
||||
return;
|
||||
}
|
||||
|
||||
var test = state.wrapSideEffect(node.left);
|
||||
if (node.operator === "&&") {
|
||||
test.expr = t.unaryExpression("!", test.expr);
|
||||
}
|
||||
return [t.ifStatement(test.expr, returnBlock(test.ref))].concat(callRight);
|
||||
// only right expression can be final and so optimized
|
||||
subTransform(node.right);
|
||||
break;
|
||||
|
||||
case "SequenceExpression":
|
||||
// only last element of sequence can be optimized
|
||||
var seq = node.expressions;
|
||||
|
||||
// only last element can be optimized
|
||||
var lastCall = subTransform(seq[seq.length - 1]);
|
||||
if (!lastCall) {
|
||||
return;
|
||||
}
|
||||
|
||||
// remove converted expression from sequence
|
||||
// and convert to regular expression if needed
|
||||
if (--seq.length === 1) {
|
||||
node = seq[0];
|
||||
}
|
||||
|
||||
return [t.expressionStatement(node)].concat(lastCall);
|
||||
subTransform(seq[seq.length - 1]);
|
||||
break;
|
||||
|
||||
case "CallExpression":
|
||||
var callee = node.callee, thisBinding;
|
||||
@@ -77,10 +47,9 @@ function transformExpression(node, scope, state) {
|
||||
args.push(thisBinding);
|
||||
}
|
||||
|
||||
return [t.returnStatement(t.callExpression(
|
||||
state.getHelperRef(),
|
||||
args
|
||||
))];
|
||||
node.callee = state.getHelperRef();
|
||||
node.arguments = args;
|
||||
break;
|
||||
}
|
||||
})(node);
|
||||
}
|
||||
@@ -92,7 +61,8 @@ var functionChildrenVisitor = {
|
||||
this.skip();
|
||||
// transform return argument into statement if
|
||||
// it contains tail recursion
|
||||
return transformExpression(node.argument, scope, state);
|
||||
transformExpression(node.argument, scope, state);
|
||||
return;
|
||||
} else if (t.isFunction(node)) {
|
||||
return this.skip();
|
||||
} else if (t.isTryStatement(parent)) {
|
||||
|
||||
Reference in New Issue
Block a user