Fix for object-rest with parameters destructuring nested rest (#4883)

This commit is contained in:
Christophe Hurpeau
2016-12-02 05:58:07 +01:00
committed by Henry Zhu
parent 06820ca17d
commit 81575bcdfe
7 changed files with 41 additions and 27 deletions

View File

@@ -1,30 +1,22 @@
export default function ({ types: t }) {
function hasRestProperty(node) {
for (let property of (node.properties)) {
if (t.isRestProperty(property)) {
return true;
function hasRestProperty(path) {
let foundRestProperty = false;
path.traverse({
RestProperty() {
foundRestProperty = true;
}
}
return false;
});
return foundRestProperty;
}
function variableDeclarationHasRestProperty(node) {
for (let declar of (node.declarations)) {
if (t.isObjectPattern(declar.id)) {
return hasRestProperty(declar.id);
function hasSpread(path) {
let foundSpreadProperty = false;
path.traverse({
SpreadProperty() {
foundSpreadProperty = true;
}
}
return false;
}
function hasSpread(node) {
for (let prop of (node.properties)) {
if (t.isSpreadProperty(prop)) {
return true;
}
}
return false;
});
return foundSpreadProperty;
}
function createObjectSpread(file, props, objRef) {
@@ -51,7 +43,7 @@ export default function ({ types: t }) {
}
function replaceRestProperty(paramsPath, i, numParams) {
if (paramsPath.isObjectPattern() && hasRestProperty(paramsPath.node)) {
if (paramsPath.isObjectPattern() && hasRestProperty(paramsPath)) {
let parentPath = paramsPath.parentPath;
let uid = parentPath.scope.generateUidIdentifier("ref");
@@ -132,7 +124,7 @@ export default function ({ types: t }) {
ExportNamedDeclaration(path) {
let declaration = path.get("declaration");
if (!declaration.isVariableDeclaration()) return;
if (!variableDeclarationHasRestProperty(declaration.node)) return;
if (!hasRestProperty(declaration)) return;
let specifiers = [];
@@ -154,7 +146,7 @@ export default function ({ types: t }) {
// ({a, ...b} = c);
AssignmentExpression(path, file) {
let leftPath = path.get("left");
if (leftPath.isObjectPattern() && hasRestProperty(leftPath.node)) {
if (leftPath.isObjectPattern() && hasRestProperty(leftPath)) {
let nodes = [];
let ref;
@@ -191,10 +183,11 @@ export default function ({ types: t }) {
// taken from transform-es2015-destructuring/src/index.js#visitor
ForXStatement(path) {
let { node, scope } = path;
let leftPath = path.get("left");
let left = node.left;
// for ({a, ...b} of []) {}
if (t.isObjectPattern(left) && hasRestProperty(left)) {
if (t.isObjectPattern(left) && hasRestProperty(leftPath)) {
let temp = scope.generateUidIdentifier("ref");
node.left = t.variableDeclaration("var", [
@@ -230,7 +223,7 @@ export default function ({ types: t }) {
},
// var a = { ...b, ...c }
ObjectExpression(path, file) {
if (!hasSpread(path.node)) return;
if (!hasSpread(path)) return;
let useBuiltIns = file.opts.useBuiltIns || false;
if (typeof useBuiltIns !== "boolean") {

View File

@@ -1,6 +1,7 @@
try {} catch({ ...a34 }) {}
try {} catch({a1, ...b1}) {}
try {} catch({a2, b2, ...c2}) {}
try {} catch({a2, b2, c2: { c3, ...c4 }}) {}
// Unchanged
try {} catch(a) {}

View File

@@ -9,6 +9,10 @@ try {} catch (_ref3) {
let { a2, b2 } = _ref3;
let c2 = babelHelpers.objectWithoutProperties(_ref3, ["a2", "b2"]);
}
try {} catch (_ref4) {
let { a2, b2, c2: { c3 } } = _ref4;
let c4 = babelHelpers.objectWithoutProperties(_ref4.c2, ["c3"]);
}
// Unchanged
try {} catch (a) {}

View File

@@ -2,6 +2,8 @@ function a({ ...a34 }) {}
function a2({a1, ...b1}) {}
function a3({a2, b2, ...c2}) {}
function a4({a3, ...c3}, {a5, ...c5}) {}
function a5({a3, b2: { ba1, ...ba2 }, ...c3}) {}
function a6({a3, b2: { ba1, ...ba2 } }) {}
// Unchanged
function b(a) {}
function b2(a, ...b) {}

View File

@@ -15,6 +15,15 @@ function a4(_ref4, _ref5) {
let { a3 } = _ref4;
let c3 = babelHelpers.objectWithoutProperties(_ref4, ["a3"]);
}
function a5(_ref6) {
let { a3, b2: { ba1 } } = _ref6;
let ba2 = babelHelpers.objectWithoutProperties(_ref6.b2, ["ba1"]),
c3 = babelHelpers.objectWithoutProperties(_ref6, ["a3", "b2"]);
}
function a6(_ref7) {
let { a3, b2: { ba1 } } = _ref7;
let ba2 = babelHelpers.objectWithoutProperties(_ref7.b2, ["ba1"]);
}
// Unchanged
function b(a) {}
function b2(a, ...b) {}

View File

@@ -15,3 +15,5 @@ let {
y: { ...d },
...g
} = complex;
let { x4: { ...y4 } } = z;

View File

@@ -19,6 +19,9 @@ const z4 = babelHelpers.objectWithoutProperties(z, ["w3", "x3", "y3"]);
let {
x: { a: xa, [d]: f }
} = complex;
let asdf = babelHelpers.objectWithoutProperties(complex.x, ["a", d]),
d = babelHelpers.objectWithoutProperties(complex.y, []),
g = babelHelpers.objectWithoutProperties(complex, ["x"]);
let {} = z;
let y4 = babelHelpers.objectWithoutProperties(z.x4, []);