codeorbit 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.
- package/LICENSE +88 -0
- package/README.md +1 -0
- package/dist/cli.js +3704 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.js +1932 -0
- package/dist/index.js.map +1 -0
- package/dist/server.js +3408 -0
- package/dist/server.js.map +1 -0
- package/package.json +85 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1932 @@
|
|
|
1
|
+
// src/infrastructure/SqliteGraphRepository.ts
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import Database from "better-sqlite3";
|
|
5
|
+
var SCHEMA_SQL = `
|
|
6
|
+
CREATE TABLE IF NOT EXISTS nodes (
|
|
7
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
8
|
+
kind TEXT NOT NULL,
|
|
9
|
+
name TEXT NOT NULL,
|
|
10
|
+
qualified_name TEXT NOT NULL UNIQUE,
|
|
11
|
+
file_path TEXT NOT NULL,
|
|
12
|
+
line_start INTEGER,
|
|
13
|
+
line_end INTEGER,
|
|
14
|
+
language TEXT,
|
|
15
|
+
parent_name TEXT,
|
|
16
|
+
params TEXT,
|
|
17
|
+
return_type TEXT,
|
|
18
|
+
modifiers TEXT,
|
|
19
|
+
is_test INTEGER DEFAULT 0,
|
|
20
|
+
file_hash TEXT,
|
|
21
|
+
extra TEXT DEFAULT '{}',
|
|
22
|
+
updated_at REAL NOT NULL
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
CREATE TABLE IF NOT EXISTS edges (
|
|
26
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
27
|
+
kind TEXT NOT NULL,
|
|
28
|
+
source_qualified TEXT NOT NULL,
|
|
29
|
+
target_qualified TEXT NOT NULL,
|
|
30
|
+
file_path TEXT NOT NULL,
|
|
31
|
+
line INTEGER DEFAULT 0,
|
|
32
|
+
extra TEXT DEFAULT '{}',
|
|
33
|
+
updated_at REAL NOT NULL
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
CREATE TABLE IF NOT EXISTS metadata (
|
|
37
|
+
key TEXT PRIMARY KEY,
|
|
38
|
+
value TEXT NOT NULL
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
CREATE INDEX IF NOT EXISTS idx_nodes_file ON nodes(file_path);
|
|
42
|
+
CREATE INDEX IF NOT EXISTS idx_nodes_kind ON nodes(kind);
|
|
43
|
+
CREATE INDEX IF NOT EXISTS idx_nodes_qualified ON nodes(qualified_name);
|
|
44
|
+
CREATE INDEX IF NOT EXISTS idx_edges_source ON edges(source_qualified);
|
|
45
|
+
CREATE INDEX IF NOT EXISTS idx_edges_target ON edges(target_qualified);
|
|
46
|
+
CREATE INDEX IF NOT EXISTS idx_edges_kind ON edges(kind);
|
|
47
|
+
CREATE INDEX IF NOT EXISTS idx_edges_file ON edges(file_path);
|
|
48
|
+
`;
|
|
49
|
+
var SqliteGraphRepository = class {
|
|
50
|
+
db;
|
|
51
|
+
adjCache = null;
|
|
52
|
+
dbPath;
|
|
53
|
+
constructor(dbPath) {
|
|
54
|
+
this.dbPath = dbPath;
|
|
55
|
+
const dir = path.dirname(dbPath);
|
|
56
|
+
if (!fs.existsSync(dir)) {
|
|
57
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
58
|
+
}
|
|
59
|
+
this.db = new Database(dbPath);
|
|
60
|
+
this.db.pragma("journal_mode = WAL");
|
|
61
|
+
this.db.pragma("busy_timeout = 5000");
|
|
62
|
+
try {
|
|
63
|
+
this.db.pragma("wal_checkpoint(TRUNCATE)");
|
|
64
|
+
} catch {
|
|
65
|
+
}
|
|
66
|
+
this._initSchema();
|
|
67
|
+
}
|
|
68
|
+
close() {
|
|
69
|
+
try {
|
|
70
|
+
this.db.pragma("wal_checkpoint(TRUNCATE)");
|
|
71
|
+
} catch {
|
|
72
|
+
}
|
|
73
|
+
this.db.close();
|
|
74
|
+
for (const suffix of ["-wal", "-shm"]) {
|
|
75
|
+
const side = this.dbPath + suffix;
|
|
76
|
+
try {
|
|
77
|
+
if (fs.existsSync(side)) {
|
|
78
|
+
fs.unlinkSync(side);
|
|
79
|
+
}
|
|
80
|
+
} catch {
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// ---------------------------------------------------------------------------
|
|
85
|
+
// Write operations
|
|
86
|
+
// ---------------------------------------------------------------------------
|
|
87
|
+
upsertNode(node, fileHash2 = "") {
|
|
88
|
+
const now = Date.now() / 1e3;
|
|
89
|
+
const qualified = this._makeQualified(node);
|
|
90
|
+
const extra = node.extra ? JSON.stringify(node.extra) : "{}";
|
|
91
|
+
this.db.prepare(`
|
|
92
|
+
INSERT INTO nodes
|
|
93
|
+
(kind, name, qualified_name, file_path, line_start, line_end,
|
|
94
|
+
language, parent_name, params, return_type, modifiers, is_test,
|
|
95
|
+
file_hash, extra, updated_at)
|
|
96
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
97
|
+
ON CONFLICT(qualified_name) DO UPDATE SET
|
|
98
|
+
kind=excluded.kind, name=excluded.name,
|
|
99
|
+
file_path=excluded.file_path, line_start=excluded.line_start,
|
|
100
|
+
line_end=excluded.line_end, language=excluded.language,
|
|
101
|
+
parent_name=excluded.parent_name, params=excluded.params,
|
|
102
|
+
return_type=excluded.return_type, modifiers=excluded.modifiers,
|
|
103
|
+
is_test=excluded.is_test, file_hash=excluded.file_hash,
|
|
104
|
+
extra=excluded.extra, updated_at=excluded.updated_at
|
|
105
|
+
`).run(
|
|
106
|
+
node.kind,
|
|
107
|
+
node.name,
|
|
108
|
+
qualified,
|
|
109
|
+
node.filePath,
|
|
110
|
+
node.lineStart,
|
|
111
|
+
node.lineEnd,
|
|
112
|
+
node.language ?? null,
|
|
113
|
+
node.parentName ?? null,
|
|
114
|
+
node.params ?? null,
|
|
115
|
+
node.returnType ?? null,
|
|
116
|
+
node.modifiers ?? null,
|
|
117
|
+
node.isTest ? 1 : 0,
|
|
118
|
+
fileHash2,
|
|
119
|
+
extra,
|
|
120
|
+
now
|
|
121
|
+
);
|
|
122
|
+
const row = this.db.prepare(
|
|
123
|
+
"SELECT id FROM nodes WHERE qualified_name = ?"
|
|
124
|
+
).get(qualified);
|
|
125
|
+
return row.id;
|
|
126
|
+
}
|
|
127
|
+
upsertEdge(edge) {
|
|
128
|
+
const now = Date.now() / 1e3;
|
|
129
|
+
const extra = edge.extra ? JSON.stringify(edge.extra) : "{}";
|
|
130
|
+
const line = edge.line ?? 0;
|
|
131
|
+
const existing = this.db.prepare(`
|
|
132
|
+
SELECT id FROM edges
|
|
133
|
+
WHERE kind=? AND source_qualified=? AND target_qualified=?
|
|
134
|
+
AND file_path=? AND line=?
|
|
135
|
+
`).get(edge.kind, edge.source, edge.target, edge.filePath, line);
|
|
136
|
+
if (existing) {
|
|
137
|
+
this.db.prepare(
|
|
138
|
+
"UPDATE edges SET line=?, extra=?, updated_at=? WHERE id=?"
|
|
139
|
+
).run(line, extra, now, existing.id);
|
|
140
|
+
return existing.id;
|
|
141
|
+
}
|
|
142
|
+
const result = this.db.prepare(`
|
|
143
|
+
INSERT INTO edges
|
|
144
|
+
(kind, source_qualified, target_qualified, file_path, line, extra, updated_at)
|
|
145
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
146
|
+
`).run(edge.kind, edge.source, edge.target, edge.filePath, line, extra, now);
|
|
147
|
+
return Number(result.lastInsertRowid);
|
|
148
|
+
}
|
|
149
|
+
removeFileData(filePath) {
|
|
150
|
+
this.db.prepare("DELETE FROM nodes WHERE file_path = ?").run(filePath);
|
|
151
|
+
this.db.prepare("DELETE FROM edges WHERE file_path = ?").run(filePath);
|
|
152
|
+
this._invalidateCache();
|
|
153
|
+
}
|
|
154
|
+
storeFileNodesEdges(filePath, nodes, edges, fileHash2 = "") {
|
|
155
|
+
const txn = this.db.transaction(() => {
|
|
156
|
+
this.removeFileData(filePath);
|
|
157
|
+
for (const node of nodes) {
|
|
158
|
+
this.upsertNode(node, fileHash2);
|
|
159
|
+
}
|
|
160
|
+
for (const edge of edges) {
|
|
161
|
+
this.upsertEdge(edge);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
txn();
|
|
165
|
+
this._invalidateCache();
|
|
166
|
+
}
|
|
167
|
+
setMetadata(key, value) {
|
|
168
|
+
this.db.prepare(
|
|
169
|
+
"INSERT OR REPLACE INTO metadata (key, value) VALUES (?, ?)"
|
|
170
|
+
).run(key, value);
|
|
171
|
+
}
|
|
172
|
+
getMetadata(key) {
|
|
173
|
+
const row = this.db.prepare(
|
|
174
|
+
"SELECT value FROM metadata WHERE key = ?"
|
|
175
|
+
).get(key);
|
|
176
|
+
return row?.value;
|
|
177
|
+
}
|
|
178
|
+
// ---------------------------------------------------------------------------
|
|
179
|
+
// Read operations
|
|
180
|
+
// ---------------------------------------------------------------------------
|
|
181
|
+
getNode(qualifiedName) {
|
|
182
|
+
const row = this.db.prepare(
|
|
183
|
+
"SELECT * FROM nodes WHERE qualified_name = ?"
|
|
184
|
+
).get(qualifiedName);
|
|
185
|
+
return row ? this._rowToNode(row) : void 0;
|
|
186
|
+
}
|
|
187
|
+
getNodesByFile(filePath) {
|
|
188
|
+
const rows = this.db.prepare(
|
|
189
|
+
"SELECT * FROM nodes WHERE file_path = ? ORDER BY line_start"
|
|
190
|
+
).all(filePath);
|
|
191
|
+
return rows.map((r) => this._rowToNode(r));
|
|
192
|
+
}
|
|
193
|
+
getEdgesBySource(qualifiedName) {
|
|
194
|
+
const rows = this.db.prepare(
|
|
195
|
+
"SELECT * FROM edges WHERE source_qualified = ?"
|
|
196
|
+
).all(qualifiedName);
|
|
197
|
+
return rows.map((r) => this._rowToEdge(r));
|
|
198
|
+
}
|
|
199
|
+
getEdgesByTarget(qualifiedName) {
|
|
200
|
+
const rows = this.db.prepare(
|
|
201
|
+
"SELECT * FROM edges WHERE target_qualified = ?"
|
|
202
|
+
).all(qualifiedName);
|
|
203
|
+
return rows.map((r) => this._rowToEdge(r));
|
|
204
|
+
}
|
|
205
|
+
searchEdgesByTargetName(name, kind = "CALLS") {
|
|
206
|
+
const rows = this.db.prepare(
|
|
207
|
+
"SELECT * FROM edges WHERE target_qualified = ? AND kind = ?"
|
|
208
|
+
).all(name, kind);
|
|
209
|
+
return rows.map((r) => this._rowToEdge(r));
|
|
210
|
+
}
|
|
211
|
+
getAllFiles() {
|
|
212
|
+
const rows = this.db.prepare(
|
|
213
|
+
"SELECT DISTINCT file_path FROM nodes WHERE kind = 'File' ORDER BY file_path"
|
|
214
|
+
).all();
|
|
215
|
+
return rows.map((r) => r.file_path);
|
|
216
|
+
}
|
|
217
|
+
searchNodes(query, limit = 20) {
|
|
218
|
+
const words = query.toLowerCase().split(/\s+/).filter(Boolean);
|
|
219
|
+
if (words.length === 0) {
|
|
220
|
+
return [];
|
|
221
|
+
}
|
|
222
|
+
const conditions = words.map(
|
|
223
|
+
() => "(LOWER(name) LIKE ? OR LOWER(qualified_name) LIKE ?)"
|
|
224
|
+
);
|
|
225
|
+
const params = [];
|
|
226
|
+
for (const word of words) {
|
|
227
|
+
params.push(`%${word}%`, `%${word}%`);
|
|
228
|
+
}
|
|
229
|
+
params.push(limit);
|
|
230
|
+
const where = conditions.join(" AND ");
|
|
231
|
+
const rows = this.db.prepare(
|
|
232
|
+
`SELECT * FROM nodes WHERE ${where} LIMIT ?`
|
|
233
|
+
).all(...params);
|
|
234
|
+
return rows.map((r) => this._rowToNode(r));
|
|
235
|
+
}
|
|
236
|
+
getSubgraph(qualifiedNames) {
|
|
237
|
+
const nodes = [];
|
|
238
|
+
for (const qn of qualifiedNames) {
|
|
239
|
+
const n = this.getNode(qn);
|
|
240
|
+
if (n) {
|
|
241
|
+
nodes.push(n);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
const qnSet = new Set(qualifiedNames);
|
|
245
|
+
const edges = [];
|
|
246
|
+
for (const qn of qualifiedNames) {
|
|
247
|
+
for (const e of this.getEdgesBySource(qn)) {
|
|
248
|
+
if (qnSet.has(e.targetQualified)) {
|
|
249
|
+
edges.push(e);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return { nodes, edges };
|
|
254
|
+
}
|
|
255
|
+
getStats() {
|
|
256
|
+
const totalNodes = this.db.prepare("SELECT COUNT(*) AS cnt FROM nodes").get().cnt;
|
|
257
|
+
const totalEdges = this.db.prepare("SELECT COUNT(*) AS cnt FROM edges").get().cnt;
|
|
258
|
+
const nodesByKind = {};
|
|
259
|
+
for (const r of this.db.prepare(
|
|
260
|
+
"SELECT kind, COUNT(*) AS cnt FROM nodes GROUP BY kind"
|
|
261
|
+
).all()) {
|
|
262
|
+
nodesByKind[r.kind] = r.cnt;
|
|
263
|
+
}
|
|
264
|
+
const edgesByKind = {};
|
|
265
|
+
for (const r of this.db.prepare(
|
|
266
|
+
"SELECT kind, COUNT(*) AS cnt FROM edges GROUP BY kind"
|
|
267
|
+
).all()) {
|
|
268
|
+
edgesByKind[r.kind] = r.cnt;
|
|
269
|
+
}
|
|
270
|
+
const languages = this.db.prepare(
|
|
271
|
+
"SELECT DISTINCT language FROM nodes WHERE language IS NOT NULL AND language != ''"
|
|
272
|
+
).all().map((r) => r.language);
|
|
273
|
+
const filesCount = this.db.prepare(
|
|
274
|
+
"SELECT COUNT(*) AS cnt FROM nodes WHERE kind = 'File'"
|
|
275
|
+
).get().cnt;
|
|
276
|
+
const lastUpdated = this.getMetadata("last_updated") ?? null;
|
|
277
|
+
let embeddingsCount = 0;
|
|
278
|
+
try {
|
|
279
|
+
embeddingsCount = this.db.prepare("SELECT COUNT(*) AS cnt FROM embeddings").get().cnt;
|
|
280
|
+
} catch {
|
|
281
|
+
}
|
|
282
|
+
return {
|
|
283
|
+
totalNodes,
|
|
284
|
+
totalEdges,
|
|
285
|
+
nodesByKind,
|
|
286
|
+
edgesByKind,
|
|
287
|
+
languages,
|
|
288
|
+
filesCount,
|
|
289
|
+
lastUpdated,
|
|
290
|
+
embeddingsCount
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
getNodesBySize(minLines = 50, maxLines, kind, filePathPattern, limit = 50) {
|
|
294
|
+
const conditions = [
|
|
295
|
+
"line_start IS NOT NULL",
|
|
296
|
+
"line_end IS NOT NULL",
|
|
297
|
+
"(line_end - line_start + 1) >= ?"
|
|
298
|
+
];
|
|
299
|
+
const params = [minLines];
|
|
300
|
+
if (maxLines !== void 0) {
|
|
301
|
+
conditions.push("(line_end - line_start + 1) <= ?");
|
|
302
|
+
params.push(maxLines);
|
|
303
|
+
}
|
|
304
|
+
if (kind) {
|
|
305
|
+
conditions.push("kind = ?");
|
|
306
|
+
params.push(kind);
|
|
307
|
+
}
|
|
308
|
+
if (filePathPattern) {
|
|
309
|
+
conditions.push("file_path LIKE ?");
|
|
310
|
+
params.push(`%${filePathPattern}%`);
|
|
311
|
+
}
|
|
312
|
+
params.push(limit);
|
|
313
|
+
const where = conditions.join(" AND ");
|
|
314
|
+
const rows = this.db.prepare(
|
|
315
|
+
`SELECT * FROM nodes WHERE ${where} ORDER BY (line_end - line_start + 1) DESC LIMIT ?`
|
|
316
|
+
).all(...params);
|
|
317
|
+
return rows.map((r) => ({
|
|
318
|
+
...this._rowToNode(r),
|
|
319
|
+
lineCount: r.line_start != null && r.line_end != null ? r.line_end - r.line_start + 1 : 0
|
|
320
|
+
}));
|
|
321
|
+
}
|
|
322
|
+
getAllEdges() {
|
|
323
|
+
const rows = this.db.prepare("SELECT * FROM edges").all();
|
|
324
|
+
return rows.map((r) => this._rowToEdge(r));
|
|
325
|
+
}
|
|
326
|
+
getEdgesAmong(qualifiedNames) {
|
|
327
|
+
if (qualifiedNames.size === 0) {
|
|
328
|
+
return [];
|
|
329
|
+
}
|
|
330
|
+
const qns = [...qualifiedNames];
|
|
331
|
+
const results = [];
|
|
332
|
+
const batchSize = 450;
|
|
333
|
+
for (let i = 0; i < qns.length; i += batchSize) {
|
|
334
|
+
const batch = qns.slice(i, i + batchSize);
|
|
335
|
+
const placeholders = batch.map(() => "?").join(",");
|
|
336
|
+
const rows = this.db.prepare(
|
|
337
|
+
`SELECT * FROM edges WHERE source_qualified IN (${placeholders})`
|
|
338
|
+
).all(...batch);
|
|
339
|
+
for (const r of rows) {
|
|
340
|
+
const edge = this._rowToEdge(r);
|
|
341
|
+
if (qualifiedNames.has(edge.targetQualified)) {
|
|
342
|
+
results.push(edge);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return results;
|
|
347
|
+
}
|
|
348
|
+
// ---------------------------------------------------------------------------
|
|
349
|
+
// Adjacency (BFS support for use cases)
|
|
350
|
+
// ---------------------------------------------------------------------------
|
|
351
|
+
getAdjacency() {
|
|
352
|
+
const cache = this._buildAdjacency();
|
|
353
|
+
return { outgoing: cache.out, incoming: cache.in };
|
|
354
|
+
}
|
|
355
|
+
// ---------------------------------------------------------------------------
|
|
356
|
+
// Private helpers
|
|
357
|
+
// ---------------------------------------------------------------------------
|
|
358
|
+
_initSchema() {
|
|
359
|
+
this.db.exec(SCHEMA_SQL);
|
|
360
|
+
this.setMetadata("schema_version", "1");
|
|
361
|
+
}
|
|
362
|
+
_invalidateCache() {
|
|
363
|
+
this.adjCache = null;
|
|
364
|
+
}
|
|
365
|
+
_buildAdjacency() {
|
|
366
|
+
if (this.adjCache) {
|
|
367
|
+
return this.adjCache;
|
|
368
|
+
}
|
|
369
|
+
const out = /* @__PURE__ */ new Map();
|
|
370
|
+
const inMap = /* @__PURE__ */ new Map();
|
|
371
|
+
const rows = this.db.prepare("SELECT source_qualified, target_qualified FROM edges").all();
|
|
372
|
+
for (const r of rows) {
|
|
373
|
+
if (!out.has(r.source_qualified)) {
|
|
374
|
+
out.set(r.source_qualified, /* @__PURE__ */ new Set());
|
|
375
|
+
}
|
|
376
|
+
out.get(r.source_qualified).add(r.target_qualified);
|
|
377
|
+
if (!inMap.has(r.target_qualified)) {
|
|
378
|
+
inMap.set(r.target_qualified, /* @__PURE__ */ new Set());
|
|
379
|
+
}
|
|
380
|
+
inMap.get(r.target_qualified).add(r.source_qualified);
|
|
381
|
+
}
|
|
382
|
+
this.adjCache = { out, in: inMap };
|
|
383
|
+
return this.adjCache;
|
|
384
|
+
}
|
|
385
|
+
_makeQualified(node) {
|
|
386
|
+
if (node.kind === "File") {
|
|
387
|
+
return node.filePath;
|
|
388
|
+
}
|
|
389
|
+
if (node.parentName) {
|
|
390
|
+
return `${node.filePath}::${node.parentName}.${node.name}`;
|
|
391
|
+
}
|
|
392
|
+
return `${node.filePath}::${node.name}`;
|
|
393
|
+
}
|
|
394
|
+
_rowToNode(row) {
|
|
395
|
+
return {
|
|
396
|
+
id: row.id,
|
|
397
|
+
kind: row.kind,
|
|
398
|
+
name: row.name,
|
|
399
|
+
qualifiedName: row.qualified_name,
|
|
400
|
+
filePath: row.file_path,
|
|
401
|
+
lineStart: row.line_start,
|
|
402
|
+
lineEnd: row.line_end,
|
|
403
|
+
language: row.language ?? null,
|
|
404
|
+
parentName: row.parent_name ?? null,
|
|
405
|
+
params: row.params ?? null,
|
|
406
|
+
returnType: row.return_type ?? null,
|
|
407
|
+
modifiers: row.modifiers ?? null,
|
|
408
|
+
isTest: row.is_test === 1,
|
|
409
|
+
fileHash: row.file_hash ?? null
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
_rowToEdge(row) {
|
|
413
|
+
return {
|
|
414
|
+
id: row.id,
|
|
415
|
+
kind: row.kind,
|
|
416
|
+
sourceQualified: row.source_qualified,
|
|
417
|
+
targetQualified: row.target_qualified,
|
|
418
|
+
filePath: row.file_path,
|
|
419
|
+
line: row.line ?? 0
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
// src/infrastructure/TreeSitterParser.ts
|
|
425
|
+
import crypto from "crypto";
|
|
426
|
+
import fs2 from "fs";
|
|
427
|
+
import { createRequire } from "module";
|
|
428
|
+
import path2 from "path";
|
|
429
|
+
import Parser from "tree-sitter";
|
|
430
|
+
var _require = createRequire(import.meta.url);
|
|
431
|
+
function loadLanguage(pkg) {
|
|
432
|
+
try {
|
|
433
|
+
const mod = _require(pkg);
|
|
434
|
+
return mod.default ?? mod;
|
|
435
|
+
} catch {
|
|
436
|
+
return null;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
var _languageCache = /* @__PURE__ */ new Map();
|
|
440
|
+
function getLanguage(name) {
|
|
441
|
+
if (_languageCache.has(name)) {
|
|
442
|
+
return _languageCache.get(name) ?? null;
|
|
443
|
+
}
|
|
444
|
+
const pkgMap = {
|
|
445
|
+
python: "tree-sitter-python",
|
|
446
|
+
javascript: "tree-sitter-javascript",
|
|
447
|
+
typescript: "tree-sitter-typescript/bindings/node/typescript",
|
|
448
|
+
tsx: "tree-sitter-typescript/bindings/node/tsx",
|
|
449
|
+
go: "tree-sitter-go",
|
|
450
|
+
rust: "tree-sitter-rust",
|
|
451
|
+
java: "tree-sitter-java",
|
|
452
|
+
c: "tree-sitter-c",
|
|
453
|
+
cpp: "tree-sitter-cpp",
|
|
454
|
+
csharp: "tree-sitter-c-sharp",
|
|
455
|
+
ruby: "tree-sitter-ruby",
|
|
456
|
+
kotlin: "tree-sitter-kotlin",
|
|
457
|
+
swift: "tree-sitter-swift",
|
|
458
|
+
php: "tree-sitter-php/php",
|
|
459
|
+
solidity: "tree-sitter-solidity",
|
|
460
|
+
vue: "tree-sitter-vue"
|
|
461
|
+
};
|
|
462
|
+
const pkg = pkgMap[name];
|
|
463
|
+
const lang = pkg ? loadLanguage(pkg) : null;
|
|
464
|
+
_languageCache.set(name, lang);
|
|
465
|
+
return lang;
|
|
466
|
+
}
|
|
467
|
+
var EXTENSION_TO_LANGUAGE = {
|
|
468
|
+
".py": "python",
|
|
469
|
+
".js": "javascript",
|
|
470
|
+
".jsx": "javascript",
|
|
471
|
+
".ts": "typescript",
|
|
472
|
+
".tsx": "tsx",
|
|
473
|
+
".go": "go",
|
|
474
|
+
".rs": "rust",
|
|
475
|
+
".java": "java",
|
|
476
|
+
".cs": "csharp",
|
|
477
|
+
".rb": "ruby",
|
|
478
|
+
".cpp": "cpp",
|
|
479
|
+
".cc": "cpp",
|
|
480
|
+
".cxx": "cpp",
|
|
481
|
+
".c": "c",
|
|
482
|
+
".h": "c",
|
|
483
|
+
".hpp": "cpp",
|
|
484
|
+
".kt": "kotlin",
|
|
485
|
+
".swift": "swift",
|
|
486
|
+
".php": "php",
|
|
487
|
+
".sol": "solidity",
|
|
488
|
+
".vue": "vue"
|
|
489
|
+
};
|
|
490
|
+
var CLASS_TYPES = {
|
|
491
|
+
python: ["class_definition"],
|
|
492
|
+
javascript: ["class_declaration", "class"],
|
|
493
|
+
typescript: ["class_declaration", "class"],
|
|
494
|
+
tsx: ["class_declaration", "class"],
|
|
495
|
+
go: ["type_declaration"],
|
|
496
|
+
rust: ["struct_item", "enum_item", "impl_item"],
|
|
497
|
+
java: ["class_declaration", "interface_declaration", "enum_declaration"],
|
|
498
|
+
c: ["struct_specifier", "type_definition"],
|
|
499
|
+
cpp: ["class_specifier", "struct_specifier"],
|
|
500
|
+
csharp: ["class_declaration", "interface_declaration", "enum_declaration", "struct_declaration"],
|
|
501
|
+
ruby: ["class", "module"],
|
|
502
|
+
kotlin: ["class_declaration", "object_declaration"],
|
|
503
|
+
swift: ["class_declaration", "struct_declaration", "protocol_declaration"],
|
|
504
|
+
php: ["class_declaration", "interface_declaration"],
|
|
505
|
+
solidity: [
|
|
506
|
+
"contract_declaration",
|
|
507
|
+
"interface_declaration",
|
|
508
|
+
"library_declaration",
|
|
509
|
+
"struct_declaration",
|
|
510
|
+
"enum_declaration",
|
|
511
|
+
"error_declaration",
|
|
512
|
+
"user_defined_type_definition"
|
|
513
|
+
]
|
|
514
|
+
};
|
|
515
|
+
var FUNCTION_TYPES = {
|
|
516
|
+
python: ["function_definition"],
|
|
517
|
+
javascript: ["function_declaration", "method_definition", "arrow_function"],
|
|
518
|
+
typescript: ["function_declaration", "method_definition", "arrow_function"],
|
|
519
|
+
tsx: ["function_declaration", "method_definition", "arrow_function"],
|
|
520
|
+
go: ["function_declaration", "method_declaration"],
|
|
521
|
+
rust: ["function_item"],
|
|
522
|
+
java: ["method_declaration", "constructor_declaration"],
|
|
523
|
+
c: ["function_definition"],
|
|
524
|
+
cpp: ["function_definition"],
|
|
525
|
+
csharp: ["method_declaration", "constructor_declaration"],
|
|
526
|
+
ruby: ["method", "singleton_method"],
|
|
527
|
+
kotlin: ["function_declaration"],
|
|
528
|
+
swift: ["function_declaration"],
|
|
529
|
+
php: ["function_definition", "method_declaration"],
|
|
530
|
+
solidity: [
|
|
531
|
+
"function_definition",
|
|
532
|
+
"constructor_definition",
|
|
533
|
+
"modifier_definition",
|
|
534
|
+
"event_definition",
|
|
535
|
+
"fallback_receive_definition"
|
|
536
|
+
]
|
|
537
|
+
};
|
|
538
|
+
var IMPORT_TYPES = {
|
|
539
|
+
python: ["import_statement", "import_from_statement"],
|
|
540
|
+
javascript: ["import_statement"],
|
|
541
|
+
typescript: ["import_statement"],
|
|
542
|
+
tsx: ["import_statement"],
|
|
543
|
+
go: ["import_declaration"],
|
|
544
|
+
rust: ["use_declaration"],
|
|
545
|
+
java: ["import_declaration"],
|
|
546
|
+
c: ["preproc_include"],
|
|
547
|
+
cpp: ["preproc_include"],
|
|
548
|
+
csharp: ["using_directive"],
|
|
549
|
+
ruby: ["call"],
|
|
550
|
+
// require / require_relative
|
|
551
|
+
kotlin: ["import_header"],
|
|
552
|
+
swift: ["import_declaration"],
|
|
553
|
+
php: ["namespace_use_declaration"],
|
|
554
|
+
solidity: ["import_directive"]
|
|
555
|
+
};
|
|
556
|
+
var CALL_TYPES = {
|
|
557
|
+
python: ["call"],
|
|
558
|
+
javascript: ["call_expression", "new_expression"],
|
|
559
|
+
typescript: ["call_expression", "new_expression"],
|
|
560
|
+
tsx: ["call_expression", "new_expression"],
|
|
561
|
+
go: ["call_expression"],
|
|
562
|
+
rust: ["call_expression", "macro_invocation"],
|
|
563
|
+
java: ["method_invocation", "object_creation_expression"],
|
|
564
|
+
c: ["call_expression"],
|
|
565
|
+
cpp: ["call_expression"],
|
|
566
|
+
csharp: ["invocation_expression", "object_creation_expression"],
|
|
567
|
+
ruby: ["call", "method_call"],
|
|
568
|
+
kotlin: ["call_expression"],
|
|
569
|
+
swift: ["call_expression"],
|
|
570
|
+
php: ["function_call_expression", "member_call_expression"],
|
|
571
|
+
solidity: ["call_expression"]
|
|
572
|
+
};
|
|
573
|
+
var TEST_PATTERNS = [
|
|
574
|
+
/^test_/,
|
|
575
|
+
/^Test/,
|
|
576
|
+
/_test$/,
|
|
577
|
+
/\.test\./,
|
|
578
|
+
/\.spec\./,
|
|
579
|
+
/_spec$/
|
|
580
|
+
];
|
|
581
|
+
var TEST_FILE_PATTERNS = [
|
|
582
|
+
/test_.*\.py$/,
|
|
583
|
+
/.*_test\.py$/,
|
|
584
|
+
/.*\.test\.[jt]sx?$/,
|
|
585
|
+
/.*\.spec\.[jt]sx?$/,
|
|
586
|
+
/.*_test\.go$/,
|
|
587
|
+
/tests?\//
|
|
588
|
+
];
|
|
589
|
+
function isTestFile(filePath) {
|
|
590
|
+
return TEST_FILE_PATTERNS.some((p) => p.test(filePath));
|
|
591
|
+
}
|
|
592
|
+
var TEST_RUNNER_NAMES = /* @__PURE__ */ new Set([
|
|
593
|
+
"describe",
|
|
594
|
+
"it",
|
|
595
|
+
"test",
|
|
596
|
+
"beforeEach",
|
|
597
|
+
"afterEach",
|
|
598
|
+
"beforeAll",
|
|
599
|
+
"afterAll"
|
|
600
|
+
]);
|
|
601
|
+
function isTestFunction(name, filePath) {
|
|
602
|
+
if (TEST_PATTERNS.some((p) => p.test(name))) {
|
|
603
|
+
return true;
|
|
604
|
+
}
|
|
605
|
+
if (isTestFile(filePath) && TEST_RUNNER_NAMES.has(name)) {
|
|
606
|
+
return true;
|
|
607
|
+
}
|
|
608
|
+
return false;
|
|
609
|
+
}
|
|
610
|
+
function fileHash(filePath) {
|
|
611
|
+
const buf = fs2.readFileSync(filePath);
|
|
612
|
+
return crypto.createHash("sha256").update(buf).digest("hex");
|
|
613
|
+
}
|
|
614
|
+
var MAX_AST_DEPTH = 180;
|
|
615
|
+
var MODULE_CACHE_MAX = 15e3;
|
|
616
|
+
var CodeParser = class {
|
|
617
|
+
parsers = /* @__PURE__ */ new Map();
|
|
618
|
+
moduleFileCache = /* @__PURE__ */ new Map();
|
|
619
|
+
getParser(language) {
|
|
620
|
+
if (this.parsers.has(language)) {
|
|
621
|
+
return this.parsers.get(language) ?? null;
|
|
622
|
+
}
|
|
623
|
+
try {
|
|
624
|
+
const lang = getLanguage(language);
|
|
625
|
+
if (!lang) {
|
|
626
|
+
return null;
|
|
627
|
+
}
|
|
628
|
+
const p = new Parser();
|
|
629
|
+
p.setLanguage(lang);
|
|
630
|
+
this.parsers.set(language, p);
|
|
631
|
+
return p;
|
|
632
|
+
} catch {
|
|
633
|
+
return null;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
detectLanguage(filePath) {
|
|
637
|
+
const ext = path2.extname(filePath).toLowerCase();
|
|
638
|
+
return EXTENSION_TO_LANGUAGE[ext] ?? null;
|
|
639
|
+
}
|
|
640
|
+
parseFile(filePath) {
|
|
641
|
+
let source;
|
|
642
|
+
try {
|
|
643
|
+
source = fs2.readFileSync(filePath);
|
|
644
|
+
} catch {
|
|
645
|
+
return [[], []];
|
|
646
|
+
}
|
|
647
|
+
return this.parseBytes(filePath, source);
|
|
648
|
+
}
|
|
649
|
+
parseBytes(filePath, source) {
|
|
650
|
+
const language = this.detectLanguage(filePath);
|
|
651
|
+
if (!language) {
|
|
652
|
+
return [[], []];
|
|
653
|
+
}
|
|
654
|
+
if (language === "vue") {
|
|
655
|
+
return this._parseVue(filePath, source);
|
|
656
|
+
}
|
|
657
|
+
const parser = this.getParser(language);
|
|
658
|
+
if (!parser) {
|
|
659
|
+
return [[], []];
|
|
660
|
+
}
|
|
661
|
+
const tree = parser.parse(source.toString("utf-8"));
|
|
662
|
+
const nodes = [];
|
|
663
|
+
const edges = [];
|
|
664
|
+
const testFile = isTestFile(filePath);
|
|
665
|
+
const lineCount = source.toString("utf-8").split("\n").length;
|
|
666
|
+
nodes.push({
|
|
667
|
+
kind: "File",
|
|
668
|
+
name: filePath,
|
|
669
|
+
filePath,
|
|
670
|
+
lineStart: 1,
|
|
671
|
+
lineEnd: lineCount,
|
|
672
|
+
language,
|
|
673
|
+
isTest: testFile
|
|
674
|
+
});
|
|
675
|
+
const [importMap, definedNames] = this._collectFileScope(
|
|
676
|
+
tree.rootNode,
|
|
677
|
+
language,
|
|
678
|
+
source
|
|
679
|
+
);
|
|
680
|
+
this._extractFromTree(
|
|
681
|
+
tree.rootNode,
|
|
682
|
+
source,
|
|
683
|
+
language,
|
|
684
|
+
filePath,
|
|
685
|
+
nodes,
|
|
686
|
+
edges,
|
|
687
|
+
void 0,
|
|
688
|
+
void 0,
|
|
689
|
+
importMap,
|
|
690
|
+
definedNames
|
|
691
|
+
);
|
|
692
|
+
const resolvedEdges = this._resolveCallTargets(nodes, edges, filePath);
|
|
693
|
+
if (testFile) {
|
|
694
|
+
const testQNames = /* @__PURE__ */ new Set();
|
|
695
|
+
for (const n of nodes) {
|
|
696
|
+
if (n.isTest) {
|
|
697
|
+
testQNames.add(this._qualify(n.name, filePath, n.parentName ?? null));
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
const testedBy = [];
|
|
701
|
+
for (const edge of resolvedEdges) {
|
|
702
|
+
if (edge.kind === "CALLS" && testQNames.has(edge.source)) {
|
|
703
|
+
testedBy.push({
|
|
704
|
+
kind: "TESTED_BY",
|
|
705
|
+
source: edge.target,
|
|
706
|
+
target: edge.source,
|
|
707
|
+
filePath: edge.filePath,
|
|
708
|
+
line: edge.line
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
resolvedEdges.push(...testedBy);
|
|
713
|
+
}
|
|
714
|
+
return [nodes, resolvedEdges];
|
|
715
|
+
}
|
|
716
|
+
_parseVue(filePath, source) {
|
|
717
|
+
const vueParser = this.getParser("vue");
|
|
718
|
+
if (!vueParser) {
|
|
719
|
+
return [[], []];
|
|
720
|
+
}
|
|
721
|
+
const tree = vueParser.parse(source.toString("utf-8"));
|
|
722
|
+
const testFile = isTestFile(filePath);
|
|
723
|
+
const lineCount = source.toString("utf-8").split("\n").length;
|
|
724
|
+
const allNodes = [{
|
|
725
|
+
kind: "File",
|
|
726
|
+
name: filePath,
|
|
727
|
+
filePath,
|
|
728
|
+
lineStart: 1,
|
|
729
|
+
lineEnd: lineCount,
|
|
730
|
+
language: "vue",
|
|
731
|
+
isTest: testFile
|
|
732
|
+
}];
|
|
733
|
+
const allEdges = [];
|
|
734
|
+
for (const child of tree.rootNode.children) {
|
|
735
|
+
if (child.type !== "script_element") {
|
|
736
|
+
continue;
|
|
737
|
+
}
|
|
738
|
+
let scriptLang = "javascript";
|
|
739
|
+
let startTag = null;
|
|
740
|
+
let rawTextNode = null;
|
|
741
|
+
for (const sub of child.children) {
|
|
742
|
+
if (sub.type === "start_tag") {
|
|
743
|
+
startTag = sub;
|
|
744
|
+
} else if (sub.type === "raw_text") {
|
|
745
|
+
rawTextNode = sub;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
if (startTag) {
|
|
749
|
+
for (const attr of startTag.children) {
|
|
750
|
+
if (attr.type === "attribute") {
|
|
751
|
+
let attrName = null;
|
|
752
|
+
let attrValue = null;
|
|
753
|
+
for (const a of attr.children) {
|
|
754
|
+
if (a.type === "attribute_name") {
|
|
755
|
+
attrName = a.text;
|
|
756
|
+
} else if (a.type === "quoted_attribute_value") {
|
|
757
|
+
for (const v of a.children) {
|
|
758
|
+
if (v.type === "attribute_value") {
|
|
759
|
+
attrValue = v.text;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
if (attrName === "lang" && (attrValue === "ts" || attrValue === "typescript")) {
|
|
765
|
+
scriptLang = "typescript";
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
if (!rawTextNode) {
|
|
771
|
+
continue;
|
|
772
|
+
}
|
|
773
|
+
const scriptSource = Buffer.from(rawTextNode.text, "utf-8");
|
|
774
|
+
const lineOffset = rawTextNode.startPosition.row;
|
|
775
|
+
const scriptParser = this.getParser(scriptLang);
|
|
776
|
+
if (!scriptParser) {
|
|
777
|
+
continue;
|
|
778
|
+
}
|
|
779
|
+
const scriptTree = scriptParser.parse(scriptSource.toString("utf-8"));
|
|
780
|
+
const [importMap, definedNames] = this._collectFileScope(
|
|
781
|
+
scriptTree.rootNode,
|
|
782
|
+
scriptLang,
|
|
783
|
+
scriptSource
|
|
784
|
+
);
|
|
785
|
+
const nodes = [];
|
|
786
|
+
const edges = [];
|
|
787
|
+
this._extractFromTree(
|
|
788
|
+
scriptTree.rootNode,
|
|
789
|
+
scriptSource,
|
|
790
|
+
scriptLang,
|
|
791
|
+
filePath,
|
|
792
|
+
nodes,
|
|
793
|
+
edges,
|
|
794
|
+
void 0,
|
|
795
|
+
void 0,
|
|
796
|
+
importMap,
|
|
797
|
+
definedNames
|
|
798
|
+
);
|
|
799
|
+
for (const n of nodes) {
|
|
800
|
+
n.lineStart += lineOffset;
|
|
801
|
+
n.lineEnd += lineOffset;
|
|
802
|
+
n.language = "vue";
|
|
803
|
+
}
|
|
804
|
+
for (const e of edges) {
|
|
805
|
+
e.line = (e.line ?? 0) + lineOffset;
|
|
806
|
+
}
|
|
807
|
+
allNodes.push(...nodes);
|
|
808
|
+
allEdges.push(...edges);
|
|
809
|
+
}
|
|
810
|
+
if (testFile) {
|
|
811
|
+
const testQNames = /* @__PURE__ */ new Set();
|
|
812
|
+
for (const n of allNodes) {
|
|
813
|
+
if (n.isTest) {
|
|
814
|
+
testQNames.add(this._qualify(n.name, filePath, n.parentName ?? null));
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
const testedBy = [];
|
|
818
|
+
for (const edge of allEdges) {
|
|
819
|
+
if (edge.kind === "CALLS" && testQNames.has(edge.source)) {
|
|
820
|
+
testedBy.push({
|
|
821
|
+
kind: "TESTED_BY",
|
|
822
|
+
source: edge.target,
|
|
823
|
+
target: edge.source,
|
|
824
|
+
filePath: edge.filePath,
|
|
825
|
+
line: edge.line
|
|
826
|
+
});
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
allEdges.push(...testedBy);
|
|
830
|
+
}
|
|
831
|
+
return [allNodes, allEdges];
|
|
832
|
+
}
|
|
833
|
+
_resolveCallTargets(nodes, edges, filePath) {
|
|
834
|
+
const symbols = /* @__PURE__ */ new Map();
|
|
835
|
+
for (const node of nodes) {
|
|
836
|
+
if (["Function", "Class", "Type", "Test"].includes(node.kind)) {
|
|
837
|
+
const bare = node.name;
|
|
838
|
+
if (!symbols.has(bare)) {
|
|
839
|
+
symbols.set(bare, this._qualify(bare, filePath, node.parentName ?? null));
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
return edges.map((edge) => {
|
|
844
|
+
if (edge.kind === "CALLS" && !edge.target.includes("::")) {
|
|
845
|
+
const qualified = symbols.get(edge.target);
|
|
846
|
+
if (qualified) {
|
|
847
|
+
return { ...edge, target: qualified };
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
return edge;
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
_extractFromTree(root, source, language, filePath, nodes, edges, enclosingClass, enclosingFunc, importMap, definedNames, depth = 0) {
|
|
854
|
+
if (depth > MAX_AST_DEPTH) {
|
|
855
|
+
return;
|
|
856
|
+
}
|
|
857
|
+
const classTypes = new Set(CLASS_TYPES[language] ?? []);
|
|
858
|
+
const funcTypes = new Set(FUNCTION_TYPES[language] ?? []);
|
|
859
|
+
const importTypes = new Set(IMPORT_TYPES[language] ?? []);
|
|
860
|
+
const callTypes = new Set(CALL_TYPES[language] ?? []);
|
|
861
|
+
for (const child of root.children) {
|
|
862
|
+
const nodeType = child.type;
|
|
863
|
+
if (classTypes.has(nodeType)) {
|
|
864
|
+
const name = this._getName(child, language, "class");
|
|
865
|
+
if (name) {
|
|
866
|
+
nodes.push({
|
|
867
|
+
kind: "Class",
|
|
868
|
+
name,
|
|
869
|
+
filePath,
|
|
870
|
+
lineStart: child.startPosition.row + 1,
|
|
871
|
+
lineEnd: child.endPosition.row + 1,
|
|
872
|
+
language,
|
|
873
|
+
parentName: enclosingClass ?? null
|
|
874
|
+
});
|
|
875
|
+
edges.push({
|
|
876
|
+
kind: "CONTAINS",
|
|
877
|
+
source: filePath,
|
|
878
|
+
target: this._qualify(name, filePath, enclosingClass ?? null),
|
|
879
|
+
filePath,
|
|
880
|
+
line: child.startPosition.row + 1
|
|
881
|
+
});
|
|
882
|
+
const bases = this._getBases(child, language);
|
|
883
|
+
for (const base of bases) {
|
|
884
|
+
edges.push({
|
|
885
|
+
kind: "INHERITS",
|
|
886
|
+
source: this._qualify(name, filePath, enclosingClass ?? null),
|
|
887
|
+
target: base,
|
|
888
|
+
filePath,
|
|
889
|
+
line: child.startPosition.row + 1
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
this._extractFromTree(
|
|
893
|
+
child,
|
|
894
|
+
source,
|
|
895
|
+
language,
|
|
896
|
+
filePath,
|
|
897
|
+
nodes,
|
|
898
|
+
edges,
|
|
899
|
+
name,
|
|
900
|
+
null,
|
|
901
|
+
importMap,
|
|
902
|
+
definedNames,
|
|
903
|
+
depth + 1
|
|
904
|
+
);
|
|
905
|
+
continue;
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
if (funcTypes.has(nodeType)) {
|
|
909
|
+
const name = this._getName(child, language, "function");
|
|
910
|
+
if (name) {
|
|
911
|
+
const isTest = isTestFunction(name, filePath);
|
|
912
|
+
const kind = isTest ? "Test" : "Function";
|
|
913
|
+
const qualified = this._qualify(name, filePath, enclosingClass ?? null);
|
|
914
|
+
const params = this._getParams(child, language);
|
|
915
|
+
const retType = this._getReturnType(child, language);
|
|
916
|
+
nodes.push({
|
|
917
|
+
kind,
|
|
918
|
+
name,
|
|
919
|
+
filePath,
|
|
920
|
+
lineStart: child.startPosition.row + 1,
|
|
921
|
+
lineEnd: child.endPosition.row + 1,
|
|
922
|
+
language,
|
|
923
|
+
parentName: enclosingClass ?? null,
|
|
924
|
+
params,
|
|
925
|
+
returnType: retType,
|
|
926
|
+
isTest
|
|
927
|
+
});
|
|
928
|
+
const container = enclosingClass ? this._qualify(enclosingClass, filePath, null) : filePath;
|
|
929
|
+
edges.push({
|
|
930
|
+
kind: "CONTAINS",
|
|
931
|
+
source: container,
|
|
932
|
+
target: qualified,
|
|
933
|
+
filePath,
|
|
934
|
+
line: child.startPosition.row + 1
|
|
935
|
+
});
|
|
936
|
+
if (language === "solidity") {
|
|
937
|
+
for (const sub of child.children) {
|
|
938
|
+
if (sub.type === "modifier_invocation") {
|
|
939
|
+
for (const ident of sub.children) {
|
|
940
|
+
if (ident.type === "identifier") {
|
|
941
|
+
edges.push({
|
|
942
|
+
kind: "CALLS",
|
|
943
|
+
source: qualified,
|
|
944
|
+
target: ident.text,
|
|
945
|
+
filePath,
|
|
946
|
+
line: sub.startPosition.row + 1
|
|
947
|
+
});
|
|
948
|
+
break;
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
this._extractFromTree(
|
|
955
|
+
child,
|
|
956
|
+
source,
|
|
957
|
+
language,
|
|
958
|
+
filePath,
|
|
959
|
+
nodes,
|
|
960
|
+
edges,
|
|
961
|
+
enclosingClass,
|
|
962
|
+
name,
|
|
963
|
+
importMap,
|
|
964
|
+
definedNames,
|
|
965
|
+
depth + 1
|
|
966
|
+
);
|
|
967
|
+
continue;
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
if (importTypes.has(nodeType)) {
|
|
971
|
+
const imports = this._extractImport(child, language);
|
|
972
|
+
for (const impTarget of imports) {
|
|
973
|
+
edges.push({
|
|
974
|
+
kind: "IMPORTS_FROM",
|
|
975
|
+
source: filePath,
|
|
976
|
+
target: impTarget,
|
|
977
|
+
filePath,
|
|
978
|
+
line: child.startPosition.row + 1
|
|
979
|
+
});
|
|
980
|
+
}
|
|
981
|
+
continue;
|
|
982
|
+
}
|
|
983
|
+
if (callTypes.has(nodeType)) {
|
|
984
|
+
const callName = this._getCallName(child, language);
|
|
985
|
+
if (callName && enclosingFunc) {
|
|
986
|
+
const caller = this._qualify(enclosingFunc, filePath, enclosingClass ?? null);
|
|
987
|
+
const target = this._resolveCallTarget(
|
|
988
|
+
callName,
|
|
989
|
+
filePath,
|
|
990
|
+
language,
|
|
991
|
+
importMap ?? /* @__PURE__ */ new Map(),
|
|
992
|
+
definedNames ?? /* @__PURE__ */ new Set()
|
|
993
|
+
);
|
|
994
|
+
edges.push({
|
|
995
|
+
kind: "CALLS",
|
|
996
|
+
source: caller,
|
|
997
|
+
target,
|
|
998
|
+
filePath,
|
|
999
|
+
line: child.startPosition.row + 1
|
|
1000
|
+
});
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
if (language === "solidity") {
|
|
1004
|
+
if (nodeType === "emit_statement" && enclosingFunc) {
|
|
1005
|
+
for (const sub of child.children) {
|
|
1006
|
+
if (sub.type === "expression") {
|
|
1007
|
+
for (const ident of sub.children) {
|
|
1008
|
+
if (ident.type === "identifier") {
|
|
1009
|
+
const caller = this._qualify(
|
|
1010
|
+
enclosingFunc,
|
|
1011
|
+
filePath,
|
|
1012
|
+
enclosingClass ?? null
|
|
1013
|
+
);
|
|
1014
|
+
edges.push({
|
|
1015
|
+
kind: "CALLS",
|
|
1016
|
+
source: caller,
|
|
1017
|
+
target: ident.text,
|
|
1018
|
+
filePath,
|
|
1019
|
+
line: child.startPosition.row + 1
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
if (nodeType === "state_variable_declaration" && enclosingClass) {
|
|
1027
|
+
let varName = null;
|
|
1028
|
+
let varVisibility = null;
|
|
1029
|
+
let varType = null;
|
|
1030
|
+
let varMutability = null;
|
|
1031
|
+
for (const sub of child.children) {
|
|
1032
|
+
if (sub.type === "identifier") {
|
|
1033
|
+
varName = sub.text;
|
|
1034
|
+
} else if (sub.type === "visibility") {
|
|
1035
|
+
varVisibility = sub.text;
|
|
1036
|
+
} else if (sub.type === "type_name") {
|
|
1037
|
+
varType = sub.text;
|
|
1038
|
+
} else if (sub.type === "constant" || sub.type === "immutable") {
|
|
1039
|
+
varMutability = sub.type;
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
if (varName) {
|
|
1043
|
+
const qualified = this._qualify(varName, filePath, enclosingClass);
|
|
1044
|
+
nodes.push({
|
|
1045
|
+
kind: "Function",
|
|
1046
|
+
name: varName,
|
|
1047
|
+
filePath,
|
|
1048
|
+
lineStart: child.startPosition.row + 1,
|
|
1049
|
+
lineEnd: child.endPosition.row + 1,
|
|
1050
|
+
language,
|
|
1051
|
+
parentName: enclosingClass,
|
|
1052
|
+
returnType: varType,
|
|
1053
|
+
modifiers: varVisibility,
|
|
1054
|
+
extra: { solidity_kind: "state_variable", mutability: varMutability }
|
|
1055
|
+
});
|
|
1056
|
+
edges.push({
|
|
1057
|
+
kind: "CONTAINS",
|
|
1058
|
+
source: this._qualify(enclosingClass, filePath, null),
|
|
1059
|
+
target: qualified,
|
|
1060
|
+
filePath,
|
|
1061
|
+
line: child.startPosition.row + 1
|
|
1062
|
+
});
|
|
1063
|
+
continue;
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
if (nodeType === "constant_variable_declaration") {
|
|
1067
|
+
let varName = null;
|
|
1068
|
+
let varType = null;
|
|
1069
|
+
for (const sub of child.children) {
|
|
1070
|
+
if (sub.type === "identifier") {
|
|
1071
|
+
varName = sub.text;
|
|
1072
|
+
} else if (sub.type === "type_name") {
|
|
1073
|
+
varType = sub.text;
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
if (varName) {
|
|
1077
|
+
const qualified = this._qualify(varName, filePath, enclosingClass ?? null);
|
|
1078
|
+
nodes.push({
|
|
1079
|
+
kind: "Function",
|
|
1080
|
+
name: varName,
|
|
1081
|
+
filePath,
|
|
1082
|
+
lineStart: child.startPosition.row + 1,
|
|
1083
|
+
lineEnd: child.endPosition.row + 1,
|
|
1084
|
+
language,
|
|
1085
|
+
parentName: enclosingClass ?? null,
|
|
1086
|
+
returnType: varType,
|
|
1087
|
+
extra: { solidity_kind: "constant" }
|
|
1088
|
+
});
|
|
1089
|
+
const container = enclosingClass ? this._qualify(enclosingClass, filePath, null) : filePath;
|
|
1090
|
+
edges.push({
|
|
1091
|
+
kind: "CONTAINS",
|
|
1092
|
+
source: container,
|
|
1093
|
+
target: qualified,
|
|
1094
|
+
filePath,
|
|
1095
|
+
line: child.startPosition.row + 1
|
|
1096
|
+
});
|
|
1097
|
+
continue;
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
if (nodeType === "using_directive") {
|
|
1101
|
+
let libName = null;
|
|
1102
|
+
for (const sub of child.children) {
|
|
1103
|
+
if (sub.type === "type_alias") {
|
|
1104
|
+
for (const ident of sub.children) {
|
|
1105
|
+
if (ident.type === "identifier") {
|
|
1106
|
+
libName = ident.text;
|
|
1107
|
+
break;
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
if (libName) {
|
|
1113
|
+
const sourceName = enclosingClass ? this._qualify(enclosingClass, filePath, null) : filePath;
|
|
1114
|
+
edges.push({
|
|
1115
|
+
kind: "DEPENDS_ON",
|
|
1116
|
+
source: sourceName,
|
|
1117
|
+
target: libName,
|
|
1118
|
+
filePath,
|
|
1119
|
+
line: child.startPosition.row + 1
|
|
1120
|
+
});
|
|
1121
|
+
}
|
|
1122
|
+
continue;
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
this._extractFromTree(
|
|
1126
|
+
child,
|
|
1127
|
+
source,
|
|
1128
|
+
language,
|
|
1129
|
+
filePath,
|
|
1130
|
+
nodes,
|
|
1131
|
+
edges,
|
|
1132
|
+
enclosingClass,
|
|
1133
|
+
enclosingFunc,
|
|
1134
|
+
importMap,
|
|
1135
|
+
definedNames,
|
|
1136
|
+
depth + 1
|
|
1137
|
+
);
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
_collectFileScope(root, language, source) {
|
|
1141
|
+
const importMap = /* @__PURE__ */ new Map();
|
|
1142
|
+
const definedNames = /* @__PURE__ */ new Set();
|
|
1143
|
+
const classTypes = new Set(CLASS_TYPES[language] ?? []);
|
|
1144
|
+
const funcTypes = new Set(FUNCTION_TYPES[language] ?? []);
|
|
1145
|
+
const importTypes = new Set(IMPORT_TYPES[language] ?? []);
|
|
1146
|
+
const decoratorWrappers = /* @__PURE__ */ new Set(["decorated_definition", "decorator"]);
|
|
1147
|
+
for (const child of root.children) {
|
|
1148
|
+
const nodeType = child.type;
|
|
1149
|
+
let target = child;
|
|
1150
|
+
if (decoratorWrappers.has(nodeType)) {
|
|
1151
|
+
for (const inner of child.children) {
|
|
1152
|
+
if (funcTypes.has(inner.type) || classTypes.has(inner.type)) {
|
|
1153
|
+
target = inner;
|
|
1154
|
+
break;
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
const targetType = target.type;
|
|
1159
|
+
if (funcTypes.has(targetType) || classTypes.has(targetType)) {
|
|
1160
|
+
const name = this._getName(
|
|
1161
|
+
target,
|
|
1162
|
+
language,
|
|
1163
|
+
classTypes.has(targetType) ? "class" : "function"
|
|
1164
|
+
);
|
|
1165
|
+
if (name) {
|
|
1166
|
+
definedNames.add(name);
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
if (importTypes.has(nodeType)) {
|
|
1170
|
+
this._collectImportNames(child, language, source, importMap);
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
return [importMap, definedNames];
|
|
1174
|
+
}
|
|
1175
|
+
_collectImportNames(node, language, _source, importMap) {
|
|
1176
|
+
if (language === "python") {
|
|
1177
|
+
if (node.type === "import_from_statement") {
|
|
1178
|
+
let module = null;
|
|
1179
|
+
let seenImport = false;
|
|
1180
|
+
for (const child of node.children) {
|
|
1181
|
+
if (child.type === "dotted_name" && !seenImport) {
|
|
1182
|
+
module = child.text;
|
|
1183
|
+
} else if (child.type === "import") {
|
|
1184
|
+
seenImport = true;
|
|
1185
|
+
} else if (seenImport && module) {
|
|
1186
|
+
if (child.type === "identifier" || child.type === "dotted_name") {
|
|
1187
|
+
importMap.set(child.text, module);
|
|
1188
|
+
} else if (child.type === "aliased_import") {
|
|
1189
|
+
const names = child.children.filter(
|
|
1190
|
+
(c) => c.type === "identifier" || c.type === "dotted_name"
|
|
1191
|
+
).map((c) => c.text);
|
|
1192
|
+
if (names.length > 0) {
|
|
1193
|
+
importMap.set(names[names.length - 1], module);
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
} else if (language === "javascript" || language === "typescript" || language === "tsx") {
|
|
1200
|
+
let module = null;
|
|
1201
|
+
for (const child of node.children) {
|
|
1202
|
+
if (child.type === "string") {
|
|
1203
|
+
module = child.text.replace(/^['"]|['"]$/g, "");
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
if (module) {
|
|
1207
|
+
for (const child of node.children) {
|
|
1208
|
+
if (child.type === "import_clause") {
|
|
1209
|
+
this._collectJsImportNames(child, module, importMap);
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
_collectJsImportNames(clauseNode, module, importMap) {
|
|
1216
|
+
for (const child of clauseNode.children) {
|
|
1217
|
+
if (child.type === "identifier") {
|
|
1218
|
+
importMap.set(child.text, module);
|
|
1219
|
+
} else if (child.type === "named_imports") {
|
|
1220
|
+
for (const spec of child.children) {
|
|
1221
|
+
if (spec.type === "import_specifier") {
|
|
1222
|
+
const names = spec.children.filter(
|
|
1223
|
+
(s) => s.type === "identifier" || s.type === "property_identifier"
|
|
1224
|
+
).map((s) => s.text);
|
|
1225
|
+
if (names.length > 0) {
|
|
1226
|
+
importMap.set(names[names.length - 1], module);
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
_resolveModuleToFile(module, filePath, language) {
|
|
1234
|
+
const callerDir = path2.dirname(filePath);
|
|
1235
|
+
const cacheKey = `${language}:${callerDir}:${module}`;
|
|
1236
|
+
if (this.moduleFileCache.has(cacheKey)) {
|
|
1237
|
+
return this.moduleFileCache.get(cacheKey) ?? null;
|
|
1238
|
+
}
|
|
1239
|
+
const resolved = this._doResolveModule(module, filePath, language);
|
|
1240
|
+
if (this.moduleFileCache.size >= MODULE_CACHE_MAX) {
|
|
1241
|
+
this.moduleFileCache.clear();
|
|
1242
|
+
}
|
|
1243
|
+
this.moduleFileCache.set(cacheKey, resolved);
|
|
1244
|
+
return resolved;
|
|
1245
|
+
}
|
|
1246
|
+
_doResolveModule(module, filePath, language) {
|
|
1247
|
+
const callerDir = path2.dirname(filePath);
|
|
1248
|
+
if (language === "python") {
|
|
1249
|
+
const relPath = module.replace(/\./g, "/");
|
|
1250
|
+
const candidates = [`${relPath}.py`, `${relPath}/__init__.py`];
|
|
1251
|
+
let current = callerDir;
|
|
1252
|
+
while (true) {
|
|
1253
|
+
for (const candidate of candidates) {
|
|
1254
|
+
const target = path2.join(current, candidate);
|
|
1255
|
+
if (fs2.existsSync(target)) {
|
|
1256
|
+
return path2.resolve(target);
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
const parent = path2.dirname(current);
|
|
1260
|
+
if (parent === current) {
|
|
1261
|
+
break;
|
|
1262
|
+
}
|
|
1263
|
+
current = parent;
|
|
1264
|
+
}
|
|
1265
|
+
} else if (language === "javascript" || language === "typescript" || language === "tsx" || language === "vue") {
|
|
1266
|
+
if (module.startsWith(".")) {
|
|
1267
|
+
const base = path2.join(callerDir, module);
|
|
1268
|
+
const extensions = [".ts", ".tsx", ".js", ".jsx", ".vue"];
|
|
1269
|
+
if (fs2.existsSync(base) && fs2.statSync(base).isFile()) {
|
|
1270
|
+
return path2.resolve(base);
|
|
1271
|
+
}
|
|
1272
|
+
for (const ext of extensions) {
|
|
1273
|
+
const target = base + ext;
|
|
1274
|
+
if (fs2.existsSync(target)) {
|
|
1275
|
+
return path2.resolve(target);
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
if (fs2.existsSync(base) && fs2.statSync(base).isDirectory()) {
|
|
1279
|
+
for (const ext of extensions) {
|
|
1280
|
+
const target = path2.join(base, `index${ext}`);
|
|
1281
|
+
if (fs2.existsSync(target)) {
|
|
1282
|
+
return path2.resolve(target);
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
}
|
|
1288
|
+
return null;
|
|
1289
|
+
}
|
|
1290
|
+
_resolveCallTarget(callName, filePath, language, importMap, definedNames) {
|
|
1291
|
+
if (definedNames.has(callName)) {
|
|
1292
|
+
return this._qualify(callName, filePath, null);
|
|
1293
|
+
}
|
|
1294
|
+
const importedFrom = importMap.get(callName);
|
|
1295
|
+
if (importedFrom) {
|
|
1296
|
+
const resolved = this._resolveModuleToFile(importedFrom, filePath, language);
|
|
1297
|
+
if (resolved) {
|
|
1298
|
+
return this._qualify(callName, resolved, null);
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
return callName;
|
|
1302
|
+
}
|
|
1303
|
+
_qualify(name, filePath, enclosingClass) {
|
|
1304
|
+
if (enclosingClass) {
|
|
1305
|
+
return `${filePath}::${enclosingClass}.${name}`;
|
|
1306
|
+
}
|
|
1307
|
+
return `${filePath}::${name}`;
|
|
1308
|
+
}
|
|
1309
|
+
_getName(node, language, kind) {
|
|
1310
|
+
if (language === "solidity") {
|
|
1311
|
+
if (node.type === "constructor_definition") {
|
|
1312
|
+
return "constructor";
|
|
1313
|
+
}
|
|
1314
|
+
if (node.type === "fallback_receive_definition") {
|
|
1315
|
+
for (const child of node.children) {
|
|
1316
|
+
if (child.type === "receive" || child.type === "fallback") {
|
|
1317
|
+
return child.text;
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
if ((language === "c" || language === "cpp") && kind === "function") {
|
|
1323
|
+
for (const child of node.children) {
|
|
1324
|
+
if (child.type === "function_declarator" || child.type === "pointer_declarator") {
|
|
1325
|
+
const result = this._getName(child, language, kind);
|
|
1326
|
+
if (result) {
|
|
1327
|
+
return result;
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
for (const child of node.children) {
|
|
1333
|
+
if ([
|
|
1334
|
+
"identifier",
|
|
1335
|
+
"name",
|
|
1336
|
+
"type_identifier",
|
|
1337
|
+
"property_identifier",
|
|
1338
|
+
"simple_identifier",
|
|
1339
|
+
"constant"
|
|
1340
|
+
].includes(child.type)) {
|
|
1341
|
+
return child.text;
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
if (language === "go" && node.type === "type_declaration") {
|
|
1345
|
+
for (const child of node.children) {
|
|
1346
|
+
if (child.type === "type_spec") {
|
|
1347
|
+
return this._getName(child, language, kind);
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
return null;
|
|
1352
|
+
}
|
|
1353
|
+
_getParams(node, language) {
|
|
1354
|
+
for (const child of node.children) {
|
|
1355
|
+
if (["parameters", "formal_parameters", "parameter_list"].includes(child.type)) {
|
|
1356
|
+
return child.text;
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
if (language === "solidity") {
|
|
1360
|
+
const params = node.children.filter((c) => c.type === "parameter").map((c) => c.text);
|
|
1361
|
+
if (params.length > 0) {
|
|
1362
|
+
return `(${params.join(", ")})`;
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
return null;
|
|
1366
|
+
}
|
|
1367
|
+
_getReturnType(node, language) {
|
|
1368
|
+
for (const child of node.children) {
|
|
1369
|
+
if ([
|
|
1370
|
+
"type",
|
|
1371
|
+
"return_type",
|
|
1372
|
+
"type_annotation",
|
|
1373
|
+
"return_type_definition"
|
|
1374
|
+
].includes(child.type)) {
|
|
1375
|
+
return child.text;
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
if (language === "python") {
|
|
1379
|
+
for (let i = 0; i < node.children.length - 1; i++) {
|
|
1380
|
+
if (node.children[i].type === "->") {
|
|
1381
|
+
return node.children[i + 1].text;
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
return null;
|
|
1386
|
+
}
|
|
1387
|
+
_getBases(node, language) {
|
|
1388
|
+
const bases = [];
|
|
1389
|
+
if (language === "python") {
|
|
1390
|
+
for (const child of node.children) {
|
|
1391
|
+
if (child.type === "argument_list") {
|
|
1392
|
+
for (const arg of child.children) {
|
|
1393
|
+
if (arg.type === "identifier" || arg.type === "attribute") {
|
|
1394
|
+
bases.push(arg.text);
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
} else if (language === "java" || language === "csharp" || language === "kotlin") {
|
|
1400
|
+
for (const child of node.children) {
|
|
1401
|
+
if ([
|
|
1402
|
+
"superclass",
|
|
1403
|
+
"super_interfaces",
|
|
1404
|
+
"extends_type",
|
|
1405
|
+
"implements_type",
|
|
1406
|
+
"type_identifier",
|
|
1407
|
+
"supertype",
|
|
1408
|
+
"delegation_specifier"
|
|
1409
|
+
].includes(child.type)) {
|
|
1410
|
+
bases.push(child.text);
|
|
1411
|
+
}
|
|
1412
|
+
}
|
|
1413
|
+
} else if (language === "cpp") {
|
|
1414
|
+
for (const child of node.children) {
|
|
1415
|
+
if (child.type === "base_class_clause") {
|
|
1416
|
+
for (const sub of child.children) {
|
|
1417
|
+
if (sub.type === "type_identifier") {
|
|
1418
|
+
bases.push(sub.text);
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
} else if (language === "typescript" || language === "javascript" || language === "tsx") {
|
|
1424
|
+
for (const child of node.children) {
|
|
1425
|
+
if (child.type === "extends_clause" || child.type === "implements_clause") {
|
|
1426
|
+
for (const sub of child.children) {
|
|
1427
|
+
if ([
|
|
1428
|
+
"identifier",
|
|
1429
|
+
"type_identifier",
|
|
1430
|
+
"nested_identifier"
|
|
1431
|
+
].includes(sub.type)) {
|
|
1432
|
+
bases.push(sub.text);
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
} else if (language === "solidity") {
|
|
1438
|
+
for (const child of node.children) {
|
|
1439
|
+
if (child.type === "inheritance_specifier") {
|
|
1440
|
+
for (const sub of child.children) {
|
|
1441
|
+
if (sub.type === "user_defined_type") {
|
|
1442
|
+
for (const ident of sub.children) {
|
|
1443
|
+
if (ident.type === "identifier") {
|
|
1444
|
+
bases.push(ident.text);
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
} else if (language === "go") {
|
|
1452
|
+
for (const child of node.children) {
|
|
1453
|
+
if (child.type === "type_spec") {
|
|
1454
|
+
for (const sub of child.children) {
|
|
1455
|
+
if (sub.type === "struct_type" || sub.type === "interface_type") {
|
|
1456
|
+
for (const fieldNode of sub.children) {
|
|
1457
|
+
if (fieldNode.type === "field_declaration_list") {
|
|
1458
|
+
for (const f of fieldNode.children) {
|
|
1459
|
+
if (f.type === "type_identifier") {
|
|
1460
|
+
bases.push(f.text);
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
return bases;
|
|
1471
|
+
}
|
|
1472
|
+
_extractImport(node, language) {
|
|
1473
|
+
const imports = [];
|
|
1474
|
+
const text = node.text.trim();
|
|
1475
|
+
if (language === "python") {
|
|
1476
|
+
if (node.type === "import_from_statement") {
|
|
1477
|
+
for (const child of node.children) {
|
|
1478
|
+
if (child.type === "dotted_name") {
|
|
1479
|
+
imports.push(child.text);
|
|
1480
|
+
break;
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
1483
|
+
} else {
|
|
1484
|
+
for (const child of node.children) {
|
|
1485
|
+
if (child.type === "dotted_name") {
|
|
1486
|
+
imports.push(child.text);
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1489
|
+
}
|
|
1490
|
+
} else if (language === "javascript" || language === "typescript" || language === "tsx") {
|
|
1491
|
+
for (const child of node.children) {
|
|
1492
|
+
if (child.type === "string") {
|
|
1493
|
+
imports.push(child.text.replace(/^['"]|['"]$/g, ""));
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
} else if (language === "go") {
|
|
1497
|
+
for (const child of node.children) {
|
|
1498
|
+
if (child.type === "import_spec_list") {
|
|
1499
|
+
for (const spec of child.children) {
|
|
1500
|
+
if (spec.type === "import_spec") {
|
|
1501
|
+
for (const s of spec.children) {
|
|
1502
|
+
if (s.type === "interpreted_string_literal") {
|
|
1503
|
+
imports.push(s.text.replace(/^"|"$/g, ""));
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
} else if (child.type === "import_spec") {
|
|
1509
|
+
for (const s of child.children) {
|
|
1510
|
+
if (s.type === "interpreted_string_literal") {
|
|
1511
|
+
imports.push(s.text.replace(/^"|"$/g, ""));
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
} else if (language === "rust") {
|
|
1517
|
+
imports.push(text.replace(/^use\s+/, "").replace(/;$/, "").trim());
|
|
1518
|
+
} else if (language === "c" || language === "cpp") {
|
|
1519
|
+
for (const child of node.children) {
|
|
1520
|
+
if (child.type === "system_lib_string" || child.type === "string_literal") {
|
|
1521
|
+
imports.push(child.text.replace(/^[<""]|[>""]$/g, ""));
|
|
1522
|
+
}
|
|
1523
|
+
}
|
|
1524
|
+
} else if (language === "java" || language === "csharp") {
|
|
1525
|
+
const parts = text.split(/\s+/);
|
|
1526
|
+
if (parts.length >= 2) {
|
|
1527
|
+
imports.push(parts[parts.length - 1].replace(/;$/, ""));
|
|
1528
|
+
}
|
|
1529
|
+
} else if (language === "solidity") {
|
|
1530
|
+
for (const child of node.children) {
|
|
1531
|
+
if (child.type === "string") {
|
|
1532
|
+
const val = child.text.replace(/^"|"$/g, "");
|
|
1533
|
+
if (val) {
|
|
1534
|
+
imports.push(val);
|
|
1535
|
+
}
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
} else if (language === "ruby") {
|
|
1539
|
+
if (text.includes("require")) {
|
|
1540
|
+
const match = /['"]([^'"]+)['"]/u.exec(text);
|
|
1541
|
+
if (match) {
|
|
1542
|
+
imports.push(match[1]);
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
} else {
|
|
1546
|
+
imports.push(text);
|
|
1547
|
+
}
|
|
1548
|
+
return imports;
|
|
1549
|
+
}
|
|
1550
|
+
_getCallName(node, language) {
|
|
1551
|
+
if (!node.children || node.children.length === 0) {
|
|
1552
|
+
return null;
|
|
1553
|
+
}
|
|
1554
|
+
let first = node.children[0];
|
|
1555
|
+
if (language === "solidity" && first.type === "expression" && first.children.length > 0) {
|
|
1556
|
+
first = first.children[0];
|
|
1557
|
+
}
|
|
1558
|
+
if (first.type === "identifier") {
|
|
1559
|
+
return first.text;
|
|
1560
|
+
}
|
|
1561
|
+
const memberTypes = [
|
|
1562
|
+
"attribute",
|
|
1563
|
+
"member_expression",
|
|
1564
|
+
"field_expression",
|
|
1565
|
+
"selector_expression"
|
|
1566
|
+
];
|
|
1567
|
+
if (memberTypes.includes(first.type)) {
|
|
1568
|
+
for (let i = first.children.length - 1; i >= 0; i--) {
|
|
1569
|
+
const child = first.children[i];
|
|
1570
|
+
if ([
|
|
1571
|
+
"identifier",
|
|
1572
|
+
"property_identifier",
|
|
1573
|
+
"field_identifier",
|
|
1574
|
+
"field_name"
|
|
1575
|
+
].includes(child.type)) {
|
|
1576
|
+
return child.text;
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
return first.text;
|
|
1580
|
+
}
|
|
1581
|
+
if (first.type === "scoped_identifier" || first.type === "qualified_name") {
|
|
1582
|
+
return first.text;
|
|
1583
|
+
}
|
|
1584
|
+
return null;
|
|
1585
|
+
}
|
|
1586
|
+
};
|
|
1587
|
+
|
|
1588
|
+
// src/usecases/buildGraph.ts
|
|
1589
|
+
import crypto2 from "crypto";
|
|
1590
|
+
import fs4 from "fs";
|
|
1591
|
+
import path4 from "path";
|
|
1592
|
+
|
|
1593
|
+
// src/infrastructure/FileSystemHelper.ts
|
|
1594
|
+
import fs3 from "fs";
|
|
1595
|
+
import path3 from "path";
|
|
1596
|
+
import micromatch from "micromatch";
|
|
1597
|
+
|
|
1598
|
+
// src/infrastructure/GitRunner.ts
|
|
1599
|
+
import { spawnSync } from "child_process";
|
|
1600
|
+
var GIT_TIMEOUT_MS = Number(process.env["CRG_GIT_TIMEOUT"] ?? 30) * 1e3;
|
|
1601
|
+
function gitRun(args, cwd) {
|
|
1602
|
+
const result = spawnSync("git", args, {
|
|
1603
|
+
cwd,
|
|
1604
|
+
encoding: "utf-8",
|
|
1605
|
+
timeout: GIT_TIMEOUT_MS,
|
|
1606
|
+
stdio: ["ignore", "pipe", "ignore"]
|
|
1607
|
+
});
|
|
1608
|
+
if (result.error || result.status !== 0) {
|
|
1609
|
+
return "";
|
|
1610
|
+
}
|
|
1611
|
+
return result.stdout ?? "";
|
|
1612
|
+
}
|
|
1613
|
+
function getChangedFiles(repoRoot, base = "HEAD~1") {
|
|
1614
|
+
let output = gitRun(["diff", "--name-only", base], repoRoot);
|
|
1615
|
+
if (!output.trim()) {
|
|
1616
|
+
output = gitRun(["diff", "--name-only", "--cached"], repoRoot);
|
|
1617
|
+
}
|
|
1618
|
+
return output.split("\n").map((f) => f.trim()).filter(Boolean);
|
|
1619
|
+
}
|
|
1620
|
+
function getAllTrackedFiles(repoRoot) {
|
|
1621
|
+
const output = gitRun(["ls-files"], repoRoot);
|
|
1622
|
+
return output.split("\n").map((f) => f.trim()).filter(Boolean);
|
|
1623
|
+
}
|
|
1624
|
+
|
|
1625
|
+
// src/infrastructure/FileSystemHelper.ts
|
|
1626
|
+
var DEFAULT_IGNORE_PATTERNS = [
|
|
1627
|
+
".codeorbit/**",
|
|
1628
|
+
"node_modules/**",
|
|
1629
|
+
".git/**",
|
|
1630
|
+
"__pycache__/**",
|
|
1631
|
+
"*.pyc",
|
|
1632
|
+
".venv/**",
|
|
1633
|
+
"venv/**",
|
|
1634
|
+
"dist/**",
|
|
1635
|
+
"build/**",
|
|
1636
|
+
".next/**",
|
|
1637
|
+
"target/**",
|
|
1638
|
+
"*.min.js",
|
|
1639
|
+
"*.min.css",
|
|
1640
|
+
"*.map",
|
|
1641
|
+
"*.lock",
|
|
1642
|
+
"package-lock.json",
|
|
1643
|
+
"yarn.lock",
|
|
1644
|
+
"*.db",
|
|
1645
|
+
"*.sqlite",
|
|
1646
|
+
"*.db-journal",
|
|
1647
|
+
"*.db-wal"
|
|
1648
|
+
];
|
|
1649
|
+
var PRUNE_DIRS = /* @__PURE__ */ new Set([
|
|
1650
|
+
"node_modules",
|
|
1651
|
+
".git",
|
|
1652
|
+
"__pycache__",
|
|
1653
|
+
".next",
|
|
1654
|
+
".nuxt",
|
|
1655
|
+
".svelte-kit",
|
|
1656
|
+
"dist",
|
|
1657
|
+
"build",
|
|
1658
|
+
".venv",
|
|
1659
|
+
"venv",
|
|
1660
|
+
"env",
|
|
1661
|
+
"target",
|
|
1662
|
+
".mypy_cache",
|
|
1663
|
+
".pytest_cache",
|
|
1664
|
+
"coverage",
|
|
1665
|
+
".tox",
|
|
1666
|
+
".cache",
|
|
1667
|
+
".tmp",
|
|
1668
|
+
"tmp",
|
|
1669
|
+
"temp",
|
|
1670
|
+
"vendor",
|
|
1671
|
+
"Pods",
|
|
1672
|
+
"DerivedData",
|
|
1673
|
+
".gradle",
|
|
1674
|
+
".idea",
|
|
1675
|
+
".vs"
|
|
1676
|
+
]);
|
|
1677
|
+
function findRepoRoot(start) {
|
|
1678
|
+
let current = start ?? process.cwd();
|
|
1679
|
+
while (true) {
|
|
1680
|
+
if (fs3.existsSync(path3.join(current, ".git"))) {
|
|
1681
|
+
return current;
|
|
1682
|
+
}
|
|
1683
|
+
const parent = path3.dirname(current);
|
|
1684
|
+
if (parent === current) {
|
|
1685
|
+
break;
|
|
1686
|
+
}
|
|
1687
|
+
current = parent;
|
|
1688
|
+
}
|
|
1689
|
+
return void 0;
|
|
1690
|
+
}
|
|
1691
|
+
function getDbPath(repoRoot) {
|
|
1692
|
+
const crgDir = path3.join(repoRoot, ".codeorbit");
|
|
1693
|
+
const newDb = path3.join(crgDir, "graph.db");
|
|
1694
|
+
if (!fs3.existsSync(crgDir)) {
|
|
1695
|
+
fs3.mkdirSync(crgDir, { recursive: true });
|
|
1696
|
+
}
|
|
1697
|
+
const innerGitignore = path3.join(crgDir, ".gitignore");
|
|
1698
|
+
if (!fs3.existsSync(innerGitignore)) {
|
|
1699
|
+
fs3.writeFileSync(
|
|
1700
|
+
innerGitignore,
|
|
1701
|
+
"# Auto-generated by codeorbit \u2014 do not commit database files.\n# The graph.db contains absolute paths and code structure metadata.\n*\n"
|
|
1702
|
+
);
|
|
1703
|
+
}
|
|
1704
|
+
const legacyDb = path3.join(repoRoot, ".codeorbit.db");
|
|
1705
|
+
if (fs3.existsSync(legacyDb) && !fs3.existsSync(newDb)) {
|
|
1706
|
+
fs3.renameSync(legacyDb, newDb);
|
|
1707
|
+
}
|
|
1708
|
+
for (const suffix of ["-wal", "-shm", "-journal"]) {
|
|
1709
|
+
const side = legacyDb + suffix;
|
|
1710
|
+
if (fs3.existsSync(side)) {
|
|
1711
|
+
try {
|
|
1712
|
+
fs3.unlinkSync(side);
|
|
1713
|
+
} catch {
|
|
1714
|
+
}
|
|
1715
|
+
}
|
|
1716
|
+
}
|
|
1717
|
+
return newDb;
|
|
1718
|
+
}
|
|
1719
|
+
function loadIgnorePatterns(repoRoot) {
|
|
1720
|
+
const patterns = [...DEFAULT_IGNORE_PATTERNS];
|
|
1721
|
+
const ignoreFile = path3.join(repoRoot, ".codeorbitignore");
|
|
1722
|
+
if (fs3.existsSync(ignoreFile)) {
|
|
1723
|
+
for (const line of fs3.readFileSync(ignoreFile, "utf-8").split("\n")) {
|
|
1724
|
+
const trimmed = line.trim();
|
|
1725
|
+
if (trimmed && !trimmed.startsWith("#")) {
|
|
1726
|
+
patterns.push(trimmed);
|
|
1727
|
+
}
|
|
1728
|
+
}
|
|
1729
|
+
}
|
|
1730
|
+
return patterns;
|
|
1731
|
+
}
|
|
1732
|
+
function shouldIgnore(filePath, patterns) {
|
|
1733
|
+
return micromatch.isMatch(filePath, patterns);
|
|
1734
|
+
}
|
|
1735
|
+
function isBinary(filePath) {
|
|
1736
|
+
try {
|
|
1737
|
+
const chunk = Buffer.alloc(8192);
|
|
1738
|
+
const fd = fs3.openSync(filePath, "r");
|
|
1739
|
+
const bytesRead = fs3.readSync(fd, chunk, 0, 8192, 0);
|
|
1740
|
+
fs3.closeSync(fd);
|
|
1741
|
+
return chunk.slice(0, bytesRead).includes(0);
|
|
1742
|
+
} catch {
|
|
1743
|
+
return true;
|
|
1744
|
+
}
|
|
1745
|
+
}
|
|
1746
|
+
function walkDir(dir, repoRoot) {
|
|
1747
|
+
const files = [];
|
|
1748
|
+
const entries = fs3.readdirSync(dir, { withFileTypes: true });
|
|
1749
|
+
for (const entry of entries) {
|
|
1750
|
+
if (entry.isDirectory()) {
|
|
1751
|
+
if (PRUNE_DIRS.has(entry.name)) {
|
|
1752
|
+
continue;
|
|
1753
|
+
}
|
|
1754
|
+
files.push(...walkDir(path3.join(dir, entry.name), repoRoot));
|
|
1755
|
+
} else if (entry.isFile()) {
|
|
1756
|
+
const rel = path3.relative(repoRoot, path3.join(dir, entry.name));
|
|
1757
|
+
files.push(rel);
|
|
1758
|
+
}
|
|
1759
|
+
}
|
|
1760
|
+
return files;
|
|
1761
|
+
}
|
|
1762
|
+
function collectAllFiles(repoRoot) {
|
|
1763
|
+
const ignorePatterns = loadIgnorePatterns(repoRoot);
|
|
1764
|
+
const parser = new CodeParser();
|
|
1765
|
+
const files = [];
|
|
1766
|
+
const tracked = getAllTrackedFiles(repoRoot);
|
|
1767
|
+
const candidates = tracked.length > 0 ? tracked : walkDir(repoRoot, repoRoot);
|
|
1768
|
+
for (const relPath of candidates) {
|
|
1769
|
+
if (shouldIgnore(relPath, ignorePatterns)) {
|
|
1770
|
+
continue;
|
|
1771
|
+
}
|
|
1772
|
+
const fullPath = path3.join(repoRoot, relPath);
|
|
1773
|
+
let stat;
|
|
1774
|
+
try {
|
|
1775
|
+
stat = fs3.lstatSync(fullPath);
|
|
1776
|
+
} catch {
|
|
1777
|
+
continue;
|
|
1778
|
+
}
|
|
1779
|
+
if (!stat.isFile() || stat.isSymbolicLink()) {
|
|
1780
|
+
continue;
|
|
1781
|
+
}
|
|
1782
|
+
if (!parser.detectLanguage(fullPath)) {
|
|
1783
|
+
continue;
|
|
1784
|
+
}
|
|
1785
|
+
if (isBinary(fullPath)) {
|
|
1786
|
+
continue;
|
|
1787
|
+
}
|
|
1788
|
+
files.push(relPath);
|
|
1789
|
+
}
|
|
1790
|
+
return files;
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
// src/usecases/buildGraph.ts
|
|
1794
|
+
function fullBuild(repoRoot, repo, parser) {
|
|
1795
|
+
const files = collectAllFiles(repoRoot);
|
|
1796
|
+
const existingFiles = new Set(repo.getAllFiles());
|
|
1797
|
+
const currentAbs = new Set(files.map((f) => path4.join(repoRoot, f)));
|
|
1798
|
+
for (const stale of existingFiles) {
|
|
1799
|
+
if (!currentAbs.has(stale)) {
|
|
1800
|
+
repo.removeFileData(stale);
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
let totalNodes = 0;
|
|
1804
|
+
let totalEdges = 0;
|
|
1805
|
+
const errors = [];
|
|
1806
|
+
for (let i = 0; i < files.length; i++) {
|
|
1807
|
+
const relPath = files[i];
|
|
1808
|
+
const fullPath = path4.join(repoRoot, relPath);
|
|
1809
|
+
try {
|
|
1810
|
+
const source = fs4.readFileSync(fullPath);
|
|
1811
|
+
const fhash = crypto2.createHash("sha256").update(source).digest("hex");
|
|
1812
|
+
const [nodes, edges] = parser.parseBytes(fullPath, source);
|
|
1813
|
+
repo.storeFileNodesEdges(fullPath, nodes, edges, fhash);
|
|
1814
|
+
totalNodes += nodes.length;
|
|
1815
|
+
totalEdges += edges.length;
|
|
1816
|
+
} catch (err) {
|
|
1817
|
+
errors.push(`${relPath}: ${String(err)}`);
|
|
1818
|
+
}
|
|
1819
|
+
if ((i + 1) % 50 === 0 || i + 1 === files.length) {
|
|
1820
|
+
process.stderr.write(`Progress: ${i + 1}/${files.length} files parsed
|
|
1821
|
+
`);
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
repo.setMetadata("last_updated", (/* @__PURE__ */ new Date()).toISOString().replace(/\..+$/, ""));
|
|
1825
|
+
repo.setMetadata("last_build_type", "full");
|
|
1826
|
+
return {
|
|
1827
|
+
buildType: "full",
|
|
1828
|
+
filesUpdated: files.length,
|
|
1829
|
+
totalNodes,
|
|
1830
|
+
totalEdges,
|
|
1831
|
+
changedFiles: files,
|
|
1832
|
+
dependentFiles: [],
|
|
1833
|
+
errors
|
|
1834
|
+
};
|
|
1835
|
+
}
|
|
1836
|
+
function incrementalUpdate(repoRoot, repo, parser, base = "HEAD~1", changedFiles) {
|
|
1837
|
+
const ignorePatterns = loadIgnorePatterns(repoRoot);
|
|
1838
|
+
const changed = changedFiles ?? getChangedFiles(repoRoot, base);
|
|
1839
|
+
if (changed.length === 0) {
|
|
1840
|
+
return {
|
|
1841
|
+
buildType: "incremental",
|
|
1842
|
+
filesUpdated: 0,
|
|
1843
|
+
totalNodes: 0,
|
|
1844
|
+
totalEdges: 0,
|
|
1845
|
+
changedFiles: [],
|
|
1846
|
+
dependentFiles: [],
|
|
1847
|
+
errors: []
|
|
1848
|
+
};
|
|
1849
|
+
}
|
|
1850
|
+
const dependentFiles = /* @__PURE__ */ new Set();
|
|
1851
|
+
for (const relPath of changed) {
|
|
1852
|
+
const fullPath = path4.join(repoRoot, relPath);
|
|
1853
|
+
const deps = findDependents(repo, fullPath);
|
|
1854
|
+
for (const d of deps) {
|
|
1855
|
+
try {
|
|
1856
|
+
dependentFiles.add(path4.relative(repoRoot, d));
|
|
1857
|
+
} catch {
|
|
1858
|
+
dependentFiles.add(d);
|
|
1859
|
+
}
|
|
1860
|
+
}
|
|
1861
|
+
}
|
|
1862
|
+
const allFiles = /* @__PURE__ */ new Set([...changed, ...dependentFiles]);
|
|
1863
|
+
let totalNodes = 0;
|
|
1864
|
+
let totalEdges = 0;
|
|
1865
|
+
const errors = [];
|
|
1866
|
+
for (const relPath of allFiles) {
|
|
1867
|
+
if (shouldIgnore(relPath, ignorePatterns)) {
|
|
1868
|
+
continue;
|
|
1869
|
+
}
|
|
1870
|
+
const absPath = path4.join(repoRoot, relPath);
|
|
1871
|
+
if (!fs4.existsSync(absPath)) {
|
|
1872
|
+
repo.removeFileData(absPath);
|
|
1873
|
+
continue;
|
|
1874
|
+
}
|
|
1875
|
+
if (!parser.detectLanguage(absPath)) {
|
|
1876
|
+
continue;
|
|
1877
|
+
}
|
|
1878
|
+
try {
|
|
1879
|
+
const source = fs4.readFileSync(absPath);
|
|
1880
|
+
const fhash = crypto2.createHash("sha256").update(source).digest("hex");
|
|
1881
|
+
const existingNodes = repo.getNodesByFile(absPath);
|
|
1882
|
+
if (existingNodes.length > 0 && existingNodes[0].fileHash === fhash) {
|
|
1883
|
+
continue;
|
|
1884
|
+
}
|
|
1885
|
+
const [nodes, edges] = parser.parseBytes(absPath, source);
|
|
1886
|
+
repo.storeFileNodesEdges(absPath, nodes, edges, fhash);
|
|
1887
|
+
totalNodes += nodes.length;
|
|
1888
|
+
totalEdges += edges.length;
|
|
1889
|
+
} catch (err) {
|
|
1890
|
+
errors.push(`${relPath}: ${String(err)}`);
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
repo.setMetadata("last_updated", (/* @__PURE__ */ new Date()).toISOString().replace(/\..+$/, ""));
|
|
1894
|
+
repo.setMetadata("last_build_type", "incremental");
|
|
1895
|
+
return {
|
|
1896
|
+
buildType: "incremental",
|
|
1897
|
+
filesUpdated: allFiles.size,
|
|
1898
|
+
totalNodes,
|
|
1899
|
+
totalEdges,
|
|
1900
|
+
changedFiles: changed,
|
|
1901
|
+
dependentFiles: [...dependentFiles],
|
|
1902
|
+
errors
|
|
1903
|
+
};
|
|
1904
|
+
}
|
|
1905
|
+
function findDependents(repo, filePath) {
|
|
1906
|
+
const dependents = /* @__PURE__ */ new Set();
|
|
1907
|
+
for (const e of repo.getEdgesByTarget(filePath)) {
|
|
1908
|
+
if (e.kind === "IMPORTS_FROM") {
|
|
1909
|
+
dependents.add(e.filePath);
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
for (const node of repo.getNodesByFile(filePath)) {
|
|
1913
|
+
for (const e of repo.getEdgesByTarget(node.qualifiedName)) {
|
|
1914
|
+
if (["CALLS", "IMPORTS_FROM", "INHERITS", "IMPLEMENTS"].includes(e.kind)) {
|
|
1915
|
+
dependents.add(e.filePath);
|
|
1916
|
+
}
|
|
1917
|
+
}
|
|
1918
|
+
}
|
|
1919
|
+
dependents.delete(filePath);
|
|
1920
|
+
return [...dependents];
|
|
1921
|
+
}
|
|
1922
|
+
export {
|
|
1923
|
+
CodeParser,
|
|
1924
|
+
SqliteGraphRepository as GraphStore,
|
|
1925
|
+
collectAllFiles,
|
|
1926
|
+
fileHash,
|
|
1927
|
+
findRepoRoot,
|
|
1928
|
+
fullBuild,
|
|
1929
|
+
getDbPath,
|
|
1930
|
+
incrementalUpdate
|
|
1931
|
+
};
|
|
1932
|
+
//# sourceMappingURL=index.js.map
|