diff --git a/src/babel/traversal/path/context.js b/src/babel/traversal/path/context.js index fe27a1c664..6790e1f54d 100644 --- a/src/babel/traversal/path/context.js +++ b/src/babel/traversal/path/context.js @@ -9,31 +9,31 @@ export function call(key) { if (!node) return; var opts = this.opts; - if (!opts[key] && !opts[node.type]) return; - var fns = [].concat(opts[key]); - if (opts[node.type]) fns = fns.concat(opts[node.type][key]); + for (var fns of [opts[key], opts[node.type] && opts[node.type][key]]){ + if (!fns) continue; - for (var fn of (fns: Array)) { - if (!fn) continue; + for (var fn of (fns: Array)) { + if (!fn) continue; - let node = this.node; - if (!node) return; + let node = this.node; + if (!node) return; - var previousType = this.type; + var previousType = this.type; - // call the function with the params (node, parent, scope, state) - var replacement = fn.call(this, node, this.parent, this.scope, this.state); + // call the function with the params (node, parent, scope, state) + var replacement = fn.call(this, node, this.parent, this.scope, this.state); - if (replacement) { - this.replaceWith(replacement, true); - } + if (replacement) { + this.replaceWith(replacement, true); + } - if (this.shouldStop || this.shouldSkip || this.removed) return; + if (this.shouldStop || this.shouldSkip || this.removed) return; - if (previousType !== this.type) { - this.queueNode(this); - return; + if (previousType !== this.type) { + this.queueNode(this); + return; + } } } } diff --git a/src/babel/traversal/visitors.js b/src/babel/traversal/visitors.js index cf46f9c845..931147ab7b 100644 --- a/src/babel/traversal/visitors.js +++ b/src/babel/traversal/visitors.js @@ -21,6 +21,8 @@ export function explode(visitor) { // ensure visitors are objects ensureEntranceObjects(visitor); + ensureCallbackArrays(visitor); + // add type wrappers for (let nodeType in visitor) { if (shouldIgnoreKey(nodeType)) continue; @@ -73,6 +75,12 @@ export function explode(visitor) { } } + for (let nodeType in visitor) { + if (shouldIgnoreKey(nodeType)) continue; + + ensureCallbackArrays(visitor[nodeType]); + } + return visitor; } @@ -126,6 +134,11 @@ function ensureEntranceObjects(obj) { } } +function ensureCallbackArrays(obj){ + if (obj.enter && !Array.isArray(obj.enter)) obj.enter = [obj.enter]; + if (obj.exit && !Array.isArray(obj.exit)) obj.exit = [obj.exit]; +} + function addQueries(visitor) { for (var selector in visitor.queries) { var fns = visitor.queries[selector];