MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / loopsInTree

Function loopsInTree

src/graph/branch-guards.ts:998–1031  ·  view source on GitHub ↗
(root: SyntaxNode, source: string, language: Language, line: number, column: number | null)

Source from the content-addressed store, hash-verified

996 * `.forEach` body under the loop it is written in.
997 */
998export function loopsInTree(root: SyntaxNode, source: string, language: Language, line: number, column: number | null): SiteLoop[] {
999 const rules = RULES_BY_LANGUAGE.get(language);
1000 if (!rules) return [];
1001 const row = line - 1;
1002 if (row < 0) return [];
1003 let col = column ?? 0;
1004 if (column === null) {
1005 const text = source.split('\n')[row] ?? '';
1006 const first = text.search(/\S/);
1007 col = first < 0 ? 0 : first;
1008 }
1009 let node: SyntaxNode | null = innermostAt(root, row, col);
1010 if (!node) return [];
1011 const found: SiteLoop[] = [];
1012 while (node) {
1013 const parent: SyntaxNode | null = node.parent;
1014 if (!parent || rules.boundaries.has(parent.type)) break;
1015 if (rules.inlineFunctions.has(parent.type)) {
1016 const holder = parent.parent?.type ?? '';
1017 if (rules.bindingParents.has(holder)) break;
1018 node = parent;
1019 continue;
1020 }
1021 const kind = LOOP_TYPES.get(parent.type);
1022 // The header, not the body: a site is in the loop only when it is under it.
1023 if (kind && !isField(parent, 'condition', node) && !isField(parent, 'value', node)) {
1024 const text = loopHeader(parent);
1025 if (text) found.push({ text, kind, branch: branchKey(parent) });
1026 }
1027 node = parent;
1028 }
1029 found.reverse();
1030 return found;
1031}
1032
1033/** Loops for many sites in one file, keyed by {@link siteKey}. */
1034export async function loopsForFile(absPath: string, language: Language, sites: readonly CallSite[]): Promise<Map<string, SiteLoop[]>> {

Callers 2

loopsInSourceFunction · 0.85
loopsForFileFunction · 0.85

Calls 8

innermostAtFunction · 0.85
isFieldFunction · 0.85
loopHeaderFunction · 0.85
branchKeyFunction · 0.85
searchMethod · 0.80
hasMethod · 0.80
pushMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected