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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user