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

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