Skip to content

Commit 12195dc

Browse files
fix: do not rename nested "exports" bindings that do not conflict (#6360)
PR #5947 added "exports" to the universal RESERVED_NAMES set, which caused every variable or parameter named "exports" to be renamed to "exports$1" in any scope, even when no conflict exists with the output format's runtime "exports" identifier. Because eval strings are not rewritten, this silently broke code like: (unused, exports) => { console.log(exports.bar); eval('exports.bar = 1'); } where the parameter was renamed but the eval argument was not. The same over-aggressive rename also applied to unrelated nested bindings in ES output, which has no runtime "exports" at all. This change removes "exports" from RESERVED_NAMES and restores the system-format-specific check in ChildScope.addUsedOutsideNames so that nested scopes in SystemJS output still treat "exports" as used when they reference an outer exported binding. Top-level protection for CJS/AMD/UMD/IIFE/SYSTEM output is unchanged: RESERVED_USED_NAMES in Chunk.ts still seeds the chunk-level usedNames with "exports", and reassigned exports still carry renderBaseName === "exports" which propagates into nested scopes via getBaseVariableName(). The deconflictTopLevelVariables cache from PR #5947 is untouched, so its performance benefits are preserved; a benchmark that reliably sees the PR's generate-time speedup (~20% vs pre-PR) shows no regression from this change. Fixes #6357 Made-with: Cursor Co-authored-by: Lukas Taegert-Atkinson <[email protected]>
1 parent b74aa39 commit 12195dc

29 files changed

Lines changed: 145 additions & 45 deletions

File tree

‎src/ast/scopes/ChildScope.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,20 @@ export default class ChildScope extends Scope {
5454

5555
addUsedOutsideNames(
5656
usedNames: Set<string>,
57+
format: InternalModuleFormat,
58+
exportNamesByVariable: ReadonlyMap<Variable, readonly string[]>,
5759
accessedGlobalsByScope: ReadonlyMap<ChildScope, ReadonlySet<string>>
5860
): void {
5961
for (const variable of this.accessedOutsideVariables.values()) {
6062
if (variable.included) {
6163
usedNames.add(variable.getBaseVariableName());
64+
// In system format, exported variables are assigned via `exports(name, value)`.
65+
// Any scope that references such a variable must treat `exports` as used so
66+
// that a nested binding of the same name does not shadow the system wrapper's
67+
// `exports` argument.
68+
if (format === 'system' && exportNamesByVariable.has(variable)) {
69+
usedNames.add('exports');
70+
}
6271
}
6372
}
6473
const accessedGlobals = accessedGlobalsByScope.get(this);
@@ -79,7 +88,7 @@ export default class ChildScope extends Scope {
7988
accessedGlobalsByScope: ReadonlyMap<ChildScope, ReadonlySet<string>>
8089
): void {
8190
const usedNames = new Set<string>();
82-
this.addUsedOutsideNames(usedNames, accessedGlobalsByScope);
91+
this.addUsedOutsideNames(usedNames, format, exportNamesByVariable, accessedGlobalsByScope);
8392
if (this.accessedDynamicImports) {
8493
for (const importExpression of this.accessedDynamicImports) {
8594
if (importExpression.inlineNamespace) {

‎src/utils/RESERVED_NAMES.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const RESERVED_NAMES: ReadonlySet<string> = new Set([
1414
'enum',
1515
'eval',
1616
'export',
17-
'exports',
1817
'extends',
1918
'false',
2019
'finally',

‎src/utils/deconflictChunk.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ export function deconflictChunk(
6363
): void {
6464
const reversedModules = [...modules].reverse();
6565
for (const module of reversedModules) {
66-
module.scope.addUsedOutsideNames(usedNames, accessedGlobalsByScope);
66+
module.scope.addUsedOutsideNames(
67+
usedNames,
68+
format,
69+
exportNamesByVariable,
70+
accessedGlobalsByScope
71+
);
6772
}
6873
deconflictTopLevelVariables(usedNames, reversedModules, includedNamespaces);
6974

‎test/chunking-form/samples/chunk-assigment-in-dynamic/_expected/amd/generated-c.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ define(['exports'], (function (exports) { 'use strict';
77
function requireC () {
88
if (hasRequiredC) return c;
99
hasRequiredC = 1;
10-
(function (exports$1) {
11-
exports$1.preFaPrint = {
10+
(function (exports) {
11+
exports.preFaPrint = {
1212
foo: 1
1313
};
1414

15-
exports$1.faPrint = exports$1.preFaPrint;
15+
exports.faPrint = exports.preFaPrint;
1616
} (c));
1717
return c;
1818
}

‎test/chunking-form/samples/chunk-assigment-in-dynamic/_expected/cjs/generated-c.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ var hasRequiredC;
77
function requireC () {
88
if (hasRequiredC) return c;
99
hasRequiredC = 1;
10-
(function (exports$1) {
11-
exports$1.preFaPrint = {
10+
(function (exports) {
11+
exports.preFaPrint = {
1212
foo: 1
1313
};
1414

15-
exports$1.faPrint = exports$1.preFaPrint;
15+
exports.faPrint = exports.preFaPrint;
1616
} (c));
1717
return c;
1818
}

‎test/chunking-form/samples/chunk-assigment-in-dynamic/_expected/es/generated-c.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ var hasRequiredC;
55
function requireC () {
66
if (hasRequiredC) return c;
77
hasRequiredC = 1;
8-
(function (exports$1) {
9-
exports$1.preFaPrint = {
8+
(function (exports) {
9+
exports.preFaPrint = {
1010
foo: 1
1111
};
1212

13-
exports$1.faPrint = exports$1.preFaPrint;
13+
exports.faPrint = exports.preFaPrint;
1414
} (c));
1515
return c;
1616
}

‎test/chunking-form/samples/chunk-assigment-in-dynamic/_expected/system/generated-c.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ System.register([], (function (exports) {
1010
function requireC () {
1111
if (hasRequiredC) return c;
1212
hasRequiredC = 1;
13-
(function (exports$1) {
14-
exports$1.preFaPrint = {
13+
(function (exports) {
14+
exports.preFaPrint = {
1515
foo: 1
1616
};
1717

18-
exports$1.faPrint = exports$1.preFaPrint;
18+
exports.faPrint = exports.preFaPrint;
1919
} (c));
2020
return c;
2121
}

‎test/form/samples/deconflict-format-specific-exports/_expected/amd.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ define(['exports'], (function (exports) { 'use strict';
1414
}
1515

1616
function nestedNoConflict() {
17-
const exports$1 = {
17+
const exports = {
1818
x: 42
1919
};
20-
console.log(exports$1);
20+
console.log(exports);
2121
}
2222

2323
exports.x = 43;

‎test/form/samples/deconflict-format-specific-exports/_expected/cjs.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ function nestedConflict() {
1414
}
1515

1616
function nestedNoConflict() {
17-
const exports$1 = {
17+
const exports = {
1818
x: 42
1919
};
20-
console.log(exports$1);
20+
console.log(exports);
2121
}
2222

2323
exports.x = 43;

‎test/form/samples/deconflict-format-specific-exports/_expected/es.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ const exports$1 = {
44
console.log(exports$1);
55

66
function nestedConflict() {
7-
const exports$1 = {
7+
const exports = {
88
x: 42
99
};
10-
console.log(exports$1);
10+
console.log(exports);
1111
x++;
1212
}
1313

1414
function nestedNoConflict() {
15-
const exports$1 = {
15+
const exports = {
1616
x: 42
1717
};
18-
console.log(exports$1);
18+
console.log(exports);
1919
}
2020

2121
var x = 43;

0 commit comments

Comments
 (0)