Skip to content

Commit 372bafa

Browse files
committed
fix mangle props with TS parameter properties
1 parent b6344e1 commit 372bafa

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

CHANGELOG.md

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

3+
## Unreleased
4+
5+
* Fix property name mangling for TypeScript parameter properties
6+
7+
This release adds TypeScript parameter properties to the set of syntax constructs affected by the new `--mangle-props=` setting. Parameter properties are a TypeScript-only shorthand way of initializing a class field directly from the constructor argument list like this:
8+
9+
```ts
10+
// Original code
11+
class Foo { constructor(public foo_) {} }
12+
13+
// Old output (with --minify --mangle-props=_)
14+
class Foo{constructor(c){this.foo_=c}}
15+
16+
// New output (with --minify --mangle-props=_)
17+
class Foo{constructor(o){this.c=o}}
18+
```
19+
320
## 0.14.15
421

522
* Add property name mangling with `--mangle-props=` ([#218](https://github.com/evanw/esbuild/issues/218))

internal/bundler/bundler_default_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5660,3 +5660,27 @@ func TestManglePropsAvoidCollisions(t *testing.T) {
56605660
},
56615661
})
56625662
}
5663+
5664+
func TestManglePropsTSParameterProperties(t *testing.T) {
5665+
loader_suite.expectBundled(t, bundled{
5666+
files: map[string]string{
5667+
"/entry.ts": `
5668+
class Foo {
5669+
constructor(
5670+
public bar: number,
5671+
public baz_: number,
5672+
) {
5673+
}
5674+
}
5675+
let foo = new Foo
5676+
console.log(foo.bar, foo.baz_)
5677+
`,
5678+
},
5679+
entryPaths: []string{"/entry.js"},
5680+
options: config.Options{
5681+
Mode: config.ModePassThrough,
5682+
AbsOutputFile: "/out.js",
5683+
MangleProps: regexp.MustCompile("_$"),
5684+
},
5685+
})
5686+
}

internal/bundler/snapshots/snapshots_loader.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,18 @@ export default function(x) {
670670
x?.a["bar_"];
671671
}
672672

673+
================================================================================
674+
TestManglePropsTSParameterProperties
675+
---------- /out.js ----------
676+
class Foo {
677+
constructor(bar, baz_) {
678+
this.bar = bar;
679+
this.a = baz_;
680+
}
681+
}
682+
let foo = new Foo();
683+
console.log(foo.bar, foo.a);
684+
673685
================================================================================
674686
TestMinifyIdentifiersImportPathFrequencyAnalysis
675687
---------- /out/import.js ----------

internal/js_parser/js_parser_lower.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,12 +2443,21 @@ func (p *parser) lowerClass(stmt js_ast.Stmt, expr js_ast.Expr, shadowRef js_ast
24432443
for _, arg := range ctor.Fn.Args {
24442444
if arg.IsTypeScriptCtorField {
24452445
if id, ok := arg.Binding.Data.(*js_ast.BIdentifier); ok {
2446-
parameterFields = append(parameterFields, js_ast.AssignStmt(
2447-
js_ast.Expr{Loc: arg.Binding.Loc, Data: &js_ast.EDot{
2446+
var target js_ast.Expr
2447+
if name := p.symbols[id.Ref.InnerIndex].OriginalName; p.isMangledProperty(name) {
2448+
target = js_ast.Expr{Loc: arg.Binding.Loc, Data: &js_ast.EIndex{
2449+
Target: js_ast.Expr{Loc: arg.Binding.Loc, Data: js_ast.EThisShared},
2450+
Index: js_ast.Expr{Loc: arg.Binding.Loc, Data: &js_ast.EMangledProp{Ref: p.symbolForMangledProperty(name)}},
2451+
}}
2452+
} else {
2453+
target = js_ast.Expr{Loc: arg.Binding.Loc, Data: &js_ast.EDot{
24482454
Target: js_ast.Expr{Loc: arg.Binding.Loc, Data: js_ast.EThisShared},
2449-
Name: p.symbols[id.Ref.InnerIndex].OriginalName,
2455+
Name: name,
24502456
NameLoc: arg.Binding.Loc,
2451-
}},
2457+
}}
2458+
}
2459+
parameterFields = append(parameterFields, js_ast.AssignStmt(
2460+
target,
24522461
js_ast.Expr{Loc: arg.Binding.Loc, Data: &js_ast.EIdentifier{Ref: id.Ref}},
24532462
))
24542463
}

0 commit comments

Comments
 (0)