Optimize node type lookup
This commit is contained in:
@@ -13,7 +13,7 @@ var find = function (obj, node, parent) {
|
||||
for (var i = 0; i < types.length; i++) {
|
||||
var type = types[i];
|
||||
|
||||
if (t["is" + type](node)) {
|
||||
if (t.is(type, node)) {
|
||||
var fn = obj[type];
|
||||
result = fn(node, parent);
|
||||
if (result != null) break;
|
||||
|
||||
@@ -7,14 +7,18 @@ t.NATIVE_TYPE_NAMES = ["Array", "Object", "Number", "Boolean", "Date", "Array",
|
||||
|
||||
//
|
||||
|
||||
var addAssert = function (type, is) {
|
||||
function registerType(type, skipAliases) {
|
||||
var is = t["is" + type] = function (node, opts) {
|
||||
return t.is(type, node, opts, skipAliases);
|
||||
};
|
||||
|
||||
t["assert" + type] = function (node, opts) {
|
||||
opts = opts || {};
|
||||
if (!is(node, opts)) {
|
||||
throw new Error("Expected type " + JSON.stringify(type) + " with option " + JSON.stringify(opts));
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
t.STATEMENT_OR_BLOCK_KEYS = ["consequent", "body"];
|
||||
|
||||
@@ -22,14 +26,48 @@ t.STATEMENT_OR_BLOCK_KEYS = ["consequent", "body"];
|
||||
|
||||
t.VISITOR_KEYS = require("./visitor-keys");
|
||||
|
||||
_.each(t.VISITOR_KEYS, function (keys, type) {
|
||||
var is = t["is" + type] = function (node, opts) {
|
||||
return node && node.type === type && t.shallowEqual(node, opts);
|
||||
};
|
||||
t.ALIAS_KEYS = require("./alias-keys");
|
||||
|
||||
addAssert(type, is);
|
||||
t.FLIPPED_ALIAS_KEYS = {};
|
||||
|
||||
_.each(t.VISITOR_KEYS, function (keys, type) {
|
||||
registerType(type, true);
|
||||
});
|
||||
|
||||
_.each(t.ALIAS_KEYS, function (aliases, type) {
|
||||
_.each(aliases, function (alias) {
|
||||
var types = t.FLIPPED_ALIAS_KEYS[alias] = t.FLIPPED_ALIAS_KEYS[alias] || [];
|
||||
types.push(type);
|
||||
});
|
||||
});
|
||||
|
||||
_.each(t.FLIPPED_ALIAS_KEYS, function (types, type) {
|
||||
t[type.toUpperCase() + "_TYPES"] = types;
|
||||
registerType(type, false);
|
||||
});
|
||||
|
||||
t.is = function (type, node, opts, skipAliases) {
|
||||
if (!node) return;
|
||||
|
||||
var typeMatches = (type === node.type);
|
||||
|
||||
if (!typeMatches && !skipAliases) {
|
||||
var aliases = t.FLIPPED_ALIAS_KEYS[type];
|
||||
|
||||
if (typeof aliases !== 'undefined')
|
||||
typeMatches = aliases.indexOf(node.type) > -1;
|
||||
}
|
||||
|
||||
if (!typeMatches) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (typeof opts !== 'undefined')
|
||||
return t.shallowEqual(node, opts);
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
t.BUILDER_KEYS = _.defaults(require("./builder-keys"), t.VISITOR_KEYS);
|
||||
@@ -45,29 +83,6 @@ _.each(t.BUILDER_KEYS, function (keys, type) {
|
||||
};
|
||||
});
|
||||
|
||||
//
|
||||
|
||||
t.ALIAS_KEYS = require("./alias-keys");
|
||||
|
||||
t.FLIPPED_ALIAS_KEYS = {};
|
||||
|
||||
_.each(t.ALIAS_KEYS, function (aliases, type) {
|
||||
_.each(aliases, function (alias) {
|
||||
var types = t.FLIPPED_ALIAS_KEYS[alias] = t.FLIPPED_ALIAS_KEYS[alias] || [];
|
||||
types.push(type);
|
||||
});
|
||||
});
|
||||
|
||||
_.each(t.FLIPPED_ALIAS_KEYS, function (types, type) {
|
||||
t[type.toUpperCase() + "_TYPES"] = types;
|
||||
|
||||
var is = t["is" + type] = function (node, opts) {
|
||||
return node && types.indexOf(node.type) >= 0 && t.shallowEqual(node, opts);
|
||||
};
|
||||
|
||||
addAssert(type, is);
|
||||
});
|
||||
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
@@ -139,17 +154,17 @@ t.toSequenceExpression = function (nodes, scope) {
|
||||
//
|
||||
|
||||
t.shallowEqual = function (actual, expected) {
|
||||
var same = true;
|
||||
var keys = Object.keys(expected);
|
||||
var key;
|
||||
|
||||
if (expected) {
|
||||
_.each(expected, function (val, key) {
|
||||
if (actual[key] !== val) {
|
||||
return same = false;
|
||||
}
|
||||
});
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
key = keys[i];
|
||||
|
||||
if (actual[key] !== expected[key])
|
||||
return false;
|
||||
}
|
||||
|
||||
return same;
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user