📄 selectedRange¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 6 |
| 📦 Imports | 5 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/website/src/components/ast/selectedRange.ts
📦 Imports¶
| Name | Source |
|---|---|
ParentNodeType |
./types |
filterProperties |
./utils |
isESNode |
./utils |
isRecord |
./utils |
isTSNode |
./utils |
Functions¶
findSelectionPath(node: object, cursorPosition: number): { node: object | null; path: string[] }¶
Parameters:
nodeobjectcursorPositionnumber
Returns: { node: object | null; path: string[] }
Calls:
visited.hasvisited.addfindInObjectnodePath.push
Internal Comments:
Code
export function findSelectionPath(
node: object,
cursorPosition: number,
): { node: object | null; path: string[] } {
const nodePath = ['ast'];
const visited = new Set<unknown>();
let currentNode: unknown = node;
while (currentNode) {
// infinite loop guard
if (visited.has(currentNode)) {
break;
}
visited.add(currentNode);
const result = findInObject(currentNode, cursorPosition, visited);
if (result) {
currentNode = result.value;
nodePath.push(...result.key);
} else {
return { node: currentNode, path: nodePath };
}
}
return { node: null, path: nodePath };
}
isInRange(offset: number, value: object): boolean¶
Parameters:
offsetnumbervalueobject
Returns: boolean
Calls:
getRangeFromNode
Code
geNodeType(value: unknown): ParentNodeType¶
Parameters:
valueunknown
Returns: ParentNodeType
Calls:
isRecord (from ./utils)isESNode (from ./utils)isTSNode (from ./utils)
Code
isIterable(key: string, value: unknown): boolean¶
Parameters:
keystringvalueunknown
Returns: boolean
Calls:
filterProperties (from ./utils)geNodeType
Code
getRangeFromNode(value: object): [number, number] | null¶
Parameters:
valueobject
Returns: [number, number] | null
Calls:
isESNode (from ./utils)isTSNode (from ./utils)
Code
findInObject(iter: object, cursorPosition: number, visited: Set<unknown>): { key: string[]; value: unknown; } | null¶
Parameters:
iterobjectcursorPositionnumbervisitedSet<unknown>
Returns: {
key: string[];
value: unknown;
} | null
Calls:
Object.entriesvisited.hasisIterablevisited.addisRecord (from ./utils)isInRangeArray.isArrayString
Internal Comments:
// we do not want to select parents in case if we do filter with esquery
// typescript array like elements have other iterable items
Code
function findInObject(
iter: object,
cursorPosition: number,
visited: Set<unknown>,
): {
key: string[];
value: unknown;
} | null {
const children = Object.entries(iter);
for (const [name, child] of children) {
// we do not want to select parents in case if we do filter with esquery
if (visited.has(child) || name === 'parent' || !isIterable(name, child)) {
continue;
}
visited.add(iter);
if (isRecord(child)) {
if (isInRange(cursorPosition, child)) {
return {
key: [name],
value: child,
};
}
} else if (Array.isArray(child)) {
for (let index = 0; index < child.length; ++index) {
const arrayChild: unknown = child[index];
// typescript array like elements have other iterable items
if (
typeof index === 'number' &&
isRecord(arrayChild) &&
isInRange(cursorPosition, arrayChild)
) {
return {
key: [name, String(index)],
value: arrayChild,
};
}
}
}
}
return null;
}
Generated by Syntax Scribe