Skip to content

Commit 2c2ef24

Browse files
committed
fix #2410: new tsx/arrow parsing rules for ts 4.6
1 parent f41b38d commit 2c2ef24

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
* Update parser for arrow functions with initial default type parameters in `.tsx` files ([#2410](https://github.com/evanw/esbuild/issues/2410))
6+
7+
TypeScript 4.6 introduced a [change to the parsing of JSX syntax in `.tsx` files](https://github.com/microsoft/TypeScript/issues/47062). Now a `<` token followed by an identifier and then a `=` token is parsed as an arrow function with a default type parameter instead of as a JSX element. This release updates esbuild's parser to match TypeScript's parser.
8+
59
* Fix an accidental infinite loop with `--define` substitution ([#2407](https://github.com/evanw/esbuild/issues/2407))
610

711
This is a fix for a regression that was introduced in esbuild version 0.14.44 where certain `--define` substitutions could result in esbuild crashing with a stack overflow. The problem was an incorrect fix for #2292. The fix merged the code paths for `--define` and `--jsx-factory` rewriting since the value substitution is now the same for both. However, doing this accidentally made `--define` substitution recursive since the JSX factory needs to be able to match against `--define` substitutions to integrate with the `--inject` feature. The fix is to only do one additional level of matching against define substitutions, and to only do this for JSX factories. Now these cases are able to build successfully without a stack overflow.

internal/js_parser/ts_parser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ func (p *parser) isTSArrowFnJSX() (isTSArrowFn bool) {
863863
// Look ahead to see if this should be an arrow function instead
864864
if p.lexer.Token == js_lexer.TIdentifier {
865865
p.lexer.Next()
866-
if p.lexer.Token == js_lexer.TComma {
866+
if p.lexer.Token == js_lexer.TComma || p.lexer.Token == js_lexer.TEquals {
867867
isTSArrowFn = true
868868
} else if p.lexer.Token == js_lexer.TExtends {
869869
p.lexer.Next()

internal/js_parser/ts_parser_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2295,6 +2295,7 @@ func TestTSJSX(t *testing.T) {
22952295
expectParseErrorTSX(t, "(<T>(y) => {}</T>)", invalid)
22962296
expectParseErrorTSX(t, "(<T extends>(y) => {}</T>)", invalid)
22972297
expectParseErrorTSX(t, "(<T extends={false}>(y) => {}</T>)", invalid)
2298+
expectPrintedTSX(t, "(<T = X>(y) => {})", "(y) => {\n};\n")
22982299
expectPrintedTSX(t, "(<T extends X>(y) => {})", "(y) => {\n};\n")
22992300
expectPrintedTSX(t, "(<T extends X = Y>(y) => {})", "(y) => {\n};\n")
23002301
expectPrintedTSX(t, "(<T,>() => {})", "() => {\n};\n")
@@ -2303,7 +2304,7 @@ func TestTSJSX(t *testing.T) {
23032304
expectParseErrorTSX(t, "(<T>() => {})", invalid+"<stdin>: ERROR: Unexpected end of file\n")
23042305
expectParseErrorTSX(t, "(<[]>(y))", "<stdin>: ERROR: Expected identifier but found \"[\"\n")
23052306
expectParseErrorTSX(t, "(<T[]>(y))", "<stdin>: ERROR: Expected \">\" but found \"[\"\n")
2306-
expectParseErrorTSX(t, "(<T = X>(y))", "<stdin>: ERROR: Expected \">\" but found \"=\"\n")
2307+
expectParseErrorTSX(t, "(<T = X>(y))", "<stdin>: ERROR: Expected \"=>\" but found \")\"\n")
23072308
expectParseErrorTSX(t, "(<T, X>(y))", "<stdin>: ERROR: Expected \"=>\" but found \")\"\n")
23082309
expectParseErrorTSX(t, "(<T, X>y => {})", "<stdin>: ERROR: Expected \"(\" but found \"y\"\n")
23092310
}

0 commit comments

Comments
 (0)