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 diff --git a/packages/ata/src/index.ts b/packages/ata/src/index.ts index 0f0070501be6..f52d3dcb04f8 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$/i.test(file) +} diff --git a/packages/sandbox/src/index.ts b/packages/sandbox/src/index.ts index fbb647ba8429..b60c9ad5114f 100644 --- a/packages/sandbox/src/index.ts +++ b/packages/sandbox/src/index.ts @@ -318,12 +318,15 @@ export const createTypeScriptSandbox = ( return (firstJS && firstJS.text) || "" } + 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 () => { const result = await getEmitResult(/*emitOnlyDtsFiles*/ undefined, /*forceDtsEmit*/ true) - 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..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.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 10ec61f02026..9827a3b528d2 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$/i.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 => {