Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/lib/mangle/staticLanguageServiceHost.ts
3520 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import ts from 'typescript';
7
import path from 'path';
8
9
export class StaticLanguageServiceHost implements ts.LanguageServiceHost {
10
11
private readonly _cmdLine: ts.ParsedCommandLine;
12
private readonly _scriptSnapshots: Map<string, ts.IScriptSnapshot> = new Map();
13
14
constructor(readonly projectPath: string) {
15
const existingOptions: Partial<ts.CompilerOptions> = {};
16
const parsed = ts.readConfigFile(projectPath, ts.sys.readFile);
17
if (parsed.error) {
18
throw parsed.error;
19
}
20
this._cmdLine = ts.parseJsonConfigFileContent(parsed.config, ts.sys, path.dirname(projectPath), existingOptions);
21
if (this._cmdLine.errors.length > 0) {
22
throw parsed.error;
23
}
24
}
25
getCompilationSettings(): ts.CompilerOptions {
26
return this._cmdLine.options;
27
}
28
getScriptFileNames(): string[] {
29
return this._cmdLine.fileNames;
30
}
31
getScriptVersion(_fileName: string): string {
32
return '1';
33
}
34
getProjectVersion(): string {
35
return '1';
36
}
37
getScriptSnapshot(fileName: string): ts.IScriptSnapshot | undefined {
38
let result: ts.IScriptSnapshot | undefined = this._scriptSnapshots.get(fileName);
39
if (result === undefined) {
40
const content = ts.sys.readFile(fileName);
41
if (content === undefined) {
42
return undefined;
43
}
44
result = ts.ScriptSnapshot.fromString(content);
45
this._scriptSnapshots.set(fileName, result);
46
}
47
return result;
48
}
49
getCurrentDirectory(): string {
50
return path.dirname(this.projectPath);
51
}
52
getDefaultLibFileName(options: ts.CompilerOptions): string {
53
return ts.getDefaultLibFilePath(options);
54
}
55
directoryExists = ts.sys.directoryExists;
56
getDirectories = ts.sys.getDirectories;
57
fileExists = ts.sys.fileExists;
58
readFile = ts.sys.readFile;
59
readDirectory = ts.sys.readDirectory;
60
// this is necessary to make source references work.
61
realpath = ts.sys.realpath;
62
}
63
64