Skip to content

Commit

Permalink
only --keep-names for lowered #private methods
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed May 5, 2024
1 parent d5e343b commit 5e7d1fc
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 7 deletions.
30 changes: 25 additions & 5 deletions internal/bundler_tests/bundler_default_test.go
Expand Up @@ -5499,7 +5499,7 @@ NOTE: The expression "b['c']" has been configured to be replaced with a constant
func TestKeepNamesAllForms(t *testing.T) {
default_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
"/keep.js": `
// Initializers
function fn() {}
function foo(fn = function() {}) {}
Expand Down Expand Up @@ -5534,12 +5534,32 @@ func TestKeepNamesAllForms(t *testing.T) {
[fn = function() {}] = [];
({ fn = function() {} } = {});
`,
"/do-not-keep.js": `
// Methods
class Foo0 { fn() {} }
class Foo1 { get fn() {} }
class Foo2 { set fn(_) {} }
class Foo3 { static fn() {} }
class Foo4 { static get fn() {} }
class Foo5 { static set fn(_) {} }
// Private methods
class Bar0 { #fn() {} }
class Bar1 { get #fn() {} }
class Bar2 { set #fn(_) {} }
class Bar3 { static #fn() {} }
class Bar4 { static get #fn() {} }
class Bar5 { static set #fn(_) {} }
`,
},
entryPaths: []string{
"/keep.js",
"/do-not-keep.js",
},
entryPaths: []string{"/entry.js"},
options: config.Options{
Mode: config.ModePassThrough,
AbsOutputFile: "/out.js",
KeepNames: true,
Mode: config.ModePassThrough,
AbsOutputDir: "/out",
KeepNames: true,
},
})
}
Expand Down
88 changes: 87 additions & 1 deletion internal/bundler_tests/snapshots/snapshots_default.txt
Expand Up @@ -2708,7 +2708,7 @@ console.log([

================================================================================
TestKeepNamesAllForms
---------- /out.js ----------
---------- /out/keep.js ----------
function fn() {
}
__name(fn, "fn");
Expand Down Expand Up @@ -2819,6 +2819,92 @@ __name(foo, "foo");
({ fn = /* @__PURE__ */ __name(function() {
}, "fn") } = {});

---------- /out/do-not-keep.js ----------
class Foo0 {
static {
__name(this, "Foo0");
}
fn() {
}
}
class Foo1 {
static {
__name(this, "Foo1");
}
get fn() {
}
}
class Foo2 {
static {
__name(this, "Foo2");
}
set fn(_) {
}
}
class Foo3 {
static {
__name(this, "Foo3");
}
static fn() {
}
}
class Foo4 {
static {
__name(this, "Foo4");
}
static get fn() {
}
}
class Foo5 {
static {
__name(this, "Foo5");
}
static set fn(_) {
}
}
class Bar0 {
static {
__name(this, "Bar0");
}
#fn() {
}
}
class Bar1 {
static {
__name(this, "Bar1");
}
get #fn() {
}
}
class Bar2 {
static {
__name(this, "Bar2");
}
set #fn(_) {
}
}
class Bar3 {
static {
__name(this, "Bar3");
}
static #fn() {
}
}
class Bar4 {
static {
__name(this, "Bar4");
}
static get #fn() {
}
}
class Bar5 {
static {
__name(this, "Bar5");
}
static set #fn(_) {
}
}

================================================================================
TestKeepNamesClassStaticName
---------- /out.js ----------
Expand Down
4 changes: 3 additions & 1 deletion internal/js_parser/js_parser.go
Expand Up @@ -11629,7 +11629,9 @@ func (p *parser) visitClass(nameScopeLoc logger.Loc, class *js_ast.Class, defaul
// will be transformed such that it is no longer an inline initializer.
nameToKeep := ""
if private, ok := property.Key.Data.(*js_ast.EPrivateIdentifier); ok {
nameToKeep = p.symbols[private.Ref.InnerIndex].OriginalName
if !property.Kind.IsMethodDefinition() || p.privateSymbolNeedsToBeLowered(private) {
nameToKeep = p.symbols[private.Ref.InnerIndex].OriginalName
}

// Lowered private methods (both instance and static) are initialized
// outside of the class body, so we must rewrite "super" property
Expand Down

0 comments on commit 5e7d1fc

Please sign in to comment.