-
-
Notifications
You must be signed in to change notification settings - Fork 371
/
createTester.ts
85 lines (80 loc) · 3.08 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
import { TypeScriptProjectHost, createLanguageService, resolveCommonLanguageId } from '@volar/language-service';
import { createTypeScriptLanguage } from '@volar/typescript';
import * as path from 'path';
import * as ts from 'typescript';
import { URI } from 'vscode-uri';
import { createParsedCommandLine, createVueLanguagePlugin, createVueServicePlugins } from '../..';
import { createMockServiceEnv } from './mockEnv';
export const rootUri = URI.file(path.resolve(__dirname, '../../../../test-workspace/language-service')).toString();
export const tester = createTester(rootUri);
function createTester(rootUri: string) {
const serviceEnv = createMockServiceEnv(rootUri, () => currentVSCodeSettings ?? defaultVSCodeSettings);
const rootPath = serviceEnv.typescript!.uriToFileName(rootUri.toString());
const realTsConfig = path.join(rootPath, 'tsconfig.json').replace(/\\/g, '/');
const parsedCommandLine = createParsedCommandLine(ts, ts.sys, realTsConfig);
parsedCommandLine.fileNames = parsedCommandLine.fileNames.map(fileName => fileName.replace(/\\/g, '/'));
const scriptSnapshots = new Map<string, ts.IScriptSnapshot>();
const projectHost: TypeScriptProjectHost = {
...ts.sys,
configFileName: realTsConfig,
getCurrentDirectory: () => rootPath,
getProjectVersion: () => '0',
getScriptFileNames: () => parsedCommandLine.fileNames,
getCompilationSettings: () => parsedCommandLine.options,
getScriptSnapshot,
getLanguageId: resolveCommonLanguageId,
scriptIdToFileName: serviceEnv.typescript!.uriToFileName,
fileNameToScriptId: serviceEnv.typescript!.fileNameToUri,
};
const vueLanguagePlugin = createVueLanguagePlugin(
ts,
serviceEnv.typescript!.uriToFileName,
ts.sys.useCaseSensitiveFileNames,
() => projectHost.getProjectVersion?.() ?? '',
() => projectHost.getScriptFileNames(),
parsedCommandLine.options,
parsedCommandLine.vueOptions,
);
const vueServicePlugins = createVueServicePlugins(ts, () => parsedCommandLine.vueOptions);
const defaultVSCodeSettings: any = {
'typescript.preferences.quoteStyle': 'single',
'javascript.preferences.quoteStyle': 'single',
'vue.inlayHints.missingProps': true,
'vue.inlayHints.optionsWrapper': true,
'vue.inlayHints.inlineHandlerLeading': true,
'vue.inlayHints.vBindShorthand': true,
};
let currentVSCodeSettings: any;
const language = createTypeScriptLanguage(
ts,
[vueLanguagePlugin],
projectHost,
);
const languageService = createLanguageService(language, vueServicePlugins, serviceEnv);
return {
serviceEnv,
projectHost,
languageService,
setVSCodeSettings,
};
function setVSCodeSettings(settings: any = undefined) {
currentVSCodeSettings = settings;
}
function getScriptSnapshot(fileName: string) {
const snapshot = scriptSnapshots.get(fileName);
if (snapshot) {
return snapshot;
}
const text = getScriptText(fileName);
if (text !== undefined) {
const snapshot = ts.ScriptSnapshot.fromString(text);
scriptSnapshots.set(fileName, snapshot);
return snapshot;
}
}
function getScriptText(fileName: string) {
if (ts.sys.fileExists(fileName)) {
return ts.sys.readFile(fileName, 'utf8');
}
}
}