fix(es/compat): Fix destructuring of an empty array (#6314)

This commit is contained in:
Alexander Akait 2022-11-02 10:06:52 +03:00 committed by GitHub
parent 06a664a1f4
commit 3bea6f7248
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 2 deletions

View File

@ -160,8 +160,9 @@ impl AssignFolder {
if is_literal(&init) { if is_literal(&init) {
match *init { match *init {
Expr::Array(arr) Expr::Array(arr)
if elems.len() == arr.elems.len() if !elems.is_empty()
|| (elems.len() < arr.elems.len() && has_rest_pat(&elems)) => && (elems.len() == arr.elems.len()
|| (elems.len() < arr.elems.len() && has_rest_pat(&elems))) =>
{ {
let mut arr_elems = Some(arr.elems.into_iter()); let mut arr_elems = Some(arr.elems.into_iter());
elems.into_iter().for_each(|p| match p { elems.into_iter().for_each(|p| match p {

View File

@ -2261,3 +2261,27 @@ test!(
"let [x] = [,];", "let [x] = [,];",
"let x;" "let x;"
); );
test!(
syntax(),
|_| tr(),
issue_6304,
"let [] = [];",
"let ref = [];"
);
test!(
syntax(),
|_| tr(),
issue_6304_1,
"let [] = [,];",
"let ref = [,];"
);
test!(
syntax(),
|_| tr(),
issue_6304_2,
"let [] = [...[1, 2, 3]];",
"let ref = [...[1, 2, 3]];"
);