also looking at this
chore(deps): update all non-major dependencies
#2997
29 files
+1047
-444
| @@ -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 | ||
| 7 | import type { Declaration } from '../types' | |
| 8 | import { hashContent } from './hash' | |
| 9 | import { scanDeclarations } from './scanner' | |
| 10 | ||
| 11 | const MAX_DECLARATION_CACHE_SIZE = 100 | |
| 12 | const 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 | */ | |
| 18 | export 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 | */ | |
| 53 | export function clearDeclarationCache(): void { | |
| 54 | declarationCache.clear() | |
| 55 | } | |