Skip to content

Commit

Permalink
Revert "Drop superfluous __name() calls (#2062)"
Browse files Browse the repository at this point in the history
This reverts commit 730df4f.
  • Loading branch information
evanw committed Apr 7, 2022
1 parent 57a75a8 commit 046f82f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 246 deletions.
71 changes: 0 additions & 71 deletions internal/bundler/bundler_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4358,77 +4358,6 @@ func TestDefineThis(t *testing.T) {
})
}

func TestKeepNamesWithNecessaryHelperFunctionCalls(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
import {
functionStmt as functionStmt1,
functionExpr as functionExpr1,
classStmt as classStmt1,
classExpr as classExpr1,
functionAnonExpr as functionAnonExpr1,
classAnonExpr as classAnonExpr1,
classAnonExprLowered as classAnonExprLowered1,
} from './copy1'
import {
functionStmt as functionStmt2,
functionExpr as functionExpr2,
classStmt as classStmt2,
classExpr as classExpr2,
functionAnonExpr as functionAnonExpr2,
classAnonExpr as classAnonExpr2,
classAnonExprLowered as classAnonExprLowered2,
} from './copy2'
console.log([
functionStmt1, functionStmt2,
functionExpr1, functionExpr2,
classStmt1, classStmt2,
classExpr1, classExpr2,
functionAnonExpr1, functionAnonExpr2,
classAnonExpr1, classAnonExpr2,
classAnonExprLowered1, classAnonExprLowered2,
])
`,

"/copy1.js": `
export function functionStmt() { return 'copy1' }
export class classStmt { foo = 'copy1' }
export let functionExpr = function fn() { return 'copy1' }
export let classExpr = class cls { foo = 'copy1' }
export let functionAnonExpr = function() { return 'copy1' }
export let classAnonExpr = class { foo = 'copy1' }
export let classAnonExprLowered = class { static foo = 'copy2' }
class classStmtSideEffect { static [copy1]() {} }
let classExprSideEffect = class clsSideEffect { static [copy1]() {} }
`,

"/copy2.js": `
export function functionStmt() { return 'copy2' }
export class classStmt { foo = 'copy2' }
export let functionExpr = function fn() { return 'copy2' }
export let classExpr = class cls { foo = 'copy2' }
export let functionAnonExpr = function() { return 'copy2' }
export let classAnonExpr = class { foo = 'copy2' }
export let classAnonExprLowered = class { static foo = 'copy2' }
class classStmtSideEffect { static [copy2]() {} }
let classExprSideEffect = class clsSideEffect { static [copy2]() {} }
`,
},
entryPaths: []string{"/entry.js"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
KeepNames: true,
UnsupportedJSFeatures: compat.ClassStaticField,
},
})
}

func TestKeepNamesTreeShaking(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
Expand Down
91 changes: 4 additions & 87 deletions internal/bundler/snapshots/snapshots_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1401,103 +1401,19 @@ TestKeepNamesTreeShaking
// entry.js
function fnStmtKeep() {
}
__name(fnStmtKeep, "fnStmtKeep");
x = fnStmtKeep;
var fnExprKeep = /* @__PURE__ */ __name(function() {
}, "keep");
x = fnExprKeep;
var clsStmtKeep = class {
};
__name(clsStmtKeep, "clsStmtKeep");
new clsStmtKeep();
var clsExprKeep = /* @__PURE__ */ __name(class {
}, "keep");
new clsExprKeep();

================================================================================
TestKeepNamesWithNecessaryHelperFunctionCalls
---------- /out.js ----------
// copy1.js
function functionStmt() {
return "copy1";
}
var classStmt = class {
foo = "copy1";
};
var functionExpr = function fn() {
return "copy1";
};
var classExpr = class cls {
foo = "copy1";
};
var functionAnonExpr = function() {
return "copy1";
};
var classAnonExpr = class {
foo = "copy1";
};
var _a;
var classAnonExprLowered = /* @__PURE__ */ __name((_a = class {
}, __publicField(_a, "foo", "copy2"), _a), "classAnonExprLowered");
var classStmtSideEffect = class {
static [copy1]() {
}
};
var classExprSideEffect = class clsSideEffect {
static [copy1]() {
}
};

// copy2.js
function functionStmt2() {
return "copy2";
}
__name(functionStmt2, "functionStmt");
var classStmt2 = class {
foo = "copy2";
};
__name(classStmt2, "classStmt");
var functionExpr2 = /* @__PURE__ */ __name(function fn2() {
return "copy2";
}, "fn");
var classExpr2 = /* @__PURE__ */ __name(class cls2 {
foo = "copy2";
}, "cls");
var functionAnonExpr2 = /* @__PURE__ */ __name(function() {
return "copy2";
}, "functionAnonExpr");
var classAnonExpr2 = /* @__PURE__ */ __name(class {
foo = "copy2";
}, "classAnonExpr");
var _a2;
var classAnonExprLowered2 = /* @__PURE__ */ __name((_a2 = class {
}, __publicField(_a2, "foo", "copy2"), _a2), "classAnonExprLowered");
var classStmtSideEffect2 = class {
static [copy2]() {
}
};
__name(classStmtSideEffect2, "classStmtSideEffect");
var classExprSideEffect2 = /* @__PURE__ */ __name(class clsSideEffect2 {
static [copy2]() {
}
}, "clsSideEffect");

// entry.js
console.log([
functionStmt,
functionStmt2,
functionExpr,
functionExpr2,
classStmt,
classStmt2,
classExpr,
classExpr2,
functionAnonExpr,
functionAnonExpr2,
classAnonExpr,
classAnonExpr2,
classAnonExprLowered,
classAnonExprLowered2
]);

================================================================================
TestLegalCommentsAvoidSlashTagEndOfFile
---------- /out/entry.js ----------
Expand Down Expand Up @@ -2894,11 +2810,12 @@ export function outer() {
let inner = function() {
return Math.random();
};
__name(inner, "inner");
const x = inner();
console.log(x);
}
}
outer();
__name(outer, "outer"), outer();

================================================================================
TestSwitchScopeNoBundle
Expand Down
1 change: 1 addition & 0 deletions internal/bundler/snapshots/snapshots_ts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,7 @@ TestTypeScriptDecoratorsKeepNames
// entry.ts
var Foo = class {
};
__name(Foo, "Foo");
Foo = __decorateClass([
decoratorMustComeAfterName
], Foo);
7 changes: 5 additions & 2 deletions internal/js_ast/js_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,6 @@ type ECall struct {
// call itself is removed due to this annotation, the arguments must remain
// if they have side effects.
CanBeUnwrappedIfUnused bool

IsKeepName bool
}

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

type SExpr struct {
Value Expr

// This is set to true for automatically-generated expressions that should
// not affect tree shaking. For example, calling a function from the runtime
// that doesn't have externally-visible side effects.
DoesNotAffectTreeShaking bool
}

type EnumValue struct {
Expand Down
24 changes: 16 additions & 8 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7617,6 +7617,7 @@ func (p *parser) mangleStmts(stmts []js_ast.Stmt, kind stmtsKind) []js_ast.Stmt
prevStmt := result[len(result)-1]
if prevS, ok := prevStmt.Data.(*js_ast.SExpr); ok {
prevS.Value = js_ast.JoinWithComma(prevS.Value, s.Value)
prevS.DoesNotAffectTreeShaking = prevS.DoesNotAffectTreeShaking && s.DoesNotAffectTreeShaking
continue
}
}
Expand Down Expand Up @@ -8852,20 +8853,21 @@ func (p *parser) keepExprSymbolName(value js_ast.Expr, name string) js_ast.Expr

// Make sure tree shaking removes this if the function is never used
value.Data.(*js_ast.ECall).CanBeUnwrappedIfUnused = true
value.Data.(*js_ast.ECall).IsKeepName = true
return value
}

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

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

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

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

case *js_ast.SExpr:
if s.DoesNotAffectTreeShaking {
// Expressions marked with this are automatically generated and have
// no side effects by construction.
break
}

if !p.exprCanBeRemovedIfUnused(s.Value) {
return false
}
Expand Down Expand Up @@ -14637,7 +14645,7 @@ func (p *parser) exprCanBeRemovedIfUnused(expr js_ast.Expr) bool {
return true

case *js_ast.ECall:
canCallBeRemoved := e.CanBeUnwrappedIfUnused || e.IsKeepName
canCallBeRemoved := e.CanBeUnwrappedIfUnused

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

0 comments on commit 046f82f

Please sign in to comment.