add support for replacement of for inits with statements - fixes #1217

This commit is contained in:
Sebastian McKenzie 2015-04-11 17:40:12 -07:00
parent 72098036b2
commit cf5d2429b4
2 changed files with 13 additions and 19 deletions

View File

@ -36,14 +36,8 @@ var hoistVariablesVisitor = {
// for (var i in test)
// for (var i = 0;;)
if (t.isFor(parent)) {
if (parent.left === node) {
return node.declarations[0].id;
}
if (parent.init === node) {
return nodes;
}
if (t.isFor(parent) && parent.left === node) {
return node.declarations[0].id;
}
return nodes;

View File

@ -91,6 +91,9 @@ export default class TraversalPath {
if (this.parentPath.isExpressionStatement() || this.parentPath.isLabeledStatement()) {
return this.parentPath.insertBefore(nodes);
} else if (this.isPreviousType("Expression") || (this.parentPath.isForStatement() && this.key === "init")) {
if (this.node) nodes.push(this.node);
this.replaceExpressionWithStatements(nodes);
} else if (this.isPreviousType("Statement")) {
this._maybePopFromStatements(nodes);
if (Array.isArray(this.container)) {
@ -101,15 +104,11 @@ export default class TraversalPath {
} else {
throw new Error("We don't know what to do with this node type. We were previously a Statement but we can't fit in here?");
}
} else if (this.isPreviousType("Expression")) {
if (this.node) nodes.push(this.node);
this.replaceExpressionWithStatements(nodes);
} else {
throw new Error("No clue what to do with this node type.");
}
}
_containerInsert(from, nodes) {
this.updateSiblingKeys(from, nodes.length);
@ -152,6 +151,13 @@ export default class TraversalPath {
if (this.parentPath.isExpressionStatement() || this.parentPath.isLabeledStatement()) {
return this.parentPath.insertAfter(nodes);
} else if (this.isPreviousType("Expression") || (this.parentPath.isForStatement() && this.key === "init")) {
if (this.node) {
var temp = this.scope.generateTemp();
nodes.unshift(t.expressionStatement(t.assignmentExpression("=", temp, this.node)));
nodes.push(t.expressionStatement(temp));
}
this.replaceExpressionWithStatements(nodes);
} else if (this.isPreviousType("Statement")) {
this._maybePopFromStatements(nodes);
if (Array.isArray(this.container)) {
@ -159,16 +165,10 @@ export default class TraversalPath {
} else if (this.isStatementOrBlock()) {
if (this.node) nodes.unshift(this.node);
this.container[this.key] = t.blockStatement(nodes);
this.replaceExpressionWithStatements(nodes);
} else {
throw new Error("We don't know what to do with this node type. We were previously a Statement but we can't fit in here?");
}
} else if (this.isPreviousType("Expression")) {
if (this.node) {
var temp = this.scope.generateTemp();
nodes.unshift(t.expressionStatement(t.assignmentExpression("=", temp, this.node)));
nodes.push(t.expressionStatement(temp));
}
this.replaceExpressionWithStatements(nodes);
} else {
throw new Error("No clue what to do with this node type.");
}