Hoist omitted keys from object spread operator (#13384)

This commit is contained in:
Alan Orozco 2021-05-28 09:32:30 -07:00 committed by GitHub
parent 612f19fbdc
commit f35513fbaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 110 additions and 35 deletions

View File

@ -178,6 +178,20 @@ export default declare((api, opts) => {
); );
} else { } else {
keyExpression = t.arrayExpression(keys); 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 [ return [

View File

@ -1,3 +1,5 @@
const _excluded = ["excluded", "excluded2", "used", "used2"],
_excluded2 = ["unused"];
// should not remove when destructuring into existing bindings // should not remove when destructuring into existing bindings
var _c = c2; var _c = c2;
({ ({
@ -12,12 +14,12 @@ function render() {
used, used,
used2: usedRenamed used2: usedRenamed
} = _this$props, } = _this$props,
props = babelHelpers.objectWithoutProperties(_this$props, ["excluded", "excluded2", "used", "used2"]); props = babelHelpers.objectWithoutProperties(_this$props, _excluded);
console.log(used, usedRenamed); console.log(used, usedRenamed);
return React.createElement("input", props); return React.createElement("input", props);
} }
function smth(_ref) { function smth(_ref) {
let rest = babelHelpers.objectWithoutProperties(_ref, ["unused"]); let rest = babelHelpers.objectWithoutProperties(_ref, _excluded2);
call(rest); call(rest);
} }

View File

@ -1,3 +1,7 @@
const _excluded = ["a1"],
_excluded2 = ["a2", "b2"],
_excluded3 = ["c3"];
try {} catch (_ref) { try {} catch (_ref) {
let a34 = babelHelpers.extends({}, _ref); let a34 = babelHelpers.extends({}, _ref);
} }
@ -6,7 +10,7 @@ try {} catch (_ref2) {
let { let {
a1 a1
} = _ref2, } = _ref2,
b1 = babelHelpers.objectWithoutProperties(_ref2, ["a1"]); b1 = babelHelpers.objectWithoutProperties(_ref2, _excluded);
} }
try {} catch (_ref3) { try {} catch (_ref3) {
@ -14,7 +18,7 @@ try {} catch (_ref3) {
a2, a2,
b2 b2
} = _ref3, } = _ref3,
c2 = babelHelpers.objectWithoutProperties(_ref3, ["a2", "b2"]); c2 = babelHelpers.objectWithoutProperties(_ref3, _excluded2);
} }
try {} catch (_ref4) { try {} catch (_ref4) {
@ -25,7 +29,7 @@ try {} catch (_ref4) {
c3 c3
} }
} = _ref4, } = _ref4,
c4 = babelHelpers.objectWithoutProperties(_ref4.c2, ["c3"]); c4 = babelHelpers.objectWithoutProperties(_ref4.c2, _excluded3);
} // Unchanged } // Unchanged

View File

@ -1,10 +1,14 @@
const _excluded = ["a"],
_excluded2 = ["a"],
_excluded3 = ["a"];
// ForXStatement // ForXStatement
for (const _ref of []) { for (const _ref of []) {
const [_ref2] = _ref; const [_ref2] = _ref;
const { const {
a a
} = _ref2, } = _ref2,
b = babelHelpers.objectWithoutProperties(_ref2, ["a"]); b = babelHelpers.objectWithoutProperties(_ref2, _excluded);
} }
for (var _ref3 of []) { for (var _ref3 of []) {
@ -12,7 +16,7 @@ for (var _ref3 of []) {
var { var {
a a
} = _ref4, } = _ref4,
b = babelHelpers.objectWithoutProperties(_ref4, ["a"]); b = babelHelpers.objectWithoutProperties(_ref4, _excluded2);
} }
async function a() { async function a() {
@ -21,7 +25,7 @@ async function a() {
var { var {
a a
} = _ref6, } = _ref6,
b = babelHelpers.objectWithoutProperties(_ref6, ["a"]); b = babelHelpers.objectWithoutProperties(_ref6, _excluded3);
} }
} // skip } // skip

View File

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

View File

@ -1,9 +1,13 @@
const _excluded = ["a"],
_excluded2 = ["a"],
_excluded3 = ["a"];
// ForXStatement // ForXStatement
for (var _ref of []) { for (var _ref of []) {
var { var {
a a
} = _ref, } = _ref,
b = babelHelpers.objectWithoutProperties(_ref, ["a"]); b = babelHelpers.objectWithoutProperties(_ref, _excluded);
} }
for (var _ref2 of []) { for (var _ref2 of []) {
@ -11,7 +15,7 @@ for (var _ref2 of []) {
({ ({
a a
} = _ref3); } = _ref3);
b = babelHelpers.objectWithoutProperties(_ref3, ["a"]); b = babelHelpers.objectWithoutProperties(_ref3, _excluded2);
_ref3; _ref3;
} }
@ -21,7 +25,7 @@ async function a() {
({ ({
a a
} = _ref5); } = _ref5);
b = babelHelpers.objectWithoutProperties(_ref5, ["a"]); b = babelHelpers.objectWithoutProperties(_ref5, _excluded3);
_ref5; _ref5;
} }
} // skip } // skip

View File

@ -1,3 +1,5 @@
const _excluded = ["X"];
_ref => { _ref => {
let R = babelHelpers.extends({}, _ref); let R = babelHelpers.extends({}, _ref);
let a = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : R; let a = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : R;
@ -7,7 +9,7 @@ _ref => {
let { let {
X: Y X: Y
} = _ref2, } = _ref2,
R = babelHelpers.objectWithoutProperties(_ref2, ["X"]); R = babelHelpers.objectWithoutProperties(_ref2, _excluded);
let { let {
a = { a = {
Y Y

View File

@ -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) { function a(_ref) {
let a34 = babelHelpers.extends({}, _ref); let a34 = babelHelpers.extends({}, _ref);
} }
@ -6,7 +16,7 @@ function a2(_ref2) {
let { let {
a1 a1
} = _ref2, } = _ref2,
b1 = babelHelpers.objectWithoutProperties(_ref2, ["a1"]); b1 = babelHelpers.objectWithoutProperties(_ref2, _excluded);
} }
function a3(_ref3) { function a3(_ref3) {
@ -14,18 +24,18 @@ function a3(_ref3) {
a2, a2,
b2 b2
} = _ref3, } = _ref3,
c2 = babelHelpers.objectWithoutProperties(_ref3, ["a2", "b2"]); c2 = babelHelpers.objectWithoutProperties(_ref3, _excluded2);
} }
function a4(_ref4, _ref5) { function a4(_ref4, _ref5) {
let { let {
a5 a5
} = _ref5, } = _ref5,
c5 = babelHelpers.objectWithoutProperties(_ref5, ["a5"]); c5 = babelHelpers.objectWithoutProperties(_ref5, _excluded3);
let { let {
a3 a3
} = _ref4, } = _ref4,
c3 = babelHelpers.objectWithoutProperties(_ref4, ["a3"]); c3 = babelHelpers.objectWithoutProperties(_ref4, _excluded4);
} }
function a5(_ref6) { function a5(_ref6) {
@ -35,8 +45,8 @@ function a5(_ref6) {
ba1 ba1
} }
} = _ref6, } = _ref6,
ba2 = babelHelpers.objectWithoutProperties(_ref6.b2, ["ba1"]), ba2 = babelHelpers.objectWithoutProperties(_ref6.b2, _excluded5),
c3 = babelHelpers.objectWithoutProperties(_ref6, ["a3", "b2"]); c3 = babelHelpers.objectWithoutProperties(_ref6, _excluded6);
} }
function a6(_ref7) { function a6(_ref7) {
@ -46,14 +56,14 @@ function a6(_ref7) {
ba1 ba1
} }
} = _ref7, } = _ref7,
ba2 = babelHelpers.objectWithoutProperties(_ref7.b2, ["ba1"]); ba2 = babelHelpers.objectWithoutProperties(_ref7.b2, _excluded7);
} }
function a7(_ref8 = {}) { function a7(_ref8 = {}) {
let { let {
a1 = 1 a1 = 1
} = _ref8, } = _ref8,
b1 = babelHelpers.objectWithoutProperties(_ref8, ["a1"]); b1 = babelHelpers.objectWithoutProperties(_ref8, _excluded8);
} }
function a8([_ref9]) { function a8([_ref9]) {
@ -64,7 +74,7 @@ function a9([_ref10]) {
let { let {
a1 a1
} = _ref10, } = _ref10,
a2 = babelHelpers.objectWithoutProperties(_ref10, ["a1"]); a2 = babelHelpers.objectWithoutProperties(_ref10, _excluded9);
} }
function a10([a1, _ref11]) { function a10([a1, _ref11]) {

View File

@ -1,3 +1,5 @@
const _excluded = ["excluded", "excluded2", "used", "used2"],
_excluded2 = ["unused"];
// should not remove when destructuring into existing bindings // should not remove when destructuring into existing bindings
var _c = c2; var _c = c2;
({ ({
@ -13,7 +15,7 @@ class Comp extends React.Component {
used, used,
used2: usedRenamed used2: usedRenamed
} = _this$props, } = _this$props,
props = babelHelpers.objectWithoutPropertiesLoose(_this$props, ["excluded", "excluded2", "used", "used2"]); props = babelHelpers.objectWithoutPropertiesLoose(_this$props, _excluded);
console.log(used, usedRenamed); console.log(used, usedRenamed);
return React.createElement("input", props); return React.createElement("input", props);
} }
@ -21,6 +23,6 @@ class Comp extends React.Component {
} }
function smth(_ref) { function smth(_ref) {
let rest = babelHelpers.objectWithoutPropertiesLoose(_ref, ["unused"]); let rest = babelHelpers.objectWithoutPropertiesLoose(_ref, _excluded2);
call(rest); call(rest);
} }

View File

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

View File

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

View File

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

View File

@ -1,3 +1,5 @@
var _excluded = ["foo"];
var _loop = function (foo, bar) { var _loop = function (foo, bar) {
() => foo; () => foo;
@ -12,7 +14,7 @@ for (var _ref of {}) {
var { var {
foo foo
} = _ref, } = _ref,
bar = babelHelpers.objectWithoutPropertiesLoose(_ref, ["foo"]); bar = babelHelpers.objectWithoutPropertiesLoose(_ref, _excluded);
_loop(foo, bar); _loop(foo, bar);
} }

View File

@ -228,6 +228,18 @@ export default declare((api, options) => {
t.memberExpression(keyExpression, t.identifier("map")), t.memberExpression(keyExpression, t.identifier("map")),
[this.addHelper("toPropertyKey")], [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( value = t.callExpression(

View File

@ -1,3 +1,4 @@
var _excluded = ["x"];
var z = {}; var z = {};
var _z = z, var _z = z,
x = Object.assign({}, _z); x = Object.assign({}, _z);
@ -10,7 +11,7 @@ var _z3 = z,
(function (_ref) { (function (_ref) {
var x = _ref.x, var x = _ref.x,
y = babelHelpers.objectWithoutProperties(_ref, ["x"]); y = babelHelpers.objectWithoutProperties(_ref, _excluded);
}); });
var _o = o; var _o = o;

View File

@ -1,3 +1,4 @@
const _excluded = ["x"];
var z = {}; var z = {};
var _z = z, var _z = z,
x = babelHelpers.extends({}, _z); x = babelHelpers.extends({}, _z);
@ -10,7 +11,7 @@ var _z3 = z,
(function (_ref) { (function (_ref) {
let x = _ref.x, let x = _ref.x,
y = babelHelpers.objectWithoutPropertiesLoose(_ref, ["x"]); y = babelHelpers.objectWithoutPropertiesLoose(_ref, _excluded);
}); });
var _o = o; var _o = o;

View File

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

View File

@ -1,10 +1,12 @@
const _excluded = ["text", "className", "id"];
function render(_ref) { function render(_ref) {
var _Component; var _Component;
let text = _ref.text, let text = _ref.text,
className = _ref.className, className = _ref.className,
id = _ref.id, id = _ref.id,
props = babelHelpers.objectWithoutProperties(_ref, ["text", "className", "id"]); props = babelHelpers.objectWithoutProperties(_ref, _excluded);
// intentionally ignoring props // intentionally ignoring props
return () => _Component || (_Component = <Component text={text} className={className} id={id} />); return () => _Component || (_Component = <Component text={text} className={className} id={id} />);
} }

View File

@ -1,7 +1,9 @@
const _excluded = ["text", "className", "id"];
function render(_ref) { function render(_ref) {
let text = _ref.text, let text = _ref.text,
className = _ref.className, className = _ref.className,
id = _ref.id, id = _ref.id,
props = babelHelpers.objectWithoutProperties(_ref, ["text", "className", "id"]); props = babelHelpers.objectWithoutProperties(_ref, _excluded);
return () => <Component text={text} className={className} id={id} {...props} />; return () => <Component text={text} className={className} id={id} {...props} />;
} }

View File

@ -1,9 +1,11 @@
const _excluded = ["outsetArrows"];
function Foo(_ref) { function Foo(_ref) {
var _div; var _div;
let { let {
outsetArrows outsetArrows
} = _ref, } = _ref,
rest = babelHelpers.objectWithoutProperties(_ref, ["outsetArrows"]); rest = babelHelpers.objectWithoutProperties(_ref, _excluded);
return useMemo(() => _div || (_div = <div outsetArrows={outsetArrows} />), [outsetArrows]); return useMemo(() => _div || (_div = <div outsetArrows={outsetArrows} />), [outsetArrows]);
} }

View File

@ -1,7 +1,9 @@
"use strict"; "use strict";
var _excluded = ["a"];
function F(_ref) { function F(_ref) {
var a = _ref.a, var a = _ref.a,
O = babelHelpers.objectWithoutProperties(_ref, ["a"]); O = babelHelpers.objectWithoutProperties(_ref, _excluded);
var b = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : O; var b = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : O;
} }