Skip to content

Commit d8d1ee0

Browse files
committed
fix #1998: keyword property name mangling
1 parent 256ab49 commit d8d1ee0

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* Fix property mangling and keyword properties ([#1998](https://github.com/evanw/esbuild/issues/1998))
6+
7+
Previously enabling property mangling with `--mangle-props=` failed to add a space before property names after a keyword. This bug has been fixed:
8+
9+
```js
10+
// Original code
11+
class Foo {
12+
static foo = {
13+
get bar() {}
14+
}
15+
}
16+
17+
// Old output (with --minify --mangle-props=.)
18+
class Foo{statics={gett(){}}}
19+
20+
// New output (with --minify --mangle-props=.)
21+
class Foo{static s={get t(){}}}
22+
```
23+
324
## 0.14.19
425

526
* Special-case `const` inlining at the top of a scope ([#1317](https://github.com/evanw/esbuild/issues/1317), [#1981](https://github.com/evanw/esbuild/issues/1981))

internal/bundler/bundler_default_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5419,6 +5419,27 @@ func TestManglePropsMinify(t *testing.T) {
54195419
})
54205420
}
54215421

5422+
func TestManglePropsKeywordPropertyMinify(t *testing.T) {
5423+
loader_suite.expectBundled(t, bundled{
5424+
files: map[string]string{
5425+
"/entry.js": `
5426+
class Foo {
5427+
static bar = { get baz() { return 123 } }
5428+
}
5429+
`,
5430+
},
5431+
entryPaths: []string{"/entry.js"},
5432+
options: config.Options{
5433+
Mode: config.ModePassThrough,
5434+
AbsOutputDir: "/out",
5435+
MangleProps: regexp.MustCompile("."),
5436+
MinifyIdentifiers: true,
5437+
MinifySyntax: true,
5438+
MinifyWhitespace: true,
5439+
},
5440+
})
5441+
}
5442+
54225443
func TestManglePropsOptionalChain(t *testing.T) {
54235444
loader_suite.expectBundled(t, bundled{
54245445
files: map[string]string{

internal/bundler/snapshots/snapshots_loader.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,11 @@ export default [
597597
})
598598
];
599599

600+
================================================================================
601+
TestManglePropsKeywordPropertyMinify
602+
---------- /out/entry.js ----------
603+
class Foo{static t={get s(){return 123}}}
604+
600605
================================================================================
601606
TestManglePropsLoweredClassFields
602607
---------- /out.js ----------

internal/js_printer/js_printer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,7 @@ func (p *printer) printProperty(item js_ast.Property) {
988988
case *js_ast.EMangledProp:
989989
p.addSourceMapping(item.Key.Loc)
990990
if name := p.renamer.NameForSymbol(key.Ref); p.canPrintIdentifier(name) {
991+
p.printSpaceBeforeIdentifier()
991992
p.printIdentifier(name)
992993

993994
// Use a shorthand property if the names are the same

scripts/end-to-end-tests.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2906,6 +2906,18 @@
29062906
}),
29072907
)
29082908

2909+
// Test minification of mangled properties (class and object) with a keyword before them
2910+
tests.push(
2911+
test(['in.js', '--outfile=node.js', '--minify', '--mangle-props=.'], {
2912+
'in.js': `
2913+
class Foo {
2914+
static bar = { get baz() { return 123 } }
2915+
}
2916+
if (Foo.bar.baz !== 123) throw 'fail'
2917+
`,
2918+
}),
2919+
)
2920+
29092921
// Test minification of hoisted top-level symbols declared in nested scopes.
29102922
// Previously this code was incorrectly transformed into this, which crashes:
29112923
//

0 commit comments

Comments
 (0)