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

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