diff --git a/packages/language-service/src/typescript_host.ts b/packages/language-service/src/typescript_host.ts index 81c325fc41..c25eb6f7ff 100644 --- a/packages/language-service/src/typescript_host.ts +++ b/packages/language-service/src/typescript_host.ts @@ -152,9 +152,13 @@ export class TypeScriptServiceHost implements LanguageServiceHost { * and templateReferences. * In addition to returning information about NgModules, this method plays the * same role as 'synchronizeHostData' in tsserver. + * @param ensureSynchronized whether or not the Language Service should make sure analyzedModules + * are synced to the last update of the project. If false, returns the set of analyzedModules + * that is already cached. This is useful if the project must not be reanalyzed, even if its + * file watchers (which are disjoint from the TypeScriptServiceHost) detect an update. */ - getAnalyzedModules(): NgAnalyzedModules { - if (this.upToDate()) { + getAnalyzedModules(ensureSynchronized = true): NgAnalyzedModules { + if (!ensureSynchronized || this.upToDate()) { return this.analyzedModules; } diff --git a/packages/language-service/test/typescript_host_spec.ts b/packages/language-service/test/typescript_host_spec.ts index c1b5c1ea19..a646329281 100644 --- a/packages/language-service/test/typescript_host_spec.ts +++ b/packages/language-service/test/typescript_host_spec.ts @@ -189,4 +189,31 @@ describe('TypeScriptServiceHost', () => { expect(directiveSymbol).toBeDefined(); expect(directiveSymbol !.name).toBe('AppComponent'); }); + + it('should allow for retreiving analyzedModules in synchronized mode', () => { + const fileName = '/app/app.component.ts'; + const tsLSHost = new MockTypescriptHost([fileName]); + const tsLS = ts.createLanguageService(tsLSHost); + const ngLSHost = new TypeScriptServiceHost(tsLSHost, tsLS); + + // Get initial state + const originalModules = ngLSHost.getAnalyzedModules(); + + // Override app.component.ts with a different component + tsLSHost.override(fileName, ` + import {Component} from '@angular/core'; + + @Component({ + template: '