Repository navigation
Expand file tree
/
Copy pathprogram.ts
More file actions
391 lines (348 loc) · 15.4 KB
/
Copy pathprogram.ts
File metadata and controls
391 lines (348 loc) · 15.4 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AotCompiler, GeneratedFile, NgAnalyzedModules, createAotCompiler, getParseErrors, isSyntaxError, toTypeScript} from '@angular/compiler';
import {MetadataCollector, ModuleMetadata} from '@angular/tsc-wrapped';
import {writeFileSync} from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
import {CompilerHost as AotCompilerHost, CompilerHostContext} from '../compiler_host';
import {TypeChecker} from '../diagnostics/check_types';
import {CompilerHost, CompilerOptions, DiagnosticCategory} from './api';
import {Diagnostic, EmitFlags, Program} from './api';
import {getAngularEmitterTransformFactory} from './node_emitter_transform';
const GENERATED_FILES = /\.ngfactory\.js$|\.ngstyle\.js$|\.ngsummary\.js$/;
const SUMMARY_JSON_FILES = /\.ngsummary.json$/;
const emptyModules: NgAnalyzedModules = {
ngModules: [],
ngModuleByPipeOrDirective: new Map(),
files: []
};
class AngularCompilerProgram implements Program {
// Initialized in the constructor
private oldTsProgram: ts.Program|undefined;
private tsProgram: ts.Program;
private aotCompilerHost: AotCompilerHost;
private compiler: AotCompiler;
private srcNames: string[];
private collector: MetadataCollector;
// Lazily initialized fields
private _analyzedModules: NgAnalyzedModules|undefined;
private _structuralDiagnostics: Diagnostic[] = [];
private _stubs: GeneratedFile[]|undefined;
private _stubFiles: string[]|undefined;
private _programWithStubsHost: ts.CompilerHost|undefined;
private _programWithStubs: ts.Program|undefined;
private _generatedFiles: GeneratedFile[]|undefined;
private _generatedFileDiagnostics: Diagnostic[]|undefined;
private _typeChecker: TypeChecker|undefined;
private _semanticDiagnostics: Diagnostic[]|undefined;
constructor(
private rootNames: string[], private options: CompilerOptions, private host: CompilerHost,
private oldProgram?: Program) {
this.oldTsProgram = oldProgram ? oldProgram.getTsProgram() : undefined;
this.tsProgram = ts.createProgram(rootNames, options, host, this.oldTsProgram);
this.srcNames = this.tsProgram.getSourceFiles().map(sf => sf.fileName);
this.aotCompilerHost = new AotCompilerHost(this.tsProgram, options, host);
if (host.readResource) {
this.aotCompilerHost.loadResource = host.readResource.bind(host);
}
const {compiler} = createAotCompiler(this.aotCompilerHost, options);
this.compiler = compiler;
this.collector = new MetadataCollector({quotedNames: true});
}
// Program implementation
getTsProgram(): ts.Program { return this.programWithStubs; }
getTsOptionDiagnostics(cancellationToken?: ts.CancellationToken) {
return this.tsProgram.getOptionsDiagnostics(cancellationToken);
}
getNgOptionDiagnostics(cancellationToken?: ts.CancellationToken): Diagnostic[] {
return getNgOptionDiagnostics(this.options);
}
getTsSyntacticDiagnostics(sourceFile?: ts.SourceFile, cancellationToken?: ts.CancellationToken):
ts.Diagnostic[] {
return this.tsProgram.getSyntacticDiagnostics(sourceFile, cancellationToken);
}
getNgStructuralDiagnostics(cancellationToken?: ts.CancellationToken): Diagnostic[] {
return this.structuralDiagnostics;
}
getTsSemanticDiagnostics(sourceFile?: ts.SourceFile, cancellationToken?: ts.CancellationToken):
ts.Diagnostic[] {
return this.programWithStubs.getSemanticDiagnostics(sourceFile, cancellationToken);
}
getNgSemanticDiagnostics(fileName?: string, cancellationToken?: ts.CancellationToken):
Diagnostic[] {
const compilerDiagnostics = this.generatedFileDiagnostics;
// If we have diagnostics during the parser phase the type check phase is not meaningful so skip
// it.
if (compilerDiagnostics && compilerDiagnostics.length) return compilerDiagnostics;
return this.typeChecker.getDiagnostics(fileName, cancellationToken);
}
loadNgStructureAsync(): Promise<void> {
return this.compiler.analyzeModulesAsync(this.rootNames)
.catch(this.catchAnalysisError.bind(this))
.then(analyzedModules => {
if (this._analyzedModules) {
throw new Error('Angular structure loaded both synchronously and asynchronsly');
}
this._analyzedModules = analyzedModules;
});
}
getLazyRoutes(cancellationToken?: ts.CancellationToken): {[route: string]: string} { return {}; }
emit({emitFlags = EmitFlags.Default, cancellationToken}:
{emitFlags?: EmitFlags, cancellationToken?: ts.CancellationToken}): ts.EmitResult {
const emitMap = new Map<string, string>();
const result = this.programWithStubs.emit(
/* targetSourceFile */ undefined,
createWriteFileCallback(emitFlags, this.host, this.collector, this.options, emitMap),
cancellationToken, (emitFlags & (EmitFlags.DTS | EmitFlags.JS)) == EmitFlags.DTS, {
after: this.options.skipTemplateCodegen ? [] : [getAngularEmitterTransformFactory(
this.generatedFiles)]
});
this.generatedFiles.forEach(file => {
if (file.source && file.source.length && SUMMARY_JSON_FILES.test(file.genFileUrl)) {
// If we have emitted the ngsummary.ts file, ensure the ngsummary.json file is emitted to
// the same location.
const emittedFile = emitMap.get(file.srcFileUrl);
const fileName = emittedFile ?
path.join(path.dirname(emittedFile), path.basename(file.genFileUrl)) :
file.genFileUrl;
this.host.writeFile(fileName, file.source, false, error => {});
}
});
return result;
}
// Private members
private get analyzedModules(): NgAnalyzedModules {
return this._analyzedModules || (this._analyzedModules = this.analyzeModules());
}
private get structuralDiagnostics(): Diagnostic[] {
return this.analyzedModules && this._structuralDiagnostics
}
private get stubs(): GeneratedFile[] {
return this._stubs || (this._stubs = this.generateStubs());
}
private get stubFiles(): string[] {
return this._stubFiles ||
(this._stubFiles = this.stubs.reduce((files: string[], generatedFile) => {
if (generatedFile.source || (generatedFile.stmts && generatedFile.stmts.length)) {
return [...files, generatedFile.genFileUrl];
}
return files;
}, []));
}
private get programWithStubsHost(): ts.CompilerHost {
return this._programWithStubsHost || (this._programWithStubsHost = createProgramWithStubsHost(
this.stubs, this.tsProgram, this.host));
}
private get programWithStubs(): ts.Program {
return this._programWithStubs || (this._programWithStubs = this.createProgramWithStubs());
}
private get generatedFiles(): GeneratedFile[] {
return this._generatedFiles || (this._generatedFiles = this.generateFiles())
}
private get typeChecker(): TypeChecker {
return (this._typeChecker && !this._typeChecker.partialResults) ?
this._typeChecker :
(this._typeChecker = this.createTypeChecker());
}
private get generatedFileDiagnostics(): Diagnostic[]|undefined {
return this.generatedFiles && this._generatedFileDiagnostics !;
}
private catchAnalysisError(e: any): NgAnalyzedModules {
if (isSyntaxError(e)) {
const parserErrors = getParseErrors(e);
if (parserErrors && parserErrors.length) {
this._structuralDiagnostics =
parserErrors.map<Diagnostic>(e => ({
message: e.contextualMessage(),
category: DiagnosticCategory.Error,
span: e.span
}));
} else {
this._structuralDiagnostics = [{message: e.message, category: DiagnosticCategory.Error}];
}
this._analyzedModules = emptyModules;
return emptyModules;
}
throw e;
}
private analyzeModules() {
try {
return this.compiler.analyzeModulesSync(this.srcNames);
} catch (e) {
return this.catchAnalysisError(e);
}
}
private generateStubs() {
return this.options.skipTemplateCodegen ? [] :
this.options.generateCodeForLibraries === false ?
this.compiler.emitAllStubs(this.analyzedModules) :
this.compiler.emitPartialStubs(this.analyzedModules);
}
private generateFiles() {
try {
// Always generate the files if requested to ensure we capture any diagnostic errors but only
// keep the results if we are not skipping template code generation.
const result = this.compiler.emitAllImpls(this.analyzedModules);
return this.options.skipTemplateCodegen ? [] : result;
} catch (e) {
if (isSyntaxError(e)) {
this._generatedFileDiagnostics = [{message: e.message, category: DiagnosticCategory.Error}];
return [];
}
throw e;
}
}
private createTypeChecker(): TypeChecker {
return new TypeChecker(
this.tsProgram, this.options, this.host, this.aotCompilerHost, this.options,
this.analyzedModules, this.generatedFiles);
}
private createProgramWithStubs(): ts.Program {
// If we are skipping code generation just use the original program.
// Otherwise, create a new program that includes the stub files.
return this.options.skipTemplateCodegen ?
this.tsProgram :
ts.createProgram(
[...this.rootNames, ...this.stubFiles], this.options, this.programWithStubsHost);
}
}
export function createProgram(
{rootNames, options, host, oldProgram}:
{rootNames: string[], options: CompilerOptions, host: CompilerHost, oldProgram?: Program}):
Program {
return new AngularCompilerProgram(rootNames, options, host, oldProgram);
}
function writeMetadata(
emitFilePath: string, sourceFile: ts.SourceFile, collector: MetadataCollector,
ngOptions: CompilerOptions) {
if (/\.js$/.test(emitFilePath)) {
const path = emitFilePath.replace(/\.js$/, '.metadata.json');
// Beginning with 2.1, TypeScript transforms the source tree before emitting it.
// We need the original, unmodified, tree which might be several levels back
// depending on the number of transforms performed. All SourceFile's prior to 2.1
// will appear to be the original source since they didn't include an original field.
let collectableFile = sourceFile;
while ((collectableFile as any).original) {
collectableFile = (collectableFile as any).original;
}
const metadata = collector.getMetadata(collectableFile, !!ngOptions.strictMetadataEmit);
if (metadata) {
const metadataText = JSON.stringify([metadata]);
writeFileSync(path, metadataText, {encoding: 'utf-8'});
}
}
}
function createWriteFileCallback(
emitFlags: EmitFlags, host: ts.CompilerHost, collector: MetadataCollector,
ngOptions: CompilerOptions, emitMap: Map<string, string>) {
const withMetadata =
(fileName: string, data: string, writeByteOrderMark: boolean,
onError?: (message: string) => void, sourceFiles?: ts.SourceFile[]) => {
const generatedFile = GENERATED_FILES.test(fileName);
if (!generatedFile || data != '') {
host.writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles);
}
if (!generatedFile && sourceFiles && sourceFiles.length == 1) {
emitMap.set(sourceFiles[0].fileName, fileName);
writeMetadata(fileName, sourceFiles[0], collector, ngOptions);
}
};
const withoutMetadata =
(fileName: string, data: string, writeByteOrderMark: boolean,
onError?: (message: string) => void, sourceFiles?: ts.SourceFile[]) => {
const generatedFile = GENERATED_FILES.test(fileName);
if (!generatedFile || data != '') {
host.writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles);
}
if (!generatedFile && sourceFiles && sourceFiles.length == 1) {
emitMap.set(sourceFiles[0].fileName, fileName);
}
};
return (emitFlags & EmitFlags.Metadata) != 0 ? withMetadata : withoutMetadata;
}
function getNgOptionDiagnostics(options: CompilerOptions): Diagnostic[] {
if (options.annotationsAs) {
switch (options.annotationsAs) {
case 'decorators':
case 'static fields':
break;
default:
return [{
message:
'Angular compiler options "annotationsAs" only supports "static fields" and "decorators"',
category: DiagnosticCategory.Error
}];
}
}
return [];
}
function createProgramWithStubsHost(
generatedFiles: GeneratedFile[], originalProgram: ts.Program,
originalHost: ts.CompilerHost): ts.CompilerHost {
interface FileData {
g: GeneratedFile
s?: ts.SourceFile;
}
return new class implements ts.CompilerHost {
private generatedFiles: Map<string, FileData>;
writeFile: ts.WriteFileCallback;
getCancellationToken: () => ts.CancellationToken;
getDefaultLibLocation: () => string;
trace: (s: string) => void;
getDirectories: (path: string) => string[];
directoryExists: (directoryName: string) => boolean;
constructor() {
this.generatedFiles =
new Map(generatedFiles.filter(g => g.source || (g.stmts && g.stmts.length))
.map<[string, FileData]>(g => [g.genFileUrl, {g}]));
this.writeFile = originalHost.writeFile;
if (originalHost.getDirectories) {
this.getDirectories = path => originalHost.getDirectories !(path);
}
if (originalHost.directoryExists) {
this.directoryExists = directoryName => originalHost.directoryExists !(directoryName);
}
if (originalHost.getCancellationToken) {
this.getCancellationToken = () => originalHost.getCancellationToken !();
}
if (originalHost.getDefaultLibLocation) {
this.getDefaultLibLocation = () => originalHost.getDefaultLibLocation !();
}
if (originalHost.trace) {
this.trace = s => originalHost.trace !(s);
}
}
getSourceFile(
fileName: string, languageVersion: ts.ScriptTarget,
onError?: ((message: string) => void)|undefined): ts.SourceFile {
const data = this.generatedFiles.get(fileName);
if (data) {
return data.s || (data.s = ts.createSourceFile(
fileName, data.g.source || toTypeScript(data.g), languageVersion));
}
return originalProgram.getSourceFile(fileName) ||
originalHost.getSourceFile(fileName, languageVersion, onError);
}
readFile(fileName: string): string {
const data = this.generatedFiles.get(fileName);
if (data) {
return data.g.source || toTypeScript(data.g);
}
return originalHost.readFile(fileName);
}
getDefaultLibFileName = (options: ts.CompilerOptions) =>
originalHost.getDefaultLibFileName(options);
getCurrentDirectory = () => originalHost.getCurrentDirectory();
getCanonicalFileName = (fileName: string) => originalHost.getCanonicalFileName(fileName);
useCaseSensitiveFileNames = () => originalHost.useCaseSensitiveFileNames();
getNewLine = () => originalHost.getNewLine();
fileExists = (fileName: string) =>
this.generatedFiles.has(fileName) || originalHost.fileExists(fileName);
};
}