Skip to content

Commit

Permalink
fix #678: insert object spread shim after super()
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jan 18, 2021
1 parent 2aa01f5 commit a6c4a03
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
37 changes: 37 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,43 @@

Due to an oversight, the `sourcesContent: false` option that was added in version 0.8.27 didn't work with the JavaScript transform API. This was unintentional and has been fixed. This fix was contributed by [@jschaf](https://github.com/jschaf).

* Insert the object spread shim in constructor methods after the `super()` call ([#678](https://github.com/evanw/esbuild/issues/678))

This fixes an issue with the transform for object spread to older compile targets. Previously the following code would be transformed to code that crashes when run if the compile target is `es2017` or lower:

```js
class Derived extends Base {
prop = null;
constructor({ ...args }) {
super(args);
}
}
```

This code was incorrectly compiled to something like this, which will throw `ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor`:

```js
class Derived extends Base {
constructor(_a) {
__publicField(this, "prop", null);
var args = __rest(_a, []);
super(args);
}
}
```

With this release, it will now be compiled to something like this instead:

```js
class Derived extends Base {
constructor(_a) {
var args = __rest(_a, []);
super(args);
__publicField(this, "prop", null);
}
}
```

* Provide minified and non-minified versions of in-browser API library ([#616](https://github.com/evanw/esbuild/issues/616))

The in-browser JavaScript API libraries for esbuild are in the [esbuild-wasm](https://www.npmjs.com/package/esbuild-wasm) package. There are two: `esbuild-wasm/lib/browser.js` in UMD format and `esbuild-wasm/esm/browser.js` in ESM format. Previously these were minified since they contain a large string of JavaScript that cannot be minified by other tools. Now they are no longer minified, and there are new minified versions available at `esbuild-wasm/lib/browser.min.js` and `esbuild-wasm/esm/browser.min.js`.
Expand Down
9 changes: 6 additions & 3 deletions internal/js_parser/js_parser_lower.go
Original file line number Diff line number Diff line change
Expand Up @@ -1923,9 +1923,12 @@ func (p *parser) lowerClass(stmt js_ast.Stmt, expr js_ast.Expr, shadowRef js_ast
// Insert the instance field initializers after the super call if there is one
stmtsFrom := ctor.Fn.Body.Stmts
stmtsTo := []js_ast.Stmt{}
if len(stmtsFrom) > 0 && js_ast.IsSuperCall(stmtsFrom[0]) {
stmtsTo = append(stmtsTo, stmtsFrom[0])
stmtsFrom = stmtsFrom[1:]
for i, stmt := range stmtsFrom {
if js_ast.IsSuperCall(stmt) {
stmtsTo = append(stmtsTo, stmtsFrom[0:i+1]...)
stmtsFrom = stmtsFrom[i+1:]
break
}
}
stmtsTo = append(stmtsTo, parameterFields...)
stmtsTo = append(stmtsTo, instanceMembers...)
Expand Down
10 changes: 10 additions & 0 deletions internal/js_parser/js_parser_lower_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,16 @@ func TestLowerClassInstance(t *testing.T) {
bar() {
}
}
`)
expectPrintedTarget(t, 2015, "class Foo extends Bar { bar() {} foo; constructor({ ...args }) { super() } }", `class Foo extends Bar {
constructor(_a) {
var args = __rest(_a, []);
super();
__publicField(this, "foo");
}
bar() {
}
}
`)
}

Expand Down

0 comments on commit a6c4a03

Please sign in to comment.