normalise Program replacement nodes

This commit is contained in:
Sebastian McKenzie 2015-05-03 23:35:39 +01:00
parent 102cbbe493
commit 0717eaddce
2 changed files with 16 additions and 3 deletions

View File

@ -9,6 +9,6 @@ export var Program = {
enter(ast) { enter(ast) {
regenerator.transform(ast); regenerator.transform(ast);
this.stop(); this.stop();
return ast; // force a checkPath, this really needs to be optimised this.checkSelf();
} }
}; };

View File

@ -118,7 +118,7 @@ export default class TraversalPath {
} else if (this.isStatementOrBlock()) { } else if (this.isStatementOrBlock()) {
if (this.node) nodes.push(this.node); if (this.node) nodes.push(this.node);
this.container[this.key] = t.blockStatement(nodes); this.container[this.key] = t.blockStatement(nodes);
this.checkPaths(this); this.checkSelf();
} else { } 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?"); 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?");
} }
@ -216,7 +216,7 @@ export default class TraversalPath {
} else if (this.isStatementOrBlock()) { } else if (this.isStatementOrBlock()) {
if (this.node) nodes.unshift(this.node); if (this.node) nodes.unshift(this.node);
this.container[this.key] = t.blockStatement(nodes); this.container[this.key] = t.blockStatement(nodes);
this.checkPaths(this); this.checkSelf();
} else { } 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?"); 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?");
} }
@ -509,6 +509,11 @@ export default class TraversalPath {
throw new Error("You passed `path.replaceWith()` a falsy node, use `path.remove()` instead"); throw new Error("You passed `path.replaceWith()` a falsy node, use `path.remove()` instead");
} }
// normalise inserting an entire AST
if (t.isProgram(replacement)) {
replacement = replacement.body;
}
if (Array.isArray(replacement)) { if (Array.isArray(replacement)) {
if (whateverAllowed) { if (whateverAllowed) {
return this.replaceWithMultiple(replacement); return this.replaceWithMultiple(replacement);
@ -545,6 +550,14 @@ export default class TraversalPath {
// potentially create new scope // potentially create new scope
this.setScope(); this.setScope();
this.checkSelf();
}
/**
* Description
*/
checkSelf() {
this.checkPaths(this); this.checkPaths(this);
} }