revgraph 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,199 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { dedupeImports, findBraceBlockEndLine, stripCommentsKeepLines } from "./common.mjs";
4
+ import { extractTsTauri } from "./tauri.mjs";
5
+
6
+ /**
7
+ * Load tsconfig/jsconfig path aliases across root and monorepo subpackages.
8
+ */
9
+ export function buildTsAliasIndex(repoRoot, allFiles) {
10
+ const configs = [];
11
+ const configFiles = allFiles.filter((f) => /(^|\/)(?:ts|js)config\.json$/.test(f));
12
+
13
+ for (const rel of configFiles) {
14
+ const abs = path.join(repoRoot, rel);
15
+ if (!fs.existsSync(abs)) continue;
16
+ try {
17
+ const stripped = stripCommentsKeepLines(fs.readFileSync(abs, "utf8"), "ts");
18
+ // Remove trailing commas valid in JSONC tsconfig files
19
+ const jsonText = stripped.replace(/,\s*([}\]])/g, "$1");
20
+ const parsed = JSON.parse(jsonText);
21
+ const co = parsed?.compilerOptions || {};
22
+ const pathsCfg = co.paths || {};
23
+ const dir = path.posix.dirname(rel);
24
+ const baseDir = dir === "." ? "" : `${dir}/`;
25
+ const baseUrl = co.baseUrl ? path.posix.normalize(path.posix.join(baseDir, co.baseUrl)) : baseDir;
26
+ const basePrefix = baseUrl && baseUrl !== "." ? `${baseUrl.replace(/\/$/, "")}/` : "";
27
+ const aliases = [];
28
+ for (const [k, vals] of Object.entries(pathsCfg)) {
29
+ if (!Array.isArray(vals) || vals.length === 0) continue;
30
+ const prefix = k.replace(/\*$/, "");
31
+ const targetRaw = String(vals[0]).replace(/\*$/, "").replace(/^\.\//, "");
32
+ const target = path.posix.normalize(`${basePrefix}${targetRaw}`).replace(/^\.\//, "");
33
+ aliases.push({ prefix, target: target.endsWith("/") ? target : `${target}/` });
34
+ }
35
+ if (aliases.length > 0) {
36
+ configs.push({ dir: baseDir, aliases });
37
+ }
38
+ } catch {
39
+ // Skip malformed tsconfig
40
+ }
41
+ }
42
+
43
+ // Deepest config directory first so nearest monorepo package tsconfig wins
44
+ configs.sort((a, b) => b.dir.length - a.dir.length);
45
+ return configs;
46
+ }
47
+
48
+ function getAliasesForFile(relPath, aliasIndex) {
49
+ for (const cfg of aliasIndex) {
50
+ if (!cfg.dir || relPath.startsWith(cfg.dir)) return cfg.aliases;
51
+ }
52
+ return [{ prefix: "@/", target: "src/" }];
53
+ }
54
+
55
+ function resolveTsSpecifier(fromFile, spec, fileSet, aliases) {
56
+ let candidateBase = null;
57
+ if (spec.startsWith(".")) {
58
+ candidateBase = path.posix.normalize(path.posix.join(path.posix.dirname(fromFile), spec));
59
+ } else {
60
+ for (const al of aliases) {
61
+ if (spec.startsWith(al.prefix)) {
62
+ candidateBase = path.posix.normalize(al.target + spec.slice(al.prefix.length));
63
+ break;
64
+ }
65
+ }
66
+ }
67
+ if (!candidateBase) return null;
68
+
69
+ const stripped = candidateBase.replace(/\.(js|jsx|mjs|cjs)$/, "");
70
+ const tryList = [
71
+ candidateBase,
72
+ `${candidateBase}.ts`,
73
+ `${candidateBase}.tsx`,
74
+ `${candidateBase}.d.ts`,
75
+ `${candidateBase}.js`,
76
+ `${candidateBase}.jsx`,
77
+ `${candidateBase}.mjs`,
78
+ `${candidateBase}.cjs`,
79
+ `${stripped}.ts`,
80
+ `${stripped}.tsx`,
81
+ `${stripped}.mjs`,
82
+ `${candidateBase}/index.ts`,
83
+ `${candidateBase}/index.tsx`,
84
+ `${candidateBase}/index.js`,
85
+ `${candidateBase}/index.mjs`,
86
+ ];
87
+ for (const cand of tryList) {
88
+ if (fileSet.has(cand)) return cand;
89
+ }
90
+ return null;
91
+ }
92
+
93
+ export function parseTsFile(relPath, content, fileSet, aliasIndex, { enableTauri = false } = {}) {
94
+ const clean = stripCommentsKeepLines(content, "ts");
95
+ const lines = clean.split("\n");
96
+ const aliases = getAliasesForFile(relPath, aliasIndex);
97
+ const imports = [];
98
+ const symbols = [];
99
+
100
+ // Static imports & re-exports
101
+ const importRe = /(?:^|\n)\s*(?:import|export)\s+(?:type\s+)?([\s\S]*?)\s+from\s+["']([^"']+)["']/g;
102
+ for (const m of clean.matchAll(importRe)) {
103
+ const resolved = resolveTsSpecifier(relPath, m[2], fileSet, aliases);
104
+ if (resolved && resolved !== relPath) {
105
+ const clause = m[1] || "";
106
+ const names = [];
107
+ const braceMatch = clause.match(/\{([^}]+)\}/);
108
+ if (braceMatch) {
109
+ for (const part of braceMatch[1].split(",")) {
110
+ const cleaned = part.replace(/\btype\b/g, "").trim();
111
+ const orig = cleaned.split(/\s+as\s+/)[0]?.trim();
112
+ if (orig && /^[A-Za-z_$][\w$]*$/.test(orig)) names.push(orig);
113
+ }
114
+ }
115
+ imports.push({ target: resolved, symbols: names, kind: "import" });
116
+ }
117
+ }
118
+
119
+ // Side-effect imports, dynamic import(), and CommonJS require()
120
+ const dynRe =
121
+ /(?:\bimport\s*\(\s*["']([^"']+)["']\s*\)|\brequire\s*\(\s*["']([^"']+)["']\s*\)|(?:^|\n)\s*import\s+["']([^"']+)["'])/g;
122
+ for (const m of clean.matchAll(dynRe)) {
123
+ const spec = m[1] || m[2] || m[3];
124
+ const resolved = resolveTsSpecifier(relPath, spec, fileSet, aliases);
125
+ if (resolved && resolved !== relPath) {
126
+ imports.push({ target: resolved, symbols: [], kind: "import" });
127
+ }
128
+ }
129
+
130
+ // Top-level symbol definitions
131
+ // shortcut: regex/brace scanner misses nested arrow helpers or decorator-wrapped exports; upgrade to AST if precision gaps appear
132
+ for (let i = 0; i < lines.length; i++) {
133
+ const line = lines[i];
134
+ if (/^\s/.test(line)) continue;
135
+
136
+ const fnMatch = line.match(
137
+ /^(export\s+)?(?:default\s+)?(?:async\s+)?function\s+([A-Za-z_$][\w$]*)/,
138
+ );
139
+ if (fnMatch) {
140
+ const endLine = findBraceBlockEndLine(lines, i);
141
+ let sigEnd = Math.min(endLine, i + 1);
142
+ for (let s = i; s < Math.min(lines.length, i + 8); s++) {
143
+ if (lines[s].includes("{")) {
144
+ sigEnd = s + 1;
145
+ break;
146
+ }
147
+ }
148
+ symbols.push({
149
+ name: fnMatch[2],
150
+ shortName: fnMatch[2],
151
+ kind: "function",
152
+ exported: Boolean(fnMatch[1]),
153
+ startLine: i + 1,
154
+ sigEndLine: sigEnd,
155
+ endLine,
156
+ });
157
+ continue;
158
+ }
159
+
160
+ const typeMatch = line.match(
161
+ /^(export\s+)?(?:default\s+)?(class|interface|type|enum)\s+([A-Za-z_$][\w$]*)/,
162
+ );
163
+ if (typeMatch) {
164
+ const endLine = findBraceBlockEndLine(lines, i, 250);
165
+ symbols.push({
166
+ name: typeMatch[3],
167
+ shortName: typeMatch[3],
168
+ kind: typeMatch[2],
169
+ exported: Boolean(typeMatch[1]),
170
+ startLine: i + 1,
171
+ sigEndLine: i + 1,
172
+ endLine,
173
+ });
174
+ continue;
175
+ }
176
+
177
+ const constMatch = line.match(/^(export\s+)?(?:const|let)\s+([A-Za-z_$][\w$]*)\s*(?::[^=]+)?=/);
178
+ if (constMatch) {
179
+ const endLine = findBraceBlockEndLine(lines, i, 300);
180
+ symbols.push({
181
+ name: constMatch[2],
182
+ shortName: constMatch[2],
183
+ kind: "const",
184
+ exported: Boolean(constMatch[1]),
185
+ startLine: i + 1,
186
+ sigEndLine: Math.min(endLine, i + 2),
187
+ endLine,
188
+ });
189
+ }
190
+ }
191
+
192
+ const tauri = enableTauri ? extractTsTauri(clean, content) : {};
193
+ return {
194
+ lang: "ts",
195
+ imports: dedupeImports(imports),
196
+ symbols,
197
+ ...tauri,
198
+ };
199
+ }