fix(web): share TypeScript source-map filename grammar (#12286)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 11:39:52 +00:00
1 parent 9c0ebe7ff7
commit 6fdfdc4af1
3 files changed
+32 -16

No files matched your search

+6 -8
View File
@@ -15,7 +15,10 @@ import { err, ok, type Result } from "../domain/result.js";
import { isUrlLikeModuleSpecifier } from "../domain/webBundleAnalyzerAst.js";
import { analyzeParsedJavaScriptReferences } from "../domain/javascript/javascriptSemanticAnalysis.js";
import { traverseJavaScriptAst } from "../domain/javascript/javascriptSemanticTraversal.js";
import { parseJavaScriptSource } from "../domain/javascript/javascriptSourceParser.js";
import {
isTypeScriptSourcePath,
parseJavaScriptSource,
} from "../domain/javascript/javascriptSourceParser.js";
import {
semanticStaticModuleSource,
semanticImportEqualsSpecifier,
@@ -1609,15 +1612,10 @@ const resolveOriginalSource = (
}
};
const sourceMediaType = (source: string | null): string => {
const path = source === null ? null : originalSourcePath(source);
return path?.endsWith(".ts") ||
path?.endsWith(".tsx") ||
path?.endsWith(".mts") ||
path?.endsWith(".cts")
const sourceMediaType = (source: string | null): string =>
source !== null && isTypeScriptSourcePath(originalSourcePath(source))
? "text/typescript"
: "text/javascript";
};
const originalDependency = (
node: t.Node,
@@ -239,7 +239,7 @@ const parserOptionsForLoader = (
allowNewTargetOutsideFunction: true,
}
: detectsJavaScriptLoader(sourcePath, packageContext) ||
/\.(?:ts|tsx|mts|cts)$/iu.test(sourcePath ?? "")
isTypeScriptSourcePath(sourcePath)
? {
allowReturnOutsideFunction: false,
allowNewTargetOutsideFunction: false,
@@ -337,7 +337,8 @@ const parseWithRuntimeContext = (
// ESM and TypeScript file loading remove the UTF-8 signature before a
// hashbang; JavaScript CommonJS loading does not. Preserve original indexes
// through Babel's origin options rather than repairing downstream ranges.
const compilerSource = /\.(?:jsx|ts|tsx|mts|cts)$/iu.test(sourcePath ?? "");
const compilerSource =
/\.jsx$/iu.test(sourcePath ?? "") || isTypeScriptSourcePath(sourcePath);
const signedHashbang = source.startsWith("\uFEFF#!");
const conditionalCommonJs =
signedHashbang &&
@@ -434,6 +435,12 @@ const sourceSyntaxPlugins: ParserPlugin[] = [
"sourcePhaseImports",
];
const javaScriptPlugins = (): ParserPlugin[] => [...sourceSyntaxPlugins, "jsx"];
const typeScriptSourceSuffix =
/\.(?:(?<declaration>d)\.)?(?:(?<module>[cm])ts|ts(?<jsx>x)?)$/iu;
/** Whether the filename supplies TypeScript grammar, independent of loader mode. */
export const isTypeScriptSourcePath = (sourcePath?: string): boolean =>
typeScriptSourceSuffix.test(sourcePath ?? "");
/**
* Babel plugins for one source path. Single owner for TypeScript/JSX
@@ -457,18 +464,19 @@ const javaScriptPlugins = (): ParserPlugin[] => [...sourceSyntaxPlugins, "jsx"];
const parserPluginsForPath = (sourcePath?: string): ParserPlugin[] => {
const path = sourcePath ?? "";
if (/\.(?:js|jsx|mjs|cjs)$/iu.test(path)) return javaScriptPlugins();
const dts = /\.d\.(?:ts|mts|cts)$/iu.test(path);
const isTsLike = /\.(?:ts|cts|mts)$/iu.test(path);
const isTsx = /\.tsx$/iu.test(path);
const explicitModuleSuffix = /\.[cm]ts$/iu.test(path);
const suffix = typeScriptSourceSuffix.exec(path)?.groups;
const jsx = suffix?.jsx !== undefined;
const dts = suffix?.declaration !== undefined && !jsx;
const typescript: ParserPlugin = [
"typescript",
{
dts,
...(explicitModuleSuffix ? { disallowAmbiguousJSXLike: true } : {}),
...(suffix?.module === undefined
? {}
: { disallowAmbiguousJSXLike: true }),
},
];
const jsxCapable = !dts && (isTsx || !isTsLike);
const jsxCapable = suffix === undefined || jsx;
return jsxCapable
? [...sourceSyntaxPlugins, "jsx", typescript]
: [...sourceSyntaxPlugins, typescript];
@@ -27,6 +27,11 @@ it("selects original source language from decoded URL paths while retaining URL
mediaType: "text/typescript",
content: typescriptContent,
},
{
path: "escaped%2E%54S?revision=2#original.js",
mediaType: "text/typescript",
content: typescriptContent,
},
{
path: "invalid%.ts?revision=2#view.tsx",
mediaType: "text/typescript",
@@ -42,6 +47,11 @@ it("selects original source language from decoded URL paths while retaining URL
mediaType: "text/typescript",
content: annotatedContent,
},
{
path: "module.MTS",
mediaType: "text/typescript",
content: annotatedContent,
},
{
path: "common.cts",
mediaType: "text/typescript",