(queries: QueryBuilder, onYield: MaybeYield)
| 4854 | } |
| 4855 | |
| 4856 | async function nixOptionPathEdges(queries: QueryBuilder, onYield: MaybeYield): Promise<Edge[]> { |
| 4857 | let scanned255 = 0; |
| 4858 | type Rec = { id: string; filePath: string; startLine: number; endLine: number; segs: string[] }; |
| 4859 | |
| 4860 | // One streaming pass over nix bindings (variables + the odd function-valued |
| 4861 | // option); memory stays O(bindings-kept), not O(all nodes) (#610). |
| 4862 | const byFile = new Map<string, Rec[]>(); |
| 4863 | let scanned = 0; |
| 4864 | for (const kind of ['variable', 'function'] as NodeKind[]) { |
| 4865 | for (const node of queries.iterateNodesByKindIn(kind, ['nix'])) { |
| 4866 | if ((++scanned255 & 63) === 0) await onYield(); |
| 4867 | if ((++scanned & 0x3fff) === 0 && onYield) await onYield(); |
| 4868 | const segs = nixLeadingPlainSegments(node.name); |
| 4869 | if (segs.length === 0) continue; |
| 4870 | const rec: Rec = { |
| 4871 | id: node.id, |
| 4872 | filePath: node.filePath, |
| 4873 | startLine: node.startLine, |
| 4874 | endLine: node.endLine, |
| 4875 | segs, |
| 4876 | }; |
| 4877 | const arr = byFile.get(node.filePath); |
| 4878 | if (arr) arr.push(rec); |
| 4879 | else byFile.set(node.filePath, [rec]); |
| 4880 | } |
| 4881 | } |
| 4882 | |
| 4883 | // Per file: walk bindings outermost-first with a stack of active option |
| 4884 | // spans, composing nested declaration paths (`options = { services.foo = { |
| 4885 | // enable = mkOption ...; }; }` registers services.foo AND services.foo.enable). |
| 4886 | // An `options` binding nested inside another option span is a SUBMODULE's |
| 4887 | // own namespace (`attrsOf (submodule { options = ...; })`) — its internals |
| 4888 | // are not globally addressable, so the sentinel blocks registration below it |
| 4889 | // while still excluding the region from write candidates. |
| 4890 | const SUBMODULE = '\u0000submodule'; |
| 4891 | const decls = new Map<string, Rec[]>(); |
| 4892 | const writes: Rec[] = []; |
| 4893 | const register = (path: string[], rec: Rec) => { |
| 4894 | if (path.length < 2 || path.includes(SUBMODULE)) return; |
| 4895 | const key = path.join('.'); |
| 4896 | const arr = decls.get(key); |
| 4897 | if (arr) arr.push(rec); |
| 4898 | else decls.set(key, [rec]); |
| 4899 | }; |
| 4900 | for (const recs of byFile.values()) { |
| 4901 | recs.sort((a, b) => a.startLine - b.startLine || b.endLine - a.endLine); |
| 4902 | const stack: Array<{ start: number; end: number; prefix: string[] }> = []; |
| 4903 | for (const rec of recs) { |
| 4904 | while (stack.length > 0 && stack[stack.length - 1]!.end < rec.startLine) stack.pop(); |
| 4905 | // Strict containment at line granularity: a one-line nested binding is |
| 4906 | // indistinguishable from its container, so it stays unclassified (rare |
| 4907 | // in module code, where option blocks are multi-line). |
| 4908 | const enclosing = |
| 4909 | stack.length > 0 && |
| 4910 | rec.startLine >= stack[stack.length - 1]!.start && |
| 4911 | rec.endLine <= stack[stack.length - 1]!.end && |
| 4912 | !(rec.startLine === stack[stack.length - 1]!.start && rec.endLine === stack[stack.length - 1]!.end) |
| 4913 | ? stack[stack.length - 1]! |
no test coverage detected