Skip to content

Commit

Permalink
fix: more
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Nov 2, 2022
1 parent 8f0334c commit 6b0cf14
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
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
28 changes: 28 additions & 0 deletions crates/swc_ecma_parser/src/parser/stmt.rs
Expand Up @@ -2499,4 +2499,32 @@ const foo;"#;

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

0 comments on commit 6b0cf14

Please sign in to comment.