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 all commits
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
14 changes: 14 additions & 0 deletions packages/babel-plugin-proposal-object-rest-spread/src/index.js
Expand Up @@ -178,6 +178,20 @@ export default declare((api, opts) => {
);
} else {
keyExpression = t.arrayExpression(keys);

if (!t.isProgram(path.scope.block)) {
// Hoist definition of excluded keys, so that it's not created each time.
const program = path.findParent(path => path.isProgram());
const id = path.scope.generateUidIdentifier("excluded");

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

keyExpression = t.cloneNode(id);
}
}

return [
Expand Down
@@ -1,3 +1,5 @@
const _excluded = ["excluded", "excluded2", "used", "used2"],
_excluded2 = ["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, _excluded);
console.log(used, usedRenamed);
return React.createElement("input", props);
}

function smth(_ref) {
let rest = babelHelpers.objectWithoutProperties(_ref, ["unused"]);
let rest = babelHelpers.objectWithoutProperties(_ref, _excluded2);
call(rest);
}
@@ -1,3 +1,7 @@
const _excluded = ["a1"],
_excluded2 = ["a2", "b2"],
_excluded3 = ["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, _excluded);
}

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

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, _excluded3);
} // Unchanged


Expand Down
@@ -1,18 +1,22 @@
const _excluded = ["a"],
_excluded2 = ["a"],
_excluded3 = ["a"];

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

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

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

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

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

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

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

Expand All @@ -21,7 +25,7 @@ async function a() {
({
a
} = _ref5);
b = babelHelpers.objectWithoutProperties(_ref5, ["a"]);
b = babelHelpers.objectWithoutProperties(_ref5, _excluded3);
_ref5;
}
} // skip
Expand Down
@@ -1,3 +1,5 @@
const _excluded = ["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, _excluded);
let {
a = {
Y
Expand Down
@@ -1,3 +1,13 @@
const _excluded = ["a1"],
_excluded2 = ["a2", "b2"],
_excluded3 = ["a5"],
_excluded4 = ["a3"],
_excluded5 = ["ba1"],
_excluded6 = ["a3", "b2"],
_excluded7 = ["ba1"],
_excluded8 = ["a1"],
_excluded9 = ["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, _excluded);
}

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

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

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, _excluded5),
c3 = babelHelpers.objectWithoutProperties(_ref6, _excluded6);
}

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, _excluded7);
}

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

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

function a10([a1, _ref11]) {
Expand Down
@@ -1,3 +1,5 @@
const _excluded = ["excluded", "excluded2", "used", "used2"],
_excluded2 = ["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, _excluded);
console.log(used, usedRenamed);
return React.createElement("input", props);
}

}

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

const _foo = foo(),
{
s
Expand All @@ -19,6 +21,6 @@ const {
let {
b
} = _ref,
c = babelHelpers.objectWithoutProperties(_ref, ["b"]);
c = babelHelpers.objectWithoutProperties(_ref, _excluded);
console.log(b, c);
});
@@ -1,3 +1,5 @@
const _excluded = ["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, _excluded);
console.log(rest);
}
} = obj1;
Expand Down
@@ -1,3 +1,5 @@
const _excluded = ["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, _excluded);
const v = b + 3;
};
@@ -1,3 +1,5 @@
var _excluded = ["foo"];

var _loop = function (foo, bar) {
() => foo;

Expand All @@ -12,7 +14,7 @@ for (var _ref of {}) {
var {
foo
} = _ref,
bar = babelHelpers.objectWithoutPropertiesLoose(_ref, ["foo"]);
bar = babelHelpers.objectWithoutPropertiesLoose(_ref, _excluded);

_loop(foo, bar);
}
12 changes: 12 additions & 0 deletions packages/babel-plugin-transform-destructuring/src/index.js
Expand Up @@ -228,6 +228,18 @@ export default declare((api, options) => {
t.memberExpression(keyExpression, t.identifier("map")),
[this.addHelper("toPropertyKey")],
);
} else if (!t.isProgram(this.scope.block)) {
// Hoist definition of excluded keys, so that it's not created each time.
const program = this.scope.path.findParent(path => path.isProgram());
const id = this.scope.generateUidIdentifier("excluded");

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

keyExpression = t.cloneNode(id);
}

value = t.callExpression(
Expand Down
@@ -1,3 +1,4 @@
var _excluded = ["x"];
var z = {};
var _z = z,
x = Object.assign({}, _z);
Expand All @@ -10,7 +11,7 @@ var _z3 = z,

(function (_ref) {
var x = _ref.x,
y = babelHelpers.objectWithoutProperties(_ref, ["x"]);
y = babelHelpers.objectWithoutProperties(_ref, _excluded);
});

var _o = o;
Expand Down
@@ -1,3 +1,4 @@
const _excluded = ["x"];
var z = {};
var _z = z,
x = babelHelpers.extends({}, _z);
Expand All @@ -10,7 +11,7 @@ var _z3 = z,

(function (_ref) {
let x = _ref.x,
y = babelHelpers.objectWithoutPropertiesLoose(_ref, ["x"]);
y = babelHelpers.objectWithoutPropertiesLoose(_ref, _excluded);
});

var _o = o;
Expand Down
@@ -1,3 +1,4 @@
var _excluded = ["x"];
var z = {};
var _z = z,
x = babelHelpers.extends({}, _z);
Expand All @@ -10,7 +11,7 @@ var _z3 = z,

(function (_ref) {
var x = _ref.x,
y = babelHelpers.objectWithoutProperties(_ref, ["x"]);
y = babelHelpers.objectWithoutProperties(_ref, _excluded);
});

var _o = o;
Expand Down