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
18 changed files with 142 additions and 37 deletions

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