-
Notifications
You must be signed in to change notification settings - Fork 71
/
host.ts
152 lines (124 loc) · 3.71 KB
/
host.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
import { tsModule } from "./tsproxy";
import * as tsTypes from "typescript";
import * as _ from "lodash";
import { normalizePath as normalize } from "@rollup/pluginutils";
import { TransformerFactoryCreator } from "./ioptions";
export class LanguageServiceHost implements tsTypes.LanguageServiceHost
{
private cwd: string;
private snapshots: { [fileName: string]: tsTypes.IScriptSnapshot } = {};
private versions: { [fileName: string]: number } = {};
private service?: tsTypes.LanguageService;
private fileNames: Set<string>;
constructor(private parsedConfig: tsTypes.ParsedCommandLine, private transformers: TransformerFactoryCreator[], cwd: string)
{
this.fileNames = new Set(parsedConfig.fileNames);
this.cwd = cwd;
}
public reset()
{
this.snapshots = {};
this.versions = {};
}
public setLanguageService(service: tsTypes.LanguageService)
{
this.service = service;
}
public setSnapshot(fileName: string, data: string): tsTypes.IScriptSnapshot
{
fileName = normalize(fileName);
const snapshot = tsModule.ScriptSnapshot.fromString(data);
this.snapshots[fileName] = snapshot;
this.versions[fileName] = (this.versions[fileName] || 0) + 1;
this.fileNames.add(fileName);
return snapshot;
}
public getScriptSnapshot(fileName: string): tsTypes.IScriptSnapshot | undefined
{
fileName = normalize(fileName);
if (_.has(this.snapshots, fileName))
return this.snapshots[fileName];
const source = tsModule.sys.readFile(fileName);
if (source)
{
this.snapshots[fileName] = tsModule.ScriptSnapshot.fromString(source);
this.versions[fileName] = (this.versions[fileName] || 0) + 1;
return this.snapshots[fileName];
}
return undefined;
}
public getCurrentDirectory()
{
return this.cwd;
}
public getScriptVersion(fileName: string)
{
fileName = normalize(fileName);
return (this.versions[fileName] || 0).toString();
}
public getScriptFileNames()
{
return Array.from(this.fileNames.values());
}
public getCompilationSettings(): tsTypes.CompilerOptions
{
return this.parsedConfig.options;
}
public getDefaultLibFileName(opts: tsTypes.CompilerOptions)
{
return tsModule.getDefaultLibFilePath(opts);
}
public useCaseSensitiveFileNames(): boolean
{
return tsModule.sys.useCaseSensitiveFileNames;
}
public readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[]): string[]
{
return tsModule.sys.readDirectory(path, extensions, exclude, include);
}
public readFile(path: string, encoding?: string): string | undefined
{
return tsModule.sys.readFile(path, encoding);
}
public fileExists(path: string): boolean
{
return tsModule.sys.fileExists(path);
}
public getTypeRootsVersion(): number
{
return 0;
}
public directoryExists(directoryName: string): boolean
{
return tsModule.sys.directoryExists(directoryName);
}
public getDirectories(directoryName: string): string[]
{
return tsModule.sys.getDirectories(directoryName);
}
public getCustomTransformers(): tsTypes.CustomTransformers | undefined
{
if (this.service === undefined || this.transformers === undefined || this.transformers.length === 0)
return undefined;
const transformer: tsTypes.CustomTransformers =
{
before: [],
after: [],
afterDeclarations: [],
};
for (const creator of this.transformers)
{
const factory = creator(this.service);
if (factory.before)
transformer.before = _.concat(transformer.before!, factory.before);
if (factory.after)
transformer.after = _.concat(transformer.after!, factory.after);
if (factory.afterDeclarations)
transformer.afterDeclarations = _.concat(transformer.afterDeclarations!, factory.afterDeclarations);
}
return transformer;
}
public trace(line: string) {
console.log(line)
}
}