(root: SyntaxNode, source: string, language: Language, line: number, column: number | null)
| 996 | * `.forEach` body under the loop it is written in. |
| 997 | */ |
| 998 | export 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}. */ |
| 1034 | export async function loopsForFile(absPath: string, language: Language, sites: readonly CallSite[]): Promise<Map<string, SiteLoop[]>> { |
no test coverage detected