-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathparser.js
More file actions
626 lines (583 loc) · 20 KB
/
Copy pathparser.js
File metadata and controls
626 lines (583 loc) · 20 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
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { Language, Parser, Query } from 'web-tree-sitter';
import { debug, warn } from '../infrastructure/logger.js';
import { getNative, getNativePackageVersion, loadNative } from '../infrastructure/native.js';
// Re-export all extractors for backward compatibility
export {
extractCSharpSymbols,
extractGoSymbols,
extractHCLSymbols,
extractJavaSymbols,
extractPHPSymbols,
extractPythonSymbols,
extractRubySymbols,
extractRustSymbols,
extractSymbols,
} from '../extractors/index.js';
import {
extractCSharpSymbols,
extractGoSymbols,
extractHCLSymbols,
extractJavaSymbols,
extractPHPSymbols,
extractPythonSymbols,
extractRubySymbols,
extractRustSymbols,
extractSymbols,
} from '../extractors/index.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
function grammarPath(name) {
return path.join(__dirname, '..', '..', 'grammars', name);
}
let _initialized = false;
// Memoized parsers — avoids reloading WASM grammars on every createParsers() call
let _cachedParsers = null;
// Cached Language objects — WASM-backed, must be .delete()'d explicitly
let _cachedLanguages = null;
// Query cache for JS/TS/TSX extractors (populated during createParsers)
const _queryCache = new Map();
// Shared patterns for all JS/TS/TSX (class_declaration excluded — name type differs)
const COMMON_QUERY_PATTERNS = [
'(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)',
'(method_definition name: (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',
'(expression_statement (assignment_expression left: (member_expression) @assign_left right: (_) @assign_right)) @assign_node',
];
// JS: class name is (identifier)
const JS_CLASS_PATTERN = '(class_declaration name: (identifier) @cls_name) @cls_node';
// TS/TSX: class name is (type_identifier), plus interface and type alias
const TS_EXTRA_PATTERNS = [
'(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',
];
export async function createParsers() {
if (_cachedParsers) return _cachedParsers;
if (!_initialized) {
await Parser.init();
_initialized = true;
}
const parsers = new Map();
const languages = new Map();
for (const entry of LANGUAGE_REGISTRY) {
try {
const lang = await Language.load(grammarPath(entry.grammarFile));
const parser = new Parser();
parser.setLanguage(lang);
parsers.set(entry.id, parser);
languages.set(entry.id, lang);
// Compile and cache tree-sitter Query for JS/TS/TSX extractors
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) {
if (entry.required) throw e;
warn(
`${entry.id} parser failed to initialize: ${e.message}. ${entry.id} files will be skipped.`,
);
parsers.set(entry.id, null);
}
}
_cachedParsers = parsers;
_cachedLanguages = languages;
return parsers;
}
/**
* 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.
*/
export function disposeParsers() {
if (_cachedParsers) {
for (const [id, parser] of _cachedParsers) {
if (parser && typeof parser.delete === 'function') {
try {
parser.delete();
} catch (e) {
debug(`Failed to dispose parser ${id}: ${e.message}`);
}
}
}
_cachedParsers = null;
}
for (const [id, query] of _queryCache) {
if (query && typeof query.delete === 'function') {
try {
query.delete();
} catch (e) {
debug(`Failed to dispose query ${id}: ${e.message}`);
}
}
}
_queryCache.clear();
if (_cachedLanguages) {
for (const [id, lang] of _cachedLanguages) {
if (lang && typeof lang.delete === 'function') {
try {
lang.delete();
} catch (e) {
debug(`Failed to dispose language ${id}: ${e.message}`);
}
}
}
_cachedLanguages = null;
}
_initialized = false;
}
export function getParser(parsers, filePath) {
const ext = path.extname(filePath);
const entry = _extToLang.get(ext);
if (!entry) return null;
return parsers.get(entry.id) || null;
}
/**
* Pre-parse files missing `_tree` via WASM so downstream phases (CFG, dataflow)
* don't each need to create parsers and re-parse independently.
* Only parses files whose extension is in SUPPORTED_EXTENSIONS.
*
* @param {Map<string, object>} fileSymbols - Map<relPath, { definitions, _tree, _langId, ... }>
* @param {string} rootDir - absolute project root
*/
export async function ensureWasmTrees(fileSymbols, rootDir) {
// Check if any file needs a tree
let needsParse = false;
for (const [relPath, symbols] of fileSymbols) {
if (!symbols._tree) {
const ext = path.extname(relPath).toLowerCase();
if (_extToLang.has(ext)) {
needsParse = true;
break;
}
}
}
if (!needsParse) return;
const parsers = await createParsers();
for (const [relPath, symbols] of fileSymbols) {
if (symbols._tree) continue;
const ext = path.extname(relPath).toLowerCase();
const entry = _extToLang.get(ext);
if (!entry) continue;
const parser = parsers.get(entry.id);
if (!parser) continue;
const absPath = path.join(rootDir, relPath);
let code;
try {
code = fs.readFileSync(absPath, 'utf-8');
} catch (e) {
debug(`ensureWasmTrees: cannot read ${relPath}: ${e.message}`);
continue;
}
try {
symbols._tree = parser.parse(code);
symbols._langId = entry.id;
} catch (e) {
debug(`ensureWasmTrees: parse failed for ${relPath}: ${e.message}`);
}
}
}
/**
* Check whether the required WASM grammar files exist on disk.
*/
export function isWasmAvailable() {
return LANGUAGE_REGISTRY.filter((e) => e.required).every((e) =>
fs.existsSync(grammarPath(e.grammarFile)),
);
}
// ── Unified API ──────────────────────────────────────────────────────────────
function resolveEngine(opts = {}) {
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
*/
function patchNativeResult(r) {
// lineCount: napi(js_name) emits "lineCount"; older binaries may emit "line_count"
r.lineCount = r.lineCount ?? r.line_count ?? null;
r._lineCount = r.lineCount;
// Backward compat for older binaries missing js_name annotations
if (r.definitions) {
for (const d of r.definitions) {
if (d.endLine === undefined && d.end_line !== undefined) {
d.endLine = d.end_line;
}
}
}
if (r.imports) {
for (const i of r.imports) {
if (i.typeOnly === undefined) i.typeOnly = i.type_only;
if (i.wildcardReexport === undefined) i.wildcardReexport = i.wildcard_reexport;
if (i.pythonImport === undefined) i.pythonImport = i.python_import;
if (i.goImport === undefined) i.goImport = i.go_import;
if (i.rustUse === undefined) i.rustUse = i.rust_use;
if (i.javaImport === undefined) i.javaImport = i.java_import;
if (i.csharpUsing === undefined) i.csharpUsing = i.csharp_using;
if (i.rubyRequire === undefined) i.rubyRequire = i.ruby_require;
if (i.phpUse === undefined) i.phpUse = i.php_use;
if (i.dynamicImport === undefined) i.dynamicImport = i.dynamic_import;
}
}
// dataflow: wrap bindingType into binding object for argFlows and mutations
if (r.dataflow) {
if (r.dataflow.argFlows) {
for (const f of r.dataflow.argFlows) {
f.binding = f.bindingType ? { type: f.bindingType } : null;
}
}
if (r.dataflow.mutations) {
for (const m of r.dataflow.mutations) {
m.binding = m.bindingType ? { type: m.bindingType } : null;
}
}
}
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 = [
{
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,
},
];
const _extToLang = new Map();
for (const entry of LANGUAGE_REGISTRY) {
for (const ext of entry.extensions) {
_extToLang.set(ext, entry);
}
}
export const SUPPORTED_EXTENSIONS = new Set(_extToLang.keys());
/**
* WASM-based typeMap backfill for older native binaries that don't emit typeMap.
* Uses tree-sitter AST extraction instead of regex to avoid false positives from
* matches inside comments and string literals.
* TODO: Remove once all published native binaries include typeMap extraction (>= 3.2.0)
*/
async function backfillTypeMap(filePath, source) {
let code = source;
if (!code) {
try {
code = fs.readFileSync(filePath, 'utf-8');
} catch {
return { typeMap: [], backfilled: false };
}
}
const parsers = await createParsers();
const extracted = wasmExtractSymbols(parsers, filePath, code);
try {
if (!extracted?.symbols?.typeMap) {
return { typeMap: [], backfilled: false };
}
const tm = extracted.symbols.typeMap;
return {
typeMap: tm instanceof Map ? tm : new Map(tm.map((e) => [e.name, e.typeName])),
backfilled: true,
};
} finally {
// Free the WASM tree to prevent memory accumulation across repeated builds
if (extracted?.tree && typeof extracted.tree.delete === 'function') {
try {
extracted.tree.delete();
} catch {}
}
}
}
/**
* WASM extraction helper: picks the right extractor based on file extension.
*/
function wasmExtractSymbols(parsers, filePath, code) {
const parser = getParser(parsers, filePath);
if (!parser) return null;
let tree;
try {
tree = parser.parse(code);
} catch (e) {
warn(`Parse error in ${filePath}: ${e.message}`);
return null;
}
const ext = path.extname(filePath);
const entry = _extToLang.get(ext);
if (!entry) return null;
const query = _queryCache.get(entry.id) || null;
const symbols = entry.extractor(tree, filePath, query);
return symbols ? { symbols, tree, langId: entry.id } : null;
}
/**
* Parse a single file and return normalized symbols.
*
* @param {string} filePath Absolute path to the file.
* @param {string} source Source code string.
* @param {object} [opts] Options: { engine: 'native'|'wasm'|'auto' }
* @returns {Promise<{definitions, calls, imports, classes, exports}|null>}
*/
export async function parseFileAuto(filePath, source, opts = {}) {
const { native } = resolveEngine(opts);
if (native) {
const result = native.parseFile(filePath, source, !!opts.dataflow, opts.ast !== false);
if (!result) return null;
const patched = patchNativeResult(result);
// Only backfill typeMap for TS/TSX — JS files have no type annotations,
// and the native engine already handles `new Expr()` patterns.
const TS_BACKFILL_EXTS = new Set(['.ts', '.tsx']);
if (
(!patched.typeMap || patched.typeMap.length === 0) &&
TS_BACKFILL_EXTS.has(path.extname(filePath))
) {
const { typeMap, backfilled } = await backfillTypeMap(filePath, source);
patched.typeMap = typeMap;
if (backfilled) patched._typeMapBackfilled = true;
}
return patched;
}
// WASM path
const parsers = await createParsers();
const extracted = wasmExtractSymbols(parsers, filePath, source);
return extracted ? extracted.symbols : null;
}
/**
* Parse multiple files in bulk and return a Map<relPath, symbols>.
*
* @param {string[]} filePaths Absolute paths to files.
* @param {string} rootDir Project root for computing relative paths.
* @param {object} [opts] Options: { engine: 'native'|'wasm'|'auto' }
* @returns {Promise<Map<string, {definitions, calls, imports, classes, exports}>>}
*/
export async function parseFilesAuto(filePaths, rootDir, opts = {}) {
const { native } = resolveEngine(opts);
const result = new Map();
if (native) {
const nativeResults = native.parseFiles(
filePaths,
rootDir,
!!opts.dataflow,
opts.ast !== false,
);
const needsTypeMap = [];
for (const r of nativeResults) {
if (!r) continue;
const patched = patchNativeResult(r);
const relPath = path.relative(rootDir, r.file).split(path.sep).join('/');
result.set(relPath, patched);
if (!patched.typeMap || patched.typeMap.length === 0) {
needsTypeMap.push({ filePath: r.file, relPath });
}
}
// Backfill typeMap via WASM for native binaries that predate the type-map feature
if (needsTypeMap.length > 0) {
// Only backfill for languages where WASM extraction can produce typeMap
// (TS/TSX have type annotations; JS only has `new Expr()` which native already handles)
const TS_EXTS = new Set(['.ts', '.tsx']);
const tsFiles = needsTypeMap.filter(({ filePath }) => TS_EXTS.has(path.extname(filePath)));
if (tsFiles.length > 0) {
const parsers = await createParsers();
for (const { filePath, relPath } of tsFiles) {
let extracted;
try {
const code = fs.readFileSync(filePath, 'utf-8');
extracted = wasmExtractSymbols(parsers, filePath, code);
if (extracted?.symbols?.typeMap) {
const symbols = result.get(relPath);
symbols.typeMap =
extracted.symbols.typeMap instanceof Map
? extracted.symbols.typeMap
: new Map(extracted.symbols.typeMap.map((e) => [e.name, e.typeName]));
symbols._typeMapBackfilled = true;
}
} catch {
/* skip — typeMap is a best-effort backfill */
} finally {
// Free the WASM tree to prevent memory accumulation across repeated builds
if (extracted?.tree && typeof extracted.tree.delete === 'function') {
try {
extracted.tree.delete();
} catch {}
}
}
}
}
}
return result;
}
// WASM path
const parsers = await createParsers();
for (const filePath of filePaths) {
let code;
try {
code = fs.readFileSync(filePath, 'utf-8');
} catch (err) {
warn(`Skipping ${path.relative(rootDir, filePath)}: ${err.message}`);
continue;
}
const extracted = wasmExtractSymbols(parsers, filePath, code);
if (extracted) {
const relPath = path.relative(rootDir, filePath).split(path.sep).join('/');
extracted.symbols._tree = extracted.tree;
extracted.symbols._langId = extracted.langId;
extracted.symbols._lineCount = code.split('\n').length;
result.set(relPath, extracted.symbols);
}
}
return result;
}
/**
* Report which engine is active.
*
* @param {object} [opts] Options: { engine: 'native'|'wasm'|'auto' }
* @returns {{ name: 'native'|'wasm', version: string|null }}
*/
export function getActiveEngine(opts = {}) {
const { name, native } = resolveEngine(opts);
let version = native
? typeof native.engineVersion === 'function'
? native.engineVersion()
: null
: null;
// Prefer platform package.json version over binary-embedded version
// to handle stale binaries that weren't recompiled during a release
if (native) {
try {
version = getNativePackageVersion() ?? version;
} catch (e) {
debug(`getNativePackageVersion failed: ${e.message}`);
}
}
return { name, version };
}
/**
* Create a native ParseTreeCache for incremental parsing.
* Returns null if the native engine is unavailable (WASM fallback).
*/
export function createParseTreeCache() {
const native = loadNative();
if (!native || !native.ParseTreeCache) return null;
return new native.ParseTreeCache();
}
/**
* Parse a file incrementally using the cache, or fall back to full parse.
*
* @param {object|null} cache ParseTreeCache instance (or null for full parse)
* @param {string} filePath Absolute path to the file
* @param {string} source Source code string
* @param {object} [opts] Options forwarded to parseFileAuto on fallback
* @returns {Promise<{definitions, calls, imports, classes, exports}|null>}
*/
export async function parseFileIncremental(cache, filePath, source, opts = {}) {
if (cache) {
const result = cache.parseFile(filePath, source);
if (!result) return null;
const patched = patchNativeResult(result);
// Only backfill typeMap for TS/TSX — JS files have no type annotations,
// and the native engine already handles `new Expr()` patterns.
const TS_BACKFILL_EXTS = new Set(['.ts', '.tsx']);
if (
(!patched.typeMap || patched.typeMap.length === 0) &&
TS_BACKFILL_EXTS.has(path.extname(filePath))
) {
const { typeMap, backfilled } = await backfillTypeMap(filePath, source);
patched.typeMap = typeMap;
if (backfilled) patched._typeMapBackfilled = true;
}
return patched;
}
return parseFileAuto(filePath, source, opts);
}