From 485a15fc6f8797ab63ae6793437d831a2848e427 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Sat, 16 Dec 2023 20:10:33 +0100 Subject: [PATCH 1/4] fix: support `.d.mts` and `.d.cts` --- packages/ata/src/index.ts | 10 +++++++--- packages/sandbox/src/index.ts | 5 ++++- packages/ts-twoslasher/src/index.ts | 2 +- packages/typescript-vfs/src/index.ts | 4 +++- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/ata/src/index.ts b/packages/ata/src/index.ts index 0f0070501be6..13ea9f9523ef 100644 --- a/packages/ata/src/index.ts +++ b/packages/ata/src/index.ts @@ -71,7 +71,7 @@ export const setupTypeAcquisition = (config: ATABootstrapConfig) => { const treesOnly = trees.filter(t => !("error" in t)) as NPMTreeMeta[] // These are the modules which we can grab directly - const hasDTS = treesOnly.filter(t => t.files.find(f => f.name.endsWith(".d.ts"))) + const hasDTS = treesOnly.filter(t => t.files.find(f => isDtsFile(f.name))) const dtsFilesFromNPM = hasDTS.map(t => treeToDTSFiles(t, `/node_modules/${t.moduleName}`)) // These are ones we need to look on DT for (which may not be there, who knows) @@ -142,7 +142,7 @@ function treeToDTSFiles(tree: NPMTreeMeta, vfsPrefix: string) { const dtsRefs: ATADownload[] = [] for (const file of tree.files) { - if (file.name.endsWith(".d.ts")) { + if (isDtsFile(file.name)) { dtsRefs.push({ moduleName: tree.moduleName, moduleVersion: tree.version, @@ -170,7 +170,7 @@ export const getReferencesForModule = (ts: typeof import("typescript"), code: st const references = meta.referencedFiles .concat(meta.importedFiles) .concat(meta.libReferenceDirectives) - .filter(f => !f.fileName.endsWith(".d.ts")) + .filter(f => !isDtsFile(f.fileName)) .filter(d => !libMap.has(d.fileName)) return references.map(r => { @@ -268,3 +268,7 @@ function getDTName(s: string) { } return s } + +function isDtsFile(file: string) { + return /\.d\.[cm]?ts$/.test(file) +} diff --git a/packages/sandbox/src/index.ts b/packages/sandbox/src/index.ts index 5933b24c6958..7a222d5841bc 100644 --- a/packages/sandbox/src/index.ts +++ b/packages/sandbox/src/index.ts @@ -314,12 +314,15 @@ export const createTypeScriptSandbox = ( return (firstJS && firstJS.text) || "" } + const isDtsFile = (name: string) => /\.d\.[cm]?ts$/.test(name) + /** Gets the DTS for the JS/TS of compiling your editor's code */ const getDTSForCode = async () => { const result = await getEmitResult() - return result.outputFiles.find((o: any) => o.name.endsWith(".d.ts"))!.text + return result.outputFiles.find((o: any) => isDtsFile(o.name))!.text } + const getWorkerProcess = async (): Promise => { const worker = await getWorker() // @ts-ignore diff --git a/packages/ts-twoslasher/src/index.ts b/packages/ts-twoslasher/src/index.ts index c377237de5ea..d309086c4e9a 100755 --- a/packages/ts-twoslasher/src/index.ts +++ b/packages/ts-twoslasher/src/index.ts @@ -742,7 +742,7 @@ export function twoslasher(code: string, extension: string, options: TwoSlashOpt // Get the file which created the file we want to show: const emitFilename = handbookOptions.showEmittedFile || defaultFileName const emitSourceFilename = - fsRoot + emitFilename.replace(".jsx", "").replace(".js", "").replace(".d.ts", "").replace(".map", "") + fsRoot + emitFilename.replace(".jsx", "").replace(".js", "").replace(/\.d\.[cm]?ts$/, "").replace(".map", "") let emitSource = filenames.find(f => f === emitSourceFilename + ".ts" || f === emitSourceFilename + ".tsx") diff --git a/packages/typescript-vfs/src/index.ts b/packages/typescript-vfs/src/index.ts index e89ba2a57857..4b532e9e1147 100755 --- a/packages/typescript-vfs/src/index.ts +++ b/packages/typescript-vfs/src/index.ts @@ -241,8 +241,10 @@ export const createDefaultMapFromNodeModules = ( return fs.readFileSync(path.join(lib, name), "utf8") } + const isDtsFile = (file: string) => /\.d\.[cm]?ts$/.test(file) + const libFiles = fs.readdirSync(tsLibDirectory || path.dirname(require.resolve("typescript"))) - const knownLibFiles = libFiles.filter(f => f.startsWith("lib.") && f.endsWith(".d.ts")) + const knownLibFiles = libFiles.filter(f => f.startsWith("lib.") && isDtsFile(f)) const fsMap = new Map() knownLibFiles.forEach(lib => { From 6e2318440b7327619d7ea998db7041ab314764dd Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Sun, 17 Dec 2023 01:32:27 +0100 Subject: [PATCH 2/4] feat: support `.d.*.ts` --- packages/ata/src/index.ts | 2 +- packages/sandbox/src/index.ts | 2 +- packages/ts-twoslasher/src/index.ts | 2 +- packages/typescript-vfs/src/index.ts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/ata/src/index.ts b/packages/ata/src/index.ts index 13ea9f9523ef..788dcf92bef5 100644 --- a/packages/ata/src/index.ts +++ b/packages/ata/src/index.ts @@ -270,5 +270,5 @@ function getDTName(s: string) { } function isDtsFile(file: string) { - return /\.d\.[cm]?ts$/.test(file) + return /\.d\.(.+\.)?[cm]?ts$/i.test(file) } diff --git a/packages/sandbox/src/index.ts b/packages/sandbox/src/index.ts index 7a222d5841bc..6f5e5b6bc635 100644 --- a/packages/sandbox/src/index.ts +++ b/packages/sandbox/src/index.ts @@ -314,7 +314,7 @@ export const createTypeScriptSandbox = ( return (firstJS && firstJS.text) || "" } - const isDtsFile = (name: string) => /\.d\.[cm]?ts$/.test(name) + const isDtsFile = (name: string) => /\.d\.(.+\.)?[cm]?ts$/i.test(name) /** Gets the DTS for the JS/TS of compiling your editor's code */ const getDTSForCode = async () => { diff --git a/packages/ts-twoslasher/src/index.ts b/packages/ts-twoslasher/src/index.ts index d309086c4e9a..1cbfe5adaaeb 100755 --- a/packages/ts-twoslasher/src/index.ts +++ b/packages/ts-twoslasher/src/index.ts @@ -742,7 +742,7 @@ export function twoslasher(code: string, extension: string, options: TwoSlashOpt // Get the file which created the file we want to show: const emitFilename = handbookOptions.showEmittedFile || defaultFileName const emitSourceFilename = - fsRoot + emitFilename.replace(".jsx", "").replace(".js", "").replace(/\.d\.[cm]?ts$/, "").replace(".map", "") + fsRoot + emitFilename.replace(".jsx", "").replace(".js", "").replace(/\.d\.(.+\.)?[cm]?ts$/i, "").replace(".map", "") let emitSource = filenames.find(f => f === emitSourceFilename + ".ts" || f === emitSourceFilename + ".tsx") diff --git a/packages/typescript-vfs/src/index.ts b/packages/typescript-vfs/src/index.ts index 4b532e9e1147..281de0602481 100755 --- a/packages/typescript-vfs/src/index.ts +++ b/packages/typescript-vfs/src/index.ts @@ -241,7 +241,7 @@ export const createDefaultMapFromNodeModules = ( return fs.readFileSync(path.join(lib, name), "utf8") } - const isDtsFile = (file: string) => /\.d\.[cm]?ts$/.test(file) + const isDtsFile = (file: string) => /\.d\.(.+\.)?[cm]?ts$/i.test(file) const libFiles = fs.readdirSync(tsLibDirectory || path.dirname(require.resolve("typescript"))) const knownLibFiles = libFiles.filter(f => f.startsWith("lib.") && isDtsFile(f)) From dc96ec3bbc085da200e25994d8362593fd6f3d6f Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 8 Jan 2024 11:03:26 +0100 Subject: [PATCH 3/4] chore: update regex --- packages/ata/src/index.ts | 2 +- packages/sandbox/src/index.ts | 2 +- packages/ts-twoslasher/src/index.ts | 2 +- packages/typescript-vfs/src/index.ts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/ata/src/index.ts b/packages/ata/src/index.ts index 788dcf92bef5..f52d3dcb04f8 100644 --- a/packages/ata/src/index.ts +++ b/packages/ata/src/index.ts @@ -270,5 +270,5 @@ function getDTName(s: string) { } function isDtsFile(file: string) { - return /\.d\.(.+\.)?[cm]?ts$/i.test(file) + return /\.d\.([^\.]+\.)?[cm]?ts$/i.test(file) } diff --git a/packages/sandbox/src/index.ts b/packages/sandbox/src/index.ts index 6f5e5b6bc635..bf46c2b1e9b0 100644 --- a/packages/sandbox/src/index.ts +++ b/packages/sandbox/src/index.ts @@ -314,7 +314,7 @@ export const createTypeScriptSandbox = ( return (firstJS && firstJS.text) || "" } - const isDtsFile = (name: string) => /\.d\.(.+\.)?[cm]?ts$/i.test(name) + const isDtsFile = (name: string) => /\.d\.([^\.]+\.)?[cm]?ts$/i.test(name) /** Gets the DTS for the JS/TS of compiling your editor's code */ const getDTSForCode = async () => { diff --git a/packages/ts-twoslasher/src/index.ts b/packages/ts-twoslasher/src/index.ts index 1cbfe5adaaeb..0864bf3de185 100755 --- a/packages/ts-twoslasher/src/index.ts +++ b/packages/ts-twoslasher/src/index.ts @@ -742,7 +742,7 @@ export function twoslasher(code: string, extension: string, options: TwoSlashOpt // Get the file which created the file we want to show: const emitFilename = handbookOptions.showEmittedFile || defaultFileName const emitSourceFilename = - fsRoot + emitFilename.replace(".jsx", "").replace(".js", "").replace(/\.d\.(.+\.)?[cm]?ts$/i, "").replace(".map", "") + fsRoot + emitFilename.replace(".jsx", "").replace(".js", "").replace(/\.d\.([^\.]+\.)?[cm]?ts$/i, "").replace(".map", "") let emitSource = filenames.find(f => f === emitSourceFilename + ".ts" || f === emitSourceFilename + ".tsx") diff --git a/packages/typescript-vfs/src/index.ts b/packages/typescript-vfs/src/index.ts index 281de0602481..dcaa7a2fee9b 100755 --- a/packages/typescript-vfs/src/index.ts +++ b/packages/typescript-vfs/src/index.ts @@ -241,7 +241,7 @@ export const createDefaultMapFromNodeModules = ( return fs.readFileSync(path.join(lib, name), "utf8") } - const isDtsFile = (file: string) => /\.d\.(.+\.)?[cm]?ts$/i.test(file) + const isDtsFile = (file: string) => /\.d\.([^\.]+\.)?[cm]?ts$/i.test(file) const libFiles = fs.readdirSync(tsLibDirectory || path.dirname(require.resolve("typescript"))) const knownLibFiles = libFiles.filter(f => f.startsWith("lib.") && isDtsFile(f)) From 81057a3f48e58261b2c259bd081b81aba62d5cec Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 3 Jun 2024 19:34:24 -0700 Subject: [PATCH 4/4] Add changeset --- .changeset/twenty-ravens-attend.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .changeset/twenty-ravens-attend.md diff --git a/.changeset/twenty-ravens-attend.md b/.changeset/twenty-ravens-attend.md new file mode 100644 index 000000000000..e3e46aa2f226 --- /dev/null +++ b/.changeset/twenty-ravens-attend.md @@ -0,0 +1,8 @@ +--- +"@typescript/vfs": patch +"@typescript/twoslash": patch +"@typescript/sandbox": patch +"@typescript/ata": patch +--- + +Handle `.d.cts` and `.d.mts` files