From 3f144e6e74f19fd345d5948baab0883bce58e0cf Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 28 Mar 2015 04:34:05 +1100 Subject: [PATCH] ensure that nodes are only traversed once per context --- src/babel/traversal/context.js | 10 ++++++++-- src/babel/traversal/path/index.js | 13 +++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/babel/traversal/context.js b/src/babel/traversal/context.js index 1642bc8b6f..1e9dd23183 100644 --- a/src/babel/traversal/context.js +++ b/src/babel/traversal/context.js @@ -18,9 +18,10 @@ export default class TraversalContext { // nothing to traverse! if (nodes.length === 0) return false; + var visited = []; + var queue = this.queue = []; var stop = false; - var done = []; // build up initial queue for (let i = 0; i < nodes.length; i++) { @@ -29,7 +30,12 @@ export default class TraversalContext { // visit the queue for (let i = 0; i < queue.length; i++) { - if (queue[i].visit()) { + var path = queue[i]; + if (visited.indexOf(path.node) >= 0) continue; + + visited.push(path.node); + + if (path.visit()) { stop = true; break; } diff --git a/src/babel/traversal/path/index.js b/src/babel/traversal/path/index.js index d6a164c01c..467c612875 100644 --- a/src/babel/traversal/path/index.js +++ b/src/babel/traversal/path/index.js @@ -97,6 +97,7 @@ export default class TraversalPath { _containerInsertAfter(nodes) { this.updateSiblingKeys(this.key + 1, nodes.length); + for (var i = 0; i < nodes.length; i++) { var to = this.key + 1 + i; this.container.splice(to, 0, nodes[i]); @@ -222,7 +223,7 @@ export default class TraversalPath { replaceInline(nodes) { if (Array.isArray(nodes)) { if (Array.isArray(this.container)) { - this._verifyNodeList("replaceInline", nodes); + this._verifyNodeList(nodes); this._containerInsertAfter(nodes); return this.remove(); } else { @@ -233,19 +234,15 @@ export default class TraversalPath { } } - _verifyNodeList(key, nodes) { - if (nodes.indexOf(this.node) >= 0) { - // todo: possibly check for inclusion of current node and yell at the user as it's an anti-pattern - } - + _verifyNodeList(nodes) { for (var i = 0; i < nodes.length; i++) { var node = nodes[i]; - if (!node) throw new Error(`Falsy node passed to \`path.${key}()\` with the index of ${i}`); + if (!node) throw new Error(`Node list has falsy node with the index of ${i}`); } } replaceWithMultiple(nodes: Array) { - this._verifyNodeList("replaceWithMultiple", nodes); + this._verifyNodeList(nodes); t.inheritsComments(nodes[0], this.node); this.container[this.key] = null; this.insertAfter(nodes);