Skip to content

Commit

Permalink
fix(fe): don't treat '<A & B>expr' as a JSX element
Browse files Browse the repository at this point in the history
  • Loading branch information
strager committed Dec 20, 2023
1 parent 970c4d5 commit 0f20583
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 1 addition & 2 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ Semantic Versioning.
* `interface I { get: any; }` (field named `get` with a type annotation) no
longer reports [E0054][] ("unexpected token"). (Implemented by [Rui
Serra][].)
* In type assertions, certain types (array types, generic arrow types, and
type references with a namespace) such as in `<string[]>expr` and
* In type assertions, certain types such as in `<string[]>expr` and
`< <T>() => RT>expr`, are no longer incorrectly interpreted as JSX.

## 2.18.0 (2023-11-03)
Expand Down
2 changes: 2 additions & 0 deletions src/quick-lint-js/fe/parse-expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3600,6 +3600,7 @@ Expression* Parser::parse_jsx_or_typescript_generic_expression(

// <T | U>expr // Type assertion.
// <T[]>expr // Type assertion.
case Token_Type::ampersand:
case Token_Type::left_square:
case Token_Type::pipe:
this->lexer_.roll_back_transaction(std::move(transaction));
Expand Down Expand Up @@ -4110,6 +4111,7 @@ Expression* Parser::parse_typescript_angle_type_assertion_expression(
// <T | U>expr
// <T[]>expr
// <ns.T>expr
case Token_Type::ampersand:
case Token_Type::dot:
case Token_Type::left_square:
case Token_Type::pipe:
Expand Down
12 changes: 12 additions & 0 deletions test/test-parse-typescript-angle-type-assertion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ TEST_F(Test_Parse_TypeScript_Angle_Type_Assertion, angle_type_assertion) {
ElementsAreArray({u8"Type1", u8"Type2", u8"expr"}));
}

{
Spy_Visitor p = test_parse_and_visit_statement(
u8"<Type1 & Type2>(expr);"_sv, no_diags, typescript_options);
EXPECT_THAT(p.visits, ElementsAreArray({
"visit_variable_type_use", // Type1
"visit_variable_type_use", // Type2
"visit_variable_use", // expr
}));
EXPECT_THAT(p.variable_uses,
ElementsAreArray({u8"Type1", u8"Type2", u8"expr"}));
}

for (String8_View code : {
u8"<Type>(expr);"_sv,
u8"<(Type)>(expr);"_sv,
Expand Down

0 comments on commit 0f20583

Please sign in to comment.