Favour extends helper over objectWithoutProperties when whole object gets copied anyway (#7390)
This commit is contained in:
parent
07ab02f6b2
commit
d682e32529
@ -71,8 +71,7 @@ For detailed information please check out [Spread VS. Object.assign](http://2ali
|
|||||||
|
|
||||||
`boolean`, defaults to `false`.
|
`boolean`, defaults to `false`.
|
||||||
|
|
||||||
Enabling this option will use `Object.assign` directly instead of the Babel's `extends` helper. Keep in mind that this flag only applies when `loose: true`.
|
Enabling this option will use `Object.assign` directly instead of the Babel's `extends` helper.
|
||||||
|
|
||||||
|
|
||||||
##### Example
|
##### Example
|
||||||
|
|
||||||
|
|||||||
@ -11,6 +11,12 @@ export default declare((api, opts) => {
|
|||||||
throw new Error(".loose must be a boolean, or undefined");
|
throw new Error(".loose must be a boolean, or undefined");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getExtendsHelper(file) {
|
||||||
|
return useBuiltIns
|
||||||
|
? t.memberExpression(t.identifier("Object"), t.identifier("assign"))
|
||||||
|
: file.addHelper("extends");
|
||||||
|
}
|
||||||
|
|
||||||
function hasRestElement(path) {
|
function hasRestElement(path) {
|
||||||
let foundRestElement = false;
|
let foundRestElement = false;
|
||||||
visitRestElements(path, () => {
|
visitRestElements(path, () => {
|
||||||
@ -97,6 +103,17 @@ export default declare((api, opts) => {
|
|||||||
const impureComputedPropertyDeclarators = replaceImpureComputedKeys(path);
|
const impureComputedPropertyDeclarators = replaceImpureComputedKeys(path);
|
||||||
const { keys, allLiteral } = extractNormalizedKeys(path);
|
const { keys, allLiteral } = extractNormalizedKeys(path);
|
||||||
|
|
||||||
|
if (keys.length === 0) {
|
||||||
|
return [
|
||||||
|
impureComputedPropertyDeclarators,
|
||||||
|
restElement.argument,
|
||||||
|
t.callExpression(getExtendsHelper(file), [
|
||||||
|
t.objectExpression([]),
|
||||||
|
t.cloneNode(objRef),
|
||||||
|
]),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
let keyExpression;
|
let keyExpression;
|
||||||
if (!allLiteral) {
|
if (!allLiteral) {
|
||||||
// map to toPropertyKey to handle the possible non-string values
|
// map to toPropertyKey to handle the possible non-string values
|
||||||
@ -396,9 +413,7 @@ export default declare((api, opts) => {
|
|||||||
|
|
||||||
let helper;
|
let helper;
|
||||||
if (loose) {
|
if (loose) {
|
||||||
helper = useBuiltIns
|
helper = getExtendsHelper(file);
|
||||||
? t.memberExpression(t.identifier("Object"), t.identifier("assign"))
|
|
||||||
: file.addHelper("extends");
|
|
||||||
} else {
|
} else {
|
||||||
helper = file.addHelper("objectSpread");
|
helper = file.addHelper("objectSpread");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
try {} catch (_ref) {
|
try {} catch (_ref) {
|
||||||
let a34 = babelHelpers.objectWithoutProperties(_ref, []);
|
let a34 = babelHelpers.extends({}, _ref);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {} catch (_ref2) {
|
try {} catch (_ref2) {
|
||||||
|
|||||||
@ -3,5 +3,5 @@ it("es7.objectRestSpread", () => {
|
|||||||
a: 1,
|
a: 1,
|
||||||
b: 2
|
b: 2
|
||||||
};
|
};
|
||||||
let copy = babelHelpers.objectWithoutProperties(original, []);
|
let copy = babelHelpers.extends({}, original);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -2,8 +2,8 @@ var _ref2;
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
[(_ref) => {
|
[(_ref) => {
|
||||||
let rest = babelHelpers.objectWithoutProperties(_ref, []);
|
let rest = babelHelpers.extends({}, _ref);
|
||||||
let b = babelHelpers.objectWithoutProperties({}, []);
|
let b = babelHelpers.extends({}, {});
|
||||||
}]: a,
|
}]: a,
|
||||||
[(_ref2 = {}, ({} = _ref2), d = babelHelpers.objectWithoutProperties(_ref2, []), _ref2)]: c
|
[(_ref2 = {}, ({} = _ref2), d = babelHelpers.extends({}, _ref2), _ref2)]: c
|
||||||
} = {};
|
} = {};
|
||||||
|
|||||||
@ -2,8 +2,8 @@ var _ref2;
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
a = (_ref) => {
|
a = (_ref) => {
|
||||||
let rest = babelHelpers.objectWithoutProperties(_ref, []);
|
let rest = babelHelpers.extends({}, _ref);
|
||||||
let b = babelHelpers.objectWithoutProperties({}, []);
|
let b = babelHelpers.extends({}, {});
|
||||||
},
|
},
|
||||||
c = (_ref2 = {}, ({} = _ref2), d = babelHelpers.objectWithoutProperties(_ref2, []), _ref2)
|
c = (_ref2 = {}, ({} = _ref2), d = babelHelpers.extends({}, _ref2), _ref2)
|
||||||
} = {};
|
} = {};
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
const bar = babelHelpers.objectWithoutProperties(obj.a, []),
|
const bar = babelHelpers.extends({}, obj.a),
|
||||||
baz = babelHelpers.objectWithoutProperties(obj.b, []),
|
baz = babelHelpers.extends({}, obj.b),
|
||||||
foo = babelHelpers.objectWithoutProperties(obj, []);
|
foo = babelHelpers.extends({}, obj);
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
function a(_ref) {
|
function a(_ref) {
|
||||||
let a34 = babelHelpers.objectWithoutProperties(_ref, []);
|
let a34 = babelHelpers.extends({}, _ref);
|
||||||
}
|
}
|
||||||
|
|
||||||
function a2(_ref2) {
|
function a2(_ref2) {
|
||||||
@ -57,7 +57,7 @@ function a7(_ref8 = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function a8([_ref9]) {
|
function a8([_ref9]) {
|
||||||
let a1 = babelHelpers.objectWithoutProperties(_ref9, []);
|
let a1 = babelHelpers.extends({}, _ref9);
|
||||||
}
|
}
|
||||||
|
|
||||||
function a9([_ref10]) {
|
function a9([_ref10]) {
|
||||||
@ -68,7 +68,7 @@ function a9([_ref10]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function a10([a1, _ref11]) {
|
function a10([a1, _ref11]) {
|
||||||
let a2 = babelHelpers.objectWithoutProperties(_ref11, []);
|
let a2 = babelHelpers.extends({}, _ref11);
|
||||||
} // Unchanged
|
} // Unchanged
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
var z = {};
|
var z = {};
|
||||||
var x = babelHelpers.objectWithoutProperties(z, []);
|
var x = babelHelpers.extends({}, z);
|
||||||
var a = babelHelpers.objectWithoutProperties({
|
var a = babelHelpers.extends({}, {
|
||||||
a: 1
|
a: 1
|
||||||
}, []);
|
});
|
||||||
var x = babelHelpers.objectWithoutProperties(a.b, []);
|
var x = babelHelpers.extends({}, a.b);
|
||||||
var x = babelHelpers.objectWithoutProperties(a(), []);
|
var x = babelHelpers.extends({}, a());
|
||||||
var {
|
var {
|
||||||
x1
|
x1
|
||||||
} = z,
|
} = z,
|
||||||
@ -36,7 +36,7 @@ let {
|
|||||||
}
|
}
|
||||||
} = complex,
|
} = complex,
|
||||||
asdf = babelHelpers.objectWithoutProperties(complex.x, ["a", d].map(babelHelpers.toPropertyKey)),
|
asdf = babelHelpers.objectWithoutProperties(complex.x, ["a", d].map(babelHelpers.toPropertyKey)),
|
||||||
d = babelHelpers.objectWithoutProperties(complex.y, []),
|
d = babelHelpers.extends({}, complex.y),
|
||||||
g = babelHelpers.objectWithoutProperties(complex, ["x"]);
|
g = babelHelpers.objectWithoutProperties(complex, ["x"]);
|
||||||
let {} = z,
|
let {} = z,
|
||||||
y4 = babelHelpers.objectWithoutProperties(z.x4, []);
|
y4 = babelHelpers.extends({}, z.x4);
|
||||||
|
|||||||
@ -5,6 +5,6 @@ var _props = babelHelpers.interopRequireDefault(require("props"));
|
|||||||
console.log(_props.default);
|
console.log(_props.default);
|
||||||
|
|
||||||
(function () {
|
(function () {
|
||||||
const props = babelHelpers.objectWithoutProperties(this.props, []);
|
const props = babelHelpers.extends({}, this.props);
|
||||||
console.log(props);
|
console.log(props);
|
||||||
})();
|
})();
|
||||||
|
|||||||
@ -3,7 +3,7 @@ const {
|
|||||||
} = a,
|
} = a,
|
||||||
y = babelHelpers.objectWithoutProperties(a, ["x"]),
|
y = babelHelpers.objectWithoutProperties(a, ["x"]),
|
||||||
z = foo(y);
|
z = foo(y);
|
||||||
const s = babelHelpers.objectWithoutProperties(r, []),
|
const s = babelHelpers.extends({}, r),
|
||||||
t = foo(s); // ordering is preserved
|
t = foo(s); // ordering is preserved
|
||||||
|
|
||||||
var l = foo(),
|
var l = foo(),
|
||||||
|
|||||||
@ -7,7 +7,7 @@ exports.default = void 0;
|
|||||||
|
|
||||||
class _default {
|
class _default {
|
||||||
method(_ref) {
|
method(_ref) {
|
||||||
let object = babelHelpers.objectWithoutProperties(_ref, []);
|
let object = Object.assign({}, _ref);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -51,3 +51,42 @@ require("@babel/core").transform("code", {
|
|||||||
plugins: ["@babel/plugin-transform-destructuring"]
|
plugins: ["@babel/plugin-transform-destructuring"]
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Options
|
||||||
|
|
||||||
|
### `loose`
|
||||||
|
|
||||||
|
`boolean`, defaults to `false`.
|
||||||
|
|
||||||
|
Enabling this option will assume that what you want to destructure is an array and won't use `Array.from` on other iterables.
|
||||||
|
|
||||||
|
### `useBuiltIns`
|
||||||
|
|
||||||
|
`boolean`, defaults to `false`.
|
||||||
|
|
||||||
|
Enabling this option will use `Object.assign` directly instead of the Babel's `extends` helper.
|
||||||
|
|
||||||
|
##### Example
|
||||||
|
|
||||||
|
**.babelrc**
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
["@babel/plugin-transform-destructuring", { "useBuiltIns": true }]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**In**
|
||||||
|
|
||||||
|
```js
|
||||||
|
var { ...x } = z;
|
||||||
|
```
|
||||||
|
|
||||||
|
**Out**
|
||||||
|
|
||||||
|
```js
|
||||||
|
var _z = z,
|
||||||
|
x = Object.assign({}, _z);
|
||||||
|
```
|
||||||
|
|||||||
@ -4,13 +4,20 @@ import { types as t } from "@babel/core";
|
|||||||
export default declare((api, options) => {
|
export default declare((api, options) => {
|
||||||
api.assertVersion(7);
|
api.assertVersion(7);
|
||||||
|
|
||||||
const { loose = false } = options;
|
const { loose = false, useBuiltIns = false } = options;
|
||||||
|
|
||||||
if (typeof loose !== "boolean") {
|
if (typeof loose !== "boolean") {
|
||||||
throw new Error(`.loose must be a boolean or undefined`);
|
throw new Error(`.loose must be a boolean or undefined`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const arrayOnlySpread = loose;
|
const arrayOnlySpread = loose;
|
||||||
|
|
||||||
|
function getExtendsHelper(file) {
|
||||||
|
return useBuiltIns
|
||||||
|
? t.memberExpression(t.identifier("Object"), t.identifier("assign"))
|
||||||
|
: file.addHelper("extends");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test if a VariableDeclaration's declarations contains any Patterns.
|
* Test if a VariableDeclaration's declarations contains any Patterns.
|
||||||
*/
|
*/
|
||||||
@ -172,14 +179,21 @@ export default declare((api, options) => {
|
|||||||
keys.push(t.cloneNode(key));
|
keys.push(t.cloneNode(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
keys = t.arrayExpression(keys);
|
let value;
|
||||||
|
if (keys.length === 0) {
|
||||||
|
value = t.callExpression(getExtendsHelper(this), [
|
||||||
|
t.objectExpression([]),
|
||||||
|
t.cloneNode(objRef),
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
keys = t.arrayExpression(keys);
|
||||||
|
|
||||||
//
|
value = t.callExpression(this.addHelper("objectWithoutProperties"), [
|
||||||
|
t.cloneNode(objRef),
|
||||||
|
keys,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
const value = t.callExpression(
|
|
||||||
this.addHelper("objectWithoutProperties"),
|
|
||||||
[t.cloneNode(objRef), keys],
|
|
||||||
);
|
|
||||||
this.nodes.push(this.buildVariableAssignment(spreadProp.argument, value));
|
this.nodes.push(this.buildVariableAssignment(spreadProp.argument, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,7 @@
|
|||||||
|
var z = {};
|
||||||
|
var { ...x } = z;
|
||||||
|
var { x, ...y } = z;
|
||||||
|
var { [x]: x, ...y } = z;
|
||||||
|
(function({ x, ...y }) { });
|
||||||
|
|
||||||
|
({ x, y, ...z } = o);
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
"external-helpers",
|
||||||
|
["transform-destructuring", { "useBuiltIns": true }],
|
||||||
|
"transform-spread",
|
||||||
|
"transform-parameters",
|
||||||
|
"transform-block-scoping",
|
||||||
|
"proposal-object-rest-spread",
|
||||||
|
"transform-regenerator"
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
var z = {};
|
||||||
|
var _z = z,
|
||||||
|
x = Object.assign({}, _z);
|
||||||
|
var _z2 = z,
|
||||||
|
x = _z2.x,
|
||||||
|
y = babelHelpers.objectWithoutProperties(_z2, ["x"]);
|
||||||
|
var _z3 = z,
|
||||||
|
x = _z3[x],
|
||||||
|
y = babelHelpers.objectWithoutProperties(_z3, [x]);
|
||||||
|
|
||||||
|
(function (_ref) {
|
||||||
|
var x = _ref.x,
|
||||||
|
y = babelHelpers.objectWithoutProperties(_ref, ["x"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
var _o = o;
|
||||||
|
x = _o.x;
|
||||||
|
y = _o.y;
|
||||||
|
z = babelHelpers.objectWithoutProperties(_o, ["x", "y"]);
|
||||||
|
_o;
|
||||||
@ -1,6 +1,6 @@
|
|||||||
var z = {};
|
var z = {};
|
||||||
var _z = z,
|
var _z = z,
|
||||||
x = babelHelpers.objectWithoutProperties(_z, []);
|
x = babelHelpers.extends({}, _z);
|
||||||
var _z2 = z,
|
var _z2 = z,
|
||||||
x = _z2.x,
|
x = _z2.x,
|
||||||
y = babelHelpers.objectWithoutProperties(_z2, ["x"]);
|
y = babelHelpers.objectWithoutProperties(_z2, ["x"]);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user