Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(language-core): add option to resolve hidden extensions #190

Merged
merged 1 commit into from
May 31, 2024
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
4 changes: 4 additions & 0 deletions packages/language-core/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export interface LanguagePlugin<T, K extends VirtualCode = VirtualCode> {
* LSP + TS Plugin
*/
extraFileExtensions: ts.FileExtensionInfo[];
/**
* LSP + TS Plugin
*/
resolveHiddenExtensions?: boolean;
/**
* LSP + TS Plugin
*/
Expand Down
18 changes: 18 additions & 0 deletions packages/typescript/lib/resolveModuleName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ export function createResolveModuleName<T>(
}
}
}
if (typescript.resolveHiddenExtensions && fileName.endsWith(`.d.ts`)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for understanding/posterity: I was confused about why we'd be checking .d.ts extension here, given that for an Ember .gts file, there wouldn't be a .d.ts file on disk. But the reason this works is that TS checks for xxx.js, xxx.ts, and then xxx.d.ts. We hook into the check for .d.ts and take that opportunity to check for extra file extensions (such as .gts)

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);
},
Expand Down
Loading