perf(javascript): reuse parsed source across analyses

This commit is contained in:
morluto
2026-07-25 09:10:51 +00:00
parent 674364443d
commit adc4852f3a
4 changed files with 76 additions and 34 deletions
+20
View File
@@ -0,0 +1,20 @@
import { parse } from "@babel/parser";
/** Babel AST produced by REA's inert JavaScript parser boundary. */
export type ParsedJavaScriptSource = ReturnType<typeof parse>;
/** Parse JavaScript or TypeScript once without retaining unused comment nodes. */
export const parseJavaScriptSource = (
source: string,
): ParsedJavaScriptSource | null => {
try {
return parse(source, {
sourceType: "unambiguous",
errorRecovery: true,
attachComment: false,
plugins: ["jsx", "typescript"],
});
} catch {
return null;
}
};