⬅️ Back to Table of Contents
📄 simple-traverse.ts
📊 Analysis Summary
Metric |
Count |
🔧 Functions |
4 |
🧱 Classes |
1 |
📦 Imports |
3 |
📊 Variables & Constants |
2 |
📑 Type Aliases |
1 |
📚 Table of Contents
🛠️ File Location:
📂 packages/typescript-estree/src/simple-traverse.ts
📦 Imports
Name |
Source |
VisitorKeys |
@typescript-eslint/visitor-keys |
visitorKeys |
@typescript-eslint/visitor-keys |
TSESTree |
./ts-estree |
Variables & Constants
Name |
Type |
Kind |
Value |
Exported |
keys |
any |
const |
allVisitorKeys[node.type] |
✗ |
childOrChildren |
TSESTree.Node |
const |
node[key] |
✗ |
Functions
isValidNode(x: unknown): x is TSESTree.Node
Code
function isValidNode(x: unknown): x is TSESTree.Node {
return (
typeof x === 'object' &&
x != null &&
'type' in x &&
typeof x.type === 'string'
);
}
- Parameters:
x: unknown
- Return Type:
x is TSESTree.Node
getVisitorKeysForNode(allVisitorKeys: typeof visitorKeys, node: TSESTree.Node): readonly (keyof TSESTree.Node)[]
Code
function getVisitorKeysForNode(
allVisitorKeys: typeof visitorKeys,
node: TSESTree.Node,
): readonly (keyof TSESTree.Node)[] {
const keys = allVisitorKeys[node.type];
return (keys ?? []) as never;
}
- Parameters:
allVisitorKeys: typeof visitorKeys
node: TSESTree.Node
- Return Type:
readonly (keyof TSESTree.Node)[]
SimpleTraverser.traverse(node: unknown, parent: TSESTree.Node | undefined): void
Code
traverse(node: unknown, parent: TSESTree.Node | undefined): void {
if (!isValidNode(node)) {
return;
}
if (this.setParentPointers) {
node.parent = parent;
}
if ('enter' in this.selectors) {
this.selectors.enter(node, parent);
} else if (node.type in this.selectors.visitors) {
this.selectors.visitors[node.type](node, parent);
}
const keys = getVisitorKeysForNode(this.allVisitorKeys, node);
if (keys.length < 1) {
return;
}
for (const key of keys) {
const childOrChildren = node[key];
if (Array.isArray(childOrChildren)) {
for (const child of childOrChildren) {
this.traverse(child, node);
}
} else {
this.traverse(childOrChildren, node);
}
}
}
- Parameters:
node: unknown
parent: TSESTree.Node | undefined
- Return Type:
void
- Calls:
isValidNode
this.selectors.enter
complex_call_1689
getVisitorKeysForNode
Array.isArray
this.traverse
simpleTraverse(startingNode: TSESTree.Node, options: SimpleTraverseOptions, setParentPointers: boolean): void
Code
export function simpleTraverse(
startingNode: TSESTree.Node,
options: SimpleTraverseOptions,
setParentPointers = false,
): void {
new SimpleTraverser(options, setParentPointers).traverse(
startingNode,
undefined,
);
}
- Parameters:
startingNode: TSESTree.Node
options: SimpleTraverseOptions
setParentPointers: boolean
- Return Type:
void
- Calls:
new SimpleTraverser(options, setParentPointers).traverse
Classes
SimpleTraverser
Class Code
class SimpleTraverser {
private readonly allVisitorKeys: Readonly<VisitorKeys> = visitorKeys;
private readonly selectors: SimpleTraverseOptions;
private readonly setParentPointers: boolean;
constructor(selectors: SimpleTraverseOptions, setParentPointers = false) {
this.selectors = selectors;
this.setParentPointers = setParentPointers;
if (selectors.visitorKeys) {
this.allVisitorKeys = selectors.visitorKeys;
}
}
traverse(node: unknown, parent: TSESTree.Node | undefined): void {
if (!isValidNode(node)) {
return;
}
if (this.setParentPointers) {
node.parent = parent;
}
if ('enter' in this.selectors) {
this.selectors.enter(node, parent);
} else if (node.type in this.selectors.visitors) {
this.selectors.visitors[node.type](node, parent);
}
const keys = getVisitorKeysForNode(this.allVisitorKeys, node);
if (keys.length < 1) {
return;
}
for (const key of keys) {
const childOrChildren = node[key];
if (Array.isArray(childOrChildren)) {
for (const child of childOrChildren) {
this.traverse(child, node);
}
} else {
this.traverse(childOrChildren, node);
}
}
}
}
Methods
traverse(node: unknown, parent: TSESTree.Node | undefined): void
Code
traverse(node: unknown, parent: TSESTree.Node | undefined): void {
if (!isValidNode(node)) {
return;
}
if (this.setParentPointers) {
node.parent = parent;
}
if ('enter' in this.selectors) {
this.selectors.enter(node, parent);
} else if (node.type in this.selectors.visitors) {
this.selectors.visitors[node.type](node, parent);
}
const keys = getVisitorKeysForNode(this.allVisitorKeys, node);
if (keys.length < 1) {
return;
}
for (const key of keys) {
const childOrChildren = node[key];
if (Array.isArray(childOrChildren)) {
for (const child of childOrChildren) {
this.traverse(child, node);
}
} else {
this.traverse(childOrChildren, node);
}
}
}
Type Aliases
SimpleTraverseOptions
type SimpleTraverseOptions = Readonly<
| {
enter: (node: TSESTree.Node, parent: TSESTree.Node | undefined) => void;
visitorKeys?: Readonly<VisitorKeys>;
}
| {
visitorKeys?: Readonly<VisitorKeys>;
visitors: Record<
string,
(node: TSESTree.Node, parent: TSESTree.Node | undefined) => void
>;
}
>;