Skip to content

Commit

Permalink
fix #1498: variable shadowing broke class lowering
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Aug 5, 2021
1 parent a7a1b7f commit 88da5ed
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@
}
```

* Fix lowering of static private methods in class expressions ([#1498](https://github.com/evanw/esbuild/issues/1498))

Previously static private methods were lowered incorrectly when present in class expressions. The class expression itself was missing in the output due to an oversight (variable shadowing). This issue has been fixed:

```js
// Original code
(class {
static #x() {}
});

// Old output (with --target=es6)
var _x, _a, x_fn;
__privateAdd(_a, _x), _x = new WeakSet(), x_fn = function() {
}, __privateAdd(_a, _x), _a;

// New output (with --target=es6)
var _x, _a, x_fn;
_a = class {
}, _x = new WeakSet(), x_fn = function() {
}, __privateAdd(_a, _x), _a;
```

## 0.12.17

* Fix a bug with private fields and logical assignment operators ([#1418](https://github.com/evanw/esbuild/issues/1418))
Expand Down
12 changes: 11 additions & 1 deletion internal/bundler/snapshots/snapshots_lower.txt
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,17 @@ _foo = new WeakMap();
TestLowerPrivateClassExpr2020NoBundle
---------- /out.js ----------
var _field, _method, method_fn, _a, _staticField, _staticMethod, staticMethod_fn;
export let Foo = (__privateAdd(_a, _staticMethod), _field = new WeakMap(), _method = new WeakSet(), method_fn = function() {
export let Foo = (_a = class {
constructor() {
__privateAdd(this, _method);
__privateAdd(this, _field, void 0);
}
foo() {
var _a2;
__privateSet(this, _field, __privateMethod(this, _method, method_fn).call(this));
__privateSet(Foo, _staticField, __privateMethod(_a2 = Foo, _staticMethod, staticMethod_fn).call(_a2));
}
}, _field = new WeakMap(), _method = new WeakSet(), method_fn = function() {
}, _staticField = new WeakMap(), _staticMethod = new WeakSet(), staticMethod_fn = function() {
}, __privateAdd(_a, _staticMethod), __privateAdd(_a, _staticField, void 0), _a);

Expand Down
6 changes: 3 additions & 3 deletions internal/js_parser/js_parser_lower.go
Original file line number Diff line number Diff line change
Expand Up @@ -2269,7 +2269,7 @@ func (p *parser) lowerClass(stmt js_ast.Stmt, expr js_ast.Expr, shadowRef js_ast
}

// Add every newly-constructed instance into this map
expr = p.callRuntime(loc, "__privateAdd", []js_ast.Expr{
methodExpr := p.callRuntime(loc, "__privateAdd", []js_ast.Expr{
target,
{Loc: loc, Data: &js_ast.EIdentifier{Ref: ref}},
})
Expand All @@ -2285,10 +2285,10 @@ func (p *parser) lowerClass(stmt js_ast.Stmt, expr js_ast.Expr, shadowRef js_ast
//
if prop.IsStatic {
// Move this property to an assignment after the class ends
staticPrivateMethods = append(staticPrivateMethods, expr)
staticPrivateMethods = append(staticPrivateMethods, methodExpr)
} else {
// Move this property to an assignment inside the class constructor
instancePrivateMethods = append(instancePrivateMethods, js_ast.Stmt{Loc: loc, Data: &js_ast.SExpr{Value: expr}})
instancePrivateMethods = append(instancePrivateMethods, js_ast.Stmt{Loc: loc, Data: &js_ast.SExpr{Value: methodExpr}})
}
}

Expand Down

0 comments on commit 88da5ed

Please sign in to comment.