UNPKG

estree-toolkit

Version:

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

41 lines (40 loc) 1.35 kB
export { evaluate, evaluateTruthy } from './evaluate.mjs'; export { hasBinding } from './hasBinding.mjs'; export const getCommonAncestor = (paths) => { const ancestorsArr = []; for (let i = 0; i < paths.length; i++) { const ancestors = []; let parent = paths[i].parentPath; while (parent != null) { ancestors.push(parent); parent = parent.parentPath; } ancestorsArr.push(ancestors.reverse()); } let common = null; let currAncestor; for (let i = 0;; i++) { currAncestor = ancestorsArr[0][i]; for (let j = 0; j < paths.length; j++) { if (currAncestor !== ancestorsArr[j][i]) return common; } common = currAncestor; } }; export const isReference = (path, includeGlobals = true) => { if (!path.scope) { throw new Error("Can't use isReference() when `scope` is not enabled"); } const binding = path.scope.getBinding(path.node.name); if (binding != null) { return path === binding.identifierPath || binding.references.includes(path); } if (!includeGlobals) return false; const globalBinding = path.scope.getGlobalBinding(path.node.name); if (globalBinding != null) { return globalBinding.references.includes(path); } return false; };