Skip to content

Commit f35513f

Browse files
authored
Hoist omitted keys from object spread operator (#13384)
1 parent 612f19f commit f35513f

21 files changed

Lines changed: 110 additions & 35 deletions

File tree

packages/babel-plugin-proposal-object-rest-spread/src/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,20 @@ export default declare((api, opts) => {
178178
);
179179
} else {
180180
keyExpression = t.arrayExpression(keys);
181+
182+
if (!t.isProgram(path.scope.block)) {
183+
// Hoist definition of excluded keys, so that it's not created each time.
184+
const program = path.findParent(path => path.isProgram());
185+
const id = path.scope.generateUidIdentifier("excluded");
186+
187+
program.scope.push({
188+
id,
189+
init: keyExpression,
190+
kind: "const",
191+
});
192+
193+
keyExpression = t.cloneNode(id);
194+
}
181195
}
182196

183197
return [

packages/babel-plugin-proposal-object-rest-spread/test/fixtures/assumption-pureGetters/rest-remove-unused-excluded-keys/output.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const _excluded = ["excluded", "excluded2", "used", "used2"],
2+
_excluded2 = ["unused"];
13
// should not remove when destructuring into existing bindings
24
var _c = c2;
35
({
@@ -12,12 +14,12 @@ function render() {
1214
used,
1315
used2: usedRenamed
1416
} = _this$props,
15-
props = babelHelpers.objectWithoutProperties(_this$props, ["excluded", "excluded2", "used", "used2"]);
17+
props = babelHelpers.objectWithoutProperties(_this$props, _excluded);
1618
console.log(used, usedRenamed);
1719
return React.createElement("input", props);
1820
}
1921

2022
function smth(_ref) {
21-
let rest = babelHelpers.objectWithoutProperties(_ref, ["unused"]);
23+
let rest = babelHelpers.objectWithoutProperties(_ref, _excluded2);
2224
call(rest);
2325
}

packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/catch-clause/output.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
const _excluded = ["a1"],
2+
_excluded2 = ["a2", "b2"],
3+
_excluded3 = ["c3"];
4+
15
try {} catch (_ref) {
26
let a34 = babelHelpers.extends({}, _ref);
37
}
@@ -6,15 +10,15 @@ try {} catch (_ref2) {
610
let {
711
a1
812
} = _ref2,
9-
b1 = babelHelpers.objectWithoutProperties(_ref2, ["a1"]);
13+
b1 = babelHelpers.objectWithoutProperties(_ref2, _excluded);
1014
}
1115

1216
try {} catch (_ref3) {
1317
let {
1418
a2,
1519
b2
1620
} = _ref3,
17-
c2 = babelHelpers.objectWithoutProperties(_ref3, ["a2", "b2"]);
21+
c2 = babelHelpers.objectWithoutProperties(_ref3, _excluded2);
1822
}
1923

2024
try {} catch (_ref4) {
@@ -25,7 +29,7 @@ try {} catch (_ref4) {
2529
c3
2630
}
2731
} = _ref4,
28-
c4 = babelHelpers.objectWithoutProperties(_ref4.c2, ["c3"]);
32+
c4 = babelHelpers.objectWithoutProperties(_ref4.c2, _excluded3);
2933
} // Unchanged
3034

3135

packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/for-x-array-pattern/output.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1+
const _excluded = ["a"],
2+
_excluded2 = ["a"],
3+
_excluded3 = ["a"];
4+
15
// ForXStatement
26
for (const _ref of []) {
37
const [_ref2] = _ref;
48
const {
59
a
610
} = _ref2,
7-
b = babelHelpers.objectWithoutProperties(_ref2, ["a"]);
11+
b = babelHelpers.objectWithoutProperties(_ref2, _excluded);
812
}
913

1014
for (var _ref3 of []) {
1115
[_ref4] = _ref3;
1216
var {
1317
a
1418
} = _ref4,
15-
b = babelHelpers.objectWithoutProperties(_ref4, ["a"]);
19+
b = babelHelpers.objectWithoutProperties(_ref4, _excluded2);
1620
}
1721

1822
async function a() {
@@ -21,7 +25,7 @@ async function a() {
2125
var {
2226
a
2327
} = _ref6,
24-
b = babelHelpers.objectWithoutProperties(_ref6, ["a"]);
28+
b = babelHelpers.objectWithoutProperties(_ref6, _excluded3);
2529
}
2630
} // skip
2731

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
const _excluded = ["a"];
2+
13
for (var _ref of []) {
24
var _ref2 = _ref;
35
({
46
a
57
} = _ref2);
6-
b = babelHelpers.objectWithoutProperties(_ref2, ["a"]);
8+
b = babelHelpers.objectWithoutProperties(_ref2, _excluded);
79
_ref2;
810
void 0;
911
}

packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/for-x/output.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
const _excluded = ["a"],
2+
_excluded2 = ["a"],
3+
_excluded3 = ["a"];
4+
15
// ForXStatement
26
for (var _ref of []) {
37
var {
48
a
59
} = _ref,
6-
b = babelHelpers.objectWithoutProperties(_ref, ["a"]);
10+
b = babelHelpers.objectWithoutProperties(_ref, _excluded);
711
}
812

913
for (var _ref2 of []) {
1014
var _ref3 = _ref2;
1115
({
1216
a
1317
} = _ref3);
14-
b = babelHelpers.objectWithoutProperties(_ref3, ["a"]);
18+
b = babelHelpers.objectWithoutProperties(_ref3, _excluded2);
1519
_ref3;
1620
}
1721

@@ -21,7 +25,7 @@ async function a() {
2125
({
2226
a
2327
} = _ref5);
24-
b = babelHelpers.objectWithoutProperties(_ref5, ["a"]);
28+
b = babelHelpers.objectWithoutProperties(_ref5, _excluded3);
2529
_ref5;
2630
}
2731
} // skip

packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/parameters-object-rest-used-in-default/output.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const _excluded = ["X"];
2+
13
_ref => {
24
let R = babelHelpers.extends({}, _ref);
35
let a = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : R;
@@ -7,7 +9,7 @@ _ref => {
79
let {
810
X: Y
911
} = _ref2,
10-
R = babelHelpers.objectWithoutProperties(_ref2, ["X"]);
12+
R = babelHelpers.objectWithoutProperties(_ref2, _excluded);
1113
let {
1214
a = {
1315
Y

packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/parameters/output.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
const _excluded = ["a1"],
2+
_excluded2 = ["a2", "b2"],
3+
_excluded3 = ["a5"],
4+
_excluded4 = ["a3"],
5+
_excluded5 = ["ba1"],
6+
_excluded6 = ["a3", "b2"],
7+
_excluded7 = ["ba1"],
8+
_excluded8 = ["a1"],
9+
_excluded9 = ["a1"];
10+
111
function a(_ref) {
212
let a34 = babelHelpers.extends({}, _ref);
313
}
@@ -6,26 +16,26 @@ function a2(_ref2) {
616
let {
717
a1
818
} = _ref2,
9-
b1 = babelHelpers.objectWithoutProperties(_ref2, ["a1"]);
19+
b1 = babelHelpers.objectWithoutProperties(_ref2, _excluded);
1020
}
1121

1222
function a3(_ref3) {
1323
let {
1424
a2,
1525
b2
1626
} = _ref3,
17-
c2 = babelHelpers.objectWithoutProperties(_ref3, ["a2", "b2"]);
27+
c2 = babelHelpers.objectWithoutProperties(_ref3, _excluded2);
1828
}
1929

2030
function a4(_ref4, _ref5) {
2131
let {
2232
a5
2333
} = _ref5,
24-
c5 = babelHelpers.objectWithoutProperties(_ref5, ["a5"]);
34+
c5 = babelHelpers.objectWithoutProperties(_ref5, _excluded3);
2535
let {
2636
a3
2737
} = _ref4,
28-
c3 = babelHelpers.objectWithoutProperties(_ref4, ["a3"]);
38+
c3 = babelHelpers.objectWithoutProperties(_ref4, _excluded4);
2939
}
3040

3141
function a5(_ref6) {
@@ -35,8 +45,8 @@ function a5(_ref6) {
3545
ba1
3646
}
3747
} = _ref6,
38-
ba2 = babelHelpers.objectWithoutProperties(_ref6.b2, ["ba1"]),
39-
c3 = babelHelpers.objectWithoutProperties(_ref6, ["a3", "b2"]);
48+
ba2 = babelHelpers.objectWithoutProperties(_ref6.b2, _excluded5),
49+
c3 = babelHelpers.objectWithoutProperties(_ref6, _excluded6);
4050
}
4151

4252
function a6(_ref7) {
@@ -46,14 +56,14 @@ function a6(_ref7) {
4656
ba1
4757
}
4858
} = _ref7,
49-
ba2 = babelHelpers.objectWithoutProperties(_ref7.b2, ["ba1"]);
59+
ba2 = babelHelpers.objectWithoutProperties(_ref7.b2, _excluded7);
5060
}
5161

5262
function a7(_ref8 = {}) {
5363
let {
5464
a1 = 1
5565
} = _ref8,
56-
b1 = babelHelpers.objectWithoutProperties(_ref8, ["a1"]);
66+
b1 = babelHelpers.objectWithoutProperties(_ref8, _excluded8);
5767
}
5868

5969
function a8([_ref9]) {
@@ -64,7 +74,7 @@ function a9([_ref10]) {
6474
let {
6575
a1
6676
} = _ref10,
67-
a2 = babelHelpers.objectWithoutProperties(_ref10, ["a1"]);
77+
a2 = babelHelpers.objectWithoutProperties(_ref10, _excluded9);
6878
}
6979

7080
function a10([a1, _ref11]) {

packages/babel-plugin-proposal-object-rest-spread/test/fixtures/object-rest/remove-unused-excluded-keys-loose/output.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const _excluded = ["excluded", "excluded2", "used", "used2"],
2+
_excluded2 = ["unused"];
13
// should not remove when destructuring into existing bindings
24
var _c = c2;
35
({
@@ -13,14 +15,14 @@ class Comp extends React.Component {
1315
used,
1416
used2: usedRenamed
1517
} = _this$props,
16-
props = babelHelpers.objectWithoutPropertiesLoose(_this$props, ["excluded", "excluded2", "used", "used2"]);
18+
props = babelHelpers.objectWithoutPropertiesLoose(_this$props, _excluded);
1719
console.log(used, usedRenamed);
1820
return React.createElement("input", props);
1921
}
2022

2123
}
2224

2325
function smth(_ref) {
24-
let rest = babelHelpers.objectWithoutPropertiesLoose(_ref, ["unused"]);
26+
let rest = babelHelpers.objectWithoutPropertiesLoose(_ref, _excluded2);
2527
call(rest);
2628
}

packages/babel-plugin-proposal-object-rest-spread/test/fixtures/regression/gh-4904/output.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const _excluded = ["b"];
2+
13
const _foo = foo(),
24
{
35
s
@@ -19,6 +21,6 @@ const {
1921
let {
2022
b
2123
} = _ref,
22-
c = babelHelpers.objectWithoutProperties(_ref, ["b"]);
24+
c = babelHelpers.objectWithoutProperties(_ref, _excluded);
2325
console.log(b, c);
2426
});

0 commit comments

Comments
 (0)