This repository has been archived by the owner on May 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.ts
52 lines (44 loc) · 1.71 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import "typescript/lib/tsserverlibrary";
function init() {
function create(info: ts.server.PluginCreateInfo): ts.LanguageService {
info.project.projectService.logger.info("deno_ls_plugin: setup");
// TypeScript only officially supports proxying the LanguageService, but
// for Deno, we just want to adjust module names that end with `.ts` so
// that they don't end with `.ts` in the editor, so that TypeScript then
// actually resolves the right host name. The plugin creator though gets
// passed a reference to the LanguageServiceHost which is what we want to
// modify, so we will cache the original function and proxy it ourselves.
const origResolveModuleNames = info.languageServiceHost.resolveModuleNames;
info.languageServiceHost.resolveModuleNames = (
moduleNames,
containingFile,
reusedNames,
redirectedReference
) => {
moduleNames = moduleNames.map(moduleName => {
// For any modules ending with `.ts` we will strip that off
if (moduleName.endsWith(".ts")) {
const newName = moduleName.slice(0, -3);
info.project.projectService.logger.info(
`deno_ls_plugin: transform "${moduleName}" to "${newName}"`
);
return newName;
}
return moduleName;
});
// We use the remapped module names to call the original method
return origResolveModuleNames.call(
info.languageServiceHost,
moduleNames,
containingFile,
reusedNames,
redirectedReference
);
};
// We aren't actually proxying the language service, so we just return the
// original
return info.languageService;
}
return { create };
}
export = init;