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

Unshadow cjs exports when transforming mutations #14708

Merged
merged 2 commits into from Jun 29, 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 @@ -183,6 +183,7 @@ const rewriteBindingInitVisitor: Visitor<RewriteBindingInitVisitorState> = {
metadata,
exportNames,
identifier(localName),
path.scope,
),
);
// @ts-expect-error todo(flow->ts): avoid mutations
Expand All @@ -203,6 +204,7 @@ const rewriteBindingInitVisitor: Visitor<RewriteBindingInitVisitorState> = {
metadata,
exportNames,
identifier(localName),
path.scope,
),
);
// @ts-expect-error todo(flow->ts): avoid mutations
Expand All @@ -218,7 +220,18 @@ const buildBindingExportAssignmentExpression = (
metadata: ModuleMetadata,
exportNames: string[],
localExpr: t.Expression,
scope: Scope,
) => {
const exportsObjectName = metadata.exportName;
for (
let currentScope = scope;
currentScope != null;
currentScope = currentScope.parent
) {
if (currentScope.hasOwnBinding(exportsObjectName)) {
currentScope.rename(exportsObjectName);
}
}
return (exportNames || []).reduce((expr, exportName) => {
// class Foo {} export { Foo, Foo as Bar };
// as
Expand All @@ -228,7 +241,7 @@ const buildBindingExportAssignmentExpression = (
return assignmentExpression(
"=",
memberExpression(
identifier(metadata.exportName),
identifier(exportsObjectName),
computed ? stringLiteral(exportName) : identifier(exportName),
/* computed */ computed,
),
Expand Down Expand Up @@ -352,6 +365,7 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
this.metadata,
exportedNames,
cloneNode(update),
path.scope,
),
);
} else {
Expand All @@ -366,6 +380,7 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
this.metadata,
exportedNames,
identifier(localName),
path.scope,
),
cloneNode(ref),
]),
Expand Down Expand Up @@ -428,6 +443,7 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
this.metadata,
exportedNames,
assignment,
path.scope,
),
);
requeueInParent(path);
Expand Down Expand Up @@ -458,6 +474,7 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
this.metadata,
exportedNames,
identifier(localName),
path.scope,
),
);
}
Expand Down
@@ -0,0 +1,8 @@
var exports = 1;

export let x = 2;

function fn() {
x = 3;
var exports = 4;
}
@@ -0,0 +1,3 @@
{
"plugins": ["transform-modules-commonjs"]
}
@@ -0,0 +1,14 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.x = void 0;
var _exports = 1;
let x = 2;
exports.x = x;

function fn() {
exports.x = x = 3;
var _exports2 = 4;
Copy link
Member Author

Choose a reason for hiding this comment

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

Before this PR, this was left as var exports = 4.

}