Skip to content

Commit

Permalink
fix(typescript): don't error on type: (a?, ...b) => ReturnType
Browse files Browse the repository at this point in the history
  • Loading branch information
strager committed Oct 14, 2023
1 parent f022b8d commit 2cd0a23
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Semantic Versioning.
[E0034][] ("redeclaration of variable").
* A namespace with the same name as an interface or type alias no longer
falsely reports [E0034][] ("redeclaration of variable").
* `(a?, ...b) => ReturnType` in a TypeScript type no longer falsely reports
[E0379][] ("optional parameter cannot be followed by a required parameter").
* Nested `module` declarations no longer falsely report [E0361][]. E0361's
message has been changed:
* Before: "module with string name is only allowed at the top level"
Expand Down
3 changes: 2 additions & 1 deletion src/quick-lint-js/fe/parse-statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2494,7 +2494,8 @@ void Parser::parse_and_visit_function_parameters(
parameter->child_0()->kind() == Expression_Kind::Optional)) {
previous_optional_span = parameter->span();
} else {
if (previous_optional_span.has_value()) {
if (previous_optional_span.has_value() &&
parameter->kind() != Expression_Kind::Spread) {
this->diag_reporter_->report(
Diag_Optional_Parameter_Cannot_Be_Followed_By_Required_Parameter{
.optional_parameter = *previous_optional_span,
Expand Down
6 changes: 6 additions & 0 deletions test/test-parse-typescript-function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,12 @@ TEST_F(Test_Parse_TypeScript_Function, optional_parameter_in_function_type) {
}
}

TEST_F(Test_Parse_TypeScript_Function,
optional_parameter_followed_by_rest_parameter_is_allowed) {
test_parse_and_visit_typescript_type_expression(
u8"(param1?, ...rest) => ReturnType"_sv, no_diags, typescript_options);
}

TEST_F(Test_Parse_TypeScript_Function,
optional_parameter_followed_by_required) {
test_parse_and_visit_typescript_type_expression(
Expand Down

0 comments on commit 2cd0a23

Please sign in to comment.