Skip to content

Commit

Permalink
fix #1512: asi issue with "." and type parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Aug 10, 2021
1 parent d945c75 commit a9456df
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@

This mirrors how the JS code generator similarly avoids the character sequence `</script`.

* Fix a TypeScript parsing edge case with ASI (Automatic Semicolon Insertion) ([#1512](https://github.com/evanw/esbuild/issues/1512))

This fixes a parsing bug where TypeScript types consisting of multiple identifiers joined together with a `.` could incorrectly extend onto the next line if the next line started with `<`. This problem was due to ASI; esbuild should be automatically inserting a semicolon at the end of the line:

```ts
let x: {
<A extends B>(): c.d
<E extends F>(): g.h
}
```

Previously the above code was a syntax error. With this release, this code is now parsed correctly.

## 0.12.19

* Add support for CSS source maps ([#519](https://github.com/evanw/esbuild/issues/519))
Expand Down
6 changes: 5 additions & 1 deletion internal/js_parser/ts_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,11 @@ func (p *parser) skipTypeScriptTypeWithOpts(level js_ast.L, opts skipTypeOpts) {
p.lexer.Expect(js_lexer.TIdentifier)
}
p.lexer.Next()
p.skipTypeScriptTypeArguments(false /* isInsideJSXElement */)

// "{ <A extends B>(): c.d \n <E extends F>(): g.h }" must not become a single type
if !p.lexer.HasNewlineBefore {
p.skipTypeScriptTypeArguments(false /* isInsideJSXElement */)
}

case js_lexer.TOpenBracket:
// "{ ['x']: string \n ['y']: string }" must not become a single type
Expand Down
6 changes: 6 additions & 0 deletions internal/js_parser/ts_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ func TestTSTypes(t *testing.T) {
expectParseErrorTS(t, "let x: typeof readonly Array", "<stdin>: error: Expected \";\" but found \"Array\"\n")
expectPrintedTS(t, "let x: `y`", "let x;\n")
expectParseErrorTS(t, "let x: tag`y`", "<stdin>: error: Expected \";\" but found \"`y`\"\n")
expectPrintedTS(t, "let x: { <A extends B>(): c.d \n <E extends F>(): g.h }", "let x;\n")
expectPrintedTSX(t, "type x = a.b \n <c></c>", "/* @__PURE__ */ React.createElement(\"c\", null);\n")
expectPrintedTS(t, "type Foo = a.b \n | c.d", "")
expectPrintedTS(t, "type Foo = a.b \n & c.d", "")
expectPrintedTS(t, "type Foo = \n | a.b \n | c.d", "")
expectPrintedTS(t, "type Foo = \n & a.b \n & c.d", "")

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 a9456df

Please sign in to comment.