fix(language-service): create StaticReflector once only (#32543)

The creation of StaticReflector in createMetadataResolver() is a very expensive operation because it involves numerous module resolutions.
To make matter worse, since the API of the Reflector does not provide the ability to invalidate its internal caches, it has to be destroyed and recreated on *every* program change.
This has a HUGE impact on performance.
This PR fixes this problem by carefully invalidating all StaticSymbols in a file that has changed, thereby reducing the overhead of recomputation on program change.

PR Close #32543
This commit is contained in:
Keen Yee Liau
2019-09-07 11:51:04 -07:00
committed by atscott
parent 900d0055e0
commit adb562bca6
5 changed files with 51 additions and 35 deletions

View File

@ -51,22 +51,24 @@ describe('TypeScriptServiceHost', () => {
expect(analyzedModules.files.length).toBe(0);
expect(analyzedModules.ngModules.length).toBe(0);
expect(analyzedModules.ngModuleByPipeOrDirective.size).toBe(0);
expect(analyzedModules.symbolsMissingModule).toBeUndefined();
expect(analyzedModules.symbolsMissingModule).toEqual([]);
});
it('should clear the caches if program changes', () => {
it('should clear the caches if new script is added', () => {
// First create a TypescriptHost with empty script names
const tsLSHost = new MockTypescriptHost([]);
const tsLS = ts.createLanguageService(tsLSHost);
const ngLSHost = new TypeScriptServiceHost(tsLSHost, tsLS);
expect(ngLSHost.getAnalyzedModules().ngModules).toEqual([]);
const oldModules = ngLSHost.getAnalyzedModules();
expect(oldModules.ngModules).toEqual([]);
// Now add a script, this would change the program
const fileName = '/app/main.ts';
const content = tsLSHost.readFile(fileName) !;
tsLSHost.addScript(fileName, content);
// If the caches are not cleared, we would get back an empty array.
// But if the caches are cleared then the analyzed modules will be non-empty.
expect(ngLSHost.getAnalyzedModules().ngModules.length).not.toEqual(0);
const newModules = ngLSHost.getAnalyzedModules();
expect(newModules.ngModules.length).toBeGreaterThan(0);
});
it('should throw if getSourceFile is called on non-TS file', () => {