From cdd69d0145d22008448f394e8b39ba98291d87fe Mon Sep 17 00:00:00 2001 From: John Daly Date: Sat, 24 Sep 2022 18:29:33 -0700 Subject: [PATCH] fix(es/parser): Allow `as` in destructuring assignment (#5948) --- crates/swc_ecma_parser/src/parser/expr/tests.rs | 9 +++++++++ crates/swc_ecma_parser/src/parser/pat.rs | 3 ++- .../tests/fixture/issue-5947/input.ts | 2 ++ .../tests/fixture/issue-5947/output.js | 2 ++ 4 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 crates/swc_ecma_transforms_typescript/tests/fixture/issue-5947/input.ts create mode 100644 crates/swc_ecma_transforms_typescript/tests/fixture/issue-5947/output.js diff --git a/crates/swc_ecma_parser/src/parser/expr/tests.rs b/crates/swc_ecma_parser/src/parser/expr/tests.rs index c79080fb55d..5e650e87fb1 100644 --- a/crates/swc_ecma_parser/src/parser/expr/tests.rs +++ b/crates/swc_ecma_parser/src/parser/expr/tests.rs @@ -510,6 +510,15 @@ fn issue_3672_2() { ); } +#[test] +fn issue_5947() { + test_parser( + "[a as number, b as number, c as string] = [1, 2, '3']", + Syntax::Typescript(Default::default()), + |p| p.parse_module(), + ); +} + #[bench] fn bench_new_expr_ts(b: &mut Bencher) { bench_parser( diff --git a/crates/swc_ecma_parser/src/parser/pat.rs b/crates/swc_ecma_parser/src/parser/pat.rs index 98bbbac814c..292cf1b7893 100644 --- a/crates/swc_ecma_parser/src/parser/pat.rs +++ b/crates/swc_ecma_parser/src/parser/pat.rs @@ -566,7 +566,8 @@ impl Parser { | Expr::Fn(..) | Expr::Class(..) | Expr::Paren(..) - | Expr::Tpl(..) => { + | Expr::Tpl(..) + | Expr::TsAs(..) => { if !expr.is_valid_simple_assignment_target(self.ctx().strict) { self.emit_err(span, SyntaxError::NotSimpleAssign) } diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-5947/input.ts b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-5947/input.ts new file mode 100644 index 00000000000..247f57b43a5 --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-5947/input.ts @@ -0,0 +1,2 @@ +let a, b; +[a as number, b as number] = [1, 2]; diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-5947/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-5947/output.js new file mode 100644 index 00000000000..4de6b9dcea9 --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-5947/output.js @@ -0,0 +1,2 @@ +let a, b; +[a, b] = [1, 2];