-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathtscache.ts
315 lines (261 loc) · 8.68 KB
/
tscache.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { IContext } from "./context";
import { Graph, alg } from "graphlib";
import { sha1 } from "object-hash";
import { RollingCache } from "./rollingcache";
import { ICache } from "./icache";
import * as _ from "lodash";
import { tsModule } from "./tsproxy";
import * as tsTypes from "typescript";
import { blue, yellow, green } from "colors/safe";
import { emptyDirSync, pathExistsSync } from "fs-extra";
import { formatHost } from "./diagnostics-format-host";
import { NoCache } from "./nocache";
export interface ICode
{
code?: string;
map?: string;
dts?: tsTypes.OutputFile;
dtsmap?: tsTypes.OutputFile;
}
export interface IRollupCode
{
code: string | undefined;
map: { mappings: string };
}
interface INodeLabel
{
dirty: boolean;
}
export interface IDiagnostics
{
flatMessage: string;
formatted: string;
fileLine?: string;
category: tsTypes.DiagnosticCategory;
code: number;
type: string;
}
interface ITypeSnapshot
{
id: string;
snapshot: tsTypes.IScriptSnapshot | undefined;
}
export function convertEmitOutput(output: tsTypes.EmitOutput): ICode
{
const out: ICode = { };
output.outputFiles.forEach((e) =>
{
if (_.endsWith(e.name, ".d.ts"))
out.dts = e;
else if (_.endsWith(e.name, ".d.ts.map"))
out.dtsmap = e;
else if (_.endsWith(e.name, ".map"))
out.map = e.text;
else
out.code = e.text;
});
return out;
}
export function convertDiagnostic(type: string, data: tsTypes.Diagnostic[]): IDiagnostics[]
{
return _.map(data, (diagnostic) =>
{
const entry: IDiagnostics =
{
flatMessage: tsModule.flattenDiagnosticMessageText(diagnostic.messageText, "\n"),
formatted: tsModule.formatDiagnosticsWithColorAndContext(data, formatHost),
category: diagnostic.category,
code: diagnostic.code,
type,
};
if (diagnostic.file && diagnostic.start !== undefined)
{
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
entry.fileLine = `${diagnostic.file.fileName}(${line + 1},${character + 1})`;
}
return entry;
});
}
export class TsCache
{
private cacheVersion = "7";
private dependencyTree: Graph;
private ambientTypes: ITypeSnapshot[];
private ambientTypesDirty = false;
private cacheDir: string;
private codeCache!: ICache<ICode | undefined>;
private typesCache!: ICache<string>;
private semanticDiagnosticsCache!: ICache<IDiagnostics[]>;
private syntacticDiagnosticsCache!: ICache<IDiagnostics[]>;
constructor(private noCache: boolean, private host: tsTypes.LanguageServiceHost, cache: string, private options: tsTypes.CompilerOptions, private rollupConfig: any, rootFilenames: string[], private context: IContext)
{
this.cacheDir = `${cache}/${sha1({
version: this.cacheVersion,
rootFilenames,
options: this.options,
rollupConfig: this.rollupConfig,
tsVersion: tsModule.version,
})}`;
this.dependencyTree = new Graph({ directed: true });
this.dependencyTree.setDefaultNodeLabel((_node: string) => ({ dirty: false }));
const automaticTypes = _.map(tsModule.getAutomaticTypeDirectiveNames(options, tsModule.sys), (entry) => tsModule.resolveTypeReferenceDirective(entry, undefined, options, tsModule.sys))
.filter((entry) => entry.resolvedTypeReferenceDirective && entry.resolvedTypeReferenceDirective.resolvedFileName)
.map((entry) => entry.resolvedTypeReferenceDirective!.resolvedFileName!);
this.ambientTypes = _.filter(rootFilenames, (file) => _.endsWith(file, ".d.ts"))
.concat(automaticTypes)
.map((id) => ({ id, snapshot: this.host.getScriptSnapshot(id) }));
this.init();
this.checkAmbientTypes();
}
public clean()
{
if (pathExistsSync(this.cacheDir))
{
this.context.info(blue(`cleaning cache: ${this.cacheDir}`));
emptyDirSync(this.cacheDir);
}
this.init();
}
public setDependency(importee: string, importer: string): void
{
// importee -> importer
this.context.debug(`${blue("dependency")} '${importee}'`);
this.context.debug(` imported by '${importer}'`);
this.dependencyTree.setEdge(importer, importee);
}
public walkTree(cb: (id: string) => void | false): void
{
const acyclic = alg.isAcyclic(this.dependencyTree);
if (acyclic)
{
_.each(alg.topsort(this.dependencyTree), (id: string) => cb(id));
return;
}
this.context.info(yellow("import tree has cycles"));
_.each(this.dependencyTree.nodes(), (id: string) => cb(id));
}
public done()
{
this.context.info(blue("rolling caches"));
this.codeCache.roll();
this.semanticDiagnosticsCache.roll();
this.syntacticDiagnosticsCache.roll();
this.typesCache.roll();
}
public getCompiled(id: string, snapshot: tsTypes.IScriptSnapshot, transform: () => ICode | undefined): ICode | undefined
{
const name = this.makeName(id, snapshot);
this.context.info(`${blue("transpiling")} '${id}'`);
this.context.debug(` cache: '${this.codeCache.path(name)}'`);
if (this.codeCache.exists(name) && !this.isDirty(id, false))
{
this.context.debug(green(" cache hit"));
const data = this.codeCache.read(name);
if (data)
{
this.codeCache.write(name, data);
return data;
}
else
this.context.warn(yellow(" cache broken, discarding"));
}
this.context.debug(yellow(" cache miss"));
const transformedData = transform();
this.codeCache.write(name, transformedData);
this.markAsDirty(id);
return transformedData;
}
public getSyntacticDiagnostics(id: string, snapshot: tsTypes.IScriptSnapshot, check: () => tsTypes.Diagnostic[]): IDiagnostics[]
{
return this.getDiagnostics("syntax", this.syntacticDiagnosticsCache, id, snapshot, check);
}
public getSemanticDiagnostics(id: string, snapshot: tsTypes.IScriptSnapshot, check: () => tsTypes.Diagnostic[]): IDiagnostics[]
{
return this.getDiagnostics("semantic", this.semanticDiagnosticsCache, id, snapshot, check);
}
private checkAmbientTypes(): void
{
this.context.debug(blue("Ambient types:"));
const typeNames = _.filter(this.ambientTypes, (snapshot) => snapshot.snapshot !== undefined)
.map((snapshot) =>
{
this.context.debug(` ${snapshot.id}`);
return this.makeName(snapshot.id, snapshot.snapshot!);
});
// types dirty if any d.ts changed, added or removed
this.ambientTypesDirty = !this.typesCache.match(typeNames);
if (this.ambientTypesDirty)
this.context.info(yellow("ambient types changed, redoing all semantic diagnostics"));
_.each(typeNames, (name) => this.typesCache.touch(name));
}
private getDiagnostics(type: string, cache: ICache<IDiagnostics[]>, id: string, snapshot: tsTypes.IScriptSnapshot, check: () => tsTypes.Diagnostic[]): IDiagnostics[]
{
const name = this.makeName(id, snapshot);
this.context.debug(` cache: '${cache.path(name)}'`);
if (cache.exists(name) && !this.isDirty(id, true))
{
this.context.debug(green(" cache hit"));
const data = cache.read(name);
if (data)
{
cache.write(name, data);
return data;
}
else
this.context.warn(yellow(" cache broken, discarding"));
}
this.context.debug(yellow(" cache miss"));
const convertedData = convertDiagnostic(type, check());
cache.write(name, convertedData);
this.markAsDirty(id);
return convertedData;
}
private init()
{
if (this.noCache)
{
this.codeCache = new NoCache<ICode>();
this.typesCache = new NoCache<string>();
this.syntacticDiagnosticsCache = new NoCache<IDiagnostics[]>();
this.semanticDiagnosticsCache = new NoCache<IDiagnostics[]>();
}
else
{
this.codeCache = new RollingCache<ICode>(`${this.cacheDir}/code`, true);
this.typesCache = new RollingCache<string>(`${this.cacheDir}/types`, true);
this.syntacticDiagnosticsCache = new RollingCache<IDiagnostics[]>(`${this.cacheDir}/syntacticDiagnostics`, true);
this.semanticDiagnosticsCache = new RollingCache<IDiagnostics[]>(`${this.cacheDir}/semanticDiagnostics`, true);
}
}
private markAsDirty(id: string): void
{
this.dependencyTree.setNode(id, { dirty: true });
}
// returns true if node or any of its imports or any of global types changed
private isDirty(id: string, checkImports: boolean): boolean
{
const label = this.dependencyTree.node(id) as INodeLabel;
if (!label)
return false;
if (!checkImports || label.dirty)
return label.dirty;
if (this.ambientTypesDirty)
return true;
const dependencies = alg.dijkstra(this.dependencyTree, id);
return _.some(dependencies, (dependency, node) =>
{
if (!node || dependency.distance === Infinity)
return false;
const l = this.dependencyTree.node(node) as INodeLabel | undefined;
const dirty = l === undefined ? true : l.dirty;
if (dirty)
this.context.debug(` import changed: ${node}`);
return dirty;
});
}
private makeName(id: string, snapshot: tsTypes.IScriptSnapshot)
{
const data = snapshot.getText(0, snapshot.getLength());
return sha1({ data, id });
}
}