Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix update expression for exported bigints #14341

Merged
merged 6 commits into from Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -112,6 +112,7 @@ export default function rewriteLiveReferences(
programPath,
// NOTE(logan): The 'Array.from' calls are to make this code with in loose mode.
new Set([...Array.from(imported.keys()), ...Array.from(exported.keys())]),
false,
);

// Rewrite reads/writes from imports and exports to have the correct behavior.
Expand Down Expand Up @@ -290,6 +291,81 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
}
},

UpdateExpression(path) {
const {
scope,
seen,
imported,
exported,
requeueInParent,
buildImportReference,
} = this;

if (seen.has(path.node)) return;

seen.add(path.node);

const arg = path.get("argument");

// No change needed
if (arg.isMemberExpression()) return;

const update = path.node;

if (arg.isIdentifier()) {
const localName = arg.node.name;

// redeclared in this scope
if (scope.getBinding(localName) !== path.scope.getBinding(localName)) {
return;
}

const exportedNames = exported.get(localName);
const importData = imported.get(localName);

if (exportedNames?.length > 0 || importData) {
if (importData) {
path.replaceWith(
assignmentExpression(
update.operator[0] + "=",
buildImportReference(importData, arg.node),
buildImportThrow(localName),
),
);
} else if (update.prefix) {
// ++foo
// => exports.foo = ++foo
path.replaceWith(
buildBindingExportAssignmentExpression(
this.metadata,
exportedNames,
cloneNode(update),
),
);
} else {
// foo++
// => (ref = i++, exports.i = i, ref)
const ref = scope.generateDeclaredUidIdentifier(localName);

path.replaceWith(
sequenceExpression([
assignmentExpression("=", cloneNode(ref), cloneNode(update)),
buildBindingExportAssignmentExpression(
this.metadata,
exportedNames,
identifier(localName),
),
cloneNode(ref),
]),
);
}
}
}

requeueInParent(path);
path.skip();
},

AssignmentExpression: {
exit(path) {
const {
Expand Down
13 changes: 11 additions & 2 deletions packages/babel-helper-simple-access/src/index.ts
Expand Up @@ -11,18 +11,27 @@ import {
} from "@babel/types";
import type { NodePath } from "@babel/traverse";

export default function simplifyAccess(path: NodePath, bindingNames) {
export default function simplifyAccess(
path: NodePath,
bindingNames,
// TODO(Babel 8): Remove this
includeUpdateExpression: boolean = true,
) {
path.traverse(simpleAssignmentVisitor, {
scope: path.scope,
bindingNames,
seen: new WeakSet(),
includeUpdateExpression,
});
}

const simpleAssignmentVisitor = {
UpdateExpression: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone accidentally updates @babel/helper-simple-access without also updating @babel/helper-module-transforms (this is a valid thing to do, according to the semver dependency versions), I fear that this will break their current code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.
What can I do? Should I bump the version of the package?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because we keep all the versions synchronized (and it would require a major bump).

It's annoying, but a solution until Babel 8 is to pass a includeUpdateExpression: boolean = true option to simplifyAccess, and only skip the UpdateExpression visitor when that option is set to false.

exit(path) {
const { scope, bindingNames } = this;
const { scope, bindingNames, includeUpdateExpression } = this;
if (!includeUpdateExpression) {
return;
}

const arg = path.get("argument");
if (!arg.isIdentifier()) return;
Expand Down
Expand Up @@ -5,10 +5,13 @@ define(["exports"], function (_exports) {
value: true
});
_exports.test = _exports.f = _exports.e = _exports.c = _exports.a = void 0;

var _test;

var test = 2;
_exports.test = test;
_exports.test = test = 5;
_exports.test = test = test + 1;
_test = test++, _exports.test = test, _test;

(function () {
var test = 2;
Expand Down
Expand Up @@ -3,10 +3,13 @@ define(["exports"], function (_exports) {

_exports.__esModule = true;
_exports.test = _exports.f = _exports.e = _exports.c = _exports.a = void 0;

var _test;

var test = 2;
_exports.test = test;
_exports.test = test = 5;
_exports.test = test = test + 1;
_test = test++, _exports.test = test, _test;

(function () {
var test = 2;
Expand Down
22 changes: 21 additions & 1 deletion packages/babel-plugin-transform-modules-commonjs/src/index.ts
Expand Up @@ -89,6 +89,26 @@ export default declare((api, options) => {
path.replaceWith(getAssertion(localName));
},

UpdateExpression(path) {
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
const arg = path.get("argument");
const localName = arg.node.name;
if (localName !== "module" && localName !== "exports") return;

const localBinding = path.scope.getBinding(localName);
const rootBinding = this.scope.getBinding(localName);

// redeclared in this scope
if (rootBinding !== localBinding) return;

path.replaceWith(
t.assignmentExpression(
path.node.operator[0] + "=",
arg.node,
getAssertion(localName),
),
);
},

AssignmentExpression(path) {
const left = path.get("left");
if (left.isIdentifier()) {
Expand Down Expand Up @@ -162,7 +182,7 @@ export default declare((api, options) => {
// These objects are specific to CommonJS and are not available in
// real ES6 implementations.
if (!allowCommonJSExports) {
simplifyAccess(path, new Set(["module", "exports"]));
simplifyAccess(path, new Set(["module", "exports"]), false);
path.traverse(moduleExportsVisitor, {
scope: path.scope,
});
Expand Down
Expand Up @@ -2,10 +2,13 @@

exports.__esModule = true;
exports.test = exports.f = exports.e = exports.c = exports.a = void 0;

var _test;

var test = 2;
exports.test = test;
exports.test = test = 5;
exports.test = test = test + 1;
_test = test++, exports.test = test, _test;

(function () {
var test = 2;
Expand Down
Expand Up @@ -4,10 +4,13 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports.test = exports.f = exports.e = exports.c = exports.a = void 0;

var _test;

var test = 2;
exports.test = test;
exports.test = test = 5;
exports.test = test = test + 1;
_test = test++, exports.test = test, _test;

(function () {
var test = 2;
Expand Down
Expand Up @@ -74,24 +74,30 @@ Bar && (Bar = (4, function () {
_baz.Baz && (_baz.Baz = (4, function () {
throw new Error('"' + "Baz" + '" is read-only.');
}()));
_foo.default = (_foo.default - 1, function () {

_foo.default -= function () {
throw new Error('"' + "Foo" + '" is read-only.');
}());
Bar = (Bar - 1, function () {
}();

Bar -= function () {
throw new Error('"' + "Bar" + '" is read-only.');
}());
_baz.Baz = (_baz.Baz - 1, function () {
}();

_baz.Baz -= function () {
throw new Error('"' + "Baz" + '" is read-only.');
}());
_foo.default = (_foo.default + 1, function () {
}();

_foo.default += function () {
throw new Error('"' + "Foo" + '" is read-only.');
}());
Bar = (Bar + 1, function () {
}();

Bar += function () {
throw new Error('"' + "Bar" + '" is read-only.');
}());
_baz.Baz = (_baz.Baz + 1, function () {
}();

_baz.Baz += function () {
throw new Error('"' + "Baz" + '" is read-only.');
}());
}();

for (let _Foo in {}) {
(function () {
Expand Down
Expand Up @@ -9,9 +9,9 @@ console.log(function () {
throw new Error("The CommonJS '" + "exports" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
}().prop);

exports = function () {
exports += function () {
throw new Error("The CommonJS '" + "exports" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
}() + 1;
}();

exports = function () {
throw new Error("The CommonJS '" + "exports" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
Expand All @@ -36,9 +36,9 @@ console.log(function () {
throw new Error("The CommonJS '" + "module" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
}().exports);

module = function () {
module += function () {
throw new Error("The CommonJS '" + "module" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
}() + 1;
}();

module = function () {
throw new Error("The CommonJS '" + "module" + "' variable is not available in ES6 modules." + "Consider setting setting sourceType:script or sourceType:unambiguous in your " + "Babel config for this file.");
Expand Down
Expand Up @@ -9,5 +9,5 @@ var _yy;

var yy = 0;
exports.yy = yy;
var zz = (_yy = +yy, exports.yy = yy = _yy + 1, _yy);
var zz = (_yy = yy++, exports.yy = yy, _yy);
exports.zz = zz;
@@ -0,0 +1,16 @@
export let foo = 1n;

export let bar = foo++;

export let baz = ++bar;

expect(foo).toBe(2n);
expect(bar).toBe(2n);
expect(baz).toBe(2n);

export { foo as foofoo, bar as barbar };
export { baz as bazbaz };

--foo;
bar--;
baz--;
@@ -0,0 +1,16 @@
export let foo = 1n;

export let bar = foo++;

export let baz = ++bar;

expect(foo).toBe(2n);
expect(bar).toBe(2n);
expect(baz).toBe(2n);

export { foo as foofoo, bar as barbar };
export { baz as bazbaz };

--foo;
bar--;
baz--;
@@ -0,0 +1,4 @@
{
"externalHelpers": false,
"minNodeVersion": "10.0.0"
}
@@ -0,0 +1,21 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.foofoo = exports.foo = exports.bazbaz = exports.baz = exports.barbar = exports.bar = void 0;

var _foo, _bar, _baz;

let foo = 1n;
exports.foofoo = exports.foo = foo;
let bar = (_foo = foo++, exports.foofoo = exports.foo = foo, _foo);
exports.barbar = exports.bar = bar;
let baz = exports.barbar = exports.bar = ++bar;
exports.bazbaz = exports.baz = baz;
expect(foo).toBe(2n);
expect(bar).toBe(2n);
expect(baz).toBe(2n);
exports.foofoo = exports.foo = --foo;
_bar = bar--, exports.barbar = exports.bar = bar, _bar;
_baz = baz--, exports.bazbaz = exports.baz = baz, _baz;
Expand Up @@ -9,7 +9,7 @@ let diffLevel = 0;
exports.diffLevel = diffLevel;

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

function diff() {
if (!(exports.diffLevel = diffLevel = +diffLevel + 1)) {
if (!(exports.diffLevel = ++diffLevel)) {
console.log("hey");
}
}
Expand Up @@ -15,10 +15,13 @@

_exports.__esModule = true;
_exports.test = _exports.f = _exports.e = _exports.c = _exports.a = void 0;

var _test;

var test = 2;
_exports.test = test;
_exports.test = test = 5;
_exports.test = test = test + 1;
_test = test++, _exports.test = test, _test;

(function () {
var test = 2;
Expand Down
Expand Up @@ -17,10 +17,13 @@
value: true
});
_exports.test = _exports.f = _exports.e = _exports.c = _exports.a = void 0;

var _test;

var test = 2;
_exports.test = test;
_exports.test = test = 5;
_exports.test = test = test + 1;
_test = test++, _exports.test = test, _test;

(function () {
var test = 2;
Expand Down