refactor(language-service): stop tracking lastKwownProgram in CompilerFactory (#41517)

With the work done in #41291, the compiler always tracks the last known
program, so there's no need to track the program in the compiler factory
anymore.

PR Close #41517
This commit is contained in:
Keen Yee Liau 2021-04-08 13:14:45 -07:00 committed by Zach Arend
parent 7a40d8cefd
commit 25e46c1fe4
4 changed files with 3 additions and 17 deletions

View File

@ -10,10 +10,8 @@ import {CompilationTicket, freshCompilationTicket, incrementalFromCompilerTicket
import {NgCompilerOptions} from '@angular/compiler-cli/src/ngtsc/core/api'; import {NgCompilerOptions} from '@angular/compiler-cli/src/ngtsc/core/api';
import {TrackedIncrementalBuildStrategy} from '@angular/compiler-cli/src/ngtsc/incremental'; import {TrackedIncrementalBuildStrategy} from '@angular/compiler-cli/src/ngtsc/incremental';
import {ProgramDriver} from '@angular/compiler-cli/src/ngtsc/program_driver'; import {ProgramDriver} from '@angular/compiler-cli/src/ngtsc/program_driver';
import * as ts from 'typescript/lib/tsserverlibrary';
import {LanguageServiceAdapter} from './adapters'; import {LanguageServiceAdapter} from './adapters';
import {isExternalTemplate} from './utils';
/** /**
* Manages the `NgCompiler` instance which backs the language service, updating or replacing it as * Manages the `NgCompiler` instance which backs the language service, updating or replacing it as
@ -27,7 +25,6 @@ import {isExternalTemplate} from './utils';
export class CompilerFactory { export class CompilerFactory {
private readonly incrementalStrategy = new TrackedIncrementalBuildStrategy(); private readonly incrementalStrategy = new TrackedIncrementalBuildStrategy();
private compiler: NgCompiler|null = null; private compiler: NgCompiler|null = null;
private lastKnownProgram: ts.Program|null = null;
constructor( constructor(
private readonly adapter: LanguageServiceAdapter, private readonly adapter: LanguageServiceAdapter,
@ -39,7 +36,7 @@ export class CompilerFactory {
const program = this.programStrategy.getProgram(); const program = this.programStrategy.getProgram();
const modifiedResourceFiles = this.adapter.getModifiedResourceFiles() ?? new Set(); const modifiedResourceFiles = this.adapter.getModifiedResourceFiles() ?? new Set();
if (this.compiler !== null && program === this.lastKnownProgram) { if (this.compiler !== null && program === this.compiler.getCurrentProgram()) {
if (modifiedResourceFiles.size > 0) { if (modifiedResourceFiles.size > 0) {
// Only resource files have changed since the last NgCompiler was created. // Only resource files have changed since the last NgCompiler was created.
const ticket = resourceChangeTicket(this.compiler, modifiedResourceFiles); const ticket = resourceChangeTicket(this.compiler, modifiedResourceFiles);
@ -54,7 +51,7 @@ export class CompilerFactory {
} }
let ticket: CompilationTicket; let ticket: CompilationTicket;
if (this.compiler === null || this.lastKnownProgram === null) { if (this.compiler === null) {
ticket = freshCompilationTicket( ticket = freshCompilationTicket(
program, this.options, this.incrementalStrategy, this.programStrategy, program, this.options, this.incrementalStrategy, this.programStrategy,
/* perfRecorder */ null, true, true); /* perfRecorder */ null, true, true);
@ -64,11 +61,6 @@ export class CompilerFactory {
modifiedResourceFiles, /* perfRecorder */ null); modifiedResourceFiles, /* perfRecorder */ null);
} }
this.compiler = NgCompiler.fromTicket(ticket, this.adapter); this.compiler = NgCompiler.fromTicket(ticket, this.adapter);
this.lastKnownProgram = program;
return this.compiler; return this.compiler;
} }
registerLastKnownProgram() {
this.lastKnownProgram = this.programStrategy.getProgram();
}
} }

View File

@ -266,7 +266,6 @@ export class LanguageService {
return undefined; return undefined;
} }
const result = builder.getCompletionEntrySymbol(entryName); const result = builder.getCompletionEntrySymbol(entryName);
this.compilerFactory.registerLastKnownProgram();
return result; return result;
}); });
} }
@ -360,8 +359,6 @@ export class LanguageService {
private withCompilerAndPerfTracing<T>(phase: PerfPhase, p: (compiler: NgCompiler) => T): T { private withCompilerAndPerfTracing<T>(phase: PerfPhase, p: (compiler: NgCompiler) => T): T {
const compiler = this.compilerFactory.getOrCreate(); const compiler = this.compilerFactory.getOrCreate();
const result = compiler.perfRecorder.inPhase(phase, () => p(compiler)); const result = compiler.perfRecorder.inPhase(phase, () => p(compiler));
this.compilerFactory.registerLastKnownProgram();
const logger = this.project.projectService.logger; const logger = this.project.projectService.logger;
if (logger.hasLevel(ts.server.LogLevel.verbose)) { if (logger.hasLevel(ts.server.LogLevel.verbose)) {
logger.perftrc(`LanguageService#${PerfPhase[phase]}: ${ logger.perftrc(`LanguageService#${PerfPhase[phase]}: ${

View File

@ -207,7 +207,7 @@ describe('language service adapter', () => {
}); });
function getLastKnownProgram(ngLS: LanguageService): ts.Program { function getLastKnownProgram(ngLS: LanguageService): ts.Program {
const program = ngLS['compilerFactory']['lastKnownProgram']; const program = ngLS['compilerFactory']['compiler']?.getCurrentProgram();
expect(program).toBeDefined(); expect(program).toBeDefined();
return program!; return program!;
} }

View File

@ -158,8 +158,6 @@ export class Project {
const ngDiagnostics = ngCompiler.getDiagnosticsForFile(sf, OptimizeFor.WholeProgram); const ngDiagnostics = ngCompiler.getDiagnosticsForFile(sf, OptimizeFor.WholeProgram);
expect(ngDiagnostics.map(diag => diag.messageText)).toEqual([]); expect(ngDiagnostics.map(diag => diag.messageText)).toEqual([]);
} }
this.ngLS.compilerFactory.registerLastKnownProgram();
} }
expectNoTemplateDiagnostics(projectFileName: string, className: string): void { expectNoTemplateDiagnostics(projectFileName: string, className: string): void {
@ -172,7 +170,6 @@ export class Project {
const component = getClassOrError(sf, className); const component = getClassOrError(sf, className);
const diags = this.getTemplateTypeChecker().getDiagnosticsForComponent(component); const diags = this.getTemplateTypeChecker().getDiagnosticsForComponent(component);
this.ngLS.compilerFactory.registerLastKnownProgram();
expect(diags.map(diag => diag.messageText)).toEqual([]); expect(diags.map(diag => diag.messageText)).toEqual([]);
} }