Skip to content

Commit

Permalink
fix #1493: nullish coalescing assignment edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Aug 4, 2021
1 parent 2041f0a commit f68030c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
# Changelog

## Unreleased

* Fix lowering of nullish coalescing assignment edge case ([#1493](https://github.com/evanw/esbuild/issues/1493))

This release fixes a bug where lowering of the `??=` nullish coalescing assignment operator failed when the target environment supported nullish coalescing and private class fields but not nullish coalescing assignment. An example target environment with this specific feature support matrix combination is node 14.8. This edge case is now lowered correctly:

```js
// Original code
class A {
#a;
f() {
this.#a ??= 1;
}
}

// Old output (with --target=node14.8)
panic: Unexpected expression of type *js_ast.EPrivateIdentifier

// New output (with --target=node14.8)
class A {
#a;
f() {
this.#a ?? (this.#a = 1);
}
}
```

## 0.12.17

* Fix a bug with private fields and logical assignment operators ([#1418](https://github.com/evanw/esbuild/issues/1418))
Expand Down
22 changes: 22 additions & 0 deletions internal/bundler/bundler_lower_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1593,3 +1593,25 @@ func TestLowerPrivateClassFieldStaticIssue1424(t *testing.T) {
},
})
}

// See https://github.com/evanw/esbuild/issues/1493 for more information
func TestLowerNullishCoalescingAssignmentIssue1493(t *testing.T) {
lower_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
class A {
#a;
f() {
this.#a ??= 1;
}
}
`,
},
entryPaths: []string{"/entry.js"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
UnsupportedJSFeatures: compat.LogicalAssignment,
},
})
}
11 changes: 11 additions & 0 deletions internal/bundler/snapshots/snapshots_lower.txt
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,17 @@ export { ns };
let ns2 = 123;
export { ns2 as sn };

================================================================================
TestLowerNullishCoalescingAssignmentIssue1493
---------- /out.js ----------
// entry.js
var A = class {
#a;
f() {
this.#a ?? (this.#a = 1);
}
};

================================================================================
TestLowerObjectSpreadNoBundle
---------- /out.js ----------
Expand Down
2 changes: 2 additions & 0 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9366,6 +9366,8 @@ func (p *parser) captureValueWithPossibleSideEffects(
valueFunc = func() js_ast.Expr { return js_ast.Expr{Loc: loc, Data: &js_ast.EBigInt{Value: e.Value}} }
case *js_ast.EString:
valueFunc = func() js_ast.Expr { return js_ast.Expr{Loc: loc, Data: &js_ast.EString{Value: e.Value}} }
case *js_ast.EPrivateIdentifier:
valueFunc = func() js_ast.Expr { return js_ast.Expr{Loc: loc, Data: &js_ast.EPrivateIdentifier{Ref: e.Ref}} }
case *js_ast.EIdentifier:
if mode == valueDefinitelyNotMutated {
valueFunc = func() js_ast.Expr {
Expand Down

0 comments on commit f68030c

Please sign in to comment.