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

fix(ecma/parser): fix parsing await as ident #6316

Merged
merged 6 commits into from Nov 3, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions crates/swc_ecma_parser/src/parser/expr/ops.rs
Expand Up @@ -331,10 +331,6 @@ impl<I: Tokens> Parser<I> {
}

if is!(self, "await") {
let ctx = self.ctx();
if ctx.in_function && !ctx.in_async {
self.emit_err(self.input.cur_span(), SyntaxError::AwaitInFunction);
}
return self.parse_await_expr();
}

Expand Down Expand Up @@ -379,14 +375,18 @@ impl<I: Tokens> Parser<I> {

let span = span!(self, start);

if is_one_of!(self, ')', ']') && !self.ctx().in_async {
if ctx.in_async || ctx.module {
if is_one_of!(self, ')', ']', ';', ',') && !ctx.in_async {
if ctx.module {
self.emit_err(span, SyntaxError::InvalidIdentInAsync);
}

return Ok(Box::new(Expr::Ident(Ident::new(js_word!("await"), span))));
}

if ctx.in_function && !ctx.in_async {
self.emit_err(self.input.cur_span(), SyntaxError::AwaitInFunction);
}

if ctx.in_parameters && !ctx.in_function {
self.emit_err(span, SyntaxError::AwaitParamInAsync);
}
Expand Down
42 changes: 42 additions & 0 deletions crates/swc_ecma_parser/src/parser/stmt.rs
Expand Up @@ -2485,4 +2485,46 @@ const foo;"#;

test_parser(src, Default::default(), |p| p.parse_script());
}

#[test]
fn issue_6301_await_expr_stmt() {
let src = "try { await; } catch { console.log('caught'); }";

test_parser(src, Default::default(), |p| p.parse_script());
}

#[test]
fn issue_6301_await_expr_stmt_1() {
let src = "try { await, await; } catch { console.log('caught'); }";

test_parser(src, Default::default(), |p| p.parse_script());
}

#[test]
fn issue_6301_await_expr_stmt_2() {
let src = "function test() { await; }";

test_parser(src, Default::default(), |p| p.parse_script());
}

#[test]
fn issue_6301_await_expr_stmt_3() {
let src = "function test() { await, await; }";

test_parser(src, Default::default(), |p| p.parse_script());
}

#[test]
fn issue_6301_await_expr_stmt_4() {
let src = "function test() { [await]; }";

test_parser(src, Default::default(), |p| p.parse_script());
}

#[test]
fn issue_6301_await_expr_stmt_5() {
let src = "function test() { (await); }";

test_parser(src, Default::default(), |p| p.parse_script());
}
}
@@ -1,12 +1,11 @@

x Unexpected token `<eof>`. Expected this, import, async, function, [ for array literal, { for object literal, @ for decorator, function, class, null, true, false, number, bigint, string, regexp,
| ` for template literal, (, or an identifier
x top level await requires target to es2017 or higher and topLevelAwait:true for ecmascript
,-[$DIR/tests/test262-parser/fail/1aefe47e20eb91fa.module.js:1:1]
1 | await
: ^^^^^
`----

x top level await requires target to es2017 or higher and topLevelAwait:true for ecmascript
x `await` cannot be used as an identifier in an async context
,-[$DIR/tests/test262-parser/fail/1aefe47e20eb91fa.module.js:1:1]
1 | await
: ^^^^^
Expand Down