Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error for missing arrow operator #476

Merged
merged 3 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,19 @@ expression* parser::parse_primary_expression(precedence prec) {
if (this->peek().type == token_type::right_paren) {
source_code_span right_paren_span = this->peek().span();
this->skip();
if (this->peek().type == token_type::equal_greater) {
this->skip();
bool is_arrow_function = this->peek().type == token_type::equal_greater;
bool is_arrow_function_without_arrow =
this->peek().type == token_type::left_curly;
if (is_arrow_function || is_arrow_function_without_arrow) {
// Arrow function: () => expression-or-block
// Arrow function: () { } // Invalid.
if (is_arrow_function) {
this->skip();
} else {
this->error_reporter_->report(
error_missing_arrow_operator_in_arrow_function{
.where = this->peek().span()});
}
expression* ast = this->parse_arrow_function_body(
function_attributes::normal, left_paren_span.begin(),
/*allow_in_operator=*/prec.in_operator);
Expand Down
6 changes: 6 additions & 0 deletions src/quick-lint-js/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,12 @@
expected_right_square) \
NOTE(QLJS_TRANSLATABLE("array started here"), left_square)) \
\
QLJS_ERROR_TYPE( \
error_missing_arrow_operator_in_arrow_function, "E176", \
{ source_code_span where; }, \
ERROR(QLJS_TRANSLATABLE("missing arrow operator for arrow function"), \
where)) \
\
QLJS_ERROR_TYPE( \
error_missing_arrow_function_parameter_list, "E105", \
{ source_code_span arrow; }, \
Expand Down
32 changes: 32 additions & 0 deletions test/test-parse-function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,38 @@ TEST(test_parse, arrow_function_with_invalid_parameters) {
}
}

TEST(test_parse, arrow_function_expression_without_arrow_operator) {
{
padded_string code(u8"(() {});"_sv);
spy_visitor v;
parser p(&code, &v);
p.parse_and_visit_module(v);
EXPECT_THAT(v.visits, ElementsAre("visit_enter_function_scope", //
"visit_enter_function_scope_body", //
"visit_exit_function_scope", //
"visit_end_of_module"));
EXPECT_THAT(v.errors,
ElementsAre(ERROR_TYPE_FIELD(
error_missing_arrow_operator_in_arrow_function, where,
offsets_matcher(&code, strlen(u8"(() "), u8"{"))));
}

{
padded_string code(u8"(()\n{});"_sv);
spy_visitor v;
parser p(&code, &v);
p.parse_and_visit_module(v);
EXPECT_THAT(v.visits, ElementsAre("visit_enter_function_scope", //
"visit_enter_function_scope_body", //
"visit_exit_function_scope", //
"visit_end_of_module"));
EXPECT_THAT(v.errors,
ElementsAre(ERROR_TYPE_FIELD(
error_missing_arrow_operator_in_arrow_function, where,
offsets_matcher(&code, strlen(u8"(()\n"), u8"{"))));
}
}

singalhimanshu marked this conversation as resolved.
Show resolved Hide resolved
TEST(test_parse, generator_function_with_misplaced_star) {
{
padded_string code(u8"function f*(x) { yield x; }"_sv);
Expand Down