clean up traverse

This commit is contained in:
Sebastian McKenzie
2015-01-01 22:34:05 +11:00
parent 9a633ebd9c
commit 07667d80ff

View File

@@ -39,25 +39,23 @@ function traverse(parent, opts, scope) {
// replace node
var maybeReplace = function (result) {
if (result === false) return;
if (result == null) return;
if (result != null) {
updated = true;
var isArray = _.isArray(result);
var isArray = _.isArray(result);
// inherit comments from original node to the first replacement node
var inheritTo = result;
if (isArray) inheritTo = result[0];
if (inheritTo) t.inheritsComments(inheritTo, node);
// inherit comments from original node to the first replacement node
var inheritTo = result;
if (isArray) inheritTo = result[0];
if (inheritTo) t.inheritsComments(inheritTo, node);
// replace the node
node = obj[key] = result;
updated = true;
// replace the node
node = obj[key] = result;
// we're replacing a statement or block node with an array of statements so we better
// ensure that it's a block
if (isArray && _.contains(t.STATEMENT_OR_BLOCK_KEYS, key) && !t.isBlockStatement(obj)) {
t.ensureBlock(obj, key);
}
// we're replacing a statement or block node with an array of statements so we better
// ensure that it's a block
if (isArray && _.contains(t.STATEMENT_OR_BLOCK_KEYS, key) && !t.isBlockStatement(obj)) {
t.ensureBlock(obj, key);
}
};
@@ -70,7 +68,8 @@ function traverse(parent, opts, scope) {
},
remove: function () {
stopped = removed = true;
this.stop();
removed = true;
}
};
@@ -84,12 +83,12 @@ function traverse(parent, opts, scope) {
maybeReplace(result);
if (removed) {
delete obj[key];
obj[key] = null;
updated = true;
}
// stop iteration
if (stopped || result === false) return;
if (stopped) return;
}
// traverse node
@@ -106,7 +105,9 @@ function traverse(parent, opts, scope) {
handle(nodes, i);
}
if (updated) parent[key] = _.flatten(parent[key]);
if (updated) {
parent[key] = _.flatten(parent[key]);
}
} else {
handle(parent, key);
}
@@ -155,7 +156,7 @@ traverse.hasType = function (tree, type, blacklistTypes) {
enter: function (node) {
if (node.type === type) {
has = true;
return false;
this.stop();
}
}
});