refactor(ivy): prep ngtsc and ngcc for upcoming import resolution work (#27743)

Upcoming work to implement import resolution will change the dependencies
of some higher-level classes in ngtsc & ngcc. This necessitates changes in
how these classes are created and the lifecycle of the ts.Program in ngtsc
& ngcc.

To avoid complicating the implementation work with refactoring as a result
of the new dependencies, the refactoring is performed in this commit as a
separate prepatory step.

In ngtsc, the testing harness is modified to allow easier access to some
aspects of the ts.Program.

In ngcc, the main change is that the DecorationAnalyzer is created with the
ts.Program as a constructor parameter. This is not a lifecycle change, as
it was previously created with the ts.TypeChecker which is derived from the
ts.Program anyways. This change requires some reorganization in ngcc to
accommodate, especially in testing harnesses where DecorationAnalyzer is
created manually in a number of specs.

PR Close #27743
This commit is contained in:
Alex Rickabaugh
2018-12-17 16:17:38 -08:00
committed by Kara Erickson
parent 2a6108af97
commit f4a9f5dae8
10 changed files with 127 additions and 139 deletions

View File

@ -13,19 +13,18 @@ import * as ts from 'typescript';
export function makeProgram(
files: {name: string, contents: string, isRoot?: boolean}[], options?: ts.CompilerOptions,
host: ts.CompilerHost = new InMemoryHost(),
checkForErrors: boolean = true): {program: ts.Program, host: ts.CompilerHost} {
host: ts.CompilerHost = new InMemoryHost(), checkForErrors: boolean = true):
{program: ts.Program, host: ts.CompilerHost, options: ts.CompilerOptions} {
files.forEach(file => host.writeFile(file.name, file.contents, false, undefined, []));
const rootNames =
files.filter(file => file.isRoot !== false).map(file => host.getCanonicalFileName(file.name));
const program = ts.createProgram(
rootNames, {
noLib: true,
experimentalDecorators: true,
moduleResolution: ts.ModuleResolutionKind.NodeJs, ...options
},
host);
const compilerOptions = {
noLib: true,
experimentalDecorators: true,
moduleResolution: ts.ModuleResolutionKind.NodeJs, ...options
};
const program = ts.createProgram(rootNames, compilerOptions, host);
if (checkForErrors) {
const diags = [...program.getSyntacticDiagnostics(), ...program.getSemanticDiagnostics()];
if (diags.length > 0) {
@ -41,7 +40,7 @@ export function makeProgram(
throw new Error(`Typescript diagnostics failed! ${errors.join(', ')}`);
}
}
return {program, host};
return {program, host, options: compilerOptions};
}
export class InMemoryHost implements ts.CompilerHost {