Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/twenty-ravens-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@typescript/vfs": patch
"@typescript/twoslash": patch
"@typescript/sandbox": patch
"@typescript/ata": patch
---

Handle `.d.cts` and `.d.mts` files
10 changes: 7 additions & 3 deletions packages/ata/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -268,3 +268,7 @@ function getDTName(s: string) {
}
return s
}

function isDtsFile(file: string) {
return /\.d\.([^\.]+\.)?[cm]?ts$/i.test(file)
}
5 changes: 4 additions & 1 deletion packages/sandbox/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TypeScriptWorker> => {
const worker = await getWorker()
// @ts-ignore
Expand Down
2 changes: 1 addition & 1 deletion packages/ts-twoslasher/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
4 changes: 3 additions & 1 deletion packages/typescript-vfs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>()
knownLibFiles.forEach(lib => {
Expand Down