mirror of
https://github.com/morluto/rea
synced 2026-10-26 14:25:49 -04:00
fix(javascript): parse declaration artifacts in ambient mode
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user