diff --git a/packages/language-service/src/typescript_host.ts b/packages/language-service/src/typescript_host.ts index a35ae27091..79b06efa31 100644 --- a/packages/language-service/src/typescript_host.ts +++ b/packages/language-service/src/typescript_host.ts @@ -147,11 +147,19 @@ export class TypeScriptServiceHost implements LanguageServiceHost { private ensureAnalyzedModules(): NgAnalyzedModules { let analyzedModules = this.analyzedModules; if (!analyzedModules) { - const analyzeHost = {isSourceFile(filePath: string) { return true; }}; - const programFiles = this.program.getSourceFiles().map(sf => sf.fileName); - - analyzedModules = this.analyzedModules = - analyzeNgModules(programFiles, analyzeHost, this.staticSymbolResolver, this.resolver); + if (this.host.getScriptFileNames().length === 0) { + analyzedModules = { + files: [], + ngModuleByPipeOrDirective: new Map(), + ngModules: [], + }; + } else { + const analyzeHost = {isSourceFile(filePath: string) { return true; }}; + const programFiles = this.program.getSourceFiles().map(sf => sf.fileName); + analyzedModules = + analyzeNgModules(programFiles, analyzeHost, this.staticSymbolResolver, this.resolver); + } + this.analyzedModules = analyzedModules; } return analyzedModules; } @@ -358,7 +366,11 @@ export class TypeScriptServiceHost implements LanguageServiceHost { if (!result) { if (!this.context) { // 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. diff --git a/packages/language-service/test/typescript_host_spec.ts b/packages/language-service/test/typescript_host_spec.ts index 7b4db27e72..5bea1caf50 100644 --- a/packages/language-service/test/typescript_host_spec.ts +++ b/packages/language-service/test/typescript_host_spec.ts @@ -39,4 +39,11 @@ describe('completions', () => { ngHost = new TypeScriptServiceHost(host, service); 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(); + }); });