From e1043fae77ca9e33b2d65ce6edc9559917a895ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 11 Oct 2023 23:03:08 +0900 Subject: [PATCH] fix(es/parser): Fix parsing of `await using` (#8101) **Related issue:** - Closes #8096 --- .../fixture/issues-8xxx/8096/input/.swcrc | 20 +++++++++++++++++++ .../tests/fixture/issues-8xxx/8096/input/1.js | 1 + .../fixture/issues-8xxx/8096/output/1.js | 1 + crates/swc_ecma_parser/src/parser/expr/ops.rs | 13 ++++++++---- crates/swc_ecma_parser/src/parser/stmt.rs | 4 +++- 5 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 crates/swc/tests/fixture/issues-8xxx/8096/input/.swcrc create mode 100644 crates/swc/tests/fixture/issues-8xxx/8096/input/1.js create mode 100644 crates/swc/tests/fixture/issues-8xxx/8096/output/1.js diff --git a/crates/swc/tests/fixture/issues-8xxx/8096/input/.swcrc b/crates/swc/tests/fixture/issues-8xxx/8096/input/.swcrc new file mode 100644 index 000000000000..79deba540748 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8096/input/.swcrc @@ -0,0 +1,20 @@ +{ + "jsc": { + "parser": { + "syntax": "typescript", + "tsx": false, + "decorators": true + }, + "target": "es2022", + "loose": false, + "minify": { + "compress": false, + "mangle": false + } + }, + "module": { + "type": "es6" + }, + "minify": false, + "isModule": true +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-8xxx/8096/input/1.js b/crates/swc/tests/fixture/issues-8xxx/8096/input/1.js new file mode 100644 index 000000000000..2f3bd01fea3f --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8096/input/1.js @@ -0,0 +1 @@ +await using(true); diff --git a/crates/swc/tests/fixture/issues-8xxx/8096/output/1.js b/crates/swc/tests/fixture/issues-8xxx/8096/output/1.js new file mode 100644 index 000000000000..2f3bd01fea3f --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8096/output/1.js @@ -0,0 +1 @@ +await using(true); diff --git a/crates/swc_ecma_parser/src/parser/expr/ops.rs b/crates/swc_ecma_parser/src/parser/expr/ops.rs index 8ccac2865a9a..0873045da978 100644 --- a/crates/swc_ecma_parser/src/parser/expr/ops.rs +++ b/crates/swc_ecma_parser/src/parser/expr/ops.rs @@ -330,7 +330,7 @@ impl Parser { } if is!(self, "await") { - return self.parse_await_expr(); + return self.parse_await_expr(None); } // UpdateExpression @@ -361,10 +361,15 @@ impl Parser { Ok(expr) } - pub(crate) fn parse_await_expr(&mut self) -> PResult> { - let start = cur_pos!(self); + pub(crate) fn parse_await_expr( + &mut self, + start_of_await_token: Option, + ) -> PResult> { + let start = start_of_await_token.unwrap_or_else(|| cur_pos!(self)); - assert_and_bump!(self, "await"); + if start_of_await_token.is_none() { + assert_and_bump!(self, "await"); + } if is!(self, '*') { syntax_error!(self, SyntaxError::AwaitStar); diff --git a/crates/swc_ecma_parser/src/parser/stmt.rs b/crates/swc_ecma_parser/src/parser/stmt.rs index 6722a859098e..d72fc67e3150 100644 --- a/crates/swc_ecma_parser/src/parser/stmt.rs +++ b/crates/swc_ecma_parser/src/parser/stmt.rs @@ -126,7 +126,9 @@ impl<'a, I: Tokens> Parser { self.emit_err(self.input.cur_span(), SyntaxError::TopLevelAwaitInScript); } + let mut eaten_await = None; if peeked_is!(self, "using") { + eaten_await = Some(self.input.cur_pos()); assert_and_bump!(self, "await"); let v = self.parse_using_decl(start, true)?; @@ -135,7 +137,7 @@ impl<'a, I: Tokens> Parser { } } - let expr = self.parse_await_expr()?; + let expr = self.parse_await_expr(eaten_await)?; let expr = self .include_in_expr(true) .parse_bin_op_recursively(expr, 0)?;