Merge pull request #1753 from loganfsmyth/optimize-call

Optimize NodePath#call a bit
This commit is contained in:
Sebastian McKenzie 2015-06-15 10:46:10 +01:00
commit 7e080aa9d2
2 changed files with 30 additions and 17 deletions

View File

@ -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;
}
}
}
}

View File

@ -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];