Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

fix(rome_js_parser): improve await handling in non-async context #3573

Merged
merged 6 commits into from
Nov 14, 2022
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
43 changes: 31 additions & 12 deletions crates/rome_js_parser/src/syntax/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,7 @@ pub(super) fn parse_unary_expr(p: &mut Parser, context: ExpressionContext) -> Pa
const UNARY_SINGLE: TokenSet =
token_set![T![delete], T![void], T![typeof], T![+], T![-], T![~], T![!]];

if (p.state.in_async()) && p.at(T![await]) {
if p.at(T![await]) {
// test await_expression
// async function test() {
// await inner();
Expand All @@ -1812,32 +1812,51 @@ pub(super) fn parse_unary_expr(p: &mut Parser, context: ExpressionContext) -> Pa
// async function test() {}
// await test();
let m = p.start();
let checkpoint = p.checkpoint();
let await_range = p.cur_range();
p.expect(T![await]);
let unary = parse_unary_expr(p, context);

parse_unary_expr(p, context)
.or_add_diagnostic(p, js_parse_error::expected_unary_expression);
let is_top_level_module_or_async_fn =
p.state.in_async() && (p.state.is_top_level() || p.state.in_function());

let mut expr = m.complete(p, JS_AWAIT_EXPRESSION);
if !is_top_level_module_or_async_fn {
// test reparse_await_as_identifier
// // SCRIPT
// function test() { a = await; }
// function test2() { return await; }
if unary.is_absent() {
p.rewind(checkpoint);
m.abandon(p);
return parse_identifier_expression(p);
}

if !p.state.is_top_level() && !p.state.in_function() {
// test_err await_in_parameter_initializer
// async function test(a = await b()) {}
// function test2(a = await b()) {}

// test_err await_in_static_initialization_block_member
// // SCRIPT
// class A {
// static {
// await;
// }
// }
// class A { static { await; } }
// class B { static { await 10; } }

// test_err await_in_non_async_function
// function test() { await 10; }

// test_err await_in_module
// let await = 10;
// console.log(await);
p.error(p.err_builder(
"`await` is only allowed within async functions and at the top levels of modules.",
expr.range(p),
await_range,
));
expr.change_to_unknown(p);

let expr = m.complete(p, JS_UNKNOWN_EXPRESSION);
return Present(expr);
}

unary.or_add_diagnostic(p, js_parse_error::expected_unary_expression);
let expr = m.complete(p, JS_AWAIT_EXPRESSION);
return Present(expr);
}

Expand Down
1 change: 1 addition & 0 deletions crates/rome_js_parser/src/syntax/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,7 @@ fn parse_arrow_function_with_single_parameter(p: &mut Parser) -> ParsedSyntax {
// test_err async_arrow_expr_await_parameter
// let a = async await => {}
// async() => { (a = await) => {} };
// async() => { (a = await 10) => {} };
p.with_state(EnterParameters(arrow_function_parameter_flags(p, flags)), parse_binding)
.expect("Expected function parameter to be present as guaranteed by is_arrow_function_with_simple_parameter");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
let a = async await => {}
async() => { (a = await) => {} };
async() => { (a = await 10) => {} };
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ JsModule {
eq_token: [email protected] "=" [] [Whitespace(" ")],
expression: JsUnknownExpression {
items: [
[email protected] "await" [] [],
JsUnknown {
items: [
[email protected] "await" [] [],
],
},
],
},
},
Expand All @@ -93,14 +97,74 @@ JsModule {
},
semicolon_token: [email protected] ";" [] [],
},
JsExpressionStatement {
expression: JsArrowFunctionExpression {
async_token: [email protected] "async" [Newline("\n")] [],
type_parameters: missing (optional),
parameters: JsParameters {
l_paren_token: [email protected] "(" [] [],
items: JsParameterList [],
r_paren_token: [email protected] ")" [] [Whitespace(" ")],
},
return_type_annotation: missing (optional),
fat_arrow_token: [email protected] "=>" [] [Whitespace(" ")],
body: JsFunctionBody {
l_curly_token: [email protected] "{" [] [Whitespace(" ")],
directives: JsDirectiveList [],
statements: JsStatementList [
JsExpressionStatement {
expression: JsArrowFunctionExpression {
async_token: missing (optional),
type_parameters: missing (optional),
parameters: JsParameters {
l_paren_token: [email protected] "(" [] [],
items: JsParameterList [
JsFormalParameter {
binding: JsIdentifierBinding {
name_token: [email protected] "a" [] [Whitespace(" ")],
},
question_mark_token: missing (optional),
type_annotation: missing (optional),
initializer: JsInitializerClause {
eq_token: [email protected] "=" [] [Whitespace(" ")],
expression: JsUnknownExpression {
items: [
[email protected] "await" [] [Whitespace(" ")],
JsNumberLiteralExpression {
value_token: [email protected] "10" [] [],
},
],
},
},
},
],
r_paren_token: [email protected] ")" [] [Whitespace(" ")],
},
return_type_annotation: missing (optional),
fat_arrow_token: [email protected] "=>" [] [Whitespace(" ")],
body: JsFunctionBody {
l_curly_token: [email protected] "{" [] [],
directives: JsDirectiveList [],
statements: JsStatementList [],
r_curly_token: [email protected] "}" [] [Whitespace(" ")],
},
},
semicolon_token: missing (optional),
},
],
r_curly_token: [email protected] "}" [] [],
},
},
semicolon_token: [email protected] ";" [] [],
},
],
eof_token: EOF@59..60 "" [Newline("\n")] [],
eof_token: EOF@96..97 "" [Newline("\n")] [],
}

0: JS_MODULE@0..60
0: JS_MODULE@0..97
0: (empty)
1: [email protected]
2: JS_MODULE_ITEM_LIST@0..59
2: JS_MODULE_ITEM_LIST@0..96
0: [email protected]
0: [email protected]
0: [email protected] "let" [] [Whitespace(" ")]
Expand Down Expand Up @@ -153,7 +217,8 @@ JsModule {
3: [email protected]
0: [email protected] "=" [] [Whitespace(" ")]
1: [email protected]
0: [email protected] "await" [] []
0: [email protected]
0: [email protected] "await" [] []
2: [email protected] ")" [] [Whitespace(" ")]
3: (empty)
4: [email protected] "=>" [] [Whitespace(" ")]
Expand All @@ -165,7 +230,50 @@ JsModule {
1: (empty)
3: [email protected] "}" [] []
1: [email protected] ";" [] []
3: [email protected] "" [Newline("\n")] []
2: [email protected]
0: [email protected]
0: [email protected] "async" [Newline("\n")] []
1: (empty)
2: [email protected]
0: [email protected] "(" [] []
1: [email protected]
2: [email protected] ")" [] [Whitespace(" ")]
3: (empty)
4: [email protected] "=>" [] [Whitespace(" ")]
5: [email protected]
0: [email protected] "{" [] [Whitespace(" ")]
1: [email protected]
2: [email protected]
0: [email protected]
0: [email protected]
0: (empty)
1: (empty)
2: [email protected]
0: [email protected] "(" [] []
1: [email protected]
0: [email protected]
0: [email protected]
0: [email protected] "a" [] [Whitespace(" ")]
1: (empty)
2: (empty)
3: [email protected]
0: [email protected] "=" [] [Whitespace(" ")]
1: [email protected]
0: [email protected] "await" [] [Whitespace(" ")]
1: [email protected]
0: [email protected] "10" [] []
2: [email protected] ")" [] [Whitespace(" ")]
3: (empty)
4: [email protected] "=>" [] [Whitespace(" ")]
5: [email protected]
0: [email protected] "{" [] []
1: [email protected]
2: [email protected]
3: [email protected] "}" [] [Whitespace(" ")]
1: (empty)
3: [email protected] "}" [] []
1: [email protected] ";" [] []
3: [email protected] "" [Newline("\n")] []
--
async_arrow_expr_await_parameter.js:1:15 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Expand All @@ -174,35 +282,31 @@ async_arrow_expr_await_parameter.js:1:15 parse ━━━━━━━━━━━
> 1 │ let a = async await => {}
│ ^^^^^
2 │ async() => { (a = await) => {} };
3 │
3 │ async() => { (a = await 10) => {} };

--
async_arrow_expr_await_parameter.js:2:24 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
async_arrow_expr_await_parameter.js:2:19 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× expected an unary expression but instead found ')'

1 │ let a = async await => {}
> 2 │ async() => { (a = await) => {} };
│ ^
3 │

i Expected an unary expression here
× Illegal use of `await` as an identifier in an async context

1 │ let a = async await => {}
> 2 │ async() => { (a = await) => {} };
│ ^
3 │
│ ^^^^^
3 │ async() => { (a = await 10) => {} };
4 │

--
async_arrow_expr_await_parameter.js:2:19 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
async_arrow_expr_await_parameter.js:3:19 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× `await` is only allowed within async functions and at the top levels of modules.

1 │ let a = async await => {}
> 2 │ async() => { (a = await) => {} };
2 │ async() => { (a = await) => {} };
> 3 │ async() => { (a = await 10) => {} };
│ ^^^^^
3
4

--
let a = async await => {}
async() => { (a = await) => {} };
async() => { (a = await 10) => {} };
2 changes: 2 additions & 0 deletions crates/rome_js_parser/test_data/inline/err/await_in_module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let await = 10;
console.log(await);
Loading