Favour extends helper over objectWithoutProperties when whole object gets copied anyway (#7390)
This commit is contained in:
committed by
GitHub
parent
07ab02f6b2
commit
d682e32529
@@ -51,3 +51,42 @@ require("@babel/core").transform("code", {
|
||||
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) => {
|
||||
api.assertVersion(7);
|
||||
|
||||
const { loose = false } = options;
|
||||
const { loose = false, useBuiltIns = false } = options;
|
||||
|
||||
if (typeof loose !== "boolean") {
|
||||
throw new Error(`.loose must be a boolean or undefined`);
|
||||
}
|
||||
|
||||
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.
|
||||
*/
|
||||
@@ -172,14 +179,21 @@ export default declare((api, options) => {
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
@@ -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 = z,
|
||||
x = babelHelpers.objectWithoutProperties(_z, []);
|
||||
x = babelHelpers.extends({}, _z);
|
||||
var _z2 = z,
|
||||
x = _z2.x,
|
||||
y = babelHelpers.objectWithoutProperties(_z2, ["x"]);
|
||||
|
||||
Reference in New Issue
Block a user