Default parameters cleanup (#3574)

* 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.
This commit is contained in:
Justin Ridgewell
2016-07-18 11:03:59 -04:00
committed by Henry Zhu
parent 88eec4b852
commit 9a6890c92f

View File

@@ -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;