feat(language-service): allow retreiving synchronized analyzed NgModules (#32779)

Sometimes modules retreived from the language service need to be
synchronized to the last time they were updated, and not updated
on-the-fly. This PR adds a flag to
`TypeScriptServiceHost#getAnalyzedModules` that retreives cached
analyzed NgModules rather than potentially recomputing them.

PR Close #32779
This commit is contained in:
ayazhafiz 2019-09-19 16:26:39 -05:00 committed by atscott
parent 28358b6395
commit 98feee7e0e
2 changed files with 33 additions and 2 deletions

View File

@ -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;
}

View File

@ -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: '<div>Hello!</div>',
})
export class HelloComponent {}
`);
// Make sure synchronized modules match the original state
const syncModules = ngLSHost.getAnalyzedModules(false);
expect(originalModules).toEqual(syncModules);
// Now, get modules for the updated project, which should not be synchronized
const updatedModules = ngLSHost.getAnalyzedModules();
expect(updatedModules).not.toEqual(syncModules);
});
});