From 9dd65c809f884d320355ebbe6163a1b43454f2a3 Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Thu, 3 Aug 2017 22:27:30 -0500 Subject: [PATCH] fixes --- .../src/index.js | 2 +- .../src/params.js | 50 ++++++++++++++++++- .../parameters/rest-nested-5656/expected.js | 2 +- .../object-rest/parameters/expected.js | 12 ++--- 4 files changed, 57 insertions(+), 9 deletions(-) diff --git a/packages/babel-plugin-transform-es2015-parameters/src/index.js b/packages/babel-plugin-transform-es2015-parameters/src/index.js index 267c5f13b6..af12fa0399 100644 --- a/packages/babel-plugin-transform-es2015-parameters/src/index.js +++ b/packages/babel-plugin-transform-es2015-parameters/src/index.js @@ -16,7 +16,7 @@ export default function() { } const convertedRest = convertFunctionRest(path); - const convertedParams = convertFunctionParams(path); + const convertedParams = convertFunctionParams(path, this.opts.loose); if (convertedRest || convertedParams) { // Manually reprocess this scope to ensure that the moved params are updated. diff --git a/packages/babel-plugin-transform-es2015-parameters/src/params.js b/packages/babel-plugin-transform-es2015-parameters/src/params.js index de87c35d8f..73215cbd09 100644 --- a/packages/babel-plugin-transform-es2015-parameters/src/params.js +++ b/packages/babel-plugin-transform-es2015-parameters/src/params.js @@ -10,6 +10,16 @@ const buildDefaultParam = template(` DEFAULT_VALUE; `); +const buildLooseDefaultParam = template(` + if (ASSIGNMENT_IDENTIFIER === UNDEFINED) { + ASSIGNMENT_IDENTIFIER = DEFAULT_VALUE; + } +`); + +const buildLooseDestructuredDefaultParam = template(` + let ASSIGNMENT_IDENTIFIER = PARAMETER_NAME === UNDEFINED ? DEFAULT_VALUE : PARAMETER_NAME ; +`); + const buildArgumentsAccess = template(` let $0 = arguments[$1]; `); @@ -35,7 +45,7 @@ const iifeVisitor = { }, }; -export default function convertFunctionParams(path) { +export default function convertFunctionParams(path, loose) { const { node, scope } = path; const state = { @@ -49,6 +59,44 @@ export default function convertFunctionParams(path) { // const params = path.get("params"); + + if (loose) { + const body = []; + for (let i = 0; i < params.length; ++i) { + const param = params[i]; + if (param.isAssignmentPattern()) { + const left = param.get("left"); + const right = param.get("right"); + + const undefinedNode = scope.buildUndefinedNode(); + + if (left.isIdentifier()) { + body.push( + buildLooseDefaultParam({ + ASSIGNMENT_IDENTIFIER: left.node, + DEFAULT_VALUE: right.node, + UNDEFINED: undefinedNode, + }), + ); + param.replaceWith(left.node); + } else if (left.isObjectPattern() || left.isArrayPattern()) { + const paramName = scope.generateUidIdentifier(); + body.push( + buildLooseDestructuredDefaultParam({ + ASSIGNMENT_IDENTIFIER: left.node, + DEFAULT_VALUE: right.node, + PARAMETER_NAME: paramName, + UNDEFINED: undefinedNode, + }), + ); + param.replaceWith(paramName); + } + } + } + path.get("body").unshiftContainer("body", body); + return; + } + for (let i = 0; i < params.length; i++) { const param = params[i]; diff --git a/packages/babel-plugin-transform-es2015-parameters/test/fixtures/parameters/rest-nested-5656/expected.js b/packages/babel-plugin-transform-es2015-parameters/test/fixtures/parameters/rest-nested-5656/expected.js index c0d587ed6e..1a76699de9 100644 --- a/packages/babel-plugin-transform-es2015-parameters/test/fixtures/parameters/rest-nested-5656/expected.js +++ b/packages/babel-plugin-transform-es2015-parameters/test/fixtures/parameters/rest-nested-5656/expected.js @@ -28,7 +28,7 @@ function d(thing) { }; { - var _args = thing; + var args = thing; foo(thing); } } 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 a24b010661..39cbd5c047 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 @@ -17,15 +17,15 @@ function a3(_ref3) { c2 = babelHelpers.objectWithoutProperties(_ref3, ["a2", "b2"]); } -function a4(_ref4, _ref5) { - let { - a5 - } = _ref5, - c5 = babelHelpers.objectWithoutProperties(_ref5, ["a5"]); +function a4(_ref5, _ref4) { let { a3 + } = _ref5, + c3 = babelHelpers.objectWithoutProperties(_ref5, ["a3"]); + let { + a5 } = _ref4, - c3 = babelHelpers.objectWithoutProperties(_ref4, ["a3"]); + c5 = babelHelpers.objectWithoutProperties(_ref4, ["a5"]); } function a5(_ref6) {