-
-
Notifications
You must be signed in to change notification settings - Fork 192
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
Suggest async
when await
is followed by arrow function.
#314
Changes from all commits
ba39488
0dd962d
5e508a2
a2148b2
3481de1
798b911
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -715,6 +715,30 @@ expression* parser::parse_await_expression(token await_token, precedence prec) { | |
} | ||
}(); | ||
|
||
if (this->peek().type == token_type::left_paren) { | ||
source_code_span operator_span = await_token.span(); | ||
buffering_error_reporter temp_error_reporter; | ||
error_reporter* old_error_reporter = | ||
std::exchange(this->error_reporter_, &temp_error_reporter); | ||
lexer_transaction transaction = this->lexer_.begin_transaction(); | ||
|
||
// Try to parse leading ( as arrow function. | ||
expression* ast = this->parse_expression(prec); | ||
|
||
this->lexer_.roll_back_transaction(std::move(transaction)); | ||
this->error_reporter_ = old_error_reporter; | ||
|
||
bool is_arrow_kind = | ||
(ast->kind() == expression_kind::arrow_function_with_statements) || | ||
(ast->kind() == expression_kind::arrow_function_with_expression); | ||
|
||
if (is_arrow_kind) { | ||
this->error_reporter_->report(error_await_creating_arrow_function{ | ||
.await_operator = operator_span, | ||
}); | ||
} | ||
} | ||
|
||
if (is_identifier) { | ||
return this->make_expression<expression::variable>( | ||
await_token.identifier_name(), await_token.type); | ||
|
@@ -727,11 +751,13 @@ expression* parser::parse_await_expression(token await_token, precedence prec) { | |
} | ||
|
||
expression* child = this->parse_expression(prec); | ||
|
||
if (child->kind() == expression_kind::_invalid) { | ||
this->error_reporter_->report(error_missing_operand_for_operator{ | ||
.where = operator_span, | ||
}); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return this->make_expression<expression::await>(child, operator_span); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -869,6 +869,64 @@ TEST_F(test_parse_expression, await_unary_operator_inside_async_functions) { | |
EXPECT_EQ(summarize(ast), "await(var x)"); | ||
EXPECT_THAT(p.errors(), IsEmpty()); | ||
} | ||
|
||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I think these new tests belong in a different |
||
test_parser p(u8"await () => {}"_sv); | ||
auto guard = p.parser().enter_function(function_attributes::async); | ||
expression* ast = p.parse_expression(); | ||
EXPECT_EQ(ast->kind(), expression_kind::await); | ||
EXPECT_EQ(ast->child_0()->kind(), | ||
expression_kind::arrow_function_with_statements); | ||
EXPECT_THAT(p.errors(), | ||
ElementsAre(ERROR_TYPE_FIELD( | ||
error_await_creating_arrow_function, await_operator, | ||
offsets_matcher(p.code(), 0, u8"await")))); | ||
} | ||
|
||
{ | ||
test_parser p(u8"await () => "_sv); | ||
auto guard = p.parser().enter_function(function_attributes::async); | ||
expression* ast = p.parse_expression(); | ||
EXPECT_EQ(ast->kind(), expression_kind::await); | ||
EXPECT_EQ(ast->child_0()->kind(), | ||
expression_kind::arrow_function_with_expression); | ||
EXPECT_THAT(p.errors(), | ||
ElementsAre(ERROR_TYPE_FIELD( | ||
error_await_creating_arrow_function, await_operator, | ||
offsets_matcher(p.code(), 0, u8"await")))); | ||
} | ||
|
||
{ | ||
test_parser p(u8"await () => {}"_sv); | ||
auto guard = p.parser().enter_function(function_attributes::normal); | ||
expression* ast = p.parse_expression(); | ||
EXPECT_EQ(ast->kind(), expression_kind::binary_operator); | ||
EXPECT_THAT( | ||
p.errors(), | ||
ElementsAre( | ||
ERROR_TYPE_FIELD(error_await_creating_arrow_function, | ||
await_operator, | ||
offsets_matcher(p.code(), 0, u8"await")), | ||
ERROR_TYPE_FIELD( | ||
error_missing_operator_between_expression_and_arrow_function, | ||
where, offsets_matcher(p.code(), 0, u8"await (")))); | ||
Comment on lines
+903
to
+912
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
{ | ||
test_parser p(u8"await () => "_sv); | ||
auto guard = p.parser().enter_function(function_attributes::normal); | ||
expression* ast = p.parse_expression(); | ||
EXPECT_EQ(ast->kind(), expression_kind::binary_operator); | ||
EXPECT_THAT( | ||
p.errors(), | ||
ElementsAre( | ||
ERROR_TYPE_FIELD(error_await_creating_arrow_function, | ||
await_operator, | ||
offsets_matcher(p.code(), 0, u8"await")), | ||
ERROR_TYPE_FIELD( | ||
error_missing_operator_between_expression_and_arrow_function, | ||
where, offsets_matcher(p.code(), 0, u8"await (")))); | ||
} | ||
} | ||
|
||
TEST_F(test_parse_expression, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code fails to catch arrow functions which don't have parentheses around the parameter list. For example:
See my comment on line 760 for a possible solution.