UNPKG

estree-toolkit

Version:

Traverser, scope tracker, and more tools for working with ESTree AST

43 lines (42 loc) 1.48 kB
import { definitions } from './definitions.mjs'; import { aliases } from './aliases.mjs'; import { toCamelCase } from './helpers.mjs'; 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; }; export const is = {}; for (const nodeType in definitions) { is[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) { is[toCamelCase(aliasName)] = (nodeOrNodePath, toMatch) => { if (nodeOrNodePath == null) return false; const node = nodeOrNodePath.ctx != null ? nodeOrNodePath.node : nodeOrNodePath; return (node != null && (node.type in aliases[aliasName]) && (toMatch != null ? matches(node, toMatch) : true)); }; }