From 6a9d77808b8513246592129e8c9e154f686fa8a9 Mon Sep 17 00:00:00 2001 From: Austaras Date: Wed, 23 Feb 2022 12:08:46 +0800 Subject: [PATCH] fix(es/parser): Emit an error for non-last rest element in an object pattern (#3675) --- crates/swc_ecma_parser/src/parser/pat.rs | 13 +++++++------ .../tests/jsx/errors/issue-3523/input.js | 4 ++++ .../tests/jsx/errors/issue-3523/input.js.stderr | 6 ++++++ 3 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 crates/swc_ecma_parser/tests/jsx/errors/issue-3523/input.js create mode 100644 crates/swc_ecma_parser/tests/jsx/errors/issue-3523/input.js.stderr diff --git a/crates/swc_ecma_parser/src/parser/pat.rs b/crates/swc_ecma_parser/src/parser/pat.rs index d84ab7c1228..80381cdd4b7 100644 --- a/crates/swc_ecma_parser/src/parser/pat.rs +++ b/crates/swc_ecma_parser/src/parser/pat.rs @@ -510,11 +510,7 @@ impl<'a, I: Tokens> Parser { self.reparse_expr_as_pat_inner(pat_ty, expr) } - pub(super) fn reparse_expr_as_pat_inner( - &mut self, - pat_ty: PatType, - expr: Box, - ) -> PResult { + fn reparse_expr_as_pat_inner(&mut self, pat_ty: PatType, expr: Box) -> PResult { // In dts, we do not reparse. debug_assert!(!self.input.syntax().dts()); @@ -608,11 +604,13 @@ impl<'a, I: Tokens> Parser { } Expr::Object(ObjectLit { span, props }) => { // {} + let len = props.len(); Ok(Pat::Object(ObjectPat { span, props: props .into_iter() - .map(|prop| { + .enumerate() + .map(|(idx, prop)| { let span = prop.span(); match prop { PropOrSpread::Prop(prop) => match *prop { @@ -643,6 +641,9 @@ impl<'a, I: Tokens> Parser { }, PropOrSpread::Spread(SpreadElement { dot3_token, expr }) => { + if idx != len - 1 { + self.emit_err(span, SyntaxError::NonLastRestParam) + }; Ok(ObjectPatProp::Rest(RestPat { span, dot3_token, diff --git a/crates/swc_ecma_parser/tests/jsx/errors/issue-3523/input.js b/crates/swc_ecma_parser/tests/jsx/errors/issue-3523/input.js new file mode 100644 index 00000000000..1dbafa65978 --- /dev/null +++ b/crates/swc_ecma_parser/tests/jsx/errors/issue-3523/input.js @@ -0,0 +1,4 @@ +var rest, b; + +for ({...rest, b} of [{} +]) ; \ No newline at end of file diff --git a/crates/swc_ecma_parser/tests/jsx/errors/issue-3523/input.js.stderr b/crates/swc_ecma_parser/tests/jsx/errors/issue-3523/input.js.stderr new file mode 100644 index 00000000000..d678014aab3 --- /dev/null +++ b/crates/swc_ecma_parser/tests/jsx/errors/issue-3523/input.js.stderr @@ -0,0 +1,6 @@ +error: Rest element must be final element + --> $DIR/tests/jsx/errors/issue-3523/input.js:3:7 + | +3 | for ({...rest, b} of [{} + | ^^^^^^^ +