Ensure destructuring's computed key handling matches object-rest-spread (#8793)

This commit is contained in:
Brian Ng
2018-10-02 20:46:08 -05:00
committed by GitHub
parent 2575312d1f
commit 36d12b5969
8 changed files with 43 additions and 9 deletions

View File

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