clearn up traversal and fix replacement node arrays not being traversed - fixes #589

This commit is contained in:
Sebastian McKenzie
2015-01-26 17:43:11 +11:00
parent 2bd1afc5fd
commit 0110d18d4c
2 changed files with 26 additions and 11 deletions

View File

@@ -115,8 +115,14 @@ TraversalContext.prototype.visitNode = function (obj, key, opts, scope, parent,
if (this.shouldSkip) return this.shouldStop;
traverseNode(node, opts, ourScope, state);
this.exitNode(obj, key, node, opts.exit, parent, ourScope, state);
if (Array.isArray(node)) {
for (var i = 0; i < node.length; i++) {
traverseNode(node[i], opts, ourScope, state);
}
} else {
traverseNode(node, opts, ourScope, state);
this.exitNode(obj, key, node, opts.exit, parent, ourScope, state);
}
return this.shouldStop;
};
@@ -133,9 +139,10 @@ TraversalContext.prototype.visit = function (node, key, opts, scope, state) {
return;
}
for (var k = 0; k < nodes.length; k++) {
if (nodes[k] && this.visitNode(nodes, k, opts, scope, node, state))
for (var i = 0; i < nodes.length; i++) {
if (nodes[i] && this.visitNode(nodes, i, opts, scope, node, state)) {
return true;
}
}
if (this.shouldFlatten) {
@@ -148,20 +155,19 @@ TraversalContext.prototype.visit = function (node, key, opts, scope, state) {
}
};
function traverseNode(node, opts, scope, state) {
function traverseNode(node, opts, scope, state, debug) {
var keys = t.VISITOR_KEYS[node.type];
if (!keys) return;
var context = new TraversalContext();
for (var j = 0; j < keys.length; j++) {
if (context.visit(node, keys[j], opts, scope, state)) {
for (var i = 0; i < keys.length; i++) {
if (context.visit(node, keys[i], opts, scope, state)) {
return;
}
}
}
function traverse(parent, opts, scope, state) {
// falsy node
if (!parent) return;
if (!scope) {
@@ -195,18 +201,21 @@ function clearNode(node) {
node.loc = null;
node.raw = null;
if (Array.isArray(node.trailingComments))
if (Array.isArray(node.trailingComments)) {
clearComments(node.trailingComments);
}
if (Array.isArray(node.leadingComments))
if (Array.isArray(node.leadingComments)) {
clearComments(node.leadingComments);
}
}
var clearVisitor = { enter: clearNode };
function clearComments(comments) {
for (var i = 0; i < comments.length; i++)
for (var i = 0; i < comments.length; i++) {
clearNode(comments[i]);
}
}
traverse.removeProperties = function (tree) {

View File

@@ -0,0 +1,6 @@
var a, b, c, d;
({a,b}) = {c,d} = {a:1,b:2,c:3,d:4};
assert.equal(a, 1);
assert.equal(b, 2);
assert.equal(c, 3);
assert.equal(d, 4);