-
-
Notifications
You must be signed in to change notification settings - Fork 371
/
apis.ts
134 lines (114 loc) · 4.48 KB
/
apis.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
import type * as ts from 'typescript/lib/tsserverlibrary';
import type { TypeScriptRuntime } from '@volar/vue-typescript';
export function register(
ts: typeof import('typescript/lib/tsserverlibrary'),
context: TypeScriptRuntime,
) {
return {
getRootFileNames,
emit,
getSyntacticDiagnostics,
getSemanticDiagnostics,
getGlobalDiagnostics,
getBindAndCheckDiagnostics,
};
function getRootFileNames() {
return getProgram().getRootFileNames().filter(fileName => context.getTsLsHost().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(
sourceFile: ts.SourceFile | undefined,
cancellationToken: ts.CancellationToken | undefined,
api: 'getSyntacticDiagnostics' | 'getSemanticDiagnostics',
): readonly ts.DiagnosticWithLocation[] | readonly ts.Diagnostic[] {
if (sourceFile) {
const mapped = context.vueFiles.fromEmbeddedFileName(sourceFile.fileName);
if (mapped) {
if (!mapped.embedded.file.capabilities.diagnostics)
return [];
const program = getProgram();
const errors = transformDiagnostics(program?.[api](sourceFile, cancellationToken) ?? []);
return errors;
}
}
return transformDiagnostics(getProgram()[api](sourceFile, cancellationToken) ?? []);
}
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, (context.vueLsHost.writeFile ?? ts.sys.writeFile), cancellationToken, emitOnlyDtsFiles, customTransformers);
return {
emitSkipped: scriptResult.emitSkipped,
emittedFiles: scriptResult.emittedFiles,
diagnostics: transformDiagnostics(scriptResult.diagnostics),
};
}
function getProgram() {
return context.getTsLs().getProgram()!;
}
// 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
) {
for (const tsOrVueLoc of context.vueFiles.fromEmbeddedLocation(
diagnostic.file.fileName,
diagnostic.start,
diagnostic.start + diagnostic.length,
data => !!data.capabilities.diagnostic,
)) {
if (!context.vueLsHost.fileExists?.(tsOrVueLoc.fileName))
continue;
let file = tsOrVueLoc.fileName === diagnostic.file.fileName
? diagnostic.file
: undefined;
if (!file) {
let docText = tsOrVueLoc.mapped?.vueFile.getContent();
if (docText === undefined) {
const snapshot = context.vueLsHost.getScriptSnapshot(tsOrVueLoc.fileName);
if (snapshot) {
docText = snapshot.getText(0, snapshot.getLength());
}
}
else {
file = ts.createSourceFile(tsOrVueLoc.fileName, docText, tsOrVueLoc.fileName.endsWith('.vue') ? ts.ScriptTarget.JSON : ts.ScriptTarget.Latest);
}
}
const newDiagnostic: T = {
...diagnostic,
file,
start: tsOrVueLoc.range.start,
length: tsOrVueLoc.range.end - tsOrVueLoc.range.start,
};
const relatedInformation = (diagnostic as ts.Diagnostic).relatedInformation;
if (relatedInformation) {
(newDiagnostic as ts.Diagnostic).relatedInformation = transformDiagnostics(relatedInformation);
}
result.push(newDiagnostic);
break;
}
}
else if (
diagnostic.file === undefined
) {
result.push(diagnostic);
}
}
return result;
}
}