Skip to content

Commit 5e7d1fc

Browse files
committed
only --keep-names for lowered #private methods
1 parent d5e343b commit 5e7d1fc

File tree

3 files changed

+115
-7
lines changed

3 files changed

+115
-7
lines changed

internal/bundler_tests/bundler_default_test.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5499,7 +5499,7 @@ NOTE: The expression "b['c']" has been configured to be replaced with a constant
54995499
func TestKeepNamesAllForms(t *testing.T) {
55005500
default_suite.expectBundled(t, bundled{
55015501
files: map[string]string{
5502-
"/entry.js": `
5502+
"/keep.js": `
55035503
// Initializers
55045504
function fn() {}
55055505
function foo(fn = function() {}) {}
@@ -5534,12 +5534,32 @@ func TestKeepNamesAllForms(t *testing.T) {
55345534
[fn = function() {}] = [];
55355535
({ fn = function() {} } = {});
55365536
`,
5537+
"/do-not-keep.js": `
5538+
// Methods
5539+
class Foo0 { fn() {} }
5540+
class Foo1 { get fn() {} }
5541+
class Foo2 { set fn(_) {} }
5542+
class Foo3 { static fn() {} }
5543+
class Foo4 { static get fn() {} }
5544+
class Foo5 { static set fn(_) {} }
5545+
5546+
// Private methods
5547+
class Bar0 { #fn() {} }
5548+
class Bar1 { get #fn() {} }
5549+
class Bar2 { set #fn(_) {} }
5550+
class Bar3 { static #fn() {} }
5551+
class Bar4 { static get #fn() {} }
5552+
class Bar5 { static set #fn(_) {} }
5553+
`,
5554+
},
5555+
entryPaths: []string{
5556+
"/keep.js",
5557+
"/do-not-keep.js",
55375558
},
5538-
entryPaths: []string{"/entry.js"},
55395559
options: config.Options{
5540-
Mode: config.ModePassThrough,
5541-
AbsOutputFile: "/out.js",
5542-
KeepNames: true,
5560+
Mode: config.ModePassThrough,
5561+
AbsOutputDir: "/out",
5562+
KeepNames: true,
55435563
},
55445564
})
55455565
}

internal/bundler_tests/snapshots/snapshots_default.txt

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2708,7 +2708,7 @@ console.log([
27082708

27092709
================================================================================
27102710
TestKeepNamesAllForms
2711-
---------- /out.js ----------
2711+
---------- /out/keep.js ----------
27122712
function fn() {
27132713
}
27142714
__name(fn, "fn");
@@ -2819,6 +2819,92 @@ __name(foo, "foo");
28192819
({ fn = /* @__PURE__ */ __name(function() {
28202820
}, "fn") } = {});
28212821

2822+
---------- /out/do-not-keep.js ----------
2823+
class Foo0 {
2824+
static {
2825+
__name(this, "Foo0");
2826+
}
2827+
fn() {
2828+
}
2829+
}
2830+
class Foo1 {
2831+
static {
2832+
__name(this, "Foo1");
2833+
}
2834+
get fn() {
2835+
}
2836+
}
2837+
class Foo2 {
2838+
static {
2839+
__name(this, "Foo2");
2840+
}
2841+
set fn(_) {
2842+
}
2843+
}
2844+
class Foo3 {
2845+
static {
2846+
__name(this, "Foo3");
2847+
}
2848+
static fn() {
2849+
}
2850+
}
2851+
class Foo4 {
2852+
static {
2853+
__name(this, "Foo4");
2854+
}
2855+
static get fn() {
2856+
}
2857+
}
2858+
class Foo5 {
2859+
static {
2860+
__name(this, "Foo5");
2861+
}
2862+
static set fn(_) {
2863+
}
2864+
}
2865+
class Bar0 {
2866+
static {
2867+
__name(this, "Bar0");
2868+
}
2869+
#fn() {
2870+
}
2871+
}
2872+
class Bar1 {
2873+
static {
2874+
__name(this, "Bar1");
2875+
}
2876+
get #fn() {
2877+
}
2878+
}
2879+
class Bar2 {
2880+
static {
2881+
__name(this, "Bar2");
2882+
}
2883+
set #fn(_) {
2884+
}
2885+
}
2886+
class Bar3 {
2887+
static {
2888+
__name(this, "Bar3");
2889+
}
2890+
static #fn() {
2891+
}
2892+
}
2893+
class Bar4 {
2894+
static {
2895+
__name(this, "Bar4");
2896+
}
2897+
static get #fn() {
2898+
}
2899+
}
2900+
class Bar5 {
2901+
static {
2902+
__name(this, "Bar5");
2903+
}
2904+
static set #fn(_) {
2905+
}
2906+
}
2907+
28222908
================================================================================
28232909
TestKeepNamesClassStaticName
28242910
---------- /out.js ----------

internal/js_parser/js_parser.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11629,7 +11629,9 @@ func (p *parser) visitClass(nameScopeLoc logger.Loc, class *js_ast.Class, defaul
1162911629
// will be transformed such that it is no longer an inline initializer.
1163011630
nameToKeep := ""
1163111631
if private, ok := property.Key.Data.(*js_ast.EPrivateIdentifier); ok {
11632-
nameToKeep = p.symbols[private.Ref.InnerIndex].OriginalName
11632+
if !property.Kind.IsMethodDefinition() || p.privateSymbolNeedsToBeLowered(private) {
11633+
nameToKeep = p.symbols[private.Ref.InnerIndex].OriginalName
11634+
}
1163311635

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

0 commit comments

Comments
 (0)