Skip to content

Commit 1261051

Browse files
Unshadow cjs exports when transforming mutations (#14708)
* Add failing test * Unshadow `cjs` exports when transforming mutations
1 parent 5fdc5eb commit 1261051

File tree

7 files changed

+43
-1
lines changed

7 files changed

+43
-1
lines changed

packages/babel-helper-module-transforms/src/rewrite-live-references.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ const rewriteBindingInitVisitor: Visitor<RewriteBindingInitVisitorState> = {
183183
metadata,
184184
exportNames,
185185
identifier(localName),
186+
path.scope,
186187
),
187188
);
188189
// @ts-expect-error todo(flow->ts): avoid mutations
@@ -203,6 +204,7 @@ const rewriteBindingInitVisitor: Visitor<RewriteBindingInitVisitorState> = {
203204
metadata,
204205
exportNames,
205206
identifier(localName),
207+
path.scope,
206208
),
207209
);
208210
// @ts-expect-error todo(flow->ts): avoid mutations
@@ -218,7 +220,18 @@ const buildBindingExportAssignmentExpression = (
218220
metadata: ModuleMetadata,
219221
exportNames: string[],
220222
localExpr: t.Expression,
223+
scope: Scope,
221224
) => {
225+
const exportsObjectName = metadata.exportName;
226+
for (
227+
let currentScope = scope;
228+
currentScope != null;
229+
currentScope = currentScope.parent
230+
) {
231+
if (currentScope.hasOwnBinding(exportsObjectName)) {
232+
currentScope.rename(exportsObjectName);
233+
}
234+
}
222235
return (exportNames || []).reduce((expr, exportName) => {
223236
// class Foo {} export { Foo, Foo as Bar };
224237
// as
@@ -228,7 +241,7 @@ const buildBindingExportAssignmentExpression = (
228241
return assignmentExpression(
229242
"=",
230243
memberExpression(
231-
identifier(metadata.exportName),
244+
identifier(exportsObjectName),
232245
computed ? stringLiteral(exportName) : identifier(exportName),
233246
/* computed */ computed,
234247
),
@@ -352,6 +365,7 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
352365
this.metadata,
353366
exportedNames,
354367
cloneNode(update),
368+
path.scope,
355369
),
356370
);
357371
} else {
@@ -366,6 +380,7 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
366380
this.metadata,
367381
exportedNames,
368382
identifier(localName),
383+
path.scope,
369384
),
370385
cloneNode(ref),
371386
]),
@@ -428,6 +443,7 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
428443
this.metadata,
429444
exportedNames,
430445
assignment,
446+
path.scope,
431447
),
432448
);
433449
requeueInParent(path);
@@ -458,6 +474,7 @@ const rewriteReferencesVisitor: Visitor<RewriteReferencesVisitorState> = {
458474
this.metadata,
459475
exportedNames,
460476
identifier(localName),
477+
path.scope,
461478
),
462479
);
463480
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var exports = 1;
2+
3+
export let x = 2;
4+
5+
function fn() {
6+
x = 3;
7+
var exports = 4;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["transform-modules-commonjs"]
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.x = void 0;
7+
var _exports = 1;
8+
let x = 2;
9+
exports.x = x;
10+
11+
function fn() {
12+
exports.x = x = 3;
13+
var _exports2 = 4;
14+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy