-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathparser.ts
More file actions
1381 lines (1303 loc) · 44.8 KB
/
Copy pathparser.ts
File metadata and controls
1381 lines (1303 loc) · 44.8 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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import type { Tree } from 'web-tree-sitter';
import { Language, Parser, Query } from 'web-tree-sitter';
import { debug, warn } from '../infrastructure/logger.js';
import { getNative, getNativePackageVersion, loadNative } from '../infrastructure/native.js';
import { ParseError, toErrorMessage } from '../shared/errors.js';
import type {
EngineMode,
ExtractorOutput,
LanguageId,
LanguageRegistryEntry,
TypeMapEntry,
} from '../types.js';
import { disposeWasmWorkerPool, getWasmWorkerPool } from './wasm-worker-pool.js';
import type { WorkerAnalysisOpts } from './wasm-worker-protocol.js';
/** Default worker opts: run all analyses so output matches parseFilesFull. */
const FULL_ANALYSIS: WorkerAnalysisOpts = {
ast: true,
complexity: true,
cfg: true,
dataflow: true,
};
/** Extract-only opts: skip visitor walk for typeMap backfill / similar fast paths. */
const EXTRACT_ONLY: WorkerAnalysisOpts = {
ast: false,
complexity: false,
cfg: false,
dataflow: false,
};
// Re-export all extractors for backward compatibility
export {
extractBashSymbols,
extractClojureSymbols,
extractCppSymbols,
extractCSharpSymbols,
extractCSymbols,
extractCudaSymbols,
extractDartSymbols,
extractElixirSymbols,
extractErlangSymbols,
extractFSharpSymbols,
extractGleamSymbols,
extractGoSymbols,
extractGroovySymbols,
extractHaskellSymbols,
extractHCLSymbols,
extractJavaSymbols,
extractJuliaSymbols,
extractKotlinSymbols,
extractLuaSymbols,
extractObjCSymbols,
extractOCamlSymbols,
extractPHPSymbols,
extractPythonSymbols,
extractRSymbols,
extractRubySymbols,
extractRustSymbols,
extractScalaSymbols,
extractSoliditySymbols,
extractSwiftSymbols,
extractSymbols,
extractVerilogSymbols,
extractZigSymbols,
} from '../extractors/index.js';
import {
extractBashSymbols,
extractClojureSymbols,
extractCppSymbols,
extractCSharpSymbols,
extractCSymbols,
extractCudaSymbols,
extractDartSymbols,
extractElixirSymbols,
extractErlangSymbols,
extractFSharpSymbols,
extractGleamSymbols,
extractGoSymbols,
extractGroovySymbols,
extractHaskellSymbols,
extractHCLSymbols,
extractJavaSymbols,
extractJuliaSymbols,
extractKotlinSymbols,
extractLuaSymbols,
extractObjCSymbols,
extractOCamlSymbols,
extractPHPSymbols,
extractPythonSymbols,
extractRSymbols,
extractRubySymbols,
extractRustSymbols,
extractScalaSymbols,
extractSoliditySymbols,
extractSwiftSymbols,
extractSymbols,
extractVerilogSymbols,
extractZigSymbols,
} from '../extractors/index.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
function grammarPath(name: string): string {
return path.join(__dirname, '..', '..', 'grammars', name);
}
let _initialized: boolean = false;
// Memoized parsers — avoids reloading WASM grammars on every createParsers() call
let _cachedParsers: Map<string, Parser | null> | null = null;
// Cached Language objects — WASM-backed, must be .delete()'d explicitly
let _cachedLanguages: Map<string, Language> | null = null;
// Query cache for JS/TS/TSX extractors (populated during createParsers)
const _queryCache: Map<string, Query> = new Map();
// Tracks whether ALL grammars have been loaded (vs. a lazy subset)
let _allParsersLoaded: boolean = false;
// In-flight grammar loads keyed by language id — prevents concurrent duplicate loads
const _loadingPromises: Map<string, Promise<void>> = new Map();
// Extensions that need typeMap backfill (type annotations only exist in TS/TSX)
const TS_BACKFILL_EXTS = new Set(['.ts', '.tsx']);
// Re-export for backward compatibility
export type { LanguageRegistryEntry } from '../types.js';
interface ParseEngineOpts {
engine?: EngineMode;
dataflow?: boolean;
ast?: boolean;
}
interface ResolvedEngine {
name: 'native' | 'wasm';
native: any;
}
interface WasmExtractResult {
symbols: ExtractorOutput;
tree: Tree;
langId: LanguageId;
}
// Shared patterns for all JS/TS/TSX (class_declaration excluded — name type differs)
const COMMON_QUERY_PATTERNS: string[] = [
'(function_declaration name: (identifier) @fn_name) @fn_node',
'(generator_function_declaration name: (identifier) @fn_name) @fn_node',
'(variable_declarator name: (identifier) @varfn_name value: (arrow_function) @varfn_value)',
'(variable_declarator name: (identifier) @varfn_name value: (function_expression) @varfn_value)',
'(variable_declarator name: (identifier) @varfn_name value: (generator_function) @varfn_value)',
'(method_definition name: (property_identifier) @meth_name) @meth_node',
'(method_definition name: (private_property_identifier) @meth_name) @meth_node',
'(import_statement source: (string) @imp_source) @imp_node',
'(export_statement) @exp_node',
'(call_expression function: (identifier) @callfn_name) @callfn_node',
'(call_expression function: (member_expression) @callmem_fn) @callmem_node',
'(call_expression function: (subscript_expression) @callsub_fn) @callsub_node',
'(new_expression constructor: (identifier) @newfn_name) @newfn_node',
'(new_expression constructor: (member_expression) @newmem_fn) @newmem_node',
'(expression_statement (assignment_expression left: (member_expression) @assign_left right: (_) @assign_right)) @assign_node',
];
// JS: class name is (identifier)
const JS_CLASS_PATTERN: string = '(class_declaration name: (identifier) @cls_name) @cls_node';
// TS/TSX: class name is (type_identifier), plus interface and type alias
// abstract_class_declaration is a separate node type in tree-sitter-typescript
const TS_EXTRA_PATTERNS: string[] = [
'(class_declaration name: (type_identifier) @cls_name) @cls_node',
'(abstract_class_declaration name: (type_identifier) @cls_name) @cls_node',
'(interface_declaration name: (type_identifier) @iface_name) @iface_node',
'(type_alias_declaration name: (type_identifier) @type_name) @type_node',
];
/**
* Load a single language grammar and cache the parser + language + query.
* Uses in-flight deduplication so concurrent callers awaiting the same grammar
* share a single load rather than producing orphaned WASM instances.
* Assumes Parser.init() has already been called and _cachedParsers/_cachedLanguages exist.
*/
async function loadLanguage(entry: LanguageRegistryEntry): Promise<void> {
if (_cachedParsers!.has(entry.id)) return;
const inflight = _loadingPromises.get(entry.id);
if (inflight) return inflight;
const p = doLoadLanguage(entry).finally(() => _loadingPromises.delete(entry.id));
_loadingPromises.set(entry.id, p);
return p;
}
async function doLoadLanguage(entry: LanguageRegistryEntry): Promise<void> {
try {
const lang = await Language.load(grammarPath(entry.grammarFile));
const parser = new Parser();
parser.setLanguage(lang);
_cachedParsers!.set(entry.id, parser);
_cachedLanguages!.set(entry.id, lang);
if (entry.extractor === extractSymbols && !_queryCache.has(entry.id)) {
const isTS = entry.id === 'typescript' || entry.id === 'tsx';
const patterns = isTS
? [...COMMON_QUERY_PATTERNS, ...TS_EXTRA_PATTERNS]
: [...COMMON_QUERY_PATTERNS, JS_CLASS_PATTERN];
_queryCache.set(entry.id, new Query(lang, patterns.join('\n')));
}
} catch (e: unknown) {
if (entry.required)
throw new ParseError(`Required parser ${entry.id} failed to initialize`, {
file: entry.grammarFile,
cause: e as Error,
});
warn(
`${entry.id} parser failed to initialize: ${(e as Error).message}. ${entry.id} files will be skipped.`,
);
_cachedParsers!.set(entry.id, null);
}
}
async function initParserRuntime(): Promise<void> {
if (!_initialized) {
await Parser.init();
_initialized = true;
}
if (!_cachedParsers) _cachedParsers = new Map();
if (!_cachedLanguages) _cachedLanguages = new Map();
}
/**
* Load only the WASM grammars needed for the given file paths.
* Grammars already in cache are reused. This avoids the ~500ms cold-start
* penalty of loading all 23+ grammars when only 1-2 are needed (e.g. incremental rebuilds).
*/
async function ensureParsersForFiles(filePaths: string[]): Promise<Map<string, Parser | null>> {
await initParserRuntime();
const needed = new Set<LanguageRegistryEntry>();
for (const fp of filePaths) {
const ext = path.extname(fp).toLowerCase();
const entry = _extToLang.get(ext);
if (entry && !_cachedParsers!.has(entry.id)) needed.add(entry);
}
for (const entry of needed) {
await loadLanguage(entry);
}
return _cachedParsers!;
}
/**
* Load ALL WASM grammars. Used by full builds and feature modules (CFG, dataflow, complexity)
* that may process files of any language.
*/
export async function createParsers(): Promise<Map<string, Parser | null>> {
if (_cachedParsers && _allParsersLoaded) return _cachedParsers;
await initParserRuntime();
for (const entry of LANGUAGE_REGISTRY) {
if (!_cachedParsers!.has(entry.id)) {
await loadLanguage(entry);
}
}
_allParsersLoaded = true;
return _cachedParsers!;
}
/**
* Dispose all cached WASM parsers and queries to free WASM linear memory.
* Call this between repeated builds in the same process (e.g. benchmarks)
* to prevent memory accumulation that can cause segfaults.
*/
function disposeMapEntries(entries: Iterable<[string, any]>, label: string): void {
for (const [id, item] of entries) {
if (item && typeof item.delete === 'function') {
try {
item.delete();
} catch (e: unknown) {
debug(`Failed to dispose ${label} ${id}: ${(e as Error).message}`);
}
}
}
}
export async function disposeParsers(): Promise<void> {
if (_cachedParsers) {
disposeMapEntries(_cachedParsers, 'parser');
_cachedParsers = null;
}
disposeMapEntries(_queryCache, 'query');
_queryCache.clear();
if (_cachedLanguages) {
disposeMapEntries(_cachedLanguages, 'language');
_cachedLanguages = null;
}
_initialized = false;
_allParsersLoaded = false;
_loadingPromises.clear();
await disposeWasmWorkerPool();
}
export function getParser(parsers: Map<string, Parser | null>, filePath: string): Parser | null {
const ext = path.extname(filePath);
const entry = _extToLang.get(ext);
if (!entry) return null;
return parsers.get(entry.id) || null;
}
/**
* Backfill missing AST-analysis data (astNodes, dataflow, def.complexity,
* def.cfg) via the WASM worker pool for files that were parsed by the native
* engine but are missing one or more analyses.
*
* Historically this function populated `symbols._tree` so the main-thread
* visitor walk in `ast-analysis/engine.ts` could run. After the worker-isolation
* refactor (#965), the worker runs every visitor itself and returns pre-computed
* analysis data — `_tree` is never set on the main thread.
*
* Name is preserved for caller compatibility; the function now ensures
* *analysis data* rather than *trees*.
*
* `needsFn` (optional): when provided, only files for which it returns true are
* re-parsed. Without it the function falls back to "any WASM-parseable file
* without _tree", which was the source of #1036 — a single file missing one
* analysis triggered a full-build re-parse of every WASM-parseable file.
*/
/**
* Select files from `fileSymbols` that still need analysis data and are
* parseable by an installed WASM grammar. Pure (no I/O) — safe to unit-test.
*/
function collectBackfillPending(
fileSymbols: Map<string, any>,
rootDir: string,
needsFn?: (relPath: string, symbols: any) => boolean,
): Array<{ relPath: string; absPath: string; symbols: any }> {
const pending: Array<{ relPath: string; absPath: string; symbols: any }> = [];
for (const [relPath, symbols] of fileSymbols) {
if (symbols._tree) continue; // legacy path — leave existing trees alone
if (!_extToLang.has(path.extname(relPath).toLowerCase())) continue;
if (needsFn && !needsFn(relPath, symbols)) continue;
pending.push({ relPath, absPath: path.join(rootDir, relPath), symbols });
}
return pending;
}
export async function ensureWasmTrees(
fileSymbols: Map<string, any>,
rootDir: string,
needsFn?: (relPath: string, symbols: any) => boolean,
): Promise<void> {
const pending = collectBackfillPending(fileSymbols, rootDir, needsFn);
if (pending.length === 0) return;
const pool = getWasmWorkerPool();
for (const { relPath, absPath, symbols } of pending) {
let code: string;
try {
code = fs.readFileSync(absPath, 'utf-8');
} catch (e: unknown) {
debug(`ensureWasmTrees: cannot read ${relPath}: ${(e as Error).message}`);
continue;
}
const output = await pool.parse(absPath, code, FULL_ANALYSIS);
if (!output) continue; // worker crashed or returned null — skip silently
mergeAnalysisData(symbols, output);
}
}
/** Fill gap-only scalar metadata (`_langId`, `_lineCount`) from the worker output. */
function mergeScalarMetadata(symbols: any, worker: ExtractorOutput): void {
if (!symbols._langId && worker._langId) symbols._langId = worker._langId;
if (!symbols._lineCount && worker._lineCount) symbols._lineCount = worker._lineCount;
}
/** Fill gap-only analysis arrays (`astNodes`, `dataflow`) from the worker output. */
function mergeAnalysisArrays(symbols: any, worker: ExtractorOutput): void {
if (!Array.isArray(symbols.astNodes) && Array.isArray(worker.astNodes)) {
symbols.astNodes = worker.astNodes;
}
if (!symbols.dataflow && worker.dataflow) symbols.dataflow = worker.dataflow;
}
/** Merge worker typeMap into existing symbols.typeMap with first-wins semantics. */
function mergeTypeMap(symbols: any, worker: ExtractorOutput): void {
if (!worker.typeMap || worker.typeMap.size === 0) return;
if (!symbols.typeMap || !(symbols.typeMap instanceof Map)) {
symbols.typeMap = new Map(worker.typeMap);
return;
}
for (const [k, v] of worker.typeMap) {
if (!symbols.typeMap.has(k)) symbols.typeMap.set(k, v);
}
}
/** Patch existing definitions with worker complexity/cfg when absent. */
function mergeDefinitionAnalysis(symbols: any, worker: ExtractorOutput): void {
const existingDefs: any[] = Array.isArray(symbols.definitions) ? symbols.definitions : [];
const workerDefs: any[] = Array.isArray(worker.definitions) ? worker.definitions : [];
if (existingDefs.length === 0 || workerDefs.length === 0) return;
// Index existing defs by (kind, name, line) — mirrors engine.ts matching key.
const byKey = new Map<string, any>();
for (const d of existingDefs) byKey.set(`${d.kind}|${d.name}|${d.line}`, d);
for (const wd of workerDefs) {
const existing = byKey.get(`${wd.kind}|${wd.name}|${wd.line}`);
if (!existing) continue;
if (!existing.complexity && wd.complexity) existing.complexity = wd.complexity;
if ((!existing.cfg || !Array.isArray(existing.cfg.blocks)) && wd.cfg?.blocks) {
existing.cfg = wd.cfg;
}
}
}
/**
* Merge pre-computed analysis data from a worker result onto existing symbols.
* Only fills gaps — never overwrites fields the caller already populated.
* Used to patch native-parsed symbols with worker-produced astNodes / dataflow /
* per-definition complexity and cfg.
*/
function mergeAnalysisData(symbols: any, worker: ExtractorOutput): void {
mergeScalarMetadata(symbols, worker);
mergeAnalysisArrays(symbols, worker);
mergeTypeMap(symbols, worker);
mergeDefinitionAnalysis(symbols, worker);
}
/**
* Check whether the required WASM grammar files exist on disk.
*/
export function isWasmAvailable(): boolean {
return LANGUAGE_REGISTRY.filter((e) => e.required).every((e) =>
fs.existsSync(grammarPath(e.grammarFile)),
);
}
/**
* Return the set of lowercase file extensions whose WASM grammar is actually
* installed on disk. Used to scope engine-parity backfill to files that WASM
* can recover — languages without an installed grammar are skipped by both
* engines, so they don't represent a native-engine drop.
*
* Cached on first call; the grammars directory is shipped immutable.
*/
let _installedWasmExts: Set<string> | null = null;
export function getInstalledWasmExtensions(): Set<string> {
if (_installedWasmExts) return _installedWasmExts;
const exts = new Set<string>();
for (const entry of LANGUAGE_REGISTRY) {
if (fs.existsSync(grammarPath(entry.grammarFile))) {
for (const ext of entry.extensions) exts.add(ext.toLowerCase());
}
}
_installedWasmExts = exts;
return exts;
}
/**
* Lowercase file extensions covered by the native Rust addon.
*
* Mirrors `LanguageKind::from_extension` in
* `crates/codegraph-core/src/parser_registry.rs`. Used to classify why the
* native orchestrator dropped a file: extensions outside this set are a
* legitimate parser limit (no Rust extractor exists), while extensions inside
* it indicate a real native bug (parse/read/extract failure).
*
* Keep this list in sync with the Rust enum — the native addon is a separate
* npm package, so JS has no runtime way to discover its language coverage.
*/
export const NATIVE_SUPPORTED_EXTENSIONS: ReadonlySet<string> = new Set([
'.js',
'.jsx',
'.mjs',
'.cjs',
'.ts',
'.tsx',
'.py',
'.pyi',
'.tf',
'.hcl',
'.go',
'.rs',
'.java',
'.cs',
'.rb',
'.rake',
'.gemspec',
'.php',
'.phtml',
'.c',
'.h',
'.cpp',
'.cc',
'.cxx',
'.hpp',
'.cu',
'.cuh',
'.kt',
'.kts',
'.swift',
'.scala',
'.sh',
'.bash',
'.ex',
'.exs',
'.lua',
'.dart',
'.zig',
'.hs',
'.ml',
'.mli',
'.fs',
'.fsx',
'.fsi',
'.m',
'.gleam',
'.jl',
'.clj',
'.cljs',
'.cljc',
'.erl',
'.hrl',
'.groovy',
'.gvy',
'.r',
'.sol',
'.v',
'.sv',
]);
/**
* Classification for a file the native orchestrator dropped.
* - `unsupported-by-native`: extension has no Rust extractor (legitimate parser limit).
* - `native-extractor-failure`: extension is supported by native but the file was
* still dropped — points at a real bug (read error, parse failure, extractor crash).
*/
export type NativeDropReason = 'unsupported-by-native' | 'native-extractor-failure';
export interface NativeDropClassification {
/** Per-reason → per-extension → list of relative paths that hit that bucket. */
byReason: Record<NativeDropReason, Map<string, string[]>>;
/** Total file count per reason. */
totals: Record<NativeDropReason, number>;
}
/**
* Group the missing files (relative paths) by drop reason and extension so the
* caller can log per-extension counts and a sample path. Pure function — no
* I/O, safe to unit-test independently of the build pipeline.
*/
export function classifyNativeDrops(relPaths: Iterable<string>): NativeDropClassification {
const byReason: Record<NativeDropReason, Map<string, string[]>> = {
'unsupported-by-native': new Map(),
'native-extractor-failure': new Map(),
};
const totals: Record<NativeDropReason, number> = {
'unsupported-by-native': 0,
'native-extractor-failure': 0,
};
for (const rel of relPaths) {
const ext = path.extname(rel).toLowerCase();
const reason: NativeDropReason = NATIVE_SUPPORTED_EXTENSIONS.has(ext)
? 'native-extractor-failure'
: 'unsupported-by-native';
const bucket = byReason[reason];
let list = bucket.get(ext);
if (!list) {
list = [];
bucket.set(ext, list);
}
list.push(rel);
totals[reason]++;
}
return { byReason, totals };
}
/**
* Render `{ ext → paths[] }` as a multi-line tabular breakdown for log lines.
* Each extension occupies its own line so a long warning scans like a table
* instead of a wall of semicolon-separated slices. Caps at 3 sample paths per
* extension and 6 extensions total to keep output bounded when many languages
* are dropped at once. Extensions are sorted by descending file count so the
* loudest offender shows up first; ties keep insertion order.
*
* Returns the empty string for empty input, and otherwise a string that
* begins with `\n` so callers can append it directly after the header line
* (`"Backfilling via WASM:" + formatDropExtensionSummary(...)`).
*
* Pure function — safe to unit-test independently.
*/
export function formatDropExtensionSummary(buckets: Map<string, string[]>): string {
const MAX_EXTS = 6;
const MAX_SAMPLES = 3;
const entries = Array.from(buckets.entries()).sort((a, b) => b[1].length - a[1].length);
if (entries.length === 0) return '';
const shown = entries.slice(0, MAX_EXTS);
const extWidth = Math.max(...shown.map(([ext]) => ext.length));
const countWidth = Math.max(...shown.map(([, paths]) => String(paths.length).length));
const lines = shown.map(([ext, paths]) => {
const sample = paths.slice(0, MAX_SAMPLES).join(', ');
const more = paths.length > MAX_SAMPLES ? ` (+${paths.length - MAX_SAMPLES} more)` : '';
return ` ${ext.padEnd(extWidth)} ${String(paths.length).padStart(countWidth)} ${sample}${more}`;
});
if (entries.length > MAX_EXTS) {
lines.push(` (+${entries.length - MAX_EXTS} more extension(s))`);
}
return `\n${lines.join('\n')}`;
}
// ── Unified API ──────────────────────────────────────────────────────────────
function resolveEngine(opts: ParseEngineOpts = {}): ResolvedEngine {
const pref = opts.engine || 'auto';
if (pref === 'wasm') return { name: 'wasm', native: null };
if (pref === 'native' || pref === 'auto') {
const native = loadNative();
if (native) return { name: 'native', native };
if (pref === 'native') {
getNative(); // throws with detailed error + install instructions
}
}
return { name: 'wasm', native: null };
}
/**
* Patch native engine output in-place for the few remaining semantic transforms.
* With #[napi(js_name)] on Rust types, most fields already arrive as camelCase.
* This only handles:
* - _lineCount compat for builder.js
* - Backward compat for older native binaries missing js_name annotations
* - dataflow argFlows/mutations bindingType -> binding wrapper
*/
/** Patch definition fields for backward compat with older native binaries. */
function patchDefinitions(definitions: any[]): void {
for (const d of definitions) {
if (d.endLine === undefined && d.end_line !== undefined) {
d.endLine = d.end_line;
}
}
}
/**
* Field renames applied to each import record to bridge older native binaries
* that emit snake_case names. Each `[camel, snake]` pair becomes:
* `if (imp[camel] === undefined) imp[camel] = imp[snake];`
* Defined as data so the loop body stays trivially linear in cognitive complexity.
*/
const IMPORT_FIELD_RENAMES: ReadonlyArray<readonly [string, string]> = [
['typeOnly', 'type_only'],
['wildcardReexport', 'wildcard_reexport'],
['pythonImport', 'python_import'],
['goImport', 'go_import'],
['rustUse', 'rust_use'],
['javaImport', 'java_import'],
['csharpUsing', 'csharp_using'],
['rubyRequire', 'ruby_require'],
['phpUse', 'php_use'],
['cInclude', 'c_include'],
['kotlinImport', 'kotlin_import'],
['swiftImport', 'swift_import'],
['scalaImport', 'scala_import'],
['bashSource', 'bash_source'],
['dynamicImport', 'dynamic_import'],
];
/** Patch import fields for backward compat with older native binaries. */
function patchImports(imports: any[]): void {
for (const i of imports) {
for (const [camel, snake] of IMPORT_FIELD_RENAMES) {
if (i[camel] === undefined) i[camel] = i[snake];
}
}
}
/** Normalize native typeMap array to a Map instance.
* Uses first-wins semantics at equal confidence to match the WASM/JS extractor. */
function patchTypeMap(r: any): void {
if (!r.typeMap) {
r.typeMap = new Map();
} else if (!(r.typeMap instanceof Map)) {
const map = new Map<string, TypeMapEntry>();
for (const e of r.typeMap as Array<{ name: string; typeName: string }>) {
if (!map.has(e.name)) {
map.set(e.name, { type: e.typeName, confidence: (e as any).confidence ?? 0.9 });
}
}
r.typeMap = map;
}
}
/** Normalize native returnTypeMap array to a Map instance, keeping highest-confidence entry per key. */
function patchReturnTypeMap(r: any): void {
if (!r.returnTypeMap || r.returnTypeMap instanceof Map) return;
const map = new Map<string, TypeMapEntry>();
for (const e of r.returnTypeMap as Array<{
name: string;
typeName: string;
confidence?: number;
}>) {
const conf = e.confidence ?? 1.0;
const existing = map.get(e.name);
if (!existing || conf > existing.confidence) {
map.set(e.name, { type: e.typeName, confidence: conf });
}
}
r.returnTypeMap = map.size > 0 ? map : new Map();
}
/** Wrap bindingType into binding object for dataflow argFlows and mutations. */
function patchDataflow(dataflow: any): void {
if (dataflow.argFlows) {
for (const f of dataflow.argFlows) {
f.binding = f.bindingType ? { type: f.bindingType } : null;
}
}
if (dataflow.mutations) {
for (const m of dataflow.mutations) {
m.binding = m.bindingType ? { type: m.bindingType } : null;
}
}
}
function patchNativeResult(r: any): ExtractorOutput {
// lineCount: napi(js_name) emits "lineCount"; older binaries may emit "line_count"
r.lineCount = r.lineCount ?? r.line_count ?? null;
r._lineCount = r.lineCount;
if (r.definitions) patchDefinitions(r.definitions);
if (r.imports) patchImports(r.imports);
patchTypeMap(r);
patchReturnTypeMap(r);
if (r.dataflow) patchDataflow(r.dataflow);
return r;
}
/**
* Declarative registry of all supported languages.
* Adding a new language requires only a new entry here + its extractor function.
*/
export const LANGUAGE_REGISTRY: LanguageRegistryEntry[] = [
{
id: 'javascript',
extensions: ['.js', '.jsx', '.mjs', '.cjs'],
grammarFile: 'tree-sitter-javascript.wasm',
extractor: extractSymbols,
required: true,
},
{
id: 'typescript',
extensions: ['.ts'],
grammarFile: 'tree-sitter-typescript.wasm',
extractor: extractSymbols,
required: true,
},
{
id: 'tsx',
extensions: ['.tsx'],
grammarFile: 'tree-sitter-tsx.wasm',
extractor: extractSymbols,
required: true,
},
{
id: 'hcl',
extensions: ['.tf', '.hcl'],
grammarFile: 'tree-sitter-hcl.wasm',
extractor: extractHCLSymbols,
required: false,
},
{
id: 'python',
extensions: ['.py', '.pyi'],
grammarFile: 'tree-sitter-python.wasm',
extractor: extractPythonSymbols,
required: false,
},
{
id: 'go',
extensions: ['.go'],
grammarFile: 'tree-sitter-go.wasm',
extractor: extractGoSymbols,
required: false,
},
{
id: 'rust',
extensions: ['.rs'],
grammarFile: 'tree-sitter-rust.wasm',
extractor: extractRustSymbols,
required: false,
},
{
id: 'java',
extensions: ['.java'],
grammarFile: 'tree-sitter-java.wasm',
extractor: extractJavaSymbols,
required: false,
},
{
id: 'csharp',
extensions: ['.cs'],
grammarFile: 'tree-sitter-c_sharp.wasm',
extractor: extractCSharpSymbols,
required: false,
},
{
id: 'ruby',
extensions: ['.rb', '.rake', '.gemspec'],
grammarFile: 'tree-sitter-ruby.wasm',
extractor: extractRubySymbols,
required: false,
},
{
id: 'php',
extensions: ['.php', '.phtml'],
grammarFile: 'tree-sitter-php.wasm',
extractor: extractPHPSymbols,
required: false,
},
{
id: 'c',
extensions: ['.c', '.h'],
grammarFile: 'tree-sitter-c.wasm',
extractor: extractCSymbols,
required: false,
},
{
id: 'cpp',
extensions: ['.cpp', '.cc', '.cxx', '.hpp'],
grammarFile: 'tree-sitter-cpp.wasm',
extractor: extractCppSymbols,
required: false,
},
{
id: 'kotlin',
extensions: ['.kt', '.kts'],
grammarFile: 'tree-sitter-kotlin.wasm',
extractor: extractKotlinSymbols,
required: false,
},
{
id: 'swift',
extensions: ['.swift'],
grammarFile: 'tree-sitter-swift.wasm',
extractor: extractSwiftSymbols,
required: false,
},
{
id: 'scala',
extensions: ['.scala'],
grammarFile: 'tree-sitter-scala.wasm',
extractor: extractScalaSymbols,
required: false,
},
{
id: 'bash',
extensions: ['.sh', '.bash'],
grammarFile: 'tree-sitter-bash.wasm',
extractor: extractBashSymbols,
required: false,
},
{
id: 'elixir',
extensions: ['.ex', '.exs'],
grammarFile: 'tree-sitter-elixir.wasm',
extractor: extractElixirSymbols,
required: false,
},
{
id: 'lua',
extensions: ['.lua'],
grammarFile: 'tree-sitter-lua.wasm',
extractor: extractLuaSymbols,
required: false,
},
{
id: 'dart',
extensions: ['.dart'],
grammarFile: 'tree-sitter-dart.wasm',
extractor: extractDartSymbols,
required: false,
},
{
id: 'zig',
extensions: ['.zig'],
grammarFile: 'tree-sitter-zig.wasm',
extractor: extractZigSymbols,
required: false,
},
{
id: 'haskell',
extensions: ['.hs'],
grammarFile: 'tree-sitter-haskell.wasm',
extractor: extractHaskellSymbols,
required: false,
},
{
id: 'ocaml',
extensions: ['.ml'],
grammarFile: 'tree-sitter-ocaml.wasm',
extractor: extractOCamlSymbols,
required: false,
},
{
id: 'ocaml-interface',
extensions: ['.mli'],
grammarFile: 'tree-sitter-ocaml_interface.wasm',
extractor: extractOCamlSymbols,
required: false,
},
{
id: 'fsharp',
extensions: ['.fs', '.fsx'],
grammarFile: 'tree-sitter-fsharp.wasm',
extractor: extractFSharpSymbols,
required: false,
},
{
id: 'fsharp-signature',
extensions: ['.fsi'],
grammarFile: 'tree-sitter-fsharp_signature.wasm',
extractor: extractFSharpSymbols,
required: false,
},
{
id: 'gleam',
extensions: ['.gleam'],
grammarFile: 'tree-sitter-gleam.wasm',
extractor: extractGleamSymbols,
required: false,
},
{
id: 'clojure',
extensions: ['.clj', '.cljs', '.cljc'],
grammarFile: 'tree-sitter-clojure.wasm',
extractor: extractClojureSymbols,
required: false,
},
{
id: 'julia',
extensions: ['.jl'],
grammarFile: 'tree-sitter-julia.wasm',
extractor: extractJuliaSymbols,
required: false,
},
{
id: 'r',
extensions: ['.r', '.R'],
grammarFile: 'tree-sitter-r.wasm',
extractor: extractRSymbols,
required: false,
},
{
id: 'erlang',
extensions: ['.erl', '.hrl'],
grammarFile: 'tree-sitter-erlang.wasm',
extractor: extractErlangSymbols,
required: false,
},
{
id: 'solidity',
extensions: ['.sol'],
grammarFile: 'tree-sitter-solidity.wasm',
extractor: extractSoliditySymbols,
required: false,
},
{
id: 'objc',
extensions: ['.m'],
grammarFile: 'tree-sitter-objc.wasm',
extractor: extractObjCSymbols,
required: false,
},
{
id: 'cuda',
extensions: ['.cu', '.cuh'],
grammarFile: 'tree-sitter-cuda.wasm',
extractor: extractCudaSymbols,
required: false,
},
{
id: 'groovy',
extensions: ['.groovy', '.gvy'],
grammarFile: 'tree-sitter-groovy.wasm',
extractor: extractGroovySymbols,
required: false,
},
{
id: 'verilog',
extensions: ['.v', '.sv'],
grammarFile: 'tree-sitter-verilog.wasm',
extractor: extractVerilogSymbols,
required: false,
},
];
const _extToLang: Map<string, LanguageRegistryEntry> = new Map();
for (const entry of LANGUAGE_REGISTRY) {
for (const ext of entry.extensions) {
_extToLang.set(ext, entry);
}