(cg: CodeGraph, projectRoot: string, requested: string)
| 59 | } |
| 60 | |
| 61 | export function buildFile(cg: CodeGraph, projectRoot: string, requested: string): unknown { |
| 62 | // Refusal first, index lookup second — a traversal out of the project is a |
| 63 | // refusal, not "no such file". See `resolveRequestedFile`. |
| 64 | const { record, storedPath } = resolveRequestedFile(cg, projectRoot, requested); |
| 65 | |
| 66 | const nodes = cg.getNodesInFile(storedPath); |
| 67 | const nodeIds = nodes.map((n) => n.id); |
| 68 | const inThisFile = new Set(nodeIds); |
| 69 | const nodeKindById = new Map(nodes.map((n) => [n.id, n.kind])); |
| 70 | const fileNode = nodes.find((n) => n.kind === 'file') ?? null; |
| 71 | |
| 72 | // --------------------------------------------------------------------------- |
| 73 | // Outline |
| 74 | // --------------------------------------------------------------------------- |
| 75 | const { entries: outline, total: outlineTotal } = buildOutlineEntries(cg, nodes); |
| 76 | |
| 77 | // --------------------------------------------------------------------------- |
| 78 | // Import rails |
| 79 | // --------------------------------------------------------------------------- |
| 80 | const importsOut = cg.getOutgoingEdgesFrom(nodeIds, ['imports']); |
| 81 | const importsIn = cg.getIncomingEdgesTo(nodeIds, ['imports']); |
| 82 | |
| 83 | const endpointIds = new Set<string>(); |
| 84 | for (const edge of importsOut) if (!inThisFile.has(edge.target)) endpointIds.add(edge.target); |
| 85 | for (const edge of importsIn) if (!inThisFile.has(edge.source)) endpointIds.add(edge.source); |
| 86 | const endpoints = cg.getNodesByIds([...endpointIds]); |
| 87 | |
| 88 | const imports = groupByFile( |
| 89 | importsOut.filter((e) => !inThisFile.has(e.target)), |
| 90 | (e) => e.target, |
| 91 | endpoints |
| 92 | ); |
| 93 | const importedBy = groupByFile( |
| 94 | importsIn.filter((e) => !inThisFile.has(e.source)), |
| 95 | (e) => e.source, |
| 96 | endpoints |
| 97 | ); |
| 98 | |
| 99 | // Import statements that never resolved — the third-party packages and |
| 100 | // runtime builtins. Attributed to the file node, which is where extraction |
| 101 | // records a file-level import. |
| 102 | const unresolvedImports = fileNode ? unresolvedImportsOf(cg, fileNode.id) : []; |
| 103 | |
| 104 | // Whether the file RUNS anything at its top level. Extraction records a |
| 105 | // statement outside any definition as an edge out of the FILE node, so a |
| 106 | // module that only defines things has none and a CLI entry point has many — |
| 107 | // the same signal `/api/entrypoints` ranks on. It is worth a line on this |
| 108 | // screen because the outline cannot show it: top-level code belongs to no |
| 109 | // symbol, so the only way to read it is to open the file node itself. |
| 110 | // A call made while initializing a module-level variable or constant is |
| 111 | // attributed to that name (#693), so those names are top-level code too and |
| 112 | // are counted with the file — the same set `getTopCallingFiles` ranks on. |
| 113 | const moduleLevelValueIds = fileNode |
| 114 | ? cg |
| 115 | .getOutgoingEdgesFrom([fileNode.id], ['contains']) |
| 116 | .map((e) => e.target) |
| 117 | .filter((id) => { |
| 118 | const kind = nodeKindById.get(id); |
no test coverage detected