Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/lib/mangle/staticLanguageServiceHost.ts
5237 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
readonly projectPath: string;
14
15
constructor(projectPath: string) {
16
this.projectPath = projectPath;
17
const existingOptions: Partial<ts.CompilerOptions> = {};
18
const parsed = ts.readConfigFile(projectPath, ts.sys.readFile);
19
if (parsed.error) {
20
throw parsed.error;
21
}
22
this._cmdLine = ts.parseJsonConfigFileContent(parsed.config, ts.sys, path.dirname(projectPath), existingOptions);
23
if (this._cmdLine.errors.length > 0) {
24
throw parsed.error;
25
}
26
}
27
getCompilationSettings(): ts.CompilerOptions {
28
return this._cmdLine.options;
29
}
30
getScriptFileNames(): string[] {
31
return this._cmdLine.fileNames;
32
}
33
getScriptVersion(_fileName: string): string {
34
return '1';
35
}
36
getProjectVersion(): string {
37
return '1';
38
}
39
getScriptSnapshot(fileName: string): ts.IScriptSnapshot | undefined {
40
let result: ts.IScriptSnapshot | undefined = this._scriptSnapshots.get(fileName);
41
if (result === undefined) {
42
const content = ts.sys.readFile(fileName);
43
if (content === undefined) {
44
return undefined;
45
}
46
result = ts.ScriptSnapshot.fromString(content);
47
this._scriptSnapshots.set(fileName, result);
48
}
49
return result;
50
}
51
getCurrentDirectory(): string {
52
return path.dirname(this.projectPath);
53
}
54
getDefaultLibFileName(options: ts.CompilerOptions): string {
55
return ts.getDefaultLibFilePath(options);
56
}
57
directoryExists = ts.sys.directoryExists;
58
getDirectories = ts.sys.getDirectories;
59
fileExists = ts.sys.fileExists;
60
readFile = ts.sys.readFile;
61
readDirectory = ts.sys.readDirectory;
62
// this is necessary to make source references work.
63
realpath = ts.sys.realpath;
64
}
65
66