From 25e46c1fe4dc32d8a0c2be294f35c2f881ed9878 Mon Sep 17 00:00:00 2001 From: Keen Yee Liau Date: Thu, 8 Apr 2021 13:14:45 -0700 Subject: [PATCH] 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 --- packages/language-service/ivy/compiler_factory.ts | 12 ++---------- packages/language-service/ivy/language_service.ts | 3 --- .../ivy/test/legacy/language_service_spec.ts | 2 +- packages/language-service/ivy/testing/src/project.ts | 3 --- 4 files changed, 3 insertions(+), 17 deletions(-) diff --git a/packages/language-service/ivy/compiler_factory.ts b/packages/language-service/ivy/compiler_factory.ts index cc62d14ea2..ee990ee8bd 100644 --- a/packages/language-service/ivy/compiler_factory.ts +++ b/packages/language-service/ivy/compiler_factory.ts @@ -10,10 +10,8 @@ import {CompilationTicket, freshCompilationTicket, incrementalFromCompilerTicket import {NgCompilerOptions} from '@angular/compiler-cli/src/ngtsc/core/api'; import {TrackedIncrementalBuildStrategy} from '@angular/compiler-cli/src/ngtsc/incremental'; import {ProgramDriver} from '@angular/compiler-cli/src/ngtsc/program_driver'; -import * as ts from 'typescript/lib/tsserverlibrary'; import {LanguageServiceAdapter} from './adapters'; -import {isExternalTemplate} from './utils'; /** * 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 { private readonly incrementalStrategy = new TrackedIncrementalBuildStrategy(); private compiler: NgCompiler|null = null; - private lastKnownProgram: ts.Program|null = null; constructor( private readonly adapter: LanguageServiceAdapter, @@ -39,7 +36,7 @@ export class CompilerFactory { const program = this.programStrategy.getProgram(); 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) { // Only resource files have changed since the last NgCompiler was created. const ticket = resourceChangeTicket(this.compiler, modifiedResourceFiles); @@ -54,7 +51,7 @@ export class CompilerFactory { } let ticket: CompilationTicket; - if (this.compiler === null || this.lastKnownProgram === null) { + if (this.compiler === null) { ticket = freshCompilationTicket( program, this.options, this.incrementalStrategy, this.programStrategy, /* perfRecorder */ null, true, true); @@ -64,11 +61,6 @@ export class CompilerFactory { modifiedResourceFiles, /* perfRecorder */ null); } this.compiler = NgCompiler.fromTicket(ticket, this.adapter); - this.lastKnownProgram = program; return this.compiler; } - - registerLastKnownProgram() { - this.lastKnownProgram = this.programStrategy.getProgram(); - } } diff --git a/packages/language-service/ivy/language_service.ts b/packages/language-service/ivy/language_service.ts index 25ec273dd7..e1607c7f32 100644 --- a/packages/language-service/ivy/language_service.ts +++ b/packages/language-service/ivy/language_service.ts @@ -266,7 +266,6 @@ export class LanguageService { return undefined; } const result = builder.getCompletionEntrySymbol(entryName); - this.compilerFactory.registerLastKnownProgram(); return result; }); } @@ -360,8 +359,6 @@ export class LanguageService { private withCompilerAndPerfTracing(phase: PerfPhase, p: (compiler: NgCompiler) => T): T { const compiler = this.compilerFactory.getOrCreate(); const result = compiler.perfRecorder.inPhase(phase, () => p(compiler)); - this.compilerFactory.registerLastKnownProgram(); - const logger = this.project.projectService.logger; if (logger.hasLevel(ts.server.LogLevel.verbose)) { logger.perftrc(`LanguageService#${PerfPhase[phase]}: ${ diff --git a/packages/language-service/ivy/test/legacy/language_service_spec.ts b/packages/language-service/ivy/test/legacy/language_service_spec.ts index 65361603ac..8f9cc991e9 100644 --- a/packages/language-service/ivy/test/legacy/language_service_spec.ts +++ b/packages/language-service/ivy/test/legacy/language_service_spec.ts @@ -207,7 +207,7 @@ describe('language service adapter', () => { }); function getLastKnownProgram(ngLS: LanguageService): ts.Program { - const program = ngLS['compilerFactory']['lastKnownProgram']; + const program = ngLS['compilerFactory']['compiler']?.getCurrentProgram(); expect(program).toBeDefined(); return program!; } diff --git a/packages/language-service/ivy/testing/src/project.ts b/packages/language-service/ivy/testing/src/project.ts index 37ae237eda..a9821c2721 100644 --- a/packages/language-service/ivy/testing/src/project.ts +++ b/packages/language-service/ivy/testing/src/project.ts @@ -158,8 +158,6 @@ export class Project { const ngDiagnostics = ngCompiler.getDiagnosticsForFile(sf, OptimizeFor.WholeProgram); expect(ngDiagnostics.map(diag => diag.messageText)).toEqual([]); } - - this.ngLS.compilerFactory.registerLastKnownProgram(); } expectNoTemplateDiagnostics(projectFileName: string, className: string): void { @@ -172,7 +170,6 @@ export class Project { const component = getClassOrError(sf, className); const diags = this.getTemplateTypeChecker().getDiagnosticsForComponent(component); - this.ngLS.compilerFactory.registerLastKnownProgram(); expect(diags.map(diag => diag.messageText)).toEqual([]); }