fix(es/parser): Emit an error for non-last rest element in an object pattern (#3675)

This commit is contained in:
Austaras 2022-02-23 12:08:46 +08:00 committed by GitHub
parent e65c0609ac
commit 6a9d77808b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 6 deletions

View File

@ -510,11 +510,7 @@ impl<'a, I: Tokens> Parser<I> {
self.reparse_expr_as_pat_inner(pat_ty, expr) self.reparse_expr_as_pat_inner(pat_ty, expr)
} }
pub(super) fn reparse_expr_as_pat_inner( fn reparse_expr_as_pat_inner(&mut self, pat_ty: PatType, expr: Box<Expr>) -> PResult<Pat> {
&mut self,
pat_ty: PatType,
expr: Box<Expr>,
) -> PResult<Pat> {
// In dts, we do not reparse. // In dts, we do not reparse.
debug_assert!(!self.input.syntax().dts()); debug_assert!(!self.input.syntax().dts());
@ -608,11 +604,13 @@ impl<'a, I: Tokens> Parser<I> {
} }
Expr::Object(ObjectLit { span, props }) => { Expr::Object(ObjectLit { span, props }) => {
// {} // {}
let len = props.len();
Ok(Pat::Object(ObjectPat { Ok(Pat::Object(ObjectPat {
span, span,
props: props props: props
.into_iter() .into_iter()
.map(|prop| { .enumerate()
.map(|(idx, prop)| {
let span = prop.span(); let span = prop.span();
match prop { match prop {
PropOrSpread::Prop(prop) => match *prop { PropOrSpread::Prop(prop) => match *prop {
@ -643,6 +641,9 @@ impl<'a, I: Tokens> Parser<I> {
}, },
PropOrSpread::Spread(SpreadElement { dot3_token, expr }) => { PropOrSpread::Spread(SpreadElement { dot3_token, expr }) => {
if idx != len - 1 {
self.emit_err(span, SyntaxError::NonLastRestParam)
};
Ok(ObjectPatProp::Rest(RestPat { Ok(ObjectPatProp::Rest(RestPat {
span, span,
dot3_token, dot3_token,

View File

@ -0,0 +1,4 @@
var rest, b;
for ({...rest, b} of [{}
]) ;

View File

@ -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 [{}
| ^^^^^^^