Skip to content

Commit 5819775

Browse files
committed
fix: deduplicate LanguageRegistryEntry, align DB type imports, type WeakMap caches (#558)
1 parent bf724f3 commit 5819775

3 files changed

Lines changed: 19 additions & 25 deletions

File tree

src/db/repository/nodes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ export function findNodeByQualifiedName(
278278

279279
// ─── Metric helpers ──────────────────────────────────────────────────────
280280

281-
const _getLineCountForNodeStmt = new WeakMap();
281+
const _getLineCountForNodeStmt: StmtCache<{ line_count: number }> = new WeakMap();
282282

283283
/**
284284
* Get line_count from node_metrics for a given node.
@@ -294,7 +294,7 @@ export function getLineCountForNode(db: BetterSqlite3Database, nodeId: number) {
294294
).get(nodeId);
295295
}
296296

297-
const _getMaxEndLineForFileStmt = new WeakMap();
297+
const _getMaxEndLineForFileStmt: StmtCache<{ max_end: number | null }> = new WeakMap();
298298

299299
/**
300300
* Get the maximum end_line across all nodes in a file.

src/domain/analysis/module-map.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import path from 'node:path';
2-
import type BetterSqlite3 from 'better-sqlite3';
32
import { openReadonlyOrFail, testFilterSQL } from '../../db/index.js';
43
import { loadConfig } from '../../infrastructure/config.js';
54
import { debug } from '../../infrastructure/logger.js';
65
import { isTestFile } from '../../infrastructure/test-filter.js';
76
import { DEAD_ROLE_PREFIX } from '../../shared/kinds.js';
7+
import type { BetterSqlite3Database } from '../../types.js';
88
import { findCycles } from '../graph/cycles.js';
99
import { LANGUAGE_REGISTRY } from '../parser.js';
1010

@@ -44,7 +44,7 @@ export const FALSE_POSITIVE_CALLER_THRESHOLD = 20;
4444
// Section helpers
4545
// ---------------------------------------------------------------------------
4646

47-
function buildTestFileIds(db: BetterSqlite3.Database): Set<number> {
47+
function buildTestFileIds(db: BetterSqlite3Database): Set<number> {
4848
const allFileNodes = db.prepare("SELECT id, file FROM nodes WHERE kind = 'file'").all() as Array<{
4949
id: number;
5050
file: string;
@@ -67,7 +67,7 @@ function buildTestFileIds(db: BetterSqlite3.Database): Set<number> {
6767
return testFileIds;
6868
}
6969

70-
function countNodesByKind(db: BetterSqlite3.Database, testFileIds: Set<number> | null) {
70+
function countNodesByKind(db: BetterSqlite3Database, testFileIds: Set<number> | null) {
7171
let nodeRows: Array<{ kind: string; c: number }>;
7272
if (testFileIds) {
7373
const allNodes = db.prepare('SELECT id, kind, file FROM nodes').all() as Array<{
@@ -94,7 +94,7 @@ function countNodesByKind(db: BetterSqlite3.Database, testFileIds: Set<number> |
9494
return { total, byKind };
9595
}
9696

97-
function countEdgesByKind(db: BetterSqlite3.Database, testFileIds: Set<number> | null) {
97+
function countEdgesByKind(db: BetterSqlite3Database, testFileIds: Set<number> | null) {
9898
let edgeRows: Array<{ kind: string; c: number }>;
9999
if (testFileIds) {
100100
const allEdges = db.prepare('SELECT source_id, target_id, kind FROM edges').all() as Array<{
@@ -123,7 +123,7 @@ function countEdgesByKind(db: BetterSqlite3.Database, testFileIds: Set<number> |
123123
return { total, byKind };
124124
}
125125

126-
function countFilesByLanguage(db: BetterSqlite3.Database, noTests: boolean) {
126+
function countFilesByLanguage(db: BetterSqlite3Database, noTests: boolean) {
127127
const extToLang = new Map<string, string>();
128128
for (const entry of LANGUAGE_REGISTRY) {
129129
for (const ext of entry.extensions) {
@@ -143,7 +143,7 @@ function countFilesByLanguage(db: BetterSqlite3.Database, noTests: boolean) {
143143
return { total: fileNodes.length, languages: Object.keys(byLanguage).length, byLanguage };
144144
}
145145

146-
function findHotspots(db: BetterSqlite3.Database, noTests: boolean, limit: number) {
146+
function findHotspots(db: BetterSqlite3Database, noTests: boolean, limit: number) {
147147
const testFilter = testFilterSQL('n.file', noTests);
148148
const hotspotRows = db
149149
.prepare(`
@@ -164,7 +164,7 @@ function findHotspots(db: BetterSqlite3.Database, noTests: boolean, limit: numbe
164164
}));
165165
}
166166

167-
function getEmbeddingsInfo(db: BetterSqlite3.Database) {
167+
function getEmbeddingsInfo(db: BetterSqlite3Database) {
168168
try {
169169
const count = db.prepare('SELECT COUNT(*) as c FROM embeddings').get() as
170170
| { c: number }
@@ -190,7 +190,7 @@ function getEmbeddingsInfo(db: BetterSqlite3.Database) {
190190
}
191191

192192
function computeQualityMetrics(
193-
db: BetterSqlite3.Database,
193+
db: BetterSqlite3Database,
194194
testFilter: string,
195195
fpThreshold = FALSE_POSITIVE_CALLER_THRESHOLD,
196196
) {
@@ -265,7 +265,7 @@ function computeQualityMetrics(
265265
};
266266
}
267267

268-
function countRoles(db: BetterSqlite3.Database, noTests: boolean) {
268+
function countRoles(db: BetterSqlite3Database, noTests: boolean) {
269269
let roleRows: Array<{ role: string; c: number }>;
270270
if (noTests) {
271271
const allRoleNodes = db
@@ -290,7 +290,7 @@ function countRoles(db: BetterSqlite3.Database, noTests: boolean) {
290290
return roles;
291291
}
292292

293-
function getComplexitySummary(db: BetterSqlite3.Database, testFilter: string) {
293+
function getComplexitySummary(db: BetterSqlite3Database, testFilter: string) {
294294
try {
295295
const cRows = db
296296
.prepare(

src/domain/parser.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { Tree } from 'web-tree-sitter';
55
import { Language, Parser, Query } from 'web-tree-sitter';
66
import { debug, warn } from '../infrastructure/logger.js';
77
import { getNative, getNativePackageVersion, loadNative } from '../infrastructure/native.js';
8+
import type { LanguageRegistryEntry } from '../types.js';
89

910
// Re-export all extractors for backward compatibility
1011
export {
@@ -51,17 +52,8 @@ const _queryCache: Map<string, Query> = new Map();
5152
// Extensions that need typeMap backfill (type annotations only exist in TS/TSX)
5253
const TS_BACKFILL_EXTS = new Set(['.ts', '.tsx']);
5354

54-
/**
55-
* Declarative registry entry for a supported language.
56-
*/
57-
export interface LanguageRegistryEntry {
58-
id: string;
59-
extensions: string[];
60-
grammarFile: string;
61-
// biome-ignore lint/suspicious/noExplicitAny: extractor signatures vary per language
62-
extractor: (...args: any[]) => any;
63-
required: boolean;
64-
}
55+
// Re-export for backward compatibility
56+
export type { LanguageRegistryEntry } from '../types.js';
6557

6658
interface EngineOpts {
6759
engine?: string;
@@ -483,8 +475,10 @@ function wasmExtractSymbols(
483475
const ext = path.extname(filePath);
484476
const entry = _extToLang.get(ext);
485477
if (!entry) return null;
486-
const query = _queryCache.get(entry.id) || null;
487-
const symbols = entry.extractor(tree, filePath, query);
478+
const query = _queryCache.get(entry.id) ?? undefined;
479+
// Query (web-tree-sitter) is structurally compatible with TreeSitterQuery at runtime
480+
// biome-ignore lint/suspicious/noExplicitAny: thin WASM wrapper type mismatch
481+
const symbols = entry.extractor(tree as any, filePath, query as any);
488482
return symbols ? { symbols, tree, langId: entry.id } : null;
489483
}
490484

0 commit comments

Comments
 (0)