Skip to content

Commit

Permalink
refactor(fe): simplify overloaded function definitions
Browse files Browse the repository at this point in the history
When we detect name mismatches in function overload sets,
declare the overloads in source code order. This removes a complicated
exception which deserved a comment.
  • Loading branch information
strager committed Sep 19, 2023
1 parent 07ae32c commit 01fa063
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
12 changes: 2 additions & 10 deletions src/quick-lint-js/fe/parse-statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1932,16 +1932,7 @@ void Parser::parse_and_visit_function_declaration(
Identifier &overload_name = overload_names[i];
if (overload_name.normalized_name() !=
real_function_name.normalized_name()) {
// If this is the first overload:
// We will declare the first overload's function name below. If it
// turns out the first overload had the wrong name, then we don't
// want to declare it twice. Instead, declare the real function's
// name here.
//
// If this is the second, third, or other overload, declare it.
const Identifier &variable_to_declare =
i == 0 ? real_function_name : overload_name;
v.visit_variable_declaration(variable_to_declare,
v.visit_variable_declaration(overload_name,
Variable_Kind::_function,
Variable_Declaration_Flags::none);

Expand All @@ -1952,6 +1943,7 @@ void Parser::parse_and_visit_function_declaration(
});
}
}
function_name = overload_names[overload_names.size() - 1];
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/test-parse-typescript-function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ TEST_F(Test_Parse_TypeScript_Function,
}));
EXPECT_THAT(
p.variable_declarations,
ElementsAreArray({function_decl(u8"g"_sv), function_decl(u8"f"_sv)}));
ElementsAreArray({function_decl(u8"f"_sv), function_decl(u8"g"_sv)}));
assert_diagnostics(
p.code, p.errors,
{
Expand Down Expand Up @@ -1179,7 +1179,7 @@ TEST_F(Test_Parse_TypeScript_Function,
"visit_variable_declaration", // h
"visit_end_of_module",
}));
EXPECT_THAT(p.variable_declarations, UnorderedElementsAreArray({
EXPECT_THAT(p.variable_declarations, ElementsAreArray({
function_decl(u8"f"_sv),
function_decl(u8"g"_sv),
function_decl(u8"h"_sv),
Expand Down Expand Up @@ -1221,7 +1221,7 @@ TEST_F(Test_Parse_TypeScript_Function,
p.parse_and_visit_module();
EXPECT_THAT(
p.variable_declarations,
ElementsAreArray({function_decl(u8"g"_sv), function_decl(u8"f"_sv)}));
ElementsAreArray({function_decl(u8"f"_sv), function_decl(u8"g"_sv)}));
assert_diagnostics(
p.code, p.errors,
{
Expand Down

0 comments on commit 01fa063

Please sign in to comment.