Skip to content

Commit

Permalink
fix(typescript): don't crash on statementless loop label in declare n…
Browse files Browse the repository at this point in the history
…amespace

Parser::parse_and_visit_statement returns false for a statementless
loop label, leading to an assertion failure. Remove the incorrect
assertion.
  • Loading branch information
strager committed Dec 30, 2023
1 parent 2947147 commit 449a12c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/quick-lint-js/fe/parse-statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3399,13 +3399,14 @@ void Parser::parse_and_visit_typescript_declare_block(
} else {
// require_declaration will cause parse_and_visit_statement to report
// Diag_Declare_Namespace_Cannot_Contain_Statement.
bool parsed_statement = this->parse_and_visit_statement(
v, Parse_Statement_Options{
.possibly_followed_by_another_statement = true,
.require_declaration = true,
.declare_keyword = declare_context.declare_keyword_span(),
});
QLJS_ASSERT(parsed_statement);
[[maybe_unused]] bool parsed_statement =
this->parse_and_visit_statement(
v,
Parse_Statement_Options{
.possibly_followed_by_another_statement = true,
.require_declaration = true,
.declare_keyword = declare_context.declare_keyword_span(),
});
}
break;

Expand Down
12 changes: 12 additions & 0 deletions test/test-parse-typescript-declare-namespace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,18 @@ TEST_F(Test_Parse_TypeScript_Declare_Namespace,
non_empty_namespace_decl(u8"ns"_sv)));
}
}

TEST_F(Test_Parse_TypeScript_Declare_Namespace,
declare_namespace_cannot_contain_loop_label) {
test_parse_and_visit_module(
u8"declare namespace ns { label: export class C {} }"_sv,
u8" ^^^^^ Diag_Declare_Namespace_Cannot_Contain_Statement.first_statement_token"_diag,
typescript_options);
test_parse_and_visit_module(
u8"declare namespace ns { label: }"_sv,
u8" ^^^^^ Diag_Declare_Namespace_Cannot_Contain_Statement.first_statement_token"_diag,
typescript_options);
}
}
}

Expand Down

0 comments on commit 449a12c

Please sign in to comment.