@@ -253,6 +253,7 @@ type Lexer struct {
253
253
JSXFactoryPragmaComment logger.Span
254
254
JSXFragmentPragmaComment logger.Span
255
255
SourceMappingURL logger.Span
256
+ BadArrowInTSXSuggestion string
256
257
257
258
// Escape sequences in string literals are decoded lazily because they are
258
259
// not interpreted inside tagged templates, and tagged templates can contain
@@ -270,6 +271,8 @@ type Lexer struct {
270
271
start int
271
272
end int
272
273
ApproximateNewlineCount int
274
+ CouldBeBadArrowInTSX int
275
+ BadArrowInTSXRange logger.Range
273
276
LegacyOctalLoc logger.Loc
274
277
AwaitKeywordLoc logger.Loc
275
278
FnOrArrowStartLoc logger.Loc
@@ -945,23 +948,36 @@ func (lexer *Lexer) NextJSXElementChild() {
945
948
} else {
946
949
replacement = "{'>'}"
947
950
}
948
- msg := logger.Msg {Kind : logger .Error , Data : lexer .tracker .MsgData (logger.Range {Loc : logger.Loc {Start : int32 (lexer .end )}, Len : 1 },
949
- fmt .Sprintf ("The character \" %c\" is not valid inside a JSX element" , lexer .codePoint )),
950
- Notes : []logger.MsgData {{Text : fmt .Sprintf ("Did you mean to escape it as %q instead?" , replacement )}}}
951
- msg .Data .Location .Suggestion = replacement
952
- if ! lexer .ts .Parse {
953
- // TypeScript treats this as an error but Babel doesn't treat this
954
- // as an error yet, so allow this in JS for now. Babel version 8
955
- // was supposed to be released in 2021 but was never released. If
956
- // it's released in the future, this can be changed to an error too.
957
- //
958
- // More context:
959
- // * TypeScript change: https://github.com/microsoft/TypeScript/issues/36341
960
- // * Babel 8 change: https://github.com/babel/babel/issues/11042
961
- // * Babel 8 release: https://github.com/babel/babel/issues/10746
962
- //
963
- msg .Kind = logger .Warning
951
+ msg := logger.Msg {
952
+ Kind : logger .Error ,
953
+ Data : lexer .tracker .MsgData (logger.Range {Loc : logger.Loc {Start : int32 (lexer .end )}, Len : 1 },
954
+ fmt .Sprintf ("The character \" %c\" is not valid inside a JSX element" , lexer .codePoint )),
964
955
}
956
+
957
+ // Attempt to provide a better error message if this looks like an arrow function
958
+ if lexer .CouldBeBadArrowInTSX > 0 && lexer .codePoint == '>' && lexer .source .Contents [lexer .end - 1 ] == '=' {
959
+ msg .Notes = []logger.MsgData {lexer .tracker .MsgData (lexer .BadArrowInTSXRange ,
960
+ "TypeScript's TSX syntax interprets arrow functions with a single generic type parameter as an opening JSX element. " +
961
+ "If you want it to be interpreted as an arrow function instead, you need to add a trailing comma after the type parameter to disambiguate:" )}
962
+ msg .Notes [0 ].Location .Suggestion = lexer .BadArrowInTSXSuggestion
963
+ } else {
964
+ msg .Notes = []logger.MsgData {{Text : fmt .Sprintf ("Did you mean to escape it as %q instead?" , replacement )}}
965
+ msg .Data .Location .Suggestion = replacement
966
+ if ! lexer .ts .Parse {
967
+ // TypeScript treats this as an error but Babel doesn't treat this
968
+ // as an error yet, so allow this in JS for now. Babel version 8
969
+ // was supposed to be released in 2021 but was never released. If
970
+ // it's released in the future, this can be changed to an error too.
971
+ //
972
+ // More context:
973
+ // * TypeScript change: https://github.com/microsoft/TypeScript/issues/36341
974
+ // * Babel 8 change: https://github.com/babel/babel/issues/11042
975
+ // * Babel 8 release: https://github.com/babel/babel/issues/10746
976
+ //
977
+ msg .Kind = logger .Warning
978
+ }
979
+ }
980
+
965
981
lexer .log .AddMsg (msg )
966
982
lexer .step ()
967
983
0 commit comments