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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 40 additions and 28 deletions

View File

@ -25,27 +25,49 @@ const simpleAssignmentVisitor = {
} }
if ( if (
path.node.prefix || path.parentPath.isExpressionStatement() &&
(path.parentPath.isExpressionStatement() && !path.isCompletionRecord()) !path.isCompletionRecord()
) { ) {
// ++i => (i += 1); // ++i => (i += 1);
const operator = path.node.operator == "++" ? "+=" : "-="; const operator = path.node.operator == "++" ? "+=" : "-=";
path.replaceWith( path.replaceWith(
t.assignmentExpression(operator, arg.node, t.numericLiteral(1)), 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 { } 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( const binary = t.binaryExpression(
path.node.operator.slice(0, 1), path.node.operator[0],
t.identifier(varName), t.identifier(varName),
t.numericLiteral(1), t.numericLiteral(1),
); );
// i++ => (_tmp = i, i = _tmp + 1, _tmp) // i++ => (_old = (+i), i = _old + 1, _old)
path.replaceWith( path.replaceWith(
t.sequenceExpression([ 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.assignmentExpression("=", t.cloneNode(arg.node), binary),
t.identifier(varName), t.identifier(varName),
]), ]),

View File

@ -5,9 +5,9 @@ Object.defineProperty(exports, "__esModule", {
}); });
exports.zz = exports.yy = void 0; exports.zz = exports.yy = void 0;
var _old; var _yy;
var yy = 0; var yy = 0;
exports.yy = yy; exports.yy = yy;
var zz = (_old = yy, exports.yy = yy = _old + 1, _old); var zz = (_yy = +yy, exports.yy = yy = _yy + 1, _yy);
exports.zz = zz; exports.zz = zz;

View File

@ -9,7 +9,7 @@ let diffLevel = 0;
exports.diffLevel = diffLevel; exports.diffLevel = diffLevel;
function diff() { function diff() {
if (!(exports.diffLevel = diffLevel = diffLevel - 1)) { if (!(exports.diffLevel = diffLevel = +diffLevel - 1)) {
console.log("hey"); console.log("hey");
} }
} }

View File

@ -9,7 +9,7 @@ let diffLevel = 0;
exports.diffLevel = diffLevel; exports.diffLevel = diffLevel;
function diff() { function diff() {
if (!(exports.diffLevel = diffLevel = diffLevel + 1)) { if (!(exports.diffLevel = diffLevel = +diffLevel + 1)) {
console.log("hey"); console.log("hey");
} }
} }

View File

@ -50,23 +50,13 @@ export default declare((api, options) => {
// if it is a non-prefix update expression (x++ etc) // if it is a non-prefix update expression (x++ etc)
// then we must replace with the expression (_export('x', x + 1), x++) // then we must replace with the expression (_export('x', x + 1), x++)
// in order to ensure the same update expression value // in order to ensure the same update expression value
let isPostUpdateExpression = path.isUpdateExpression() && !node.prefix; const isPostUpdateExpression = path.isUpdateExpression({ prefix: false });
if (isPostUpdateExpression) { if (isPostUpdateExpression) {
if (node.operator === "++") { node = t.binaryExpression(
node = t.binaryExpression( node.operator[0],
"+", t.unaryExpression("+", t.cloneNode(node.argument)),
t.cloneNode(node.argument), t.numericLiteral(1),
t.numericLiteral(1), );
);
} else if (node.operator === "--") {
node = t.binaryExpression(
"-",
t.cloneNode(node.argument),
t.numericLiteral(1),
);
} else {
isPostUpdateExpression = false;
}
} }
for (const exportedName of exportedNames) { for (const exportedName of exportedNames) {

View File

@ -5,7 +5,7 @@ System.register([], function (_export, _context) {
function a() { function a() {
alert("a"); alert("a");
_export("c", c + 1), c++; _export("c", +c + 1), c++;
} }
_export("a", a); _export("a", a);

View File

@ -11,7 +11,7 @@ System.register([], function (_export, _context) {
_export("test", test = 5); _export("test", test = 5);
_export("test", test + 1), test++; _export("test", +test + 1), test++;
(function () { (function () {
var test = 2; var test = 2;