estree-toolkit
Version:
Traverser, scope tracker, and more tools for working with ESTree AST
46 lines (45 loc) • 1.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.is = void 0;
const definitions_1 = require("./definitions");
const aliases_1 = require("./aliases");
const helpers_1 = require("./helpers");
const matches = (object, toMatch) => {
for (const key in toMatch) {
const value = toMatch[key];
if (typeof value == 'function') {
if (!value(object[key], object))
return false;
}
else if (value !== object[key]) {
return false;
}
}
return true;
};
exports.is = {};
for (const nodeType in definitions_1.definitions) {
exports.is[(0, helpers_1.toCamelCase)(nodeType)] = (nodeOrNodePath, toMatch) => {
if (nodeOrNodePath == null)
return false;
// We shouldn't believe in micro-benchmarks but it seems that
// checking for a property is faster than `instanceof` calls
// for `NodePath`
const node = nodeOrNodePath.ctx != null
? nodeOrNodePath.node
: nodeOrNodePath;
return (node != null && node.type === nodeType &&
(toMatch != null ? matches(node, toMatch) : true));
};
}
for (const aliasName in aliases_1.aliases) {
exports.is[(0, helpers_1.toCamelCase)(aliasName)] = (nodeOrNodePath, toMatch) => {
if (nodeOrNodePath == null)
return false;
const node = nodeOrNodePath.ctx != null
? nodeOrNodePath.node
: nodeOrNodePath;
return (node != null && (node.type in aliases_1.aliases[aliasName]) &&
(toMatch != null ? matches(node, toMatch) : true));
};
}