fix(javascript): parse declaration artifacts in ambient mode

This commit is contained in:
Rudy Celekli
2026-10-09 12:09:15 -04:00
committed by morluto
parent a0523b375e
commit 4896adcd0f
2 changed files with 63 additions and 6 deletions
@@ -1,4 +1,4 @@
import { parse } from "@babel/parser";
import { parse, type ParserPlugin } from "@babel/parser";
/** Babel AST produced by REA's inert JavaScript parser boundary. */
export type ParsedJavaScriptSource = ReturnType<typeof parse>;
@@ -8,6 +8,10 @@ export const parseJavaScriptSource = (
source: string,
sourcePath?: string,
): ParsedJavaScriptSource | null => {
const typescript: ParserPlugin = [
"typescript",
{ dts: /\.d\.(?:ts|mts|cts)$/iu.test(sourcePath ?? "") },
];
try {
return parse(source, {
sourceType: "unambiguous",
@@ -17,12 +21,11 @@ export const parseJavaScriptSource = (
// forms, including parameter decorators. Without it a decorated
// TypeScript source fails to parse at all rather than reporting
// recovered syntax, which loses every fact derived from that source.
// Plain .ts artifacts admit angle-bracket type assertions instead of JSX.
// Plain .ts and .cts artifacts admit angle-bracket type assertions instead of JSX.
// Unknown paths retain the existing JSX-capable parser contract.
plugins:
sourcePath?.toLowerCase().endsWith(".ts") === true
? ["decorators-legacy", "typescript"]
: ["decorators-legacy", "jsx", "typescript"],
plugins: /\.(?:ts|cts)$/iu.test(sourcePath ?? "")
? ["decorators-legacy", typescript]
: ["decorators-legacy", "jsx", typescript],
});
} catch (cause: unknown) {
// Unparseable source is represented by the null return.