add skipKey method to skip traversal of subsequent keys

This commit is contained in:
Sebastian McKenzie 2015-06-05 07:45:19 +01:00
parent 4a7a02b9af
commit 9dc72e71d4
3 changed files with 16 additions and 8 deletions

View File

@ -32,15 +32,14 @@ traverse.visitors = visitors;
traverse.verify = visitors.verify;
traverse.explode = visitors.explode;
traverse.node = function (node, opts, scope, state, parentPath) {
traverse.node = function (node, opts, scope, state, parentPath, skipKeys?) {
var keys = t.VISITOR_KEYS[node.type];
if (!keys) return;
var context = new TraversalContext(scope, opts, state, parentPath);
for (var i = 0; i < keys.length; i++) {
if (context.visit(node, keys[i])) {
return;
}
for (var key of (keys: Array)) {
if (skipKeys && skipKeys[key]) continue;
if (context.visit(node, key)) return;
}
};

View File

@ -71,10 +71,10 @@ export function visit(): boolean {
// traverse over these replacement nodes we purposely don't call exitNode
// as the original node has been destroyed
for (var i = 0; i < node.length; i++) {
traverse.node(node[i], opts, this.scope, this.state, this);
traverse.node(node[i], opts, this.scope, this.state, this, this.skipKeys);
}
} else {
traverse.node(node, opts, this.scope, this.state, this);
traverse.node(node, opts, this.scope, this.state, this, this.skipKeys);
this.call("exit");
}
}
@ -90,6 +90,14 @@ export function skip() {
this.shouldSkip = true;
}
/**
* Description
*/
export function skipKey(key) {
this.skipKeys[key] = true;
}
/**
* Description
*/
@ -119,6 +127,7 @@ export function setContext(context, file) {
this.shouldSkip = false;
this.shouldStop = false;
this.removed = false;
this.skipKeys = {};
if (context) {
this.context = context;

View File

@ -164,7 +164,7 @@ function shouldIgnoreKey(key) {
if (key === "enter" || key === "exit" || key === "shouldSkip") return true;
// ignore other options
if (key === "blacklist" || key === "noScope") return true;
if (key === "blacklist" || key === "noScope" || key === "skipKeys") return true;
return false;
}