Ensure destructuring's computed key handling matches object-rest-spread (#8793)
This commit is contained in:
parent
2575312d1f
commit
36d12b5969
@ -170,7 +170,8 @@ export default declare((api, options) => {
|
||||
pushObjectRest(pattern, objRef, spreadProp, spreadPropIndex) {
|
||||
// get all the keys that appear in this object before the current spread
|
||||
|
||||
let keys = [];
|
||||
const keys = [];
|
||||
let allLiteral = true;
|
||||
|
||||
for (let i = 0; i < pattern.properties.length; i++) {
|
||||
const prop = pattern.properties[i];
|
||||
@ -182,11 +183,15 @@ export default declare((api, options) => {
|
||||
// ignore other spread properties
|
||||
if (t.isRestElement(prop)) continue;
|
||||
|
||||
let key = prop.key;
|
||||
const key = prop.key;
|
||||
if (t.isIdentifier(key) && !prop.computed) {
|
||||
key = t.stringLiteral(prop.key.name);
|
||||
keys.push(t.stringLiteral(key.name));
|
||||
} else if (t.isLiteral(key)) {
|
||||
keys.push(t.stringLiteral(String(key.value)));
|
||||
} else {
|
||||
keys.push(t.cloneNode(key));
|
||||
allLiteral = false;
|
||||
}
|
||||
keys.push(t.cloneNode(key));
|
||||
}
|
||||
|
||||
let value;
|
||||
@ -196,11 +201,18 @@ export default declare((api, options) => {
|
||||
t.cloneNode(objRef),
|
||||
]);
|
||||
} else {
|
||||
keys = t.arrayExpression(keys);
|
||||
let keyExpression = t.arrayExpression(keys);
|
||||
|
||||
if (!allLiteral) {
|
||||
keyExpression = t.callExpression(
|
||||
t.memberExpression(keyExpression, t.identifier("map")),
|
||||
[this.addHelper("toPropertyKey")],
|
||||
);
|
||||
}
|
||||
|
||||
value = t.callExpression(
|
||||
this.addHelper(`objectWithoutProperties${loose ? "Loose" : ""}`),
|
||||
[t.cloneNode(objRef), keys],
|
||||
[t.cloneNode(objRef), keyExpression],
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ var _z2 = z,
|
||||
y = babelHelpers.objectWithoutProperties(_z2, ["x"]);
|
||||
var _z3 = z,
|
||||
x = _z3[x],
|
||||
y = babelHelpers.objectWithoutProperties(_z3, [x]);
|
||||
y = babelHelpers.objectWithoutProperties(_z3, [x].map(babelHelpers.toPropertyKey));
|
||||
|
||||
(function (_ref) {
|
||||
var x = _ref.x,
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
function _toPropertyKey(key) { if (typeof key === "symbol") { return key; } else { return String(key); } }
|
||||
|
||||
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
||||
|
||||
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
||||
@ -13,7 +15,7 @@ var _z2 = z,
|
||||
|
||||
var _z3 = z,
|
||||
x = _z3[x],
|
||||
y = _objectWithoutPropertiesLoose(_z3, [x]);
|
||||
y = _objectWithoutPropertiesLoose(_z3, [x].map(_toPropertyKey));
|
||||
|
||||
(function (_ref) {
|
||||
let x = _ref.x,
|
||||
|
||||
@ -6,7 +6,7 @@ var _z2 = z,
|
||||
y = babelHelpers.objectWithoutProperties(_z2, ["x"]);
|
||||
var _z3 = z,
|
||||
x = _z3[x],
|
||||
y = babelHelpers.objectWithoutProperties(_z3, [x]);
|
||||
y = babelHelpers.objectWithoutProperties(_z3, [x].map(babelHelpers.toPropertyKey));
|
||||
|
||||
(function (_ref) {
|
||||
var x = _ref.x,
|
||||
|
||||
@ -0,0 +1,4 @@
|
||||
const { [(() => 1)()]: a, ...rest } = { 1: "a" };
|
||||
|
||||
expect(a).toBe("a");
|
||||
expect(rest).toEqual({});
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["transform-destructuring", "proposal-object-rest-spread"]
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
const foo = {
|
||||
1: "a",
|
||||
2: "b",
|
||||
3: "c",
|
||||
};
|
||||
|
||||
const { [1]: bar, ...rest } = foo;
|
||||
|
||||
expect(bar).toBe("a");
|
||||
expect(rest).toEqual({ 2: "b", 3: "c" });
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["transform-destructuring", "proposal-object-rest-spread"]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user