fix binding kind of destructured variables. (#4813)

Fixes #4516 and any other code that hoists into a scope
where function params are destructured.
This commit is contained in:
Samuel Reed
2016-11-08 12:51:54 -06:00
committed by Henry Zhu
parent 723ca0eef8
commit 5678e61c0f
13 changed files with 86 additions and 0 deletions

View File

@@ -496,12 +496,26 @@ export default function ({ types: t }) {
for (const node of nodes) {
const tail = nodesOut[nodesOut.length - 1];
if (tail && t.isVariableDeclaration(tail) && t.isVariableDeclaration(node) && tail.kind === node.kind) {
// Create a single compound let/var rather than many.
tail.declarations.push(...node.declarations);
} else {
nodesOut.push(node);
}
}
// Need to unmark the current binding to this var as a param, or other hoists
// could be placed above this ref.
// https://github.com/babel/babel/issues/4516
for (const nodeOut of nodesOut) {
if (!nodeOut.declarations) continue;
for (const declaration of nodeOut.declarations) {
const {name} = declaration.id;
if (scope.bindings[name]) {
scope.bindings[name].kind = nodeOut.kind;
}
}
}
if (nodesOut.length === 1) {
path.replaceWith(nodesOut[0]);
} else {