Skip to content

Commit

Permalink
refactor(fe): inline parse_..._type_expression_or_type_predicate helpers
Browse files Browse the repository at this point in the history
parse_and_visit_typescript_type_expression_or_type_predicate and
parse_and_visit_colon_typescript_type_expression_or_type_predicate
just translate arguments then forward to
parse_and_visit_typescript_type_expression. This is no simpler than
just calling parse_and_visit_typescript_type_expression, so make
callers call the parse function directly.
  • Loading branch information
strager committed Nov 7, 2023
1 parent 15b65ab commit aaed137
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 42 deletions.
42 changes: 27 additions & 15 deletions src/quick-lint-js/fe/parse-expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,9 +889,11 @@ Expression* Parser::parse_async_expression_only(
Buffering_Visitor return_type_visits(&this->type_expression_memory_);
if (this->peek().type == Token_Type::colon && this->options_.typescript) {
// async (params): ReturnType => {} // TypeScript only.
this->parse_and_visit_typescript_colon_type_expression_or_type_predicate(
return_type_visits,
/*allow_parenthesized_type=*/false);
this->parse_and_visit_typescript_colon_type_expression(
return_type_visits, TypeScript_Type_Parse_Options{
.allow_parenthesized_type = false,
.allow_type_predicate = true,
});
}

bool is_arrow_function = this->peek().type == Token_Type::equal_greater;
Expand Down Expand Up @@ -1028,9 +1030,11 @@ Expression* Parser::parse_async_expression_only(
// code path).
Buffering_Visitor return_type_visits(&this->type_expression_memory_);
if (this->peek().type == Token_Type::colon) {
this->parse_and_visit_typescript_colon_type_expression_or_type_predicate(
return_type_visits,
/*allow_parenthesized_type=*/false);
this->parse_and_visit_typescript_colon_type_expression(
return_type_visits, TypeScript_Type_Parse_Options{
.allow_parenthesized_type = false,
.allow_type_predicate = true,
});
}
QLJS_PARSER_UNIMPLEMENTED_IF_NOT_TOKEN(Token_Type::equal_greater);

Expand Down Expand Up @@ -2168,9 +2172,13 @@ Expression* Parser::parse_expression_remainder(Parse_Visitor_Base& v,
bool is_possibly_arrow_function_return_type_annotation =
child->kind() == Expression_Kind::Paren ||
child->kind() == Expression_Kind::Paren_Empty;
this->parse_and_visit_typescript_colon_type_expression_or_type_predicate(
type_visitor, /*allow_parenthesized_type=*/
!is_possibly_arrow_function_return_type_annotation);
this->parse_and_visit_typescript_colon_type_expression(
type_visitor,
TypeScript_Type_Parse_Options{
.allow_parenthesized_type =
!is_possibly_arrow_function_return_type_annotation,
.allow_type_predicate = true,
});
const Char8* type_end = this->lexer_.end_of_previous_token();
binary_builder.replace_last(
this->make_expression<Expression::Type_Annotated>(
Expand Down Expand Up @@ -3870,9 +3878,11 @@ Expression* Parser::parse_typescript_generic_arrow_expression(

Buffering_Visitor return_type_visits(&this->type_expression_memory_);
if (this->peek().type == Token_Type::colon) {
this->parse_and_visit_typescript_colon_type_expression_or_type_predicate(
return_type_visits,
/*allow_parenthesized_type=*/false);
this->parse_and_visit_typescript_colon_type_expression(
return_type_visits, TypeScript_Type_Parse_Options{
.allow_parenthesized_type = false,
.allow_type_predicate = true,
});
}

QLJS_PARSER_UNIMPLEMENTED_IF_NOT_TOKEN(Token_Type::equal_greater);
Expand Down Expand Up @@ -3941,9 +3951,11 @@ Expression* Parser::parse_typescript_angle_type_assertion_expression(
ast->kind() == Expression_Kind::Paren_Empty) {
Buffering_Visitor return_type_visits(&this->type_expression_memory_);
if (this->peek().type == Token_Type::colon) {
this->parse_and_visit_typescript_colon_type_expression_or_type_predicate(
return_type_visits,
/*allow_parenthesized_type=*/false);
this->parse_and_visit_typescript_colon_type_expression(
return_type_visits, TypeScript_Type_Parse_Options{
.allow_parenthesized_type = false,
.allow_type_predicate = true,
});
}
if (this->peek().type == Token_Type::equal_greater) {
// <T>(param) => body
Expand Down
7 changes: 5 additions & 2 deletions src/quick-lint-js/fe/parse-statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2274,8 +2274,11 @@ Parser::parse_and_visit_function_parameter_list(
this->skip();

if (this->peek().type == Token_Type::colon) {
this->parse_and_visit_typescript_colon_type_expression_or_type_predicate(
v, /*allow_parenthesized_type=*/true);
this->parse_and_visit_typescript_colon_type_expression(
v, TypeScript_Type_Parse_Options{
.allow_parenthesized_type = true,
.allow_type_predicate = true,
});
}

if (this->peek().type == Token_Type::equal_greater) {
Expand Down
32 changes: 12 additions & 20 deletions src/quick-lint-js/fe/parse-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ void Parser::parse_typescript_colon_for_type() {

void Parser::parse_and_visit_typescript_colon_type_expression(
Parse_Visitor_Base &v) {
this->parse_and_visit_typescript_colon_type_expression(
v, TypeScript_Type_Parse_Options());
}

void Parser::parse_and_visit_typescript_colon_type_expression(
Parse_Visitor_Base &v, const TypeScript_Type_Parse_Options &parse_options) {
this->parse_typescript_colon_for_type();
this->parse_and_visit_typescript_type_expression(v);
this->parse_and_visit_typescript_type_expression(v, parse_options);
}

void Parser::parse_and_visit_typescript_type_expression(Parse_Visitor_Base &v) {
Expand Down Expand Up @@ -694,23 +700,6 @@ void Parser::parse_and_visit_typescript_type_expression(
}
}

void Parser::parse_and_visit_typescript_colon_type_expression_or_type_predicate(
Parse_Visitor_Base &v, bool allow_parenthesized_type) {
this->parse_typescript_colon_for_type();
this->parse_and_visit_typescript_type_expression_or_type_predicate(
v, /*allow_parenthesized_type=*/
allow_parenthesized_type);
}

void Parser::parse_and_visit_typescript_type_expression_or_type_predicate(
Parse_Visitor_Base &v, bool allow_parenthesized_type) {
this->parse_and_visit_typescript_type_expression(
v, TypeScript_Type_Parse_Options{
.allow_parenthesized_type = allow_parenthesized_type,
.allow_type_predicate = true,
});
}

void Parser::parse_and_visit_typescript_arrow_type_expression(
Parse_Visitor_Base &v) {
v.visit_enter_function_scope();
Expand Down Expand Up @@ -741,8 +730,11 @@ void Parser::
this->skip();
QLJS_PARSER_UNIMPLEMENTED_IF_NOT_TOKEN(Token_Type::equal_greater);
this->skip();
this->parse_and_visit_typescript_type_expression_or_type_predicate(
v, /*allow_parenthesized_type=*/false);
this->parse_and_visit_typescript_type_expression(
v, TypeScript_Type_Parse_Options{
.allow_parenthesized_type = false,
.allow_type_predicate = true,
});
}

Parser::TypeScript_Type_Arrow_Or_Paren
Expand Down
7 changes: 2 additions & 5 deletions src/quick-lint-js/fe/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,12 @@ class Parser {
};

void parse_and_visit_typescript_colon_type_expression(Parse_Visitor_Base &v);
void parse_and_visit_typescript_colon_type_expression(
Parse_Visitor_Base &v, const TypeScript_Type_Parse_Options &);
void parse_and_visit_typescript_type_expression(Parse_Visitor_Base &v);
void parse_and_visit_typescript_type_expression(
Parse_Visitor_Base &v, const TypeScript_Type_Parse_Options &);

void parse_and_visit_typescript_colon_type_expression_or_type_predicate(
Parse_Visitor_Base &v, bool allow_parenthesized_type);
void parse_and_visit_typescript_type_expression_or_type_predicate(
Parse_Visitor_Base &v, bool allow_parenthesized_type);

enum class TypeScript_Type_Arrow_Or_Paren {
arrow,
paren,
Expand Down

0 comments on commit aaed137

Please sign in to comment.