From 4a1965511f3bb91ce44f8936fd7324781ebcd3bd Mon Sep 17 00:00:00 2001 From: Christophe Hurpeau Date: Tue, 10 Jan 2017 22:31:43 +0100 Subject: [PATCH] fix: plugin-transform-object-rest-spread param with default value --- .../src/index.js | 19 ++++++++++++------- .../fixtures/object-rest/parameters/actual.js | 1 + .../object-rest/parameters/expected.js | 4 ++++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/babel-plugin-transform-object-rest-spread/src/index.js b/packages/babel-plugin-transform-object-rest-spread/src/index.js index b705529236b4..4e2be6de80d3 100644 --- a/packages/babel-plugin-transform-object-rest-spread/src/index.js +++ b/packages/babel-plugin-transform-object-rest-spread/src/index.js @@ -42,19 +42,23 @@ export default function ({ types: t }) { ]; } - function replaceRestProperty(paramsPath, i, numParams) { - if (paramsPath.isObjectPattern() && hasRestProperty(paramsPath)) { - const parentPath = paramsPath.parentPath; + function replaceRestProperty(parentPath, paramPath, i, numParams) { + if (paramPath.isAssignmentPattern()) { + replaceRestProperty(parentPath, paramPath.get("left"), i, numParams); + return; + } + + if (paramPath.isObjectPattern() && hasRestProperty(paramPath)) { const uid = parentPath.scope.generateUidIdentifier("ref"); const declar = t.variableDeclaration("let", [ - t.variableDeclarator(paramsPath.node, uid) + t.variableDeclarator(paramPath.node, uid) ]); declar._blockHoist = i ? numParams - i : 1; parentPath.ensureBlock(); parentPath.get("body").unshiftContainer("body", declar); - paramsPath.replaceWith(uid); + paramPath.replaceWith(uid); } } @@ -67,7 +71,7 @@ export default function ({ types: t }) { Function(path) { const params = path.get("params"); for (let i = 0; i < params.length; i++) { - replaceRestProperty(params[i], i, params.length); + replaceRestProperty(params[i].parentPath, params[i], i, params.length); } }, // adapted from transform-es2015-destructuring/src/index.js#pushObjectRest @@ -141,7 +145,8 @@ export default function ({ types: t }) { }, // try {} catch ({a, ...b}) {} CatchClause(path) { - replaceRestProperty(path.get("param")); + const paramPath = path.get("param"); + replaceRestProperty(paramPath.parentPath, paramPath); }, // ({a, ...b} = c); AssignmentExpression(path, file) { diff --git a/packages/babel-plugin-transform-object-rest-spread/test/fixtures/object-rest/parameters/actual.js b/packages/babel-plugin-transform-object-rest-spread/test/fixtures/object-rest/parameters/actual.js index a3beccb8a4f5..553131f613e2 100644 --- a/packages/babel-plugin-transform-object-rest-spread/test/fixtures/object-rest/parameters/actual.js +++ b/packages/babel-plugin-transform-object-rest-spread/test/fixtures/object-rest/parameters/actual.js @@ -4,6 +4,7 @@ function a3({a2, b2, ...c2}) {} function a4({a3, ...c3}, {a5, ...c5}) {} function a5({a3, b2: { ba1, ...ba2 }, ...c3}) {} function a6({a3, b2: { ba1, ...ba2 } }) {} +function a7({a1 = 1, ...b1} = {}) {} // Unchanged function b(a) {} function b2(a, ...b) {} diff --git a/packages/babel-plugin-transform-object-rest-spread/test/fixtures/object-rest/parameters/expected.js b/packages/babel-plugin-transform-object-rest-spread/test/fixtures/object-rest/parameters/expected.js index a3ec9786f754..71c7876f1311 100644 --- a/packages/babel-plugin-transform-object-rest-spread/test/fixtures/object-rest/parameters/expected.js +++ b/packages/babel-plugin-transform-object-rest-spread/test/fixtures/object-rest/parameters/expected.js @@ -24,6 +24,10 @@ function a6(_ref7) { let { a3, b2: { ba1 } } = _ref7; let ba2 = babelHelpers.objectWithoutProperties(_ref7.b2, ["ba1"]); } +function a7(_ref8 = {}) { + let { a1 = 1 } = _ref8; + let b1 = babelHelpers.objectWithoutProperties(_ref8, ["a1"]); +} // Unchanged function b(a) {} function b2(a, ...b) {}