fix(language-service): Fix crash when no script files are found (#20550)
Fixes the crash in typescript host when getScriptFileNames() returns an empty array. PR Close #19325 PR Close #20550
This commit is contained in:
parent
ba850b36de
commit
54bfe14313
|
@ -147,12 +147,20 @@ export class TypeScriptServiceHost implements LanguageServiceHost {
|
||||||
private ensureAnalyzedModules(): NgAnalyzedModules {
|
private ensureAnalyzedModules(): NgAnalyzedModules {
|
||||||
let analyzedModules = this.analyzedModules;
|
let analyzedModules = this.analyzedModules;
|
||||||
if (!analyzedModules) {
|
if (!analyzedModules) {
|
||||||
|
if (this.host.getScriptFileNames().length === 0) {
|
||||||
|
analyzedModules = {
|
||||||
|
files: [],
|
||||||
|
ngModuleByPipeOrDirective: new Map(),
|
||||||
|
ngModules: [],
|
||||||
|
};
|
||||||
|
} else {
|
||||||
const analyzeHost = {isSourceFile(filePath: string) { return true; }};
|
const analyzeHost = {isSourceFile(filePath: string) { return true; }};
|
||||||
const programFiles = this.program.getSourceFiles().map(sf => sf.fileName);
|
const programFiles = this.program.getSourceFiles().map(sf => sf.fileName);
|
||||||
|
analyzedModules =
|
||||||
analyzedModules = this.analyzedModules =
|
|
||||||
analyzeNgModules(programFiles, analyzeHost, this.staticSymbolResolver, this.resolver);
|
analyzeNgModules(programFiles, analyzeHost, this.staticSymbolResolver, this.resolver);
|
||||||
}
|
}
|
||||||
|
this.analyzedModules = analyzedModules;
|
||||||
|
}
|
||||||
return analyzedModules;
|
return analyzedModules;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -358,7 +366,11 @@ export class TypeScriptServiceHost implements LanguageServiceHost {
|
||||||
if (!result) {
|
if (!result) {
|
||||||
if (!this.context) {
|
if (!this.context) {
|
||||||
// Make up a context by finding the first script and using that as the base dir.
|
// Make up a context by finding the first script and using that as the base dir.
|
||||||
this.context = this.host.getScriptFileNames()[0];
|
const scriptFileNames = this.host.getScriptFileNames();
|
||||||
|
if (0 === scriptFileNames.length) {
|
||||||
|
throw new Error('Internal error: no script file names found');
|
||||||
|
}
|
||||||
|
this.context = scriptFileNames[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the file context's directory as the base directory.
|
// Use the file context's directory as the base directory.
|
||||||
|
|
|
@ -39,4 +39,11 @@ describe('completions', () => {
|
||||||
ngHost = new TypeScriptServiceHost(host, service);
|
ngHost = new TypeScriptServiceHost(host, service);
|
||||||
expect(ngHost.getAnalyzedModules()).toBeDefined();
|
expect(ngHost.getAnalyzedModules()).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should not throw if there is no script names', () => {
|
||||||
|
host = new MockTypescriptHost([], toh);
|
||||||
|
service = ts.createLanguageService(host);
|
||||||
|
ngHost = new TypeScriptServiceHost(host, service);
|
||||||
|
expect(() => ngHost.getAnalyzedModules()).not.toThrow();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue