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 all 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
14 changes: 12 additions & 2 deletions packages/babel-helper-simple-access/src/index.ts
Expand Up @@ -11,18 +11,28 @@ 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 = {
// TODO(Babel 8): Remove UpdateExpression
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
181 changes: 181 additions & 0 deletions packages/babel-helper-simple-access/test/index.js
@@ -0,0 +1,181 @@
import * as babel from "@babel/core";
import simplifyAccess from "../lib/index.js";

const plugin = (_api, options) => {
const { includeUpdateExpression, bindingNames } = options;

return {
visitor: {
Program(path) {
simplifyAccess.default(
path,
new Set(bindingNames),
includeUpdateExpression,
);
},
},
};
};

it("simplifyAccess with default config", function () {
const code = `
let a = foo++;
a = ++foo;
foo++;
++foo;
++a;
a++;
foo = a++;
foo = ++a;

let b = bar--;
b = --bar;
bar--;
--bar;
--b;
b--;
bar = b--;
bar = --b;

let c = baz += 1;
baz += 1;
c += 1;

function f() {
let foo = 1;
let a = foo++;
a = ++foo;
foo++;
++foo;
++a;
a++;
foo = a++;
foo = ++a;
}
`;

const output = babel.transformSync(code, {
configFile: false,
ast: false,
plugins: [[plugin, { bindingNames: ["foo", "bar", "baz"] }]],
}).code;

expect(output).toMatchInlineSnapshot(`
"var _foo, _bar;

let a = (_foo = +foo, foo = _foo + 1, _foo);
a = foo = +foo + 1;
foo = foo + 1;
foo = foo + 1;
++a;
a++;
foo = a++;
foo = ++a;
let b = (_bar = +bar, bar = _bar - 1, _bar);
b = bar = +bar - 1;
bar = bar - 1;
bar = bar - 1;
--b;
b--;
bar = b--;
bar = --b;
let c = baz = baz + 1;
baz = baz + 1;
c += 1;

function f() {
let foo = 1;
let a = foo++;
a = ++foo;
foo++;
++foo;
++a;
a++;
foo = a++;
foo = ++a;
}"
`);
});

it("simplifyAccess with includeUpdateExpression=false", function () {
const code = `
let a = foo++;
a = ++foo;
foo++;
++foo;
++a;
a++;
foo = a++;
foo = ++a;

let b = bar--;
b = --bar;
bar--;
--bar;
--b;
b--;
bar = b--;
bar = --b;

let c = baz += 1;
baz += 1;
c += 1;

function f() {
let foo = 1;
let a = foo++;
a = ++foo;
foo++;
++foo;
++a;
a++;
foo = a++;
foo = ++a;
}
`;

const output = babel.transformSync(code, {
configFile: false,
ast: false,
plugins: [
[
plugin,
{ includeUpdateExpression: false, bindingNames: ["foo", "bar", "baz"] },
],
],
}).code;

expect(output).toMatchInlineSnapshot(`
"let a = foo++;
a = ++foo;
foo++;
++foo;
++a;
a++;
foo = a++;
foo = ++a;
let b = bar--;
b = --bar;
bar--;
--bar;
--b;
b--;
bar = b--;
bar = --b;
let c = baz = baz + 1;
baz = baz + 1;
c += 1;

function f() {
let foo = 1;
let a = foo++;
a = ++foo;
foo++;
++foo;
++a;
a++;
foo = a++;
foo = ++a;
}"
`);
});
1 change: 1 addition & 0 deletions packages/babel-helper-simple-access/test/package.json
@@ -0,0 +1 @@
{ "type": "module" }
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