Skip to content

Commit 046f82f

Browse files
committed
Revert "Drop superfluous __name() calls (#2062)"
This reverts commit 730df4f.
1 parent 57a75a8 commit 046f82f

File tree

6 files changed

+49
-246
lines changed

6 files changed

+49
-246
lines changed

internal/bundler/bundler_default_test.go

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4358,77 +4358,6 @@ func TestDefineThis(t *testing.T) {
43584358
})
43594359
}
43604360

4361-
func TestKeepNamesWithNecessaryHelperFunctionCalls(t *testing.T) {
4362-
default_suite.expectBundled(t, bundled{
4363-
files: map[string]string{
4364-
"/entry.js": `
4365-
import {
4366-
functionStmt as functionStmt1,
4367-
functionExpr as functionExpr1,
4368-
classStmt as classStmt1,
4369-
classExpr as classExpr1,
4370-
functionAnonExpr as functionAnonExpr1,
4371-
classAnonExpr as classAnonExpr1,
4372-
classAnonExprLowered as classAnonExprLowered1,
4373-
} from './copy1'
4374-
4375-
import {
4376-
functionStmt as functionStmt2,
4377-
functionExpr as functionExpr2,
4378-
classStmt as classStmt2,
4379-
classExpr as classExpr2,
4380-
functionAnonExpr as functionAnonExpr2,
4381-
classAnonExpr as classAnonExpr2,
4382-
classAnonExprLowered as classAnonExprLowered2,
4383-
} from './copy2'
4384-
4385-
console.log([
4386-
functionStmt1, functionStmt2,
4387-
functionExpr1, functionExpr2,
4388-
classStmt1, classStmt2,
4389-
classExpr1, classExpr2,
4390-
functionAnonExpr1, functionAnonExpr2,
4391-
classAnonExpr1, classAnonExpr2,
4392-
classAnonExprLowered1, classAnonExprLowered2,
4393-
])
4394-
`,
4395-
4396-
"/copy1.js": `
4397-
export function functionStmt() { return 'copy1' }
4398-
export class classStmt { foo = 'copy1' }
4399-
export let functionExpr = function fn() { return 'copy1' }
4400-
export let classExpr = class cls { foo = 'copy1' }
4401-
export let functionAnonExpr = function() { return 'copy1' }
4402-
export let classAnonExpr = class { foo = 'copy1' }
4403-
export let classAnonExprLowered = class { static foo = 'copy2' }
4404-
4405-
class classStmtSideEffect { static [copy1]() {} }
4406-
let classExprSideEffect = class clsSideEffect { static [copy1]() {} }
4407-
`,
4408-
4409-
"/copy2.js": `
4410-
export function functionStmt() { return 'copy2' }
4411-
export class classStmt { foo = 'copy2' }
4412-
export let functionExpr = function fn() { return 'copy2' }
4413-
export let classExpr = class cls { foo = 'copy2' }
4414-
export let functionAnonExpr = function() { return 'copy2' }
4415-
export let classAnonExpr = class { foo = 'copy2' }
4416-
export let classAnonExprLowered = class { static foo = 'copy2' }
4417-
4418-
class classStmtSideEffect { static [copy2]() {} }
4419-
let classExprSideEffect = class clsSideEffect { static [copy2]() {} }
4420-
`,
4421-
},
4422-
entryPaths: []string{"/entry.js"},
4423-
options: config.Options{
4424-
Mode: config.ModeBundle,
4425-
AbsOutputFile: "/out.js",
4426-
KeepNames: true,
4427-
UnsupportedJSFeatures: compat.ClassStaticField,
4428-
},
4429-
})
4430-
}
4431-
44324361
func TestKeepNamesTreeShaking(t *testing.T) {
44334362
default_suite.expectBundled(t, bundled{
44344363
files: map[string]string{

internal/bundler/snapshots/snapshots_default.txt

Lines changed: 4 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,103 +1401,19 @@ TestKeepNamesTreeShaking
14011401
// entry.js
14021402
function fnStmtKeep() {
14031403
}
1404+
__name(fnStmtKeep, "fnStmtKeep");
14041405
x = fnStmtKeep;
14051406
var fnExprKeep = /* @__PURE__ */ __name(function() {
14061407
}, "keep");
14071408
x = fnExprKeep;
14081409
var clsStmtKeep = class {
14091410
};
1411+
__name(clsStmtKeep, "clsStmtKeep");
14101412
new clsStmtKeep();
14111413
var clsExprKeep = /* @__PURE__ */ __name(class {
14121414
}, "keep");
14131415
new clsExprKeep();
14141416

1415-
================================================================================
1416-
TestKeepNamesWithNecessaryHelperFunctionCalls
1417-
---------- /out.js ----------
1418-
// copy1.js
1419-
function functionStmt() {
1420-
return "copy1";
1421-
}
1422-
var classStmt = class {
1423-
foo = "copy1";
1424-
};
1425-
var functionExpr = function fn() {
1426-
return "copy1";
1427-
};
1428-
var classExpr = class cls {
1429-
foo = "copy1";
1430-
};
1431-
var functionAnonExpr = function() {
1432-
return "copy1";
1433-
};
1434-
var classAnonExpr = class {
1435-
foo = "copy1";
1436-
};
1437-
var _a;
1438-
var classAnonExprLowered = /* @__PURE__ */ __name((_a = class {
1439-
}, __publicField(_a, "foo", "copy2"), _a), "classAnonExprLowered");
1440-
var classStmtSideEffect = class {
1441-
static [copy1]() {
1442-
}
1443-
};
1444-
var classExprSideEffect = class clsSideEffect {
1445-
static [copy1]() {
1446-
}
1447-
};
1448-
1449-
// copy2.js
1450-
function functionStmt2() {
1451-
return "copy2";
1452-
}
1453-
__name(functionStmt2, "functionStmt");
1454-
var classStmt2 = class {
1455-
foo = "copy2";
1456-
};
1457-
__name(classStmt2, "classStmt");
1458-
var functionExpr2 = /* @__PURE__ */ __name(function fn2() {
1459-
return "copy2";
1460-
}, "fn");
1461-
var classExpr2 = /* @__PURE__ */ __name(class cls2 {
1462-
foo = "copy2";
1463-
}, "cls");
1464-
var functionAnonExpr2 = /* @__PURE__ */ __name(function() {
1465-
return "copy2";
1466-
}, "functionAnonExpr");
1467-
var classAnonExpr2 = /* @__PURE__ */ __name(class {
1468-
foo = "copy2";
1469-
}, "classAnonExpr");
1470-
var _a2;
1471-
var classAnonExprLowered2 = /* @__PURE__ */ __name((_a2 = class {
1472-
}, __publicField(_a2, "foo", "copy2"), _a2), "classAnonExprLowered");
1473-
var classStmtSideEffect2 = class {
1474-
static [copy2]() {
1475-
}
1476-
};
1477-
__name(classStmtSideEffect2, "classStmtSideEffect");
1478-
var classExprSideEffect2 = /* @__PURE__ */ __name(class clsSideEffect2 {
1479-
static [copy2]() {
1480-
}
1481-
}, "clsSideEffect");
1482-
1483-
// entry.js
1484-
console.log([
1485-
functionStmt,
1486-
functionStmt2,
1487-
functionExpr,
1488-
functionExpr2,
1489-
classStmt,
1490-
classStmt2,
1491-
classExpr,
1492-
classExpr2,
1493-
functionAnonExpr,
1494-
functionAnonExpr2,
1495-
classAnonExpr,
1496-
classAnonExpr2,
1497-
classAnonExprLowered,
1498-
classAnonExprLowered2
1499-
]);
1500-
15011417
================================================================================
15021418
TestLegalCommentsAvoidSlashTagEndOfFile
15031419
---------- /out/entry.js ----------
@@ -2894,11 +2810,12 @@ export function outer() {
28942810
let inner = function() {
28952811
return Math.random();
28962812
};
2813+
__name(inner, "inner");
28972814
const x = inner();
28982815
console.log(x);
28992816
}
29002817
}
2901-
outer();
2818+
__name(outer, "outer"), outer();
29022819

29032820
================================================================================
29042821
TestSwitchScopeNoBundle

internal/bundler/snapshots/snapshots_ts.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,7 @@ TestTypeScriptDecoratorsKeepNames
11531153
// entry.ts
11541154
var Foo = class {
11551155
};
1156+
__name(Foo, "Foo");
11561157
Foo = __decorateClass([
11571158
decoratorMustComeAfterName
11581159
], Foo);

internal/js_ast/js_ast.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,6 @@ type ECall struct {
516516
// call itself is removed due to this annotation, the arguments must remain
517517
// if they have side effects.
518518
CanBeUnwrappedIfUnused bool
519-
520-
IsKeepName bool
521519
}
522520

523521
func (a *ECall) HasSameFlagsAs(b *ECall) bool {
@@ -850,6 +848,11 @@ type SLazyExport struct {
850848

851849
type SExpr struct {
852850
Value Expr
851+
852+
// This is set to true for automatically-generated expressions that should
853+
// not affect tree shaking. For example, calling a function from the runtime
854+
// that doesn't have externally-visible side effects.
855+
DoesNotAffectTreeShaking bool
853856
}
854857

855858
type EnumValue struct {

internal/js_parser/js_parser.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7617,6 +7617,7 @@ func (p *parser) mangleStmts(stmts []js_ast.Stmt, kind stmtsKind) []js_ast.Stmt
76177617
prevStmt := result[len(result)-1]
76187618
if prevS, ok := prevStmt.Data.(*js_ast.SExpr); ok {
76197619
prevS.Value = js_ast.JoinWithComma(prevS.Value, s.Value)
7620+
prevS.DoesNotAffectTreeShaking = prevS.DoesNotAffectTreeShaking && s.DoesNotAffectTreeShaking
76207621
continue
76217622
}
76227623
}
@@ -8852,20 +8853,21 @@ func (p *parser) keepExprSymbolName(value js_ast.Expr, name string) js_ast.Expr
88528853

88538854
// Make sure tree shaking removes this if the function is never used
88548855
value.Data.(*js_ast.ECall).CanBeUnwrappedIfUnused = true
8855-
value.Data.(*js_ast.ECall).IsKeepName = true
88568856
return value
88578857
}
88588858

88598859
func (p *parser) keepStmtSymbolName(loc logger.Loc, ref js_ast.Ref, name string) js_ast.Stmt {
88608860
p.symbols[ref.InnerIndex].Flags |= js_ast.DidKeepName
88618861

8862-
call := p.callRuntime(loc, "__name", []js_ast.Expr{
8863-
{Loc: loc, Data: &js_ast.EIdentifier{Ref: ref}},
8864-
{Loc: loc, Data: &js_ast.EString{Value: helpers.StringToUTF16(name)}},
8865-
})
8866-
call.Data.(*js_ast.ECall).IsKeepName = true
8862+
return js_ast.Stmt{Loc: loc, Data: &js_ast.SExpr{
8863+
Value: p.callRuntime(loc, "__name", []js_ast.Expr{
8864+
{Loc: loc, Data: &js_ast.EIdentifier{Ref: ref}},
8865+
{Loc: loc, Data: &js_ast.EString{Value: helpers.StringToUTF16(name)}},
8866+
}),
88678867

8868-
return js_ast.Stmt{Loc: loc, Data: &js_ast.SExpr{Value: call}}
8868+
// Make sure tree shaking removes this if the function is never used
8869+
DoesNotAffectTreeShaking: true,
8870+
}}
88698871
}
88708872

88718873
func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_ast.Stmt {
@@ -14459,6 +14461,12 @@ func (p *parser) stmtsCanBeRemovedIfUnused(stmts []js_ast.Stmt) bool {
1445914461
}
1446014462

1446114463
case *js_ast.SExpr:
14464+
if s.DoesNotAffectTreeShaking {
14465+
// Expressions marked with this are automatically generated and have
14466+
// no side effects by construction.
14467+
break
14468+
}
14469+
1446214470
if !p.exprCanBeRemovedIfUnused(s.Value) {
1446314471
return false
1446414472
}
@@ -14637,7 +14645,7 @@ func (p *parser) exprCanBeRemovedIfUnused(expr js_ast.Expr) bool {
1463714645
return true
1463814646

1463914647
case *js_ast.ECall:
14640-
canCallBeRemoved := e.CanBeUnwrappedIfUnused || e.IsKeepName
14648+
canCallBeRemoved := e.CanBeUnwrappedIfUnused
1464114649

1464214650
// Consider calls to our runtime "__publicField" function to be free of
1464314651
// side effects for the purpose of expression removal. This allows class

0 commit comments

Comments
 (0)