ReviewOS

also looking at this

stacks/dtsx

chore(deps): update all non-major dependencies

#2997
Closed renovate[bot] wants to merge renovate/all-minor-patch into main
29 files +1047 -444
packages/dtsx/src/extractor/extract.tsadded+55-0
Changes to packages/dtsx/src/extractor/extract.ts
@@ -0,0 +1,55 @@
1/**
2 * Lightweight extraction module — NO TypeScript dependency.
3 * Only uses the string-based scanner for maximum startup speed.
4 * Used by process-source.ts and the CLI fast path.
5 */
6
7import type { Declaration } from '../types'
8import { hashContent } from './hash'
9import { scanDeclarations } from './scanner'
10
11const MAX_DECLARATION_CACHE_SIZE = 100
12const declarationCache = new Map<string, { declarations: Declaration[], contentHash: number | bigint, lastAccess: number }>()
13
14/**
15 * Extract declarations using the fast string scanner (no TS parser).
16 * Results are cached by file path + keepComments flag.
17 */
18export function extractDeclarations(sourceCode: string, filePath: string, keepComments: boolean = true): Declaration[] {
19 const contentHash = hashContent(sourceCode)
20 const cacheKey = `${filePath}:${keepComments ? 1 : 0}`
21 const cached = declarationCache.get(cacheKey)
22 const now = Date.now()
23
24 if (cached && cached.contentHash === contentHash) {
25 cached.lastAccess = now
26 return cached.declarations
27 }
28
29 const declarations = scanDeclarations(sourceCode, filePath, keepComments)
30
31 declarationCache.set(cacheKey, { declarations, contentHash, lastAccess: now })
32
33 if (declarationCache.size > MAX_DECLARATION_CACHE_SIZE) {
34 let oldestKey: string | null = null
35 let oldestTime = Infinity
36 for (const [key, entry] of declarationCache) {
37 if (entry.lastAccess < oldestTime) {
38 oldestTime = entry.lastAccess
39 oldestKey = key
40 }
41 }
42 if (oldestKey) {
43 declarationCache.delete(oldestKey)
44 }
45 }
46
47 return declarations
48}
49
50/**
51 * Clear the declaration cache (for benchmarks and testing)
52 */
53export function clearDeclarationCache(): void {
54 declarationCache.clear()
55}