forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host.ts
302 lines (265 loc) · 12.8 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
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
/**
* @license
* Copyright Google LLC 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 ts from 'typescript';
import {ErrorCode, ngErrorCode} from '../../diagnostics';
import {findFlatIndexEntryPoint, FlatIndexGenerator} from '../../entry_point';
import {AbsoluteFsPath, resolve} from '../../file_system';
import {FactoryGenerator, isShim, ShimAdapter, ShimReferenceTagger, SummaryGenerator} from '../../shims';
import {FactoryTracker, PerFileShimGenerator, TopLevelShimGenerator} from '../../shims/api';
import {TypeCheckShimGenerator} from '../../typecheck';
import {normalizeSeparators} from '../../util/src/path';
import {getRootDirs, isNonDeclarationTsPath, RequiredDelegations} from '../../util/src/typescript';
import {ExtendedTsCompilerHost, NgCompilerAdapter, NgCompilerOptions, UnifiedModulesHost} from '../api';
// A persistent source of bugs in CompilerHost delegation has been the addition by TS of new,
// optional methods on ts.CompilerHost. Since these methods are optional, it's not a type error that
// the delegating host doesn't implement or delegate them. This causes subtle runtime failures. No
// more. This infrastructure ensures that failing to delegate a method is a compile-time error.
/**
* Delegates all methods of `ExtendedTsCompilerHost` to a delegate, with the exception of
* `getSourceFile` and `fileExists` which are implemented in `NgCompilerHost`.
*
* If a new method is added to `ts.CompilerHost` which is not delegated, a type error will be
* generated for this class.
*/
export class DelegatingCompilerHost implements
Omit<RequiredDelegations<ExtendedTsCompilerHost>, 'getSourceFile'|'fileExists'> {
constructor(protected delegate: ExtendedTsCompilerHost) {}
private delegateMethod<M extends keyof ExtendedTsCompilerHost>(name: M):
ExtendedTsCompilerHost[M] {
return this.delegate[name] !== undefined ? (this.delegate[name] as any).bind(this.delegate) :
undefined;
}
// Excluded are 'getSourceFile' and 'fileExists', which are actually implemented by NgCompilerHost
// below.
createHash = this.delegateMethod('createHash');
directoryExists = this.delegateMethod('directoryExists');
fileNameToModuleName = this.delegateMethod('fileNameToModuleName');
getCancellationToken = this.delegateMethod('getCancellationToken');
getCanonicalFileName = this.delegateMethod('getCanonicalFileName');
getCurrentDirectory = this.delegateMethod('getCurrentDirectory');
getDefaultLibFileName = this.delegateMethod('getDefaultLibFileName');
getDefaultLibLocation = this.delegateMethod('getDefaultLibLocation');
getDirectories = this.delegateMethod('getDirectories');
getEnvironmentVariable = this.delegateMethod('getEnvironmentVariable');
getModifiedResourceFiles = this.delegateMethod('getModifiedResourceFiles');
getNewLine = this.delegateMethod('getNewLine');
getParsedCommandLine = this.delegateMethod('getParsedCommandLine');
getSourceFileByPath = this.delegateMethod('getSourceFileByPath');
readDirectory = this.delegateMethod('readDirectory');
readFile = this.delegateMethod('readFile');
readResource = this.delegateMethod('readResource');
transformResource = this.delegateMethod('transformResource');
realpath = this.delegateMethod('realpath');
resolveModuleNames = this.delegateMethod('resolveModuleNames');
resolveTypeReferenceDirectives = this.delegateMethod('resolveTypeReferenceDirectives');
resourceNameToFileName = this.delegateMethod('resourceNameToFileName');
trace = this.delegateMethod('trace');
useCaseSensitiveFileNames = this.delegateMethod('useCaseSensitiveFileNames');
writeFile = this.delegateMethod('writeFile');
getModuleResolutionCache = this.delegateMethod('getModuleResolutionCache');
// @ts-expect-error 'hasInvalidatedResolutions' is visible (and thus required here) in latest TSC
// main. It's already present, so the code works at runtime.
// TODO: remove this comment including the suppression once Angular uses a TSC version that
// includes this change (github.com/microsoft/TypeScript@a455955).
hasInvalidatedResolutions = this.delegateMethod('hasInvalidatedResolutions');
}
/**
* A wrapper around `ts.CompilerHost` (plus any extension methods from `ExtendedTsCompilerHost`).
*
* In order for a consumer to include Angular compilation in their TypeScript compiler, the
* `ts.Program` must be created with a host that adds Angular-specific files (e.g. factories,
* summaries, the template type-checking file, etc) to the compilation. `NgCompilerHost` is the
* host implementation which supports this.
*
* The interface implementations here ensure that `NgCompilerHost` fully delegates to
* `ExtendedTsCompilerHost` methods whenever present.
*/
export class NgCompilerHost extends DelegatingCompilerHost implements
RequiredDelegations<ExtendedTsCompilerHost>, ExtendedTsCompilerHost, NgCompilerAdapter {
readonly factoryTracker: FactoryTracker|null = null;
readonly entryPoint: AbsoluteFsPath|null = null;
readonly constructionDiagnostics: ts.Diagnostic[];
readonly inputFiles: ReadonlyArray<string>;
readonly rootDirs: ReadonlyArray<AbsoluteFsPath>;
constructor(
delegate: ExtendedTsCompilerHost, inputFiles: ReadonlyArray<string>,
rootDirs: ReadonlyArray<AbsoluteFsPath>, private shimAdapter: ShimAdapter,
private shimTagger: ShimReferenceTagger, entryPoint: AbsoluteFsPath|null,
factoryTracker: FactoryTracker|null, diagnostics: ts.Diagnostic[]) {
super(delegate);
this.factoryTracker = factoryTracker;
this.entryPoint = entryPoint;
this.constructionDiagnostics = diagnostics;
this.inputFiles = [...inputFiles, ...shimAdapter.extraInputFiles];
this.rootDirs = rootDirs;
if (this.resolveModuleNames === undefined) {
// In order to reuse the module resolution cache during the creation of the type-check
// program, we'll need to provide `resolveModuleNames` if the delegate did not provide one.
this.resolveModuleNames = this.createCachedResolveModuleNamesFunction();
}
}
/**
* Retrieves a set of `ts.SourceFile`s which should not be emitted as JS files.
*
* Available after this host is used to create a `ts.Program` (which causes all the files in the
* program to be enumerated).
*/
get ignoreForEmit(): Set<ts.SourceFile> {
return this.shimAdapter.ignoreForEmit;
}
/**
* Retrieve the array of shim extension prefixes for which shims were created for each original
* file.
*/
get shimExtensionPrefixes(): string[] {
return this.shimAdapter.extensionPrefixes;
}
/**
* Performs cleanup that needs to happen after a `ts.Program` has been created using this host.
*/
postProgramCreationCleanup(): void {
this.shimTagger.finalize();
}
/**
* Create an `NgCompilerHost` from a delegate host, an array of input filenames, and the full set
* of TypeScript and Angular compiler options.
*/
static wrap(
delegate: ts.CompilerHost, inputFiles: ReadonlyArray<string>, options: NgCompilerOptions,
oldProgram: ts.Program|null): NgCompilerHost {
// TODO(alxhub): remove the fallback to allowEmptyCodegenFiles after verifying that the rest of
// our build tooling is no longer relying on it.
const allowEmptyCodegenFiles = options.allowEmptyCodegenFiles || false;
const shouldGenerateFactoryShims = options.generateNgFactoryShims !== undefined ?
options.generateNgFactoryShims :
allowEmptyCodegenFiles;
const shouldGenerateSummaryShims = options.generateNgSummaryShims !== undefined ?
options.generateNgSummaryShims :
allowEmptyCodegenFiles;
const topLevelShimGenerators: TopLevelShimGenerator[] = [];
const perFileShimGenerators: PerFileShimGenerator[] = [];
if (shouldGenerateSummaryShims) {
// Summary generation.
perFileShimGenerators.push(new SummaryGenerator());
}
let factoryTracker: FactoryTracker|null = null;
if (shouldGenerateFactoryShims) {
const factoryGenerator = new FactoryGenerator();
perFileShimGenerators.push(factoryGenerator);
factoryTracker = factoryGenerator;
}
const rootDirs = getRootDirs(delegate, options as ts.CompilerOptions);
perFileShimGenerators.push(new TypeCheckShimGenerator());
let diagnostics: ts.Diagnostic[] = [];
const normalizedTsInputFiles: AbsoluteFsPath[] = [];
for (const inputFile of inputFiles) {
if (!isNonDeclarationTsPath(inputFile)) {
continue;
}
normalizedTsInputFiles.push(resolve(inputFile));
}
let entryPoint: AbsoluteFsPath|null = null;
if (options.flatModuleOutFile != null && options.flatModuleOutFile !== '') {
entryPoint = findFlatIndexEntryPoint(normalizedTsInputFiles);
if (entryPoint === null) {
// This error message talks specifically about having a single .ts file in "files". However
// the actual logic is a bit more permissive. If a single file exists, that will be taken,
// otherwise the highest level (shortest path) "index.ts" file will be used as the flat
// module entry point instead. If neither of these conditions apply, the error below is
// given.
//
// The user is not informed about the "index.ts" option as this behavior is deprecated -
// an explicit entrypoint should always be specified.
diagnostics.push({
category: ts.DiagnosticCategory.Error,
code: ngErrorCode(ErrorCode.CONFIG_FLAT_MODULE_NO_INDEX),
file: undefined,
start: undefined,
length: undefined,
messageText:
'Angular compiler option "flatModuleOutFile" requires one and only one .ts file in the "files" field.',
});
} else {
const flatModuleId = options.flatModuleId || null;
const flatModuleOutFile = normalizeSeparators(options.flatModuleOutFile);
const flatIndexGenerator =
new FlatIndexGenerator(entryPoint, flatModuleOutFile, flatModuleId);
topLevelShimGenerators.push(flatIndexGenerator);
}
}
const shimAdapter = new ShimAdapter(
delegate, normalizedTsInputFiles, topLevelShimGenerators, perFileShimGenerators,
oldProgram);
const shimTagger =
new ShimReferenceTagger(perFileShimGenerators.map(gen => gen.extensionPrefix));
return new NgCompilerHost(
delegate, inputFiles, rootDirs, shimAdapter, shimTagger, entryPoint, factoryTracker,
diagnostics);
}
/**
* Check whether the given `ts.SourceFile` is a shim file.
*
* If this returns false, the file is user-provided.
*/
isShim(sf: ts.SourceFile): boolean {
return isShim(sf);
}
/**
* Check whether the given `ts.SourceFile` is a resource file.
*
* This simply returns `false` for the compiler-cli since resource files are not added as root
* files to the project.
*/
isResource(sf: ts.SourceFile): boolean {
return false;
}
getSourceFile(
fileName: string, languageVersion: ts.ScriptTarget,
onError?: ((message: string) => void)|undefined,
shouldCreateNewSourceFile?: boolean|undefined): ts.SourceFile|undefined {
// Is this a previously known shim?
const shimSf = this.shimAdapter.maybeGenerate(resolve(fileName));
if (shimSf !== null) {
// Yes, so return it.
return shimSf;
}
// No, so it's a file which might need shims (or a file which doesn't exist).
const sf =
this.delegate.getSourceFile(fileName, languageVersion, onError, shouldCreateNewSourceFile);
if (sf === undefined) {
return undefined;
}
this.shimTagger.tag(sf);
return sf;
}
fileExists(fileName: string): boolean {
// Consider the file as existing whenever
// 1) it really does exist in the delegate host, or
// 2) at least one of the shim generators recognizes it
// Note that we can pass the file name as branded absolute fs path because TypeScript
// internally only passes POSIX-like paths.
//
// Also note that the `maybeGenerate` check below checks for both `null` and `undefined`.
return this.delegate.fileExists(fileName) ||
this.shimAdapter.maybeGenerate(resolve(fileName)) != null;
}
get unifiedModulesHost(): UnifiedModulesHost|null {
return this.fileNameToModuleName !== undefined ? this as UnifiedModulesHost : null;
}
private createCachedResolveModuleNamesFunction(): ts.CompilerHost['resolveModuleNames'] {
const moduleResolutionCache = ts.createModuleResolutionCache(
this.getCurrentDirectory(), this.getCanonicalFileName.bind(this));
return (moduleNames, containingFile, reusedNames, redirectedReference, options) => {
return moduleNames.map(moduleName => {
const module = ts.resolveModuleName(
moduleName, containingFile, options, this, moduleResolutionCache, redirectedReference);
return module.resolvedModule;
});
};
}
}