-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-language-service-host.js
40 lines (36 loc) · 1.59 KB
/
custom-language-service-host.js
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
const { existsSync, readFileSync } = require('fs');
const { ScriptSnapshot, sys, getDefaultLibFileName } = require('typescript');
/**
* @param {import('typescript').Program} program
* @param {{[key:string]:string}} scriptFileMap
* @returns {import('typescript').LanguageServiceHost}
*/
function getLanguageServiceHostFromProgram(program, scriptFileMap) {
return {
getCompilationSettings: () => program.getCompilerOptions(),
getScriptFileNames: () => program.getSourceFiles().map((x) => x.fileName),
getScriptVersion: (fileName) => '1',
getScriptSnapshot: (fileName) => {
var overriddenFile = scriptFileMap[fileName] || scriptFileMap[fileName.replaceAll('/', '\\')];
if (overriddenFile) {
return ScriptSnapshot.fromString(overriddenFile);
}
if (!existsSync(fileName)) {
var sourceFile = program.getSourceFiles().find((x) => x.fileName.endsWith(fileName));
return ScriptSnapshot.fromString(sourceFile.text);
}
return ScriptSnapshot.fromString(readFileSync(fileName).toString());
},
getCurrentDirectory: sys.getCurrentDirectory,
getDefaultLibFileName: getDefaultLibFileName,
readFile: (fileName) => {
var overriddenFile = scriptFileMap[fileName];
if (overriddenFile) return overriddenFile;
else return readFileSync(fileName).toString();
},
fileExists: (fileName) => {
return existsSync(fileName);
},
};
}
module.exports = { getLanguageServiceHostFromProgram };