From bfbdb8f84da7518f79b7d05039cbdc8b91b5ddc7 Mon Sep 17 00:00:00 2001 From: JoostK Date: Fri, 19 Mar 2021 22:48:32 +0100 Subject: [PATCH] refactor(compiler-cli): cleanup redundant storage of reuse `ts.Program` (#41289) In the compiler, the `NgtscProgram` is responsible for creating the `ts.Program` instance to use, potentially using a `ts.Program` from a prior compilation to enable incremental compilation. It used to track a `reuseTsProgram` for this purpose, however the `ts.Program` that should be used as reuse program is also tracked by the `NgCompiler` instance that is used by `NgtscProgram`. The `NgtscProgram` can leverage the state from `NgCompiler` instead of keeping track of it by itself. PR Close #41289 --- packages/compiler-cli/src/ngtsc/program.ts | 29 ++++++---------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/program.ts b/packages/compiler-cli/src/ngtsc/program.ts index a0074dd7e7..356cbc8451 100644 --- a/packages/compiler-cli/src/ngtsc/program.ts +++ b/packages/compiler-cli/src/ngtsc/program.ts @@ -38,20 +38,6 @@ export class NgtscProgram implements api.Program { */ private tsProgram: ts.Program; - /** - * The TypeScript program to use for the next incremental compilation. - * - * Once a TS program is used to create another (an incremental compilation operation), it can no - * longer be used to do so again. - * - * Since template type-checking uses the primary program to create a type-checking program, after - * this happens the primary program is no longer suitable for starting a subsequent compilation, - * and the template type-checking program should be used instead. - * - * Thus, the program which should be used for the next incremental compilation is tracked in - * `reuseTsProgram`, separately from the "primary" program which is always used for emit. - */ - private reuseTsProgram: ts.Program; private closureCompilerEnabled: boolean; private host: NgCompilerHost; private incrementalStrategy: TrackedIncrementalBuildStrategy; @@ -70,7 +56,7 @@ export class NgtscProgram implements api.Program { this.closureCompilerEnabled = !!options.annotateForClosureCompiler; - const reuseProgram = oldProgram?.reuseTsProgram; + const reuseProgram = oldProgram?.compiler.getCurrentProgram(); this.host = NgCompilerHost.wrap(delegateHost, rootNames, options, reuseProgram ?? null); if (reuseProgram !== undefined) { @@ -84,7 +70,6 @@ export class NgtscProgram implements api.Program { this.tsProgram = perfRecorder.inPhase( PerfPhase.TypeScriptProgramCreate, () => ts.createProgram(this.host.inputFiles, options, this.host, reuseProgram)); - this.reuseTsProgram = this.tsProgram; perfRecorder.phase(PerfPhase.Unaccounted); perfRecorder.memory(PerfCheckpoint.TypeScriptProgramCreate); @@ -137,7 +122,7 @@ export class NgtscProgram implements api.Program { } getReuseTsProgram(): ts.Program { - return this.reuseTsProgram; + return this.compiler.getCurrentProgram(); } getTsOptionDiagnostics(cancellationToken?: ts.CancellationToken| @@ -220,11 +205,11 @@ export class NgtscProgram implements api.Program { } } - const diagnostics = sf === undefined ? - this.compiler.getDiagnostics() : - this.compiler.getDiagnosticsForFile(sf, OptimizeFor.WholeProgram); - this.reuseTsProgram = this.compiler.getCurrentProgram(); - return diagnostics; + if (sf === undefined) { + return this.compiler.getDiagnostics(); + } else { + return this.compiler.getDiagnosticsForFile(sf, OptimizeFor.WholeProgram); + } } /**