167 lines · 7.3 KBRaw
| 1 | import type { CodeEdge, CodeEdgeKind, CodeFile, CodeGraph, CodeSymbol } from "../contract/views.ts"; |
| 2 | import { outline, type FileOutline, type Reference } from "./outline.ts"; |
| 3 | |
| 4 | export interface SourceFile { |
| 5 | readonly path: string; |
| 6 | readonly text: string; |
| 7 | } |
| 8 | |
| 9 | /** A workspace package: its name, its folder, and the files its `exports` map names. */ |
| 10 | export interface WorkspacePackage { |
| 11 | readonly name: string; |
| 12 | readonly dir: string; |
| 13 | readonly exports: ReadonlyMap<string, string>; |
| 14 | } |
| 15 | |
| 16 | const EXTENSIONS = [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"]; |
| 17 | const MAX_EXPORT_HOPS = 8; |
| 18 | |
| 19 | interface ParsedFile { |
| 20 | readonly path: string; |
| 21 | readonly lines: number; |
| 22 | readonly outline: FileOutline; |
| 23 | readonly ids: readonly (string | null)[]; |
| 24 | } |
| 25 | |
| 26 | /** Builds the code map of a set of source files: each file's declarations, then the uses between them across imports. */ |
| 27 | export function buildGraph(commit: string, sources: readonly SourceFile[], packages: readonly WorkspacePackage[], skipped: number): CodeGraph { |
| 28 | const files = new Map<string, ParsedFile>(); |
| 29 | const symbols = new Map<string, CodeSymbol>(); |
| 30 | for (const source of sources) files.set(source.path, parse(source, symbols)); |
| 31 | const resolver = new Resolver(files, packages); |
| 32 | const edges = new Map<string, { from: string; to: string; kind: CodeEdgeKind; count: number }>(); |
| 33 | for (const file of files.values()) { |
| 34 | file.outline.references.forEach((references, index) => { |
| 35 | const from = file.ids[index]; |
| 36 | if (!from) return; |
| 37 | for (const reference of references) { |
| 38 | const to = resolver.target(file, index, reference); |
| 39 | if (!to || to === from || symbols.get(to)?.parent === from) continue; |
| 40 | const kind = edgeKind(reference, symbols.get(to)!); |
| 41 | const key = `${from}\u0000${to}\u0000${kind}`; |
| 42 | const edge = edges.get(key); |
| 43 | if (edge) edge.count++; |
| 44 | else edges.set(key, { from, to, kind, count: 1 }); |
| 45 | } |
| 46 | }); |
| 47 | } |
| 48 | const fileList: CodeFile[] = [...files.values()].map((file) => ({ path: file.path, lines: file.lines })).sort((a, b) => a.path.localeCompare(b.path)); |
| 49 | return { commit, files: fileList, symbols: [...symbols.values()], edges: [...edges.values()] satisfies CodeEdge[], skipped }; |
| 50 | } |
| 51 | |
| 52 | function parse(source: SourceFile, symbols: Map<string, CodeSymbol>): ParsedFile { |
| 53 | const found = outline(source.text); |
| 54 | const ids = found.symbols.map((symbol) => { |
| 55 | const parent = symbol.parent === null ? null : found.symbols[symbol.parent]; |
| 56 | const id = parent ? `${source.path}#${parent.name}.${symbol.name}` : `${source.path}#${symbol.name}`; |
| 57 | const known = symbols.get(id); |
| 58 | if (known) { |
| 59 | symbols.set(id, { ...known, end: Math.max(known.end, symbol.end) }); |
| 60 | return id; |
| 61 | } |
| 62 | symbols.set(id, { id, file: source.path, name: symbol.name, kind: symbol.kind, parent: parent ? `${source.path}#${parent.name}` : null, start: symbol.start, end: symbol.end, exported: symbol.exported, doc: symbol.doc }); |
| 63 | return id; |
| 64 | }); |
| 65 | return { path: source.path, lines: source.text.split("\n").length, outline: found, ids }; |
| 66 | } |
| 67 | |
| 68 | function edgeKind(reference: Reference, target: CodeSymbol): CodeEdgeKind { |
| 69 | if (target.kind === "type" || target.kind === "interface") return "types"; |
| 70 | if (reference.construct && target.kind === "class") return "creates"; |
| 71 | if (reference.call) return "calls"; |
| 72 | return "reads"; |
| 73 | } |
| 74 | |
| 75 | class Resolver { |
| 76 | readonly #files: ReadonlyMap<string, ParsedFile>; |
| 77 | readonly #packages: readonly WorkspacePackage[]; |
| 78 | |
| 79 | constructor(files: ReadonlyMap<string, ParsedFile>, packages: readonly WorkspacePackage[]) { |
| 80 | this.#files = files; |
| 81 | this.#packages = [...packages].sort((a, b) => b.name.length - a.name.length); |
| 82 | } |
| 83 | |
| 84 | /** The symbol a reference points at: a member of its class, a declaration in its file, or an imported declaration. */ |
| 85 | target(file: ParsedFile, index: number, reference: Reference): string | null { |
| 86 | const symbols = file.outline.symbols; |
| 87 | const own = symbols[index]; |
| 88 | if (reference.member) { |
| 89 | const owner = own.parent ?? (own.kind === "class" ? index : null); |
| 90 | if (owner === null) return null; |
| 91 | const member = symbols.findIndex((symbol) => symbol.parent === owner && symbol.name === reference.name); |
| 92 | return member >= 0 ? file.ids[member] : null; |
| 93 | } |
| 94 | const local = symbols.findIndex((symbol) => symbol.parent === null && symbol.name === reference.name); |
| 95 | if (local >= 0) return file.ids[local]; |
| 96 | const binding = file.outline.imports.find((entry) => entry.local === reference.name); |
| 97 | if (!binding || binding.imported === "*") return null; |
| 98 | const target = this.#module(file.path, binding.module); |
| 99 | return target ? this.#exported(target, binding.imported, 0) : null; |
| 100 | } |
| 101 | |
| 102 | #exported(file: ParsedFile, name: string, hops: number): string | null { |
| 103 | if (hops > MAX_EXPORT_HOPS) return null; |
| 104 | const symbols = file.outline.symbols; |
| 105 | for (const entry of file.outline.exports) { |
| 106 | if (entry.exported !== name) continue; |
| 107 | if (entry.local !== undefined) { |
| 108 | const local = symbols.findIndex((symbol) => symbol.parent === null && symbol.name === entry.local); |
| 109 | if (local >= 0) return file.ids[local]; |
| 110 | const binding = file.outline.imports.find((imported) => imported.local === entry.local); |
| 111 | const next = binding && binding.imported !== "*" ? this.#module(file.path, binding.module) : null; |
| 112 | return next && binding ? this.#exported(next, binding.imported, hops + 1) : null; |
| 113 | } |
| 114 | if (entry.module && entry.imported && entry.imported !== "*") { |
| 115 | const next = this.#module(file.path, entry.module); |
| 116 | return next ? this.#exported(next, entry.imported, hops + 1) : null; |
| 117 | } |
| 118 | } |
| 119 | if (name !== "default") { |
| 120 | const declared = symbols.findIndex((symbol) => symbol.parent === null && symbol.exported && symbol.name === name); |
| 121 | if (declared >= 0) return file.ids[declared]; |
| 122 | } |
| 123 | for (const entry of file.outline.exports) { |
| 124 | if (entry.exported !== "*" || !entry.module) continue; |
| 125 | const next = this.#module(file.path, entry.module); |
| 126 | const found = next && this.#exported(next, name, hops + 1); |
| 127 | if (found) return found; |
| 128 | } |
| 129 | return null; |
| 130 | } |
| 131 | |
| 132 | #module(from: string, specifier: string): ParsedFile | null { |
| 133 | if (specifier.startsWith(".")) return this.#file(join(dirname(from), specifier)); |
| 134 | const pkg = this.#packages.find((entry) => specifier === entry.name || specifier.startsWith(`${entry.name}/`)); |
| 135 | if (!pkg) return null; |
| 136 | const subpath = `.${specifier.slice(pkg.name.length)}`; |
| 137 | const mapped = pkg.exports.get(subpath); |
| 138 | return this.#file(join(pkg.dir, mapped ?? subpath)); |
| 139 | } |
| 140 | |
| 141 | #file(path: string): ParsedFile | null { |
| 142 | const exact = this.#files.get(path); |
| 143 | if (exact) return exact; |
| 144 | const bare = path.replace(/\.(m|c)?jsx?$/, ""); |
| 145 | for (const ext of EXTENSIONS) { |
| 146 | const found = this.#files.get(bare + ext) ?? this.#files.get(`${path}/index${ext}`); |
| 147 | if (found) return found; |
| 148 | } |
| 149 | return null; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | function dirname(path: string): string { |
| 154 | const slash = path.lastIndexOf("/"); |
| 155 | return slash < 0 ? "" : path.slice(0, slash); |
| 156 | } |
| 157 | |
| 158 | /** Joins and normalises a repository path. Paths never leave the repository root. */ |
| 159 | export function join(base: string, relative: string): string { |
| 160 | const parts: string[] = []; |
| 161 | for (const part of `${base}/${relative}`.split("/")) { |
| 162 | if (!part || part === ".") continue; |
| 163 | if (part === "..") parts.pop(); |
| 164 | else parts.push(part); |
| 165 | } |
| 166 | return parts.join("/"); |
| 167 | } |