Skip to content

Commit

Permalink
allow ASI before < in typescript type parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Apr 19, 2022
1 parent 0818e03 commit ba3f604
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

The upcoming version of TypeScript adds the `moduleSuffixes` field to `tsconfig.json` that introduces more rules to import path resolution. Setting `moduleSuffixes` to `[".ios", ".native", ""]` will try to look at the the relative files `./foo.ios.ts`, `./foo.native.ts`, and finally `./foo.ts` for an import path of `./foo`. Note that the empty string `""` in `moduleSuffixes` is necessary for TypeScript to also look-up `./foo.ts`. This was announced in the [TypeScript 4.7 beta blog post](https://devblogs.microsoft.com/typescript/announcing-typescript-4-7-beta/#resolution-customization-with-modulesuffixes).

* Match the new ASI behavior from TypeScript nightly builds

This release updates esbuild to match some very recent behavior changes in the TypeScript parser regarding automatic semicolon insertion. For more information, see the following issues:

* https://github.com/microsoft/TypeScript/issues/48711

## 0.14.36

* Revert path metadata validation for now ([#2177](https://github.com/evanw/esbuild/issues/2177))
Expand Down
4 changes: 3 additions & 1 deletion internal/js_parser/ts_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,9 @@ func (p *parser) skipTypeScriptTypeWithOpts(level js_ast.L, opts skipTypeOpts) {
p.lexer.Next()
}

p.skipTypeScriptTypeArguments(false /* isInsideJSXElement */)
if !p.lexer.HasNewlineBefore {
p.skipTypeScriptTypeArguments(false /* isInsideJSXElement */)
}
}

case js_lexer.TOpenBracket:
Expand Down
20 changes: 13 additions & 7 deletions internal/js_parser/ts_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1900,13 +1900,19 @@ func TestTSInstantiationExpression(t *testing.T) {
expectParseErrorTS(t, "const a8 = f<number><number>;", "<stdin>: ERROR: Unexpected \";\"\n")
expectParseErrorTS(t, "const b1 = f?.<number>;", "<stdin>: ERROR: Expected \"(\" but found \";\"\n")

// The TypeScript compiler doesn't do semicolon insertion before "<" when
// inside "typeof" but does in other situations. Was this an oversight? Not sure,
// but we replicate this behavior because it matters when JSX syntax is enabled.
expectPrintedTSX(t, "type x = typeof y\n<number>\n1", "1;\n")
expectParseErrorTS(t, "type x = typeof y\n<number>\n1\n</number>", "<stdin>: ERROR: Unterminated regular expression\n")
expectParseErrorTSX(t, "type x = y\n<number>\n1", "<stdin>: ERROR: Unexpected end of file\n")
expectPrintedTSX(t, "type x = y\n<number>\n1\n</number>", "/* @__PURE__ */ React.createElement(\"number\", null, \"1\");\n")
// See: https://github.com/microsoft/TypeScript/issues/48711
expectPrintedTS(t, "type x = y\n<number>\nz", "z;\n")
expectPrintedTSX(t, "type x = y\n<number>\nz\n</number>", "/* @__PURE__ */ React.createElement(\"number\", null, \"z\");\n")
expectPrintedTS(t, "type x = typeof y\n<number>\nz", "z;\n")
expectPrintedTSX(t, "type x = typeof y\n<number>\nz\n</number>", "/* @__PURE__ */ React.createElement(\"number\", null, \"z\");\n")
expectPrintedTS(t, "interface Foo { \n (a: number): a \n <T>(): void \n }", "")
expectPrintedTSX(t, "interface Foo { \n (a: number): a \n <T>(): void \n }", "")
expectPrintedTS(t, "interface Foo { \n (a: number): typeof a \n <T>(): void \n }", "")
expectPrintedTSX(t, "interface Foo { \n (a: number): typeof a \n <T>(): void \n }", "")
expectParseErrorTS(t, "type x = y\n<number>\nz\n</number>", "<stdin>: ERROR: Unterminated regular expression\n")
expectParseErrorTSX(t, "type x = y\n<number>\nz", "<stdin>: ERROR: Unexpected end of file\n")
expectParseErrorTS(t, "type x = typeof y\n<number>\nz\n</number>", "<stdin>: ERROR: Unterminated regular expression\n")
expectParseErrorTSX(t, "type x = typeof y\n<number>\nz", "<stdin>: ERROR: Unexpected end of file\n")
}

func TestTSExponentiation(t *testing.T) {
Expand Down

0 comments on commit ba3f604

Please sign in to comment.