From 32312020f9dd04215efd287c0c17a4049e8f9aac Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Sat, 1 Jun 2024 04:32:37 +0800 Subject: [PATCH] feat(language-core): add option to resolve hidden extensions (#190) --- packages/language-core/lib/types.ts | 4 ++++ packages/typescript/lib/resolveModuleName.ts | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/packages/language-core/lib/types.ts b/packages/language-core/lib/types.ts index 6f9d85c5..9d1cfa4b 100644 --- a/packages/language-core/lib/types.ts +++ b/packages/language-core/lib/types.ts @@ -97,6 +97,10 @@ export interface LanguagePlugin { * LSP + TS Plugin */ extraFileExtensions: ts.FileExtensionInfo[]; + /** + * LSP + TS Plugin + */ + resolveHiddenExtensions?: boolean; /** * LSP + TS Plugin */ diff --git a/packages/typescript/lib/resolveModuleName.ts b/packages/typescript/lib/resolveModuleName.ts index dc51797d..a6c429ea 100644 --- a/packages/typescript/lib/resolveModuleName.ts +++ b/packages/typescript/lib/resolveModuleName.ts @@ -52,6 +52,24 @@ export function createResolveModuleName( } } } + if (typescript.resolveHiddenExtensions && fileName.endsWith(`.d.ts`)) { + for (const { extension } of typescript.extraFileExtensions) { + const sourceFileName = fileName.slice(0, -`.d.ts`.length) + `.${extension}`; + if (fileExists(sourceFileName)) { + const sourceScript = getSourceScript(sourceFileName); + if (sourceScript?.generated) { + const serviceScript = sourceScript.generated.languagePlugin.typescript?.getServiceScript(sourceScript.generated.root); + if (serviceScript) { + toSourceFileInfo.set(fileName, { + sourceFileName, + extension: serviceScript.extension, + }); + return true; + } + } + } + } + } } return host.fileExists(fileName); },