test(language-service): do not invalidate @angular/core (#36845)
Fix typo and add test cases for https://github.com/angular/angular/pull/36783 PR closes https://github.com/angular/vscode-ng-language-service/issues/713 PR Close #36845
This commit is contained in:
parent
52d2e46700
commit
62ba0acfb5
|
@ -220,8 +220,12 @@ export class TypeScriptServiceHost implements LanguageServiceHost {
|
|||
const ANGULAR_CORE = '@angular/core';
|
||||
const corePath = this.reflectorHost.moduleNameToFileName(ANGULAR_CORE);
|
||||
for (const {fileName} of program.getSourceFiles()) {
|
||||
// If the `@angular/core` has been edited, the language service should be restart,
|
||||
// so ignore the change of `@angular/core`.
|
||||
// If `@angular/core` is edited, the language service would have to be
|
||||
// restarted, so ignore changes to `@angular/core`.
|
||||
// When the StaticReflector is initialized at startup, it loads core
|
||||
// symbols from @angular/core by calling initializeConversionMap(). This
|
||||
// is only done once. If the file is invalidated, some of the core symbols
|
||||
// will be lost permanently.
|
||||
if (fileName === corePath) {
|
||||
continue;
|
||||
}
|
||||
|
|
|
@ -176,4 +176,43 @@ describe('TypeScriptServiceHost', () => {
|
|||
// files have changed.
|
||||
expect(newModules).toBe(oldModules);
|
||||
});
|
||||
|
||||
it('should not reload @angular/core on changes', () => {
|
||||
const tsLSHost = new MockTypescriptHost(['/app/main.ts']);
|
||||
const tsLS = ts.createLanguageService(tsLSHost);
|
||||
const ngLSHost = new TypeScriptServiceHost(tsLSHost, tsLS);
|
||||
const oldModules = ngLSHost.getAnalyzedModules();
|
||||
const ngCore = '/node_modules/@angular/core/core.d.ts';
|
||||
const originalContent = tsLSHost.readFile(ngCore);
|
||||
const oldVersion = tsLSHost.getScriptVersion(ngCore);
|
||||
tsLSHost.override(ngCore, originalContent + '\n\n');
|
||||
const newVersion = tsLSHost.getScriptVersion(ngCore);
|
||||
expect(newVersion).not.toBe(oldVersion);
|
||||
const newModules = ngLSHost.getAnalyzedModules();
|
||||
// Had @angular/core been invalidated, we'd get a different instance of
|
||||
// analyzed modules, with one module missing - ApplicationModule
|
||||
// The absence of this module will cause language service to stop working.
|
||||
expect(newModules).toBe(oldModules);
|
||||
const ApplicationModule =
|
||||
newModules.ngModules.find(m => m.type.reference.name === 'ApplicationModule');
|
||||
expect(ApplicationModule).toBeDefined();
|
||||
});
|
||||
|
||||
it('should reload @angular/common on changes', () => {
|
||||
const tsLSHost = new MockTypescriptHost(['/app/main.ts']);
|
||||
const tsLS = ts.createLanguageService(tsLSHost);
|
||||
const ngLSHost = new TypeScriptServiceHost(tsLSHost, tsLS);
|
||||
const oldModules = ngLSHost.getAnalyzedModules();
|
||||
const ngCommon = '/node_modules/@angular/common/common.d.ts';
|
||||
const originalContent = tsLSHost.readFile(ngCommon);
|
||||
const oldVersion = tsLSHost.getScriptVersion(ngCommon);
|
||||
tsLSHost.override(ngCommon, originalContent + '\n\n');
|
||||
const newVersion = tsLSHost.getScriptVersion(ngCommon);
|
||||
expect(newVersion).not.toBe(oldVersion);
|
||||
const newModules = ngLSHost.getAnalyzedModules();
|
||||
// We get a new instance of analyzed modules
|
||||
expect(newModules).not.toBe(oldModules);
|
||||
// But the content should be exactly the same
|
||||
expect(newModules).toEqual(oldModules);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue