Skip to content

Commit

Permalink
fix(typescript): don't double-parse for 'await <T>() => {x}'
Browse files Browse the repository at this point in the history
visit_variable_use("x") occurs twice when parsing
'await <T>() => {x}'. This is bad. Fix it.
  • Loading branch information
strager committed Dec 30, 2023
1 parent 82f50c7 commit 158256e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ Semantic Versioning.
to make an optional type").
* `class T<T> {}` no longer falsely reports [E0034][] ("redeclaration of
variable").
* `await <T>() => {}` no longer does confusing things.

## 2.18.0 (2023-11-03)

Expand Down
7 changes: 3 additions & 4 deletions src/quick-lint-js/fe/parse-expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1287,16 +1287,15 @@ Expression* Parser::parse_await_expression(Parse_Visitor_Base& v,
Parser_Transaction transaction = this->begin_transaction();

bool parsed_ok = this->catch_fatal_parse_errors([&] {
// FIXME(#831): v should not be used here. We should use a
// buffering_visitor.
if (this->in_top_level_) {
// Try to parse the / as a regular expression literal or the < as a
// JSX element.
[[maybe_unused]] Expression* ast = this->parse_expression(v, prec);
[[maybe_unused]] Expression* ast =
this->parse_expression(Null_Visitor::instance, prec);
} else {
// Try to parse the / or < as a binary operator.
[[maybe_unused]] Expression* ast = this->parse_expression_remainder(
v,
Null_Visitor::instance,
this->make_expression<Expression::Variable>(
await_token.identifier_name(), await_token.type),
prec);
Expand Down
11 changes: 11 additions & 0 deletions test/test-parse-typescript-generic-arrow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,17 @@ TEST_F(Test_Parse_TypeScript_Generic_Arrow,
}
}
}

TEST_F(Test_Parse_TypeScript_Generic_Arrow,
generic_async_function_with_await_keyword) {
{
Spy_Visitor p = test_parse_and_visit_expression(
u8"await <T>() => { await(myPromise); }"_sv,
u8"^^^^^ Diag_Await_Followed_By_Arrow_Function.await_operator"_diag,
typescript_options);
EXPECT_THAT(p.variable_uses, ElementsAreArray({u8"myPromise"_sv}));
}
}
}
}

Expand Down

0 comments on commit 158256e

Please sign in to comment.