Favour extends helper over objectWithoutProperties when whole object gets copied anyway (#7390)

This commit is contained in:
Mateusz Burzyński 2018-03-14 22:59:02 +01:00 committed by GitHub
parent 07ab02f6b2
commit d682e32529
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 142 additions and 37 deletions

View File

@ -71,8 +71,7 @@ For detailed information please check out [Spread VS. Object.assign](http://2ali
`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

View File

@ -11,6 +11,12 @@ export default declare((api, opts) => {
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) {
let foundRestElement = false;
visitRestElements(path, () => {
@ -97,6 +103,17 @@ export default declare((api, opts) => {
const impureComputedPropertyDeclarators = replaceImpureComputedKeys(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;
if (!allLiteral) {
// map to toPropertyKey to handle the possible non-string values
@ -396,9 +413,7 @@ export default declare((api, opts) => {
let helper;
if (loose) {
helper = useBuiltIns
? t.memberExpression(t.identifier("Object"), t.identifier("assign"))
: file.addHelper("extends");
helper = getExtendsHelper(file);
} else {
helper = file.addHelper("objectSpread");
}

View File

@ -1,5 +1,5 @@
try {} catch (_ref) {
let a34 = babelHelpers.objectWithoutProperties(_ref, []);
let a34 = babelHelpers.extends({}, _ref);
}
try {} catch (_ref2) {

View File

@ -3,5 +3,5 @@ it("es7.objectRestSpread", () => {
a: 1,
b: 2
};
let copy = babelHelpers.objectWithoutProperties(original, []);
let copy = babelHelpers.extends({}, original);
});

View File

@ -2,8 +2,8 @@ var _ref2;
const {
[(_ref) => {
let rest = babelHelpers.objectWithoutProperties(_ref, []);
let b = babelHelpers.objectWithoutProperties({}, []);
let rest = babelHelpers.extends({}, _ref);
let b = babelHelpers.extends({}, {});
}]: a,
[(_ref2 = {}, ({} = _ref2), d = babelHelpers.objectWithoutProperties(_ref2, []), _ref2)]: c
[(_ref2 = {}, ({} = _ref2), d = babelHelpers.extends({}, _ref2), _ref2)]: c
} = {};

View File

@ -2,8 +2,8 @@ var _ref2;
const {
a = (_ref) => {
let rest = babelHelpers.objectWithoutProperties(_ref, []);
let b = babelHelpers.objectWithoutProperties({}, []);
let rest = babelHelpers.extends({}, _ref);
let b = babelHelpers.extends({}, {});
},
c = (_ref2 = {}, ({} = _ref2), d = babelHelpers.objectWithoutProperties(_ref2, []), _ref2)
c = (_ref2 = {}, ({} = _ref2), d = babelHelpers.extends({}, _ref2), _ref2)
} = {};

View File

@ -1,3 +1,3 @@
const bar = babelHelpers.objectWithoutProperties(obj.a, []),
baz = babelHelpers.objectWithoutProperties(obj.b, []),
foo = babelHelpers.objectWithoutProperties(obj, []);
const bar = babelHelpers.extends({}, obj.a),
baz = babelHelpers.extends({}, obj.b),
foo = babelHelpers.extends({}, obj);

View File

@ -1,5 +1,5 @@
function a(_ref) {
let a34 = babelHelpers.objectWithoutProperties(_ref, []);
let a34 = babelHelpers.extends({}, _ref);
}
function a2(_ref2) {
@ -57,7 +57,7 @@ function a7(_ref8 = {}) {
}
function a8([_ref9]) {
let a1 = babelHelpers.objectWithoutProperties(_ref9, []);
let a1 = babelHelpers.extends({}, _ref9);
}
function a9([_ref10]) {
@ -68,7 +68,7 @@ function a9([_ref10]) {
}
function a10([a1, _ref11]) {
let a2 = babelHelpers.objectWithoutProperties(_ref11, []);
let a2 = babelHelpers.extends({}, _ref11);
} // Unchanged

View File

@ -1,10 +1,10 @@
var z = {};
var x = babelHelpers.objectWithoutProperties(z, []);
var a = babelHelpers.objectWithoutProperties({
var x = babelHelpers.extends({}, z);
var a = babelHelpers.extends({}, {
a: 1
}, []);
var x = babelHelpers.objectWithoutProperties(a.b, []);
var x = babelHelpers.objectWithoutProperties(a(), []);
});
var x = babelHelpers.extends({}, a.b);
var x = babelHelpers.extends({}, a());
var {
x1
} = z,
@ -36,7 +36,7 @@ let {
}
} = complex,
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"]);
let {} = z,
y4 = babelHelpers.objectWithoutProperties(z.x4, []);
y4 = babelHelpers.extends({}, z.x4);

View File

@ -5,6 +5,6 @@ var _props = babelHelpers.interopRequireDefault(require("props"));
console.log(_props.default);
(function () {
const props = babelHelpers.objectWithoutProperties(this.props, []);
const props = babelHelpers.extends({}, this.props);
console.log(props);
})();

View File

@ -3,7 +3,7 @@ const {
} = a,
y = babelHelpers.objectWithoutProperties(a, ["x"]),
z = foo(y);
const s = babelHelpers.objectWithoutProperties(r, []),
const s = babelHelpers.extends({}, r),
t = foo(s); // ordering is preserved
var l = foo(),

View File

@ -7,7 +7,7 @@ exports.default = void 0;
class _default {
method(_ref) {
let object = babelHelpers.objectWithoutProperties(_ref, []);
let object = Object.assign({}, _ref);
}
}

View File

@ -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);
```

View File

@ -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));
}

View File

@ -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);

View File

@ -0,0 +1,11 @@
{
"plugins": [
"external-helpers",
["transform-destructuring", { "useBuiltIns": true }],
"transform-spread",
"transform-parameters",
"transform-block-scoping",
"proposal-object-rest-spread",
"transform-regenerator"
]
}

View File

@ -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;

View File

@ -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"]);