Skip to content

Commit f92491f

Browse files
committed
refactor(fe): deduplicate list of switch cases for declare statements
1 parent 246fd7a commit f92491f

File tree

2 files changed

+45
-52
lines changed

2 files changed

+45
-52
lines changed

src/quick-lint-js/fe/parse-statement.cpp

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2845,25 +2845,6 @@ void Parser::parse_and_visit_typescript_declare_namespace_or_module(
28452845
v.visit_enter_namespace_scope();
28462846
for (;;) {
28472847
switch (this->peek().type) {
2848-
// TODO(strager): Deduplicate list with
2849-
// parse_and_visit_possible_declare_statement.
2850-
case Token_Type::kw_abstract:
2851-
case Token_Type::kw_async:
2852-
case Token_Type::kw_class:
2853-
case Token_Type::kw_const:
2854-
case Token_Type::kw_enum:
2855-
case Token_Type::kw_function:
2856-
case Token_Type::kw_import:
2857-
case Token_Type::kw_interface:
2858-
case Token_Type::kw_let:
2859-
case Token_Type::kw_module:
2860-
case Token_Type::kw_namespace:
2861-
case Token_Type::kw_type:
2862-
case Token_Type::kw_var:
2863-
this->is_current_typescript_namespace_non_empty_ = true;
2864-
this->parse_and_visit_declare_statement(v, declare_context);
2865-
break;
2866-
28672848
case Token_Type::kw_export:
28682849
this->is_current_typescript_namespace_non_empty_ = true;
28692850
this->parse_and_visit_export(v, declare_context);
@@ -2894,18 +2875,22 @@ void Parser::parse_and_visit_typescript_declare_namespace_or_module(
28942875
break;
28952876
}
28962877

2897-
default: {
2898-
// require_declaration will cause parse_and_visit_statement to report
2899-
// Diag_Declare_Namespace_Cannot_Contain_Statement.
2900-
bool parsed_statement = this->parse_and_visit_statement(
2901-
v, Parse_Statement_Options{
2902-
.possibly_followed_by_another_statement = true,
2903-
.require_declaration = true,
2904-
.declare_keyword = declare_keyword_span,
2905-
});
2906-
QLJS_ASSERT(parsed_statement);
2878+
default:
2879+
if (this->is_declare_statement_start_token(this->peek().type)) {
2880+
this->is_current_typescript_namespace_non_empty_ = true;
2881+
this->parse_and_visit_declare_statement(v, declare_context);
2882+
} else {
2883+
// require_declaration will cause parse_and_visit_statement to report
2884+
// Diag_Declare_Namespace_Cannot_Contain_Statement.
2885+
bool parsed_statement = this->parse_and_visit_statement(
2886+
v, Parse_Statement_Options{
2887+
.possibly_followed_by_another_statement = true,
2888+
.require_declaration = true,
2889+
.declare_keyword = declare_keyword_span,
2890+
});
2891+
QLJS_ASSERT(parsed_statement);
2892+
}
29072893
break;
2908-
}
29092894

29102895
case Token_Type::regexp:
29112896
QLJS_UNREACHABLE();
@@ -5447,34 +5432,19 @@ Parser::parse_and_visit_possible_declare_statement(Parse_Visitor_Base &v) {
54475432
return Parse_Possible_Declare_Result::declare_is_expression_or_loop_label;
54485433
}
54495434

5450-
switch (this->peek().type) {
5451-
// declare class C { }
5452-
// declare import fs from 'fs'; // Invalid.
5453-
// declare import ns = otherns; // Invalid.
5454-
case Token_Type::kw_abstract:
5455-
case Token_Type::kw_async:
5456-
case Token_Type::kw_class:
5457-
case Token_Type::kw_const:
5458-
case Token_Type::kw_enum:
5459-
case Token_Type::kw_function:
5460-
case Token_Type::kw_import:
5461-
case Token_Type::kw_interface:
5462-
case Token_Type::kw_let:
5463-
case Token_Type::kw_module:
5464-
case Token_Type::kw_namespace:
5465-
case Token_Type::kw_type:
5466-
case Token_Type::kw_var:
5435+
if (this->is_declare_statement_start_token(this->peek().type)) {
5436+
// declare class C { }
5437+
// declare import fs from 'fs'; // Invalid.
5438+
// declare import ns = otherns; // Invalid.
54675439
this->lexer_.commit_transaction(std::move(transaction));
54685440
this->parse_and_visit_declare_statement(
54695441
v, TypeScript_Declare_Context{
54705442
.direct_declare_keyword = declare_keyword_span,
54715443
});
54725444
return Parse_Possible_Declare_Result::parsed;
5473-
5474-
// declare: // Label.
5475-
// declare();
5476-
case Token_Type::colon:
5477-
default:
5445+
} else {
5446+
// declare: // Label.
5447+
// declare();
54785448
this->lexer_.roll_back_transaction(std::move(transaction));
54795449
return Parse_Possible_Declare_Result::declare_is_expression_or_loop_label;
54805450
}
@@ -5706,6 +5676,27 @@ void Parser::parse_and_visit_declare_statement(
57065676
break;
57075677
}
57085678
}
5679+
5680+
bool Parser::is_declare_statement_start_token(Token_Type token_type) {
5681+
switch (token_type) {
5682+
case Token_Type::kw_abstract:
5683+
case Token_Type::kw_async:
5684+
case Token_Type::kw_class:
5685+
case Token_Type::kw_const:
5686+
case Token_Type::kw_enum:
5687+
case Token_Type::kw_function:
5688+
case Token_Type::kw_import:
5689+
case Token_Type::kw_interface:
5690+
case Token_Type::kw_let:
5691+
case Token_Type::kw_module:
5692+
case Token_Type::kw_namespace:
5693+
case Token_Type::kw_type:
5694+
case Token_Type::kw_var:
5695+
return true;
5696+
default:
5697+
return false;
5698+
}
5699+
}
57095700
}
57105701

57115702
// quick-lint-js finds bugs in JavaScript programs.

src/quick-lint-js/fe/parse.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,8 +1118,10 @@ class Parser {
11181118

11191119
// Precondition: declare_context.declare_namespace_declare_keyword.has_value()
11201120
// || declare_context.direct_declare_keyword.has_value()
1121+
// Precondition: is_declare_statement_start_token(this->peek().type)
11211122
void parse_and_visit_declare_statement(
11221123
Parse_Visitor_Base &v, const TypeScript_Declare_Context &declare_context);
1124+
bool is_declare_statement_start_token(Token_Type);
11231125
};
11241126

11251127
template <class Expected_Parentheses_Error, class Expected_Parenthesis_Error,

0 commit comments

Comments
 (0)