From 9dc72e71d478d6c60af4a40209ea6d25cefddc11 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 5 Jun 2015 07:45:19 +0100 Subject: [PATCH] add skipKey method to skip traversal of subsequent keys --- src/babel/traversal/index.js | 9 ++++----- src/babel/traversal/path/context.js | 13 +++++++++++-- src/babel/traversal/visitors.js | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/babel/traversal/index.js b/src/babel/traversal/index.js index 6c0e4ac178..d11dbc9a15 100644 --- a/src/babel/traversal/index.js +++ b/src/babel/traversal/index.js @@ -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; } }; diff --git a/src/babel/traversal/path/context.js b/src/babel/traversal/path/context.js index 11674a9c2f..1383949d9a 100644 --- a/src/babel/traversal/path/context.js +++ b/src/babel/traversal/path/context.js @@ -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; diff --git a/src/babel/traversal/visitors.js b/src/babel/traversal/visitors.js index 5ec9531f7b..cf46f9c845 100644 --- a/src/babel/traversal/visitors.js +++ b/src/babel/traversal/visitors.js @@ -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; }