Skip to content

Commit 83bbd2c

Browse files
committed
fix: address review feedback on WASM tree cleanup and JS backfill skip
- Consolidate duplicated tree.delete() in backfillTypeMap into a single finally block, preventing future early-return paths from leaking trees - Skip WASM typeMap backfill for JS files in parseFileAuto and parseFileIncremental single-file paths, matching the bulk path behavior Impact: 3 functions changed, 2 affected
1 parent c0c0c9b commit 83bbd2c

1 file changed

Lines changed: 24 additions & 15 deletions

File tree

src/domain/parser.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -395,26 +395,23 @@ async function backfillTypeMap(filePath, source) {
395395
}
396396
const parsers = await createParsers();
397397
const extracted = wasmExtractSymbols(parsers, filePath, code);
398-
if (!extracted?.symbols?.typeMap) {
398+
try {
399+
if (!extracted?.symbols?.typeMap) {
400+
return { typeMap: [], backfilled: false };
401+
}
402+
const tm = extracted.symbols.typeMap;
403+
return {
404+
typeMap: tm instanceof Map ? tm : new Map(tm.map((e) => [e.name, e.typeName])),
405+
backfilled: true,
406+
};
407+
} finally {
399408
// Free the WASM tree to prevent memory accumulation across repeated builds
400409
if (extracted?.tree && typeof extracted.tree.delete === 'function') {
401410
try {
402411
extracted.tree.delete();
403412
} catch {}
404413
}
405-
return { typeMap: [], backfilled: false };
406-
}
407-
const tm = extracted.symbols.typeMap;
408-
// Free the WASM tree — only the typeMap data is needed
409-
if (extracted.tree && typeof extracted.tree.delete === 'function') {
410-
try {
411-
extracted.tree.delete();
412-
} catch {}
413414
}
414-
return {
415-
typeMap: tm instanceof Map ? tm : new Map(tm.map((e) => [e.name, e.typeName])),
416-
backfilled: true,
417-
};
418415
}
419416

420417
/**
@@ -455,7 +452,13 @@ export async function parseFileAuto(filePath, source, opts = {}) {
455452
const result = native.parseFile(filePath, source, !!opts.dataflow, opts.ast !== false);
456453
if (!result) return null;
457454
const patched = patchNativeResult(result);
458-
if (!patched.typeMap || patched.typeMap.length === 0) {
455+
// Only backfill typeMap for TS/TSX — JS files have no type annotations,
456+
// and the native engine already handles `new Expr()` patterns.
457+
const TS_BACKFILL_EXTS = new Set(['.ts', '.tsx']);
458+
if (
459+
(!patched.typeMap || patched.typeMap.length === 0) &&
460+
TS_BACKFILL_EXTS.has(path.extname(filePath))
461+
) {
459462
const { typeMap, backfilled } = await backfillTypeMap(filePath, source);
460463
patched.typeMap = typeMap;
461464
if (backfilled) patched._typeMapBackfilled = true;
@@ -606,7 +609,13 @@ export async function parseFileIncremental(cache, filePath, source, opts = {}) {
606609
const result = cache.parseFile(filePath, source);
607610
if (!result) return null;
608611
const patched = patchNativeResult(result);
609-
if (!patched.typeMap || patched.typeMap.length === 0) {
612+
// Only backfill typeMap for TS/TSX — JS files have no type annotations,
613+
// and the native engine already handles `new Expr()` patterns.
614+
const TS_BACKFILL_EXTS = new Set(['.ts', '.tsx']);
615+
if (
616+
(!patched.typeMap || patched.typeMap.length === 0) &&
617+
TS_BACKFILL_EXTS.has(path.extname(filePath))
618+
) {
610619
const { typeMap, backfilled } = await backfillTypeMap(filePath, source);
611620
patched.typeMap = typeMap;
612621
if (backfilled) patched._typeMapBackfilled = true;

0 commit comments

Comments
 (0)