From 9a6890c92f2ebeadac9c204baa772091a320b110 Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Mon, 18 Jul 2016 11:03:59 -0400 Subject: [PATCH] Default parameters cleanup (#3574) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Remove buildDefaultParamAssign This seems to have been [added](https://github.com/babel/babel/commit/4b85b05839017ab2abc03ba2d1 e875a63f002890) in v5.5.4 to address [#1690](https://phabricator.babeljs.io/T1690). It [became “dead”](https://github.com/babel/babel/blob/v6.0.0/packages/babel-core/t est/fixtures/transformation/es6.parameters/default-before-last/expected. js) (as far as I can tell) in the [v6.0.0 commit](https://github.com/babel/babel/commit/ae7d5367f1c3d438667242d692 5db024f875fccd). Either way, this code is never executed. - `pushDefNode` is only called when the param is a default param. - `buildDefaultParamAssign` (which is inside `pushDefNode`) only runs of the param index is less than or equals to the lastNonDefaultParam. AKA, is this param before any non-defaulting params. Which is a contradiction. * Small optimization No need to check if we need an iife if we already know we need one. --- .../src/default.js | 33 +++++-------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/packages/babel-plugin-transform-es2015-parameters/src/default.js b/packages/babel-plugin-transform-es2015-parameters/src/default.js index 05f1f328e1..fefa7461d8 100644 --- a/packages/babel-plugin-transform-es2015-parameters/src/default.js +++ b/packages/babel-plugin-transform-es2015-parameters/src/default.js @@ -13,10 +13,6 @@ let buildDefaultParam = template(` ARGUMENTS[ARGUMENT_KEY]; `); -let buildDefaultParamAssign = template(` - if (VARIABLE_NAME === undefined) VARIABLE_NAME = DEFAULT_VALUE; -`); - let buildCutOff = template(` let $0 = $1[$2]; `); @@ -64,29 +60,16 @@ export let visitor = { // push a default parameter definition function pushDefNode(left, right, i) { - let defNode; - if (exceedsLastNonDefault(i) || t.isPattern(left)) { - defNode = buildDefaultParam({ - VARIABLE_NAME: left, - DEFAULT_VALUE: right, - ARGUMENT_KEY: t.numericLiteral(i), - ARGUMENTS: argsIdentifier - }); - } else { - defNode = buildDefaultParamAssign({ - VARIABLE_NAME: left, - DEFAULT_VALUE: right - }); - } + const defNode = buildDefaultParam({ + VARIABLE_NAME: left, + DEFAULT_VALUE: right, + ARGUMENT_KEY: t.numericLiteral(i), + ARGUMENTS: argsIdentifier + }); defNode._blockHoist = node.params.length - i; body.push(defNode); } - // check if an index exceeds the functions arity - function exceedsLastNonDefault(i) { - return i + 1 > lastNonDefaultParam; - } - // let lastNonDefaultParam = getFunctionArity(node); @@ -96,7 +79,7 @@ export let visitor = { let param = params[i]; if (!param.isAssignmentPattern()) { - if (!param.isIdentifier()) { + if (!state.iife && !param.isIdentifier()) { param.traverse(iifeVisitor, state); } @@ -107,7 +90,7 @@ export let visitor = { let right = param.get("right"); // - if (exceedsLastNonDefault(i) || left.isPattern()) { + if (i >= lastNonDefaultParam || left.isPattern()) { let placeholder = scope.generateUidIdentifier("x"); placeholder._isDefaultPlaceholder = true; node.params[i] = placeholder;