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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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) { pushObjectRest(pattern, objRef, spreadProp, spreadPropIndex) {
// get all the keys that appear in this object before the current spread // 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++) { for (let i = 0; i < pattern.properties.length; i++) {
const prop = pattern.properties[i]; const prop = pattern.properties[i];
@ -182,11 +183,15 @@ export default declare((api, options) => {
// ignore other spread properties // ignore other spread properties
if (t.isRestElement(prop)) continue; if (t.isRestElement(prop)) continue;
let key = prop.key; const key = prop.key;
if (t.isIdentifier(key) && !prop.computed) { 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)); keys.push(t.cloneNode(key));
allLiteral = false;
}
} }
let value; let value;
@ -196,11 +201,18 @@ export default declare((api, options) => {
t.cloneNode(objRef), t.cloneNode(objRef),
]); ]);
} else { } 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( value = t.callExpression(
this.addHelper(`objectWithoutProperties${loose ? "Loose" : ""}`), this.addHelper(`objectWithoutProperties${loose ? "Loose" : ""}`),
[t.cloneNode(objRef), keys], [t.cloneNode(objRef), keyExpression],
); );
} }

View File

@ -6,7 +6,7 @@ var _z2 = z,
y = babelHelpers.objectWithoutProperties(_z2, ["x"]); y = babelHelpers.objectWithoutProperties(_z2, ["x"]);
var _z3 = z, var _z3 = z,
x = _z3[x], x = _z3[x],
y = babelHelpers.objectWithoutProperties(_z3, [x]); y = babelHelpers.objectWithoutProperties(_z3, [x].map(babelHelpers.toPropertyKey));
(function (_ref) { (function (_ref) {
var x = _ref.x, var x = _ref.x,

View File

@ -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 _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); } 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, var _z3 = z,
x = _z3[x], x = _z3[x],
y = _objectWithoutPropertiesLoose(_z3, [x]); y = _objectWithoutPropertiesLoose(_z3, [x].map(_toPropertyKey));
(function (_ref) { (function (_ref) {
let x = _ref.x, let x = _ref.x,

View File

@ -6,7 +6,7 @@ var _z2 = z,
y = babelHelpers.objectWithoutProperties(_z2, ["x"]); y = babelHelpers.objectWithoutProperties(_z2, ["x"]);
var _z3 = z, var _z3 = z,
x = _z3[x], x = _z3[x],
y = babelHelpers.objectWithoutProperties(_z3, [x]); y = babelHelpers.objectWithoutProperties(_z3, [x].map(babelHelpers.toPropertyKey));
(function (_ref) { (function (_ref) {
var x = _ref.x, var x = _ref.x,

View File

@ -0,0 +1,4 @@
const { [(() => 1)()]: a, ...rest } = { 1: "a" };
expect(a).toBe("a");
expect(rest).toEqual({});

View File

@ -0,0 +1,3 @@
{
"plugins": ["transform-destructuring", "proposal-object-rest-spread"]
}

View File

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

View File

@ -0,0 +1,3 @@
{
"plugins": ["transform-destructuring", "proposal-object-rest-spread"]
}