Fix incorrect destructuring in for loop let initialization (#5763)

* Fix incorrect destructuring in for loop `let` initialization

* Improve approach and fix tests
This commit is contained in:
Buu Nguyen
2017-05-31 12:29:50 -07:00
committed by Henry Zhu
parent 63b7137dac
commit f58f4ac351
7 changed files with 24 additions and 22 deletions

View File

@@ -459,6 +459,7 @@ export default function ({ types: t }) {
if (!parent || !path.container) return; // i don't know why this is necessary - TODO
if (!variableDeclarationHasPattern(node)) return;
const nodeKind = node.kind;
const nodes = [];
let declar;
@@ -493,13 +494,12 @@ export default function ({ types: t }) {
const nodesOut = [];
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.
if (tail && t.isVariableDeclaration(tail) && t.isVariableDeclaration(node)) {
// Create a single compound declarations
tail.declarations.push(...node.declarations);
} else {
// Make sure the original node kind is used for each compound declaration
node.kind = nodeKind;
nodesOut.push(node);
}
}