Skip to content

Commit b9c0446

Browse files
authored
fix(es/compat): Properly handle rest assignment in for-in/of head (#10489)
**Related issue:** - Closes #10488
1 parent 73377f0 commit b9c0446

File tree

7 files changed

+79
-36
lines changed

7 files changed

+79
-36
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "ecmascript"
5+
},
6+
"target": "es5"
7+
},
8+
"module": {
9+
"type": "es6"
10+
},
11+
"minify": false,
12+
"isModule": true
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let rest;
2+
3+
([{ ...rest }] = [{}])
4+
5+
for ([{ ...rest }] of [[{}]]);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var _extends = require("@swc/helpers/_/_extends");
2+
var _object_destructuring_empty = require("@swc/helpers/_/_object_destructuring_empty");
3+
var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
4+
var rest;
5+
var _tmp;
6+
var ref;
7+
_tmp = [
8+
{}
9+
], rest = _extends._({}, _object_destructuring_empty._(_tmp[0])), ref = _sliced_to_array._(_tmp, 1), _object_destructuring_empty._(ref[0]), ref, _tmp;
10+
for(var _i = 0, _iter = [
11+
[
12+
{}
13+
]
14+
]; _i < _iter.length; _i++){
15+
var _ref = _iter[_i];
16+
var _ref1;
17+
var ref1;
18+
_ref1 = _ref, rest = _extends._({}, _object_destructuring_empty._(_ref1[0])), ref1 = _sliced_to_array._(_ref1, 1), _object_destructuring_empty._(ref1[0]), ref1, _ref1;
19+
;
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "ecmascript"
5+
},
6+
"target": "es2017"
7+
},
8+
"module": {
9+
"type": "es6"
10+
},
11+
"minify": false,
12+
"isModule": true
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let rest;
2+
3+
([{ ...rest }] = [{}])
4+
5+
for ([{ ...rest }] of [[{}]]);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var _extends = require("@swc/helpers/_/_extends");
2+
var _object_destructuring_empty = require("@swc/helpers/_/_object_destructuring_empty");
3+
let rest;
4+
var _tmp;
5+
_tmp = [
6+
{}
7+
], rest = _extends._({}, _object_destructuring_empty._(_tmp[0])), [{}] = _tmp, _tmp;
8+
for (var _ref of [
9+
[
10+
{}
11+
]
12+
]){
13+
var _ref1;
14+
_ref1 = _ref, rest = _extends._({}, _object_destructuring_empty._(_ref1[0])), [{}] = _ref1, _ref1;
15+
;
16+
}

crates/swc_ecma_compat_es2018/src/object_rest.rs

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ macro_rules! impl_for_for_stmt {
3737
return;
3838
}
3939

40-
let mut stmt = None;
40+
let stmt;
4141

4242
let left = match &mut for_stmt.left {
4343
ForHead::VarDecl(var_decl) => {
@@ -81,44 +81,15 @@ macro_rules! impl_for_for_stmt {
8181
}
8282
ForHead::Pat(pat) => {
8383
let var_ident = private_ident!("_ref");
84-
let index = self.vars.len();
8584
let pat = pat.take();
8685

8786
// initialize (or destructure)
88-
match &*pat {
89-
Pat::Object(ObjectPat { ref props, .. }) if props.is_empty() => {}
90-
Pat::Object(ObjectPat { .. }) => {
91-
stmt = Some(Stmt::Expr(ExprStmt {
92-
span: DUMMY_SP,
93-
expr: Box::new(
94-
AssignExpr {
95-
span: DUMMY_SP,
96-
op: op!("="),
97-
left: pat.try_into().unwrap(),
98-
right: Box::new(Expr::Ident(var_ident.clone())),
99-
}
100-
.into(),
101-
),
102-
}));
103-
}
104-
_ => {
105-
// insert at index to create
106-
// `var { a } = _ref, b = _object_without_properties(_ref, ['a']);`
107-
// instead of
108-
// var b = _object_without_properties(_ref, ['a']), { a } = _ref;
109-
110-
// println!("Var(0): folded pat = var_ident",);
111-
self.vars.insert(
112-
index,
113-
VarDeclarator {
114-
span: DUMMY_SP,
115-
name: *pat,
116-
init: Some(Box::new(Expr::Ident(var_ident.clone()))),
117-
definite: false,
118-
},
119-
);
120-
}
121-
}
87+
stmt = Some(
88+
var_ident
89+
.clone()
90+
.make_assign_to(op!("="), pat.try_into().unwrap())
91+
.into_stmt(),
92+
);
12293

12394
// `var _ref` in `for (var _ref in foo)`
12495
VarDecl {

0 commit comments

Comments
 (0)