From 6fdfdc4af124aee698cf18fed98b11ab08db591b Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 06:39:52 -0500 Subject: [PATCH] fix(web): share TypeScript source-map filename grammar (#12286) --- src/browser/WebSourceMapFetcher.ts | 14 +++++------ .../javascript/javascriptSourceParser.ts | 24 ++++++++++++------- .../webSourceMapSourceLanguage.test.ts | 10 ++++++++ 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/browser/WebSourceMapFetcher.ts b/src/browser/WebSourceMapFetcher.ts index 4cb348a28a..71fa9c5df5 100644 --- a/src/browser/WebSourceMapFetcher.ts +++ b/src/browser/WebSourceMapFetcher.ts @@ -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, diff --git a/src/domain/javascript/javascriptSourceParser.ts b/src/domain/javascript/javascriptSourceParser.ts index c09c488304..f4b7fb0a1b 100644 --- a/src/domain/javascript/javascriptSourceParser.ts +++ b/src/domain/javascript/javascriptSourceParser.ts @@ -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 = + /\.(?:(?d)\.)?(?:(?[cm])ts|ts(?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]; diff --git a/tests/boundary/browser/webSourceMapSourceLanguage.test.ts b/tests/boundary/browser/webSourceMapSourceLanguage.test.ts index dc54e436dc..7ac881ef06 100644 --- a/tests/boundary/browser/webSourceMapSourceLanguage.test.ts +++ b/tests/boundary/browser/webSourceMapSourceLanguage.test.ts @@ -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",