Skip to content

Commit

Permalink
fix(language-service): Do not cache empty analyzed modules
Browse files Browse the repository at this point in the history
This commit fixes a bug whereby the empty set of analyzed modules is
cached and subsequently produces the incorrect error 'Component ... is
not included in a module...'

PR Close angular#19405
  • Loading branch information
kyliau committed Jan 5, 2018
1 parent f9436f7 commit 047f120
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/language-service/src/typescript_host.ts
Expand Up @@ -158,8 +158,8 @@ export class TypeScriptServiceHost implements LanguageServiceHost {
const programFiles = this.program.getSourceFiles().map(sf => sf.fileName);
analyzedModules =
analyzeNgModules(programFiles, analyzeHost, this.staticSymbolResolver, this.resolver);
this.analyzedModules = analyzedModules;
}
this.analyzedModules = analyzedModules;
}
return analyzedModules;
}
Expand Down Expand Up @@ -634,4 +634,4 @@ function convertChain(chain: FormattedMessageChain): DiagnosticMessageChain {

function errorToDiagnosticWithChain(error: FormattedError, span: Span): DeclarationError {
return {message: error.chain ? convertChain(error.chain) : error.message, span};
}
}
2 changes: 1 addition & 1 deletion packages/language-service/test/diagnostics_spec.ts
Expand Up @@ -248,7 +248,7 @@ describe('diagnostics', () => {
template: \`
<div *ngIf="comps | async; let comps; else loading">
</div>
<ng-template #loading>Loading comps...</ng-template>
<ng-template #loading>Loading comps...</ng-template>
\`
})
export class MyComponent {}
Expand Down
17 changes: 17 additions & 0 deletions packages/language-service/test/typescript_host_spec.ts
Expand Up @@ -46,4 +46,21 @@ describe('completions', () => {
ngHost = new TypeScriptServiceHost(host, service);
expect(() => ngHost.getAnalyzedModules()).not.toThrow();
});

it('should not cache analyzed modules if there is none', () => {
// First create a TypescriptHost with empty script names
host = new MockTypescriptHost([], toh);
service = ts.createLanguageService(host);
ngHost = new TypeScriptServiceHost(host, service);
expect(ngHost.getAnalyzedModules().ngModules).toEqual([]);
// Now add a script
const fileName = '/app/main.ts';
const content = (host as MockTypescriptHost).getFileContent(fileName) !;
(host as MockTypescriptHost).addScript(fileName, content);
// Had we cached the empty analyzed modules earlier, we would get back an
// empty array. But if we didn't cache it, then the analyzed modules will
// be non-empty.
expect(ngHost.getAnalyzedModules().ngModules.length).not.toEqual(0);
});

});

0 comments on commit 047f120

Please sign in to comment.