From 3754f7615f7e5b2449918387cfb593b81c6fcf91 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 29 Mar 2015 01:50:36 +1100 Subject: [PATCH] make insertBefore functionality the same as insertBefore in traversal path and add getStatementParent method --- src/babel/traversal/path/index.js | 51 ++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/src/babel/traversal/path/index.js b/src/babel/traversal/path/index.js index 467c612875..54663b5224 100644 --- a/src/babel/traversal/path/index.js +++ b/src/babel/traversal/path/index.js @@ -87,7 +87,16 @@ export default class TraversalPath { insertBefore(nodes) { this.checkNodes(nodes); - if (this.isPreviousType("Expression")) { + if (this.isPreviousType("Statement")) { + if (Array.isArray(this.container)) { + this._containerInsertBefore(nodes); + } else if (this.isStatementOrBlock()) { + if (this.node) nodes.push(this.node); + this.container[this.key] = t.blockStatement(nodes); + } else { + throw new Error("no idea what to do with this"); + } + } else if (this.isPreviousType("Expression")) { if (this.node) nodes.push(this.node); this.replaceExpressionWithStatements(nodes); } else { @@ -95,11 +104,12 @@ export default class TraversalPath { } } - _containerInsertAfter(nodes) { - this.updateSiblingKeys(this.key + 1, nodes.length); + + _containerInsert(from, nodes) { + this.updateSiblingKeys(from, nodes.length); for (var i = 0; i < nodes.length; i++) { - var to = this.key + 1 + i; + var to = from + i; this.container.splice(to, 0, nodes[i]); if (this.context) { @@ -108,13 +118,26 @@ export default class TraversalPath { } } + _containerInsertBefore(nodes) { + this._containerInsert(this.key, nodes); + } + + _containerInsertAfter(nodes) { + this._containerInsert(this.key + 1, nodes); + } + + isStatementOrBlock() { + return includes(t.STATEMENT_OR_BLOCK_KEYS, this.key) && !t.isBlockStatement(this.container); + } + insertAfter(nodes) { this.checkNodes(nodes); if (this.isPreviousType("Statement")) { if (Array.isArray(this.container)) { this._containerInsertAfter(nodes); - } else if (includes(t.STATEMENT_OR_BLOCK_KEYS, this.key) && !t.isBlockStatement(this.container)) { + } else if (this.isStatementOrBlock()) { + if (this.node) nodes.unshift(this.node); this.container[this.key] = t.blockStatement(nodes); } else { throw new Error("no idea what to do with this"); @@ -293,6 +316,24 @@ export default class TraversalPath { } } + getStatementParent(): ?TraversalPath { + var path = this; + + do { + if (!path.parentPath || (Array.isArray(path.container) && path.isStatement())) { + break; + } else { + path = path.parentPath; + } + } while (path); + + if (path && (path.isProgram() || path.isFile())) { + throw new Error("File/Program node, we can't possibly find a statement parent to this"); + } + + return path; + } + getLastStatements(): Array { var paths = [];