Correct update expression Number coercion (#7766)

* Correct update expression Number coercion

You have to `ToNumber` whatever the `UpdateExpression` argument is.

* Fix systemjs update expression
This commit is contained in:
Justin Ridgewell
2018-04-22 13:50:11 -04:00
committed by GitHub
parent 890a45216f
commit 34d73ebef0
7 changed files with 40 additions and 28 deletions

View File

@@ -25,27 +25,49 @@ const simpleAssignmentVisitor = {
}
if (
path.node.prefix ||
(path.parentPath.isExpressionStatement() && !path.isCompletionRecord())
path.parentPath.isExpressionStatement() &&
!path.isCompletionRecord()
) {
// ++i => (i += 1);
const operator = path.node.operator == "++" ? "+=" : "-=";
path.replaceWith(
t.assignmentExpression(operator, arg.node, t.numericLiteral(1)),
);
} else if (path.node.prefix) {
// ++i => (i = (+i) + 1);
path.replaceWith(
t.assignmentExpression(
"=",
t.identifier(localName),
t.binaryExpression(
path.node.operator[0],
t.unaryExpression("+", arg.node),
t.numericLiteral(1),
),
),
);
} else {
const varName = path.scope.generateDeclaredUidIdentifier("old").name;
const old = path.scope.generateUidIdentifierBasedOnNode(
arg.node,
"old",
);
const varName = old.name;
path.scope.push({ id: old });
const binary = t.binaryExpression(
path.node.operator.slice(0, 1),
path.node.operator[0],
t.identifier(varName),
t.numericLiteral(1),
);
// i++ => (_tmp = i, i = _tmp + 1, _tmp)
// i++ => (_old = (+i), i = _old + 1, _old)
path.replaceWith(
t.sequenceExpression([
t.assignmentExpression("=", t.identifier(varName), arg.node),
t.assignmentExpression(
"=",
t.identifier(varName),
t.unaryExpression("+", arg.node),
),
t.assignmentExpression("=", t.cloneNode(arg.node), binary),
t.identifier(varName),
]),