Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hoist omitted keys from object spread operator #13384

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/babel-plugin-proposal-object-rest-spread/src/index.js
Expand Up @@ -180,6 +180,19 @@ export default declare((api, opts) => {
keyExpression = t.arrayExpression(keys);
}

// Hoist definition of excluded keys, so that it's not created each time.
if (!t.isProgram(path.scope.block)) {
const id = path.scope.generateUidIdentifier();
alanorozco marked this conversation as resolved.
Show resolved Hide resolved

file.path.scope.push({
id,
init: keyExpression,
kind: "const",
});

keyExpression = t.cloneNode(id);
alanorozco marked this conversation as resolved.
Show resolved Hide resolved
}

return [
impureComputedPropertyDeclarators,
restElement.argument,
Expand Down
@@ -1,3 +1,5 @@
const _temp = ["excluded", "excluded2", "used", "used2"],
_temp2 = ["unused"];
// should not remove when destructuring into existing bindings
var _c = c2;
({
Expand All @@ -12,12 +14,12 @@ function render() {
used,
used2: usedRenamed
} = _this$props,
props = babelHelpers.objectWithoutProperties(_this$props, ["excluded", "excluded2", "used", "used2"]);
props = babelHelpers.objectWithoutProperties(_this$props, _temp);
console.log(used, usedRenamed);
return React.createElement("input", props);
}

function smth(_ref) {
let rest = babelHelpers.objectWithoutProperties(_ref, ["unused"]);
let rest = babelHelpers.objectWithoutProperties(_ref, _temp2);
call(rest);
}
@@ -1,3 +1,7 @@
const _temp = ["a1"],
_temp2 = ["a2", "b2"],
_temp3 = ["c3"];

try {} catch (_ref) {
let a34 = babelHelpers.extends({}, _ref);
}
Expand All @@ -6,15 +10,15 @@ try {} catch (_ref2) {
let {
a1
} = _ref2,
b1 = babelHelpers.objectWithoutProperties(_ref2, ["a1"]);
b1 = babelHelpers.objectWithoutProperties(_ref2, _temp);
}

try {} catch (_ref3) {
let {
a2,
b2
} = _ref3,
c2 = babelHelpers.objectWithoutProperties(_ref3, ["a2", "b2"]);
c2 = babelHelpers.objectWithoutProperties(_ref3, _temp2);
}

try {} catch (_ref4) {
Expand All @@ -25,7 +29,7 @@ try {} catch (_ref4) {
c3
}
} = _ref4,
c4 = babelHelpers.objectWithoutProperties(_ref4.c2, ["c3"]);
c4 = babelHelpers.objectWithoutProperties(_ref4.c2, _temp3);
} // Unchanged


Expand Down
@@ -1,18 +1,22 @@
const _temp = ["a"],
_temp2 = ["a"],
_temp3 = ["a"];

// ForXStatement
for (const _ref of []) {
const [_ref2] = _ref;
const {
a
} = _ref2,
b = babelHelpers.objectWithoutProperties(_ref2, ["a"]);
b = babelHelpers.objectWithoutProperties(_ref2, _temp);
}

for (var _ref3 of []) {
[_ref4] = _ref3;
var {
a
} = _ref4,
b = babelHelpers.objectWithoutProperties(_ref4, ["a"]);
b = babelHelpers.objectWithoutProperties(_ref4, _temp2);
}

async function a() {
Expand All @@ -21,7 +25,7 @@ async function a() {
var {
a
} = _ref6,
b = babelHelpers.objectWithoutProperties(_ref6, ["a"]);
b = babelHelpers.objectWithoutProperties(_ref6, _temp3);
}
} // skip

Expand Down
@@ -1,9 +1,11 @@
const _temp = ["a"];

for (var _ref of []) {
var _ref2 = _ref;
({
a
} = _ref2);
b = babelHelpers.objectWithoutProperties(_ref2, ["a"]);
b = babelHelpers.objectWithoutProperties(_ref2, _temp);
_ref2;
void 0;
}
@@ -1,17 +1,21 @@
const _temp = ["a"],
_temp2 = ["a"],
_temp3 = ["a"];

// ForXStatement
for (var _ref of []) {
var {
a
} = _ref,
b = babelHelpers.objectWithoutProperties(_ref, ["a"]);
b = babelHelpers.objectWithoutProperties(_ref, _temp);
}

for (var _ref2 of []) {
var _ref3 = _ref2;
({
a
} = _ref3);
b = babelHelpers.objectWithoutProperties(_ref3, ["a"]);
b = babelHelpers.objectWithoutProperties(_ref3, _temp2);
_ref3;
}

Expand All @@ -21,7 +25,7 @@ async function a() {
({
a
} = _ref5);
b = babelHelpers.objectWithoutProperties(_ref5, ["a"]);
b = babelHelpers.objectWithoutProperties(_ref5, _temp3);
_ref5;
}
} // skip
Expand Down
@@ -1,3 +1,5 @@
const _temp = ["X"];

_ref => {
let R = babelHelpers.extends({}, _ref);
let a = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : R;
Expand All @@ -7,7 +9,7 @@ _ref => {
let {
X: Y
} = _ref2,
R = babelHelpers.objectWithoutProperties(_ref2, ["X"]);
R = babelHelpers.objectWithoutProperties(_ref2, _temp);
let {
a = {
Y
Expand Down
@@ -1,3 +1,13 @@
const _temp = ["a1"],
_temp2 = ["a2", "b2"],
_temp3 = ["a5"],
_temp4 = ["a3"],
_temp5 = ["ba1"],
_temp6 = ["a3", "b2"],
_temp7 = ["ba1"],
_temp8 = ["a1"],
_temp9 = ["a1"];

function a(_ref) {
let a34 = babelHelpers.extends({}, _ref);
}
Expand All @@ -6,26 +16,26 @@ function a2(_ref2) {
let {
a1
} = _ref2,
b1 = babelHelpers.objectWithoutProperties(_ref2, ["a1"]);
b1 = babelHelpers.objectWithoutProperties(_ref2, _temp);
}

function a3(_ref3) {
let {
a2,
b2
} = _ref3,
c2 = babelHelpers.objectWithoutProperties(_ref3, ["a2", "b2"]);
c2 = babelHelpers.objectWithoutProperties(_ref3, _temp2);
}

function a4(_ref4, _ref5) {
let {
a5
} = _ref5,
c5 = babelHelpers.objectWithoutProperties(_ref5, ["a5"]);
c5 = babelHelpers.objectWithoutProperties(_ref5, _temp3);
let {
a3
} = _ref4,
c3 = babelHelpers.objectWithoutProperties(_ref4, ["a3"]);
c3 = babelHelpers.objectWithoutProperties(_ref4, _temp4);
}

function a5(_ref6) {
Expand All @@ -35,8 +45,8 @@ function a5(_ref6) {
ba1
}
} = _ref6,
ba2 = babelHelpers.objectWithoutProperties(_ref6.b2, ["ba1"]),
c3 = babelHelpers.objectWithoutProperties(_ref6, ["a3", "b2"]);
ba2 = babelHelpers.objectWithoutProperties(_ref6.b2, _temp5),
c3 = babelHelpers.objectWithoutProperties(_ref6, _temp6);
}

function a6(_ref7) {
Expand All @@ -46,14 +56,14 @@ function a6(_ref7) {
ba1
}
} = _ref7,
ba2 = babelHelpers.objectWithoutProperties(_ref7.b2, ["ba1"]);
ba2 = babelHelpers.objectWithoutProperties(_ref7.b2, _temp7);
}

function a7(_ref8 = {}) {
let {
a1 = 1
} = _ref8,
b1 = babelHelpers.objectWithoutProperties(_ref8, ["a1"]);
b1 = babelHelpers.objectWithoutProperties(_ref8, _temp8);
}

function a8([_ref9]) {
Expand All @@ -64,7 +74,7 @@ function a9([_ref10]) {
let {
a1
} = _ref10,
a2 = babelHelpers.objectWithoutProperties(_ref10, ["a1"]);
a2 = babelHelpers.objectWithoutProperties(_ref10, _temp9);
}

function a10([a1, _ref11]) {
Expand Down
@@ -1,3 +1,5 @@
const _temp = ["excluded", "excluded2", "used", "used2"],
_temp2 = ["unused"];
// should not remove when destructuring into existing bindings
var _c = c2;
({
Expand All @@ -13,14 +15,14 @@ class Comp extends React.Component {
used,
used2: usedRenamed
} = _this$props,
props = babelHelpers.objectWithoutPropertiesLoose(_this$props, ["excluded", "excluded2", "used", "used2"]);
props = babelHelpers.objectWithoutPropertiesLoose(_this$props, _temp);
console.log(used, usedRenamed);
return React.createElement("input", props);
}

}

function smth(_ref) {
let rest = babelHelpers.objectWithoutPropertiesLoose(_ref, ["unused"]);
let rest = babelHelpers.objectWithoutPropertiesLoose(_ref, _temp2);
call(rest);
}
@@ -1,3 +1,5 @@
const _temp = ["b"];

const _foo = foo(),
{
s
Expand All @@ -19,6 +21,6 @@ const {
let {
b
} = _ref,
c = babelHelpers.objectWithoutProperties(_ref, ["b"]);
c = babelHelpers.objectWithoutProperties(_ref, _temp);
console.log(b, c);
});
@@ -1,3 +1,5 @@
const _temp = ["a"];

function fn0(obj0) {
const {
fn1 = (obj1 = {}) => {
Expand All @@ -6,7 +8,7 @@ function fn0(obj0) {
const {
a
} = obj2,
rest = babelHelpers.objectWithoutProperties(obj2, ["a"]);
rest = babelHelpers.objectWithoutProperties(obj2, _temp);
console.log(rest);
}
} = obj1;
Expand Down
@@ -1,3 +1,5 @@
const _temp = ["a", "b", "c"];

const get = () => {
fireTheMissiles();
return 3;
Expand All @@ -8,6 +10,6 @@ const f = _ref => {
a = get(),
b
} = _ref,
z = babelHelpers.objectWithoutPropertiesLoose(_ref, ["a", "b", "c"]);
z = babelHelpers.objectWithoutPropertiesLoose(_ref, _temp);
const v = b + 3;
};