-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathengine.ts
More file actions
466 lines (412 loc) · 17 KB
/
engine.ts
File metadata and controls
466 lines (412 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/**
* Unified AST analysis engine — orchestrates all analysis passes in one file-iteration loop.
*
* Replaces the 4 sequential buildXxx calls in builder.js with a single coordinated pass:
* - AST node extraction (calls, new, string, regex, throw, await)
* - Complexity metrics (cognitive, cyclomatic, nesting, Halstead, MI)
* - CFG construction (basic blocks + edges)
* - Dataflow analysis (define-use chains, arg flows, mutations)
*
* All 4 analyses run as visitors in a single DFS walk via walkWithVisitors.
*
* Optimization strategy: for files with WASM trees, run all applicable visitors
* in a single walkWithVisitors call. Store results in the format that buildXxx
* functions already expect as pre-computed data (same fields as native engine
* output). This eliminates redundant tree traversals per file.
*/
import path from 'node:path';
import { performance } from 'node:perf_hooks';
import { bulkNodeIdsByFile } from '../db/index.js';
import { debug } from '../infrastructure/logger.js';
import type {
AnalysisOpts,
AnalysisTiming,
ASTNodeRow,
BetterSqlite3Database,
CfgBlock,
CfgEdge,
DataflowResult,
Definition,
EngineOpts,
ExtractorOutput,
TreeSitterNode,
Visitor,
WalkOptions,
WalkResults,
} from '../types.js';
import { computeLOCMetrics, computeMaintainabilityIndex } from './metrics.js';
import {
AST_TYPE_MAPS,
CFG_RULES,
COMPLEXITY_RULES,
DATAFLOW_RULES,
HALSTEAD_RULES,
} from './rules/index.js';
import { buildExtensionSet, buildExtToLangMap } from './shared.js';
import { walkWithVisitors } from './visitor.js';
import { functionName as getFuncName } from './visitor-utils.js';
import { createAstStoreVisitor } from './visitors/ast-store-visitor.js';
import { createCfgVisitor } from './visitors/cfg-visitor.js';
import { createComplexityVisitor } from './visitors/complexity-visitor.js';
import { createDataflowVisitor } from './visitors/dataflow-visitor.js';
// ─── Visitor result shapes (internal, not exported) ──────────────────────
interface ComplexityFuncResult {
funcNode: TreeSitterNode;
funcName: string | null;
metrics: {
cognitive: number;
cyclomatic: number;
maxNesting: number;
halstead?: { volume: number; difficulty: number; effort: number; bugs: number };
};
}
interface CfgFuncResult {
funcNode: TreeSitterNode;
blocks: CfgBlock[];
edges: CfgEdge[];
cyclomatic?: number;
}
interface SetupResult {
visitors: Visitor[];
walkerOpts: WalkOptions;
astVisitor: Visitor | null;
complexityVisitor: Visitor | null;
cfgVisitor: Visitor | null;
dataflowVisitor: Visitor | null;
}
// ─── Extension sets for quick language-support checks ────────────────────
const CFG_EXTENSIONS = buildExtensionSet(CFG_RULES);
const COMPLEXITY_EXTENSIONS = buildExtensionSet(COMPLEXITY_RULES);
const DATAFLOW_EXTENSIONS = buildExtensionSet(DATAFLOW_RULES);
const WALK_EXTENSIONS = buildExtensionSet(AST_TYPE_MAPS);
// ─── Lazy imports (heavy modules loaded only when needed) ────────────────
let _parserModule: Awaited<typeof import('../domain/parser.js')> | null = null;
async function getParserModule(): Promise<typeof import('../domain/parser.js')> {
if (!_parserModule) _parserModule = await import('../domain/parser.js');
return _parserModule;
}
// ─── WASM pre-parse ─────────────────────────────────────────────────────
async function ensureWasmTreesIfNeeded(
fileSymbols: Map<string, ExtractorOutput>,
opts: AnalysisOpts,
rootDir: string,
): Promise<void> {
const doComplexity = opts.complexity !== false;
const doCfg = opts.cfg !== false;
const doDataflow = opts.dataflow !== false;
if (!doComplexity && !doCfg && !doDataflow) return;
let needsWasmTrees = false;
for (const [relPath, symbols] of fileSymbols) {
if (symbols._tree) continue;
const ext = path.extname(relPath).toLowerCase();
const defs = symbols.definitions || [];
// Only consider definitions with a real function body.
// Interface/type property signatures are extracted as methods but correctly
// lack complexity/CFG data from the native engine. Exclude them by:
// 1. Single-line span (endLine === line) — type property on one line
// 2. Dotted names (e.g. "Interface.prop") — child definitions of types
const hasFuncBody = (d: {
name: string;
kind: string;
line: number;
endLine?: number | null;
}) =>
(d.kind === 'function' || d.kind === 'method') &&
d.line > 0 &&
d.endLine != null &&
d.endLine > d.line &&
!d.name.includes('.');
const needsComplexity =
doComplexity &&
COMPLEXITY_EXTENSIONS.has(ext) &&
defs.some((d) => hasFuncBody(d) && !d.complexity);
const needsCfg =
doCfg &&
CFG_EXTENSIONS.has(ext) &&
defs.some((d) => hasFuncBody(d) && d.cfg !== null && !Array.isArray(d.cfg?.blocks));
const needsDataflow = doDataflow && !symbols.dataflow && DATAFLOW_EXTENSIONS.has(ext);
if (needsComplexity || needsCfg || needsDataflow) {
needsWasmTrees = true;
break;
}
}
if (needsWasmTrees) {
try {
const { ensureWasmTrees } = await getParserModule();
await ensureWasmTrees(fileSymbols, rootDir);
} catch (err: unknown) {
debug(`ensureWasmTrees failed: ${(err as Error).message}`);
}
}
}
// ─── Per-file visitor setup ─────────────────────────────────────────────
function setupVisitors(
db: BetterSqlite3Database,
relPath: string,
symbols: ExtractorOutput,
langId: string,
opts: AnalysisOpts,
): SetupResult {
const ext = path.extname(relPath).toLowerCase();
const defs = symbols.definitions || [];
const doAst = opts.ast !== false;
const doComplexity = opts.complexity !== false;
const doCfg = opts.cfg !== false;
const doDataflow = opts.dataflow !== false;
const visitors: Visitor[] = [];
const walkerOpts: WalkOptions = {
functionNodeTypes: new Set<string>(),
nestingNodeTypes: new Set<string>(),
getFunctionName: (_node: TreeSitterNode) => null,
};
// AST-store visitor
let astVisitor: Visitor | null = null;
const astTypeMap = AST_TYPE_MAPS.get(langId);
if (doAst && astTypeMap && WALK_EXTENSIONS.has(ext) && !Array.isArray(symbols.astNodes)) {
const nodeIdMap = new Map<string, number>();
for (const row of bulkNodeIdsByFile(db, relPath)) {
nodeIdMap.set(`${row.name}|${row.kind}|${row.line}`, row.id);
}
astVisitor = createAstStoreVisitor(astTypeMap, defs, relPath, nodeIdMap);
visitors.push(astVisitor);
}
// Complexity visitor (file-level mode)
let complexityVisitor: Visitor | null = null;
const cRules = COMPLEXITY_RULES.get(langId);
const hRules = HALSTEAD_RULES.get(langId);
if (doComplexity && cRules) {
// Only trigger WASM complexity for definitions with real function bodies.
// Interface/type property signatures (dotted names, single-line span)
// correctly lack native complexity data and should not trigger a fallback.
const needsWasmComplexity = defs.some(
(d) =>
(d.kind === 'function' || d.kind === 'method') &&
d.line > 0 &&
d.endLine != null &&
d.endLine > d.line &&
!d.name.includes('.') &&
!d.complexity,
);
if (needsWasmComplexity) {
complexityVisitor = createComplexityVisitor(cRules, hRules, { fileLevelWalk: true, langId });
visitors.push(complexityVisitor);
for (const t of cRules.nestingNodes) walkerOpts.nestingNodeTypes?.add(t);
const dfRules = DATAFLOW_RULES.get(langId);
walkerOpts.getFunctionName = (node: TreeSitterNode): string | null => {
const nameNode = node.childForFieldName('name');
if (nameNode) return nameNode.text;
// biome-ignore lint/suspicious/noExplicitAny: DataflowRulesConfig is structurally compatible at runtime
if (dfRules) return getFuncName(node, dfRules as any);
return null;
};
}
}
// CFG visitor
let cfgVisitor: Visitor | null = null;
const cfgRulesForLang = CFG_RULES.get(langId);
if (doCfg && cfgRulesForLang && CFG_EXTENSIONS.has(ext)) {
const needsWasmCfg = defs.some(
(d) =>
(d.kind === 'function' || d.kind === 'method') &&
d.line > 0 &&
d.endLine != null &&
d.endLine > d.line &&
!d.name.includes('.') &&
d.cfg !== null &&
!Array.isArray(d.cfg?.blocks),
);
if (needsWasmCfg) {
cfgVisitor = createCfgVisitor(cfgRulesForLang);
visitors.push(cfgVisitor);
}
}
// Dataflow visitor
let dataflowVisitor: Visitor | null = null;
const dfRules = DATAFLOW_RULES.get(langId);
if (doDataflow && dfRules && DATAFLOW_EXTENSIONS.has(ext) && !symbols.dataflow) {
dataflowVisitor = createDataflowVisitor(dfRules);
visitors.push(dataflowVisitor);
}
return { visitors, walkerOpts, astVisitor, complexityVisitor, cfgVisitor, dataflowVisitor };
}
// ─── Result storage helpers ─────────────────────────────────────────────
function storeComplexityResults(results: WalkResults, defs: Definition[], langId: string): void {
// biome-ignore lint/complexity/useLiteralKeys: bracket notation required by noPropertyAccessFromIndexSignature
const complexityResults = (results['complexity'] || []) as ComplexityFuncResult[];
const resultByLine = new Map<number, ComplexityFuncResult[]>();
for (const r of complexityResults) {
if (r.funcNode) {
const line = r.funcNode.startPosition.row + 1;
if (!resultByLine.has(line)) resultByLine.set(line, []);
resultByLine.get(line)?.push(r);
}
}
for (const def of defs) {
if ((def.kind === 'function' || def.kind === 'method') && def.line && !def.complexity) {
const candidates = resultByLine.get(def.line);
const funcResult = !candidates
? undefined
: candidates.length === 1
? candidates[0]
: (candidates.find((r) => {
const n = r.funcNode.childForFieldName('name');
return n && n.text === def.name;
}) ?? candidates[0]);
if (funcResult) {
const { metrics } = funcResult;
const loc = computeLOCMetrics(funcResult.funcNode, langId);
const volume = metrics.halstead ? metrics.halstead.volume : 0;
const commentRatio = loc.loc > 0 ? loc.commentLines / loc.loc : 0;
const mi = computeMaintainabilityIndex(volume, metrics.cyclomatic, loc.sloc, commentRatio);
def.complexity = {
cognitive: metrics.cognitive,
cyclomatic: metrics.cyclomatic,
maxNesting: metrics.maxNesting,
halstead: metrics.halstead,
loc,
maintainabilityIndex: mi,
};
}
}
}
}
function storeCfgResults(results: WalkResults, defs: Definition[]): void {
// biome-ignore lint/complexity/useLiteralKeys: bracket notation required by noPropertyAccessFromIndexSignature
const cfgResults = (results['cfg'] || []) as CfgFuncResult[];
const cfgByLine = new Map<number, CfgFuncResult[]>();
for (const r of cfgResults) {
if (r.funcNode) {
const line = r.funcNode.startPosition.row + 1;
if (!cfgByLine.has(line)) cfgByLine.set(line, []);
cfgByLine.get(line)?.push(r);
}
}
for (const def of defs) {
if (
(def.kind === 'function' || def.kind === 'method') &&
def.line &&
!def.cfg?.blocks?.length
) {
const candidates = cfgByLine.get(def.line);
const cfgResult = !candidates
? undefined
: candidates.length === 1
? candidates[0]
: (candidates.find((r) => {
const n = r.funcNode.childForFieldName('name');
return n && n.text === def.name;
}) ?? candidates[0]);
if (cfgResult) {
def.cfg = { blocks: cfgResult.blocks, edges: cfgResult.edges };
// Override complexity's cyclomatic with CFG-derived value (single source of truth)
if (def.complexity && cfgResult.cyclomatic != null) {
def.complexity.cyclomatic = cfgResult.cyclomatic;
const { loc, halstead } = def.complexity;
const volume = halstead ? halstead.volume : 0;
const commentRatio = loc && loc.loc > 0 ? loc.commentLines / loc.loc : 0;
def.complexity.maintainabilityIndex = computeMaintainabilityIndex(
volume,
cfgResult.cyclomatic,
loc?.sloc ?? 0,
commentRatio,
);
}
}
}
}
}
// ─── Build delegation ───────────────────────────────────────────────────
async function delegateToBuildFunctions(
db: BetterSqlite3Database,
fileSymbols: Map<string, ExtractorOutput>,
rootDir: string,
opts: AnalysisOpts,
engineOpts: EngineOpts | undefined,
timing: AnalysisTiming,
): Promise<void> {
if (opts.ast !== false) {
const t0 = performance.now();
try {
const { buildAstNodes } = await import('../features/ast.js');
// biome-ignore lint/suspicious/noExplicitAny: ExtractorOutput is a superset of the local FileSymbols expected by buildAstNodes
await buildAstNodes(db, fileSymbols as Map<string, any>, rootDir, engineOpts);
} catch (err: unknown) {
debug(`buildAstNodes failed: ${(err as Error).message}`);
}
timing.astMs = performance.now() - t0;
}
if (opts.complexity !== false) {
const t0 = performance.now();
try {
const { buildComplexityMetrics } = await import('../features/complexity.js');
// biome-ignore lint/suspicious/noExplicitAny: ExtractorOutput is a superset of the local FileSymbols expected by buildComplexityMetrics
await buildComplexityMetrics(db, fileSymbols as Map<string, any>, rootDir, engineOpts);
} catch (err: unknown) {
debug(`buildComplexityMetrics failed: ${(err as Error).message}`);
}
timing.complexityMs = performance.now() - t0;
}
if (opts.cfg !== false) {
const t0 = performance.now();
try {
const { buildCFGData } = await import('../features/cfg.js');
await buildCFGData(db, fileSymbols, rootDir, engineOpts);
} catch (err: unknown) {
debug(`buildCFGData failed: ${(err as Error).message}`);
}
timing.cfgMs = performance.now() - t0;
}
if (opts.dataflow !== false) {
const t0 = performance.now();
try {
const { buildDataflowEdges } = await import('../features/dataflow.js');
await buildDataflowEdges(db, fileSymbols, rootDir, engineOpts);
} catch (err: unknown) {
debug(`buildDataflowEdges failed: ${(err as Error).message}`);
}
timing.dataflowMs = performance.now() - t0;
}
}
// ─── Public API ──────────────────────────────────────────────────────────
export async function runAnalyses(
db: BetterSqlite3Database,
fileSymbols: Map<string, ExtractorOutput>,
rootDir: string,
opts: AnalysisOpts,
engineOpts?: EngineOpts,
): Promise<AnalysisTiming> {
const timing: AnalysisTiming = { astMs: 0, complexityMs: 0, cfgMs: 0, dataflowMs: 0 };
const doAst = opts.ast !== false;
const doComplexity = opts.complexity !== false;
const doCfg = opts.cfg !== false;
const doDataflow = opts.dataflow !== false;
if (!doAst && !doComplexity && !doCfg && !doDataflow) return timing;
const extToLang = buildExtToLangMap();
// WASM pre-parse for files that need it
await ensureWasmTreesIfNeeded(fileSymbols, opts, rootDir);
// Unified pre-walk: run all applicable visitors in a single DFS per file
const t0walk = performance.now();
for (const [relPath, symbols] of fileSymbols) {
if (!symbols._tree) continue;
const ext = path.extname(relPath).toLowerCase();
const langId = symbols._langId || extToLang.get(ext);
if (!langId) continue;
const { visitors, walkerOpts, astVisitor, complexityVisitor, cfgVisitor, dataflowVisitor } =
setupVisitors(db, relPath, symbols, langId, opts);
if (visitors.length === 0) continue;
const results = walkWithVisitors(symbols._tree.rootNode, visitors, langId, walkerOpts);
const defs = symbols.definitions || [];
if (astVisitor) {
const astRows = (results['ast-store'] || []) as ASTNodeRow[];
if (astRows.length > 0) symbols.astNodes = astRows;
}
if (complexityVisitor) storeComplexityResults(results, defs, langId);
if (cfgVisitor) storeCfgResults(results, defs);
// biome-ignore lint/complexity/useLiteralKeys: bracket notation required by noPropertyAccessFromIndexSignature
if (dataflowVisitor) symbols.dataflow = results['dataflow'] as DataflowResult;
}
timing._unifiedWalkMs = performance.now() - t0walk;
// Delegate to buildXxx functions for DB writes + native fallback
await delegateToBuildFunctions(db, fileSymbols, rootDir, opts, engineOpts, timing);
return timing;
}