Skip to content

Commit 308ad74

Browse files
committed
fix #4471: renaming of nested var declarations
1 parent f013f5f commit 308ad74

5 files changed

Lines changed: 84 additions & 19 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
new (foo()?.bar)();
2525
```
2626
27+
* Fix renaming of nested `var` declarations ([#4471](https://github.com/evanw/esbuild/issues/4471))
28+
29+
This release fixes a bug where `var` declarations in nested scopes that are hoisted up to module scope were not correctly being renamed during bundling. That could previously lead to name collisions when minification was disabled, which could potentially cause a behavior change. The bug has been fixed so that these hoisted declarations are now considered to be module-level symbols during the name collision avoidance pass.
30+
2731
* Emit `var` instead of `const` for certain TypeScript-only constructs for ES5 ([#4448](https://github.com/evanw/esbuild/issues/4448))
2832

2933
While esbuild doesn't generally support converting `const` to `var` for ES5 due to nested scoping rules (which is currently a build-time error), esbuild previously incorrectly converted TypeScript-only `import` assignment constructs into a `const` declaration even when targeting ES5. With this release, esbuild will now use `var` for this case instead:

‎internal/bundler_tests/bundler_default_test.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2746,6 +2746,32 @@ func TestManyEntryPoints(t *testing.T) {
27462746
})
27472747
}
27482748

2749+
func TestRenameNestedVar(t *testing.T) {
2750+
default_suite.expectBundled(t, bundled{
2751+
files: map[string]string{
2752+
"/entry.js": `
2753+
import { foo } from './foo'
2754+
var topLevel = 0
2755+
{ var nested = 1 }
2756+
function fn() { var inner = 2 }
2757+
foo(topLevel, nested, fn)
2758+
`,
2759+
"/foo.js": `
2760+
export function foo(a, b) {}
2761+
var topLevel = 0
2762+
{ var nested = 1 }
2763+
function fn() { var inner = 2 }
2764+
foo(topLevel, nested, fn)
2765+
`,
2766+
},
2767+
entryPaths: []string{"/entry.js"},
2768+
options: config.Options{
2769+
Mode: config.ModeBundle,
2770+
AbsOutputFile: "/out.js",
2771+
},
2772+
})
2773+
}
2774+
27492775
func TestRenamePrivateIdentifiersNoBundle(t *testing.T) {
27502776
default_suite.expectBundled(t, bundled{
27512777
files: map[string]string{

‎internal/bundler_tests/snapshots/snapshots_default.txt‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5897,6 +5897,33 @@ foo: {
58975897
}
58985898
}
58995899

5900+
================================================================================
5901+
TestRenameNestedVar
5902+
---------- /out.js ----------
5903+
// foo.js
5904+
function foo(a, b) {
5905+
}
5906+
var topLevel = 0;
5907+
{
5908+
nested = 1;
5909+
}
5910+
var nested;
5911+
function fn() {
5912+
var inner = 2;
5913+
}
5914+
foo(topLevel, nested, fn);
5915+
5916+
// entry.js
5917+
var topLevel2 = 0;
5918+
{
5919+
nested2 = 1;
5920+
}
5921+
var nested2;
5922+
function fn2() {
5923+
var inner = 2;
5924+
}
5925+
foo(topLevel2, nested2, fn2);
5926+
59005927
================================================================================
59015928
TestRenamePrivateIdentifiersNoBundle
59025929
---------- /out.js ----------
@@ -6444,10 +6471,10 @@ var Foo = class {
64446471
};
64456472
new Foo(foo(objFoo));
64466473
if (nested) {
6447-
let bar = function(x = this) {
6474+
let bar2 = function(x = this) {
64486475
console.log(this);
64496476
};
6450-
bar2 = bar;
6477+
bar = bar2;
64516478
const objBar = {
64526479
foo(x = this) {
64536480
console.log(this);
@@ -6463,9 +6490,9 @@ if (nested) {
64636490
console.log(this);
64646491
}
64656492
}
6466-
new Bar(bar(objBar));
6493+
new Bar(bar2(objBar));
64676494
}
6468-
var bar2;
6495+
var bar;
64696496

64706497
================================================================================
64716498
TestThisOutsideFunction
@@ -7061,9 +7088,9 @@ var k;
70617088
---------- /out/nested.js ----------
70627089
// nested.js
70637090
if (true) {
7064-
let l = function() {
7091+
let l2 = function() {
70657092
};
7066-
l2 = l;
7093+
l = l2;
70677094
for (; 0; ) ;
70687095
for ({ c, x: [d] } = {}; 0; ) ;
70697096
for (e of []) ;
@@ -7084,7 +7111,7 @@ var h;
70847111
var i;
70857112
var j;
70867113
var k;
7087-
var l2;
7114+
var l;
70887115

70897116
---------- /out/let.js ----------
70907117
// let.js
@@ -7152,9 +7179,9 @@ function l() {
71527179

71537180
---------- /out/nested.js ----------
71547181
if (true) {
7155-
let l = function() {
7182+
let l2 = function() {
71567183
};
7157-
var l2 = l;
7184+
var l = l2;
71587185
var a;
71597186
for (var b; 0; ) ;
71607187
for (var { c, x: [d] } = {}; 0; ) ;

‎internal/bundler_tests/snapshots/snapshots_ts.txt‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,10 +2080,10 @@ var Foo = class {
20802080
};
20812081
new Foo(foo(objFoo));
20822082
if (nested) {
2083-
let bar = function(x = this) {
2083+
let bar2 = function(x = this) {
20842084
console.log(this);
20852085
};
2086-
bar2 = bar;
2086+
bar = bar2;
20872087
const objBar = {
20882088
foo(x = this) {
20892089
console.log(this);
@@ -2103,9 +2103,9 @@ if (nested) {
21032103
console.log(this);
21042104
}
21052105
}
2106-
new Bar(bar(objBar));
2106+
new Bar(bar2(objBar));
21072107
}
2108-
var bar2;
2108+
var bar;
21092109

21102110
================================================================================
21112111
TestThisInsideFunctionTSNoBundle
@@ -2229,10 +2229,10 @@ var Foo = class {
22292229
};
22302230
new Foo(foo(objFoo));
22312231
if (nested) {
2232-
let bar = function(x = this) {
2232+
let bar2 = function(x = this) {
22332233
console.log(this);
22342234
};
2235-
bar2 = bar;
2235+
bar = bar2;
22362236
const objBar = {
22372237
foo(x = this) {
22382238
console.log(this);
@@ -2248,6 +2248,6 @@ if (nested) {
22482248
console.log(this);
22492249
}
22502250
}
2251-
new Bar(bar(objBar));
2251+
new Bar(bar2(objBar));
22522252
}
2253-
var bar2;
2253+
var bar;

‎internal/js_parser/js_parser.go‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,6 @@ func (p *parser) declareSymbol(kind ast.SymbolKind, loc logger.Loc, name string)
15231523
// Overwrite this name in the declaring scope
15241524
p.currentScope.Members[name] = js_ast.ScopeMember{Ref: ref, Loc: loc}
15251525
return ref
1526-
15271526
}
15281527

15291528
// This type is just so we can use Go's native sort function
@@ -10019,9 +10018,18 @@ func (p *parser) visitForLoopInit(stmt js_ast.Stmt, isInOrOf bool) js_ast.Stmt {
1001910018
}
1002010019

1002110020
func (p *parser) recordDeclaredSymbol(ref ast.Ref) {
10021+
isTopLevel := p.currentScope == p.moduleScope
10022+
10023+
// Check whether this symbol was hoisted out of a nested scope into the module scope
10024+
if !isTopLevel {
10025+
if symbol := p.symbols[ref.InnerIndex]; symbol.Kind.IsHoisted() && p.moduleScope.Members[symbol.OriginalName].Ref == ref {
10026+
isTopLevel = true
10027+
}
10028+
}
10029+
1002210030
p.declaredSymbols = append(p.declaredSymbols, js_ast.DeclaredSymbol{
1002310031
Ref: ref,
10024-
IsTopLevel: p.currentScope == p.moduleScope,
10032+
IsTopLevel: isTopLevel,
1002510033
})
1002610034
}
1002710035

0 commit comments

Comments
 (0)