Skip to content

Commit

Permalink
fix #2937: parse rest bindings in TypeScript types
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 19, 2023
1 parent d8b028f commit 42d3b2f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,16 @@

## Unreleased

* Parse rest bindings in TypeScript types ([#2937](https://github.com/evanw/esbuild/issues/2937))

Previously esbuild was unable to parse the following valid TypeScript code:

```ts
let tuple: (...[e1, e2, ...es]: any) => any
```

This release includes support for parsing code like this.

* Fix TypeScript code translation for certain computed `declare` class fields ([#2914](https://github.com/evanw/esbuild/issues/2914))

In TypeScript, the key of a computed `declare` class field should only be preserved if there are no decorators for that field. Previously esbuild always preserved the key, but esbuild will now remove the key to match the output of the TypeScript compiler:
Expand Down
5 changes: 5 additions & 0 deletions internal/js_parser/ts_parser.go
Expand Up @@ -30,6 +30,11 @@ func (p *parser) skipTypeScriptBinding() {

// "[a, b]"
for p.lexer.Token != js_lexer.TCloseBracket {
// "[...a]"
if p.lexer.Token == js_lexer.TDotDotDot {
p.lexer.Next()
}

p.skipTypeScriptBinding()
if p.lexer.Token != js_lexer.TComma {
break
Expand Down
6 changes: 6 additions & 0 deletions internal/js_parser/ts_parser_test.go
Expand Up @@ -183,6 +183,12 @@ func TestTSTypes(t *testing.T) {
expectPrintedTS(t, "type Foo = {} extends (infer T extends {}) ? A<T> : never", "")
expectPrintedTS(t, "let x: A extends B<infer C extends D> ? D : never", "let x;\n")
expectPrintedTS(t, "let x: A extends B<infer C extends D ? infer C : never> ? D : never", "let x;\n")
expectPrintedTS(t, "let x: ([e1, e2, ...es]: any) => any", "let x;\n")
expectPrintedTS(t, "let x: (...[e1, e2, es]: any) => any", "let x;\n")
expectPrintedTS(t, "let x: (...[e1, e2, ...es]: any) => any", "let x;\n")
expectPrintedTS(t, "let x: (y, [e1, e2, ...es]: any) => any", "let x;\n")
expectPrintedTS(t, "let x: (y, ...[e1, e2, es]: any) => any", "let x;\n")
expectPrintedTS(t, "let x: (y, ...[e1, e2, ...es]: any) => any", "let x;\n")

expectPrintedTS(t, "let x: A.B<X.Y>", "let x;\n")
expectPrintedTS(t, "let x: A.B<X.Y>=2", "let x = 2;\n")
Expand Down

0 comments on commit 42d3b2f

Please sign in to comment.