-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
getProgram.ts
185 lines (150 loc) Β· 5.88 KB
/
getProgram.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import type * as ts from 'typescript/lib/tsserverlibrary';
import type * as embedded from '@volar/language-core';
export function getProgram(
ts: typeof import('typescript/lib/tsserverlibrary'),
core: embedded.LanguageContext,
ls: ts.LanguageService,
) {
const proxy: Partial<ts.Program> = {
getRootFileNames,
emit,
getSyntacticDiagnostics,
getSemanticDiagnostics,
getGlobalDiagnostics,
// @ts-expect-error
getBindAndCheckDiagnostics,
};
return new Proxy({}, {
get: (target: any, property: keyof ts.Program) => {
if (property in proxy) {
return proxy[property];
}
const program = getProgram();
if (property in program) {
return program[property];
}
return target[property];
},
// #17
// notice: https://github.com/vuejs/language-tools/issues/2403
set: (target, property, newValue) => {
const program = getProgram() as any;
target[property] = program[property] = newValue;
return true;
},
});
function getProgram() {
return ls.getProgram()!;
}
function getRootFileNames() {
return getProgram().getRootFileNames().filter(fileName => core.typescript.languageServiceHost.fileExists?.(fileName));
}
// for vue-tsc --noEmit --watch
function getBindAndCheckDiagnostics(sourceFile?: ts.SourceFile, cancellationToken?: ts.CancellationToken) {
return getSourceFileDiagnosticsWorker(sourceFile, cancellationToken, 'getBindAndCheckDiagnostics' as 'getSemanticDiagnostics');
}
// for vue-tsc --noEmit
function getSyntacticDiagnostics(sourceFile?: ts.SourceFile, cancellationToken?: ts.CancellationToken) {
return getSourceFileDiagnosticsWorker(sourceFile, cancellationToken, 'getSyntacticDiagnostics');
}
function getSemanticDiagnostics(sourceFile?: ts.SourceFile, cancellationToken?: ts.CancellationToken) {
return getSourceFileDiagnosticsWorker(sourceFile, cancellationToken, 'getSemanticDiagnostics');
}
function getSourceFileDiagnosticsWorker<T extends 'getSyntacticDiagnostics' | 'getSemanticDiagnostics'>(
sourceFile: ts.SourceFile | undefined,
cancellationToken: ts.CancellationToken | undefined,
api: T,
): ReturnType<ts.Program[T]> {
if (sourceFile) {
const [virtualFile, source] = core.virtualFiles.getVirtualFile(sourceFile.fileName);
if (virtualFile && source) {
if (!virtualFile.capabilities.diagnostic)
return [] as any;
const errors = transformDiagnostics(ls.getProgram()?.[api](sourceFile, cancellationToken) ?? []);
return errors as any;
}
}
return transformDiagnostics(getProgram()[api](sourceFile, cancellationToken) ?? []) as any;
}
function getGlobalDiagnostics(cancellationToken?: ts.CancellationToken): readonly ts.Diagnostic[] {
return transformDiagnostics(getProgram().getGlobalDiagnostics(cancellationToken) ?? []);
}
function emit(targetSourceFile?: ts.SourceFile, _writeFile?: ts.WriteFileCallback, cancellationToken?: ts.CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: ts.CustomTransformers): ts.EmitResult {
const scriptResult = getProgram().emit(targetSourceFile, (core.typescript.languageServiceHost.writeFile ?? ts.sys.writeFile), cancellationToken, emitOnlyDtsFiles, customTransformers);
return {
emitSkipped: scriptResult.emitSkipped,
emittedFiles: scriptResult.emittedFiles,
diagnostics: transformDiagnostics(scriptResult.diagnostics),
};
}
// transform
function transformDiagnostics<T extends ts.Diagnostic | ts.DiagnosticWithLocation | ts.DiagnosticRelatedInformation>(diagnostics: readonly T[]): T[] {
const result: T[] = [];
for (const diagnostic of diagnostics) {
if (
diagnostic.file !== undefined
&& diagnostic.start !== undefined
&& diagnostic.length !== undefined
) {
const [virtualFile, source] = core.virtualFiles.getVirtualFile(diagnostic.file.fileName);
if (virtualFile && source) {
if (core.typescript.languageServiceHost.fileExists?.(source.fileName) === false)
continue;
for (const [sourceFileName, map] of core.virtualFiles.getMaps(virtualFile)) {
if (sourceFileName !== source.fileName)
continue;
for (const start of map.toSourceOffsets(diagnostic.start)) {
if (!start[1].data.diagnostic)
continue;
for (const end of map.toSourceOffsets(diagnostic.start + diagnostic.length, true)) {
if (!end[1].data.diagnostic)
continue;
onMapping(diagnostic, source.fileName, start[0], end[0], source.snapshot.getText(0, source.snapshot.getLength()));
break;
}
break;
}
}
}
else {
if (core.typescript.languageServiceHost.fileExists?.(diagnostic.file.fileName) === false)
continue;
onMapping(diagnostic, diagnostic.file.fileName, diagnostic.start, diagnostic.start + diagnostic.length, diagnostic.file.text);
}
}
else if (diagnostic.file === undefined) {
result.push(diagnostic);
}
}
return result;
function onMapping(diagnostic: T, fileName: string, start: number, end: number, docText: string | undefined) {
let file = fileName === diagnostic.file?.fileName
? diagnostic.file
: undefined;
if (!file) {
if (docText === undefined) {
const snapshot = core.typescript.languageServiceHost.getScriptSnapshot(fileName);
if (snapshot) {
docText = snapshot.getText(0, snapshot.getLength());
}
}
else {
file = ts.createSourceFile(fileName, docText, ts.ScriptTarget.Latest, undefined, ts.ScriptKind.Deferred);
(file as any).parseDiagnostics = []; // not important
(file as any).resolvedPath = fileName; // fix https://github.com/vuejs/language-tools/issues/2622 for TS 5.0
}
}
const newDiagnostic: T = {
...diagnostic,
file,
start: start,
length: end - start,
};
const relatedInformation = (diagnostic as ts.Diagnostic).relatedInformation;
if (relatedInformation) {
(newDiagnostic as ts.Diagnostic).relatedInformation = transformDiagnostics(relatedInformation);
}
result.push(newDiagnostic);
}
}
}