/
createTester.ts
114 lines (108 loc) · 3.4 KB
/
createTester.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { createConfig, VueLanguageServiceHost } from '../..';
import * as ts from 'typescript/lib/tsserverlibrary';
import * as path from 'path';
import * as shared from '@volar/shared';
import { URI } from 'vscode-uri';
import { createLanguageService } from '@volar/language-service';
const testRoot = path.resolve(__dirname, '../../../vue-test-workspace');
export const rootUri = URI.file(testRoot);
export const tester = createTester(testRoot);
function createTester(root: string) {
const parseConfigHost: ts.ParseConfigHost = {
...ts.sys,
readDirectory: (path, extensions, exclude, include, depth) => {
return ts.sys.readDirectory(path, [...extensions, '.vue'], exclude, include, depth);
},
};
const realTsConfig = shared.normalizeFileName(path.join(root, 'tsconfig.json'));
const config = ts.readJsonConfigFile(realTsConfig, ts.sys.readFile);
const parsedCommandLine = ts.parseJsonSourceFileConfigFileContent(config, parseConfigHost, path.dirname(realTsConfig), {}, realTsConfig);
parsedCommandLine.fileNames = parsedCommandLine.fileNames.map(shared.normalizeFileName);
const scriptVersions = new Map<string, string>();
const scriptSnapshots = new Map<string, [string, ts.IScriptSnapshot]>();
const host: VueLanguageServiceHost = {
// ts
getNewLine: () => ts.sys.newLine,
useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
readFile: ts.sys.readFile,
writeFile: ts.sys.writeFile,
fileExists: ts.sys.fileExists,
directoryExists: ts.sys.directoryExists,
getDirectories: ts.sys.getDirectories,
readDirectory: ts.sys.readDirectory,
realpath: ts.sys.realpath,
// custom
getDefaultLibFileName: options => ts.getDefaultLibFilePath(options),
getProjectVersion: () => '0',
getScriptFileNames: () => parsedCommandLine.fileNames,
getCurrentDirectory: () => root,
getCompilationSettings: () => parsedCommandLine.options,
getScriptVersion,
getScriptSnapshot,
getVueCompilationSettings: () => ({}),
getTypeScriptModule: () => ts,
};
const vscodeSettings: any = {
typescript: {
preferences: {
quoteStyle: 'single',
},
},
javascript: {
preferences: {
quoteStyle: 'single',
},
},
};
const languageServiceConfig = createConfig({}, ts, {}, {});
const languageService = createLanguageService(host, languageServiceConfig, {
rootUri,
configurationHost: {
async getConfiguration(section: string) {
const keys = section.split('.');
let settings = vscodeSettings;
for (const key of keys) {
if (key in settings) {
settings = settings[key];
}
else {
settings = undefined;
break;
}
}
return settings;
},
onDidChangeConfiguration() { },
},
documentContext: {
resolveReference: (ref, _base) => {
return ref;
},
},
});
return {
host,
languageService,
};
function getScriptVersion(fileName: string) {
return scriptVersions.get(fileName) ?? '';
}
function getScriptSnapshot(fileName: string) {
const version = getScriptVersion(fileName);
const cache = scriptSnapshots.get(fileName);
if (cache && cache[0] === version) {
return cache[1];
}
const text = getScriptText(fileName);
if (text !== undefined) {
const snapshot = ts.ScriptSnapshot.fromString(text);
scriptSnapshots.set(fileName, [version.toString(), snapshot]);
return snapshot;
}
}
function getScriptText(fileName: string) {
if (ts.sys.fileExists(fileName)) {
return ts.sys.readFile(fileName, 'utf8');
}
}
}