Skip to content

Commit 1393e47

Browse files
Improve error for missing arrow operator
The following code: let f = () {} Should report error_missing_arrow_operator_in_arrow_function Fix: #461
1 parent 85c4b7c commit 1393e47

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

src/parse.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,19 @@ expression* parser::parse_primary_expression(precedence prec) {
280280
if (this->peek().type == token_type::right_paren) {
281281
source_code_span right_paren_span = this->peek().span();
282282
this->skip();
283-
if (this->peek().type == token_type::equal_greater) {
284-
this->skip();
283+
bool is_arrow_function = this->peek().type == token_type::equal_greater;
284+
bool is_arrow_function_without_arrow =
285+
this->peek().type == token_type::left_curly;
286+
if (is_arrow_function || is_arrow_function_without_arrow) {
285287
// Arrow function: () => expression-or-block
288+
// Arrow function: () { } // Invalid.
289+
if (is_arrow_function) {
290+
this->skip();
291+
} else {
292+
this->error_reporter_->report(
293+
error_missing_arrow_operator_in_arrow_function{
294+
.where = this->peek().span()});
295+
}
286296
expression* ast = this->parse_arrow_function_body(
287297
function_attributes::normal, left_paren_span.begin(),
288298
/*allow_in_operator=*/prec.in_operator);

src/quick-lint-js/error.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,12 @@
666666
expected_right_square) \
667667
NOTE(QLJS_TRANSLATABLE("array started here"), left_square)) \
668668
\
669+
QLJS_ERROR_TYPE( \
670+
error_missing_arrow_operator_in_arrow_function, "E176", \
671+
{ source_code_span where; }, \
672+
ERROR(QLJS_TRANSLATABLE("missing arrow operator for arrow function"), \
673+
where)) \
674+
\
669675
QLJS_ERROR_TYPE( \
670676
error_missing_arrow_function_parameter_list, "E105", \
671677
{ source_code_span arrow; }, \

test/test-parse-function.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,38 @@ TEST(test_parse, arrow_function_with_invalid_parameters) {
975975
}
976976
}
977977

978+
TEST(test_parse, arrow_function_expression_without_arrow_operator) {
979+
{
980+
padded_string code(u8"(() {});"_sv);
981+
spy_visitor v;
982+
parser p(&code, &v);
983+
p.parse_and_visit_module(v);
984+
EXPECT_THAT(v.visits, ElementsAre("visit_enter_function_scope", //
985+
"visit_enter_function_scope_body", //
986+
"visit_exit_function_scope", //
987+
"visit_end_of_module"));
988+
EXPECT_THAT(v.errors,
989+
ElementsAre(ERROR_TYPE_FIELD(
990+
error_missing_arrow_operator_in_arrow_function, where,
991+
offsets_matcher(&code, strlen(u8"(() "), u8"{"))));
992+
}
993+
994+
{
995+
padded_string code(u8"(()\n{});"_sv);
996+
spy_visitor v;
997+
parser p(&code, &v);
998+
p.parse_and_visit_module(v);
999+
EXPECT_THAT(v.visits, ElementsAre("visit_enter_function_scope", //
1000+
"visit_enter_function_scope_body", //
1001+
"visit_exit_function_scope", //
1002+
"visit_end_of_module"));
1003+
EXPECT_THAT(v.errors,
1004+
ElementsAre(ERROR_TYPE_FIELD(
1005+
error_missing_arrow_operator_in_arrow_function, where,
1006+
offsets_matcher(&code, strlen(u8"(()\n"), u8"{"))));
1007+
}
1008+
}
1009+
9781010
TEST(test_parse, generator_function_with_misplaced_star) {
9791011
{
9801012
padded_string code(u8"function f*(x) { yield x; }"_sv);

0 commit comments

Comments
 (0)