Skip to content

Commit a5b4af0

Browse files
chuckjazmhevery
authored andcommitted
fix(language-service): do not crash when Angular cannot be located (#14123)
Fixes #14122 PR Close #14123
1 parent d942031 commit a5b4af0

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

modules/@angular/compiler/src/aot/static_symbol_resolver.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,12 @@ export class StaticSymbolResolver {
273273
getSymbolByModule(module: string, symbolName: string, containingFile?: string): StaticSymbol {
274274
const filePath = this.resolveModule(module, containingFile);
275275
if (!filePath) {
276-
throw new Error(`Could not resolve module ${module} relative to ${containingFile}`);
276+
this.reportError(
277+
new Error(`Could not resolve module ${module}${containingFile ? ` relative to $ {
278+
containingFile
279+
} `: ''}`),
280+
null);
281+
return this.getStaticSymbol(`ERROR:${module}`, symbolName);
277282
}
278283
return this.getStaticSymbol(filePath, symbolName);
279284
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import * as ts from 'typescript';
10+
11+
import {createLanguageService} from '../src/language_service';
12+
import {Completions, LanguageService} from '../src/types';
13+
import {TypeScriptServiceHost} from '../src/typescript_host';
14+
15+
import {toh} from './test_data';
16+
import {MockTypescriptHost} from './test_utils';
17+
18+
describe('service without angular', () => {
19+
let mockHost = new MockTypescriptHost(['/app/main.ts', '/app/parsing-cases.ts'], toh);
20+
mockHost.forgetAngular();
21+
let service = ts.createLanguageService(mockHost);
22+
let ngHost = new TypeScriptServiceHost(mockHost, service);
23+
let ngService = createLanguageService(ngHost);
24+
const fileName = '/app/test.ng';
25+
let position = mockHost.getMarkerLocations(fileName)['h1-content'];
26+
27+
it('should not crash a get template references',
28+
() => expect(() => ngService.getTemplateReferences()));
29+
it('should not crash a get dianostics',
30+
() => expect(() => ngService.getDiagnostics(fileName)).not.toThrow());
31+
it('should not crash a completion',
32+
() => expect(() => ngService.getCompletionsAt(fileName, position)).not.toThrow());
33+
it('should not crash a get defintion',
34+
() => expect(() => ngService.getDefinitionAt(fileName, position)).not.toThrow());
35+
it('should not crash a hover', () => expect(() => ngService.getHoverAt(fileName, position)));
36+
});

modules/@angular/language-service/test/test_utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ export class MockTypescriptHost implements ts.LanguageServiceHost {
9797
this.scriptNames.push(fileName);
9898
}
9999

100+
forgetAngular() { this.angularPath = undefined; }
101+
100102
getCompilationSettings(): ts.CompilerOptions {
101103
return {
102104
target: ts.ScriptTarget.ES5,

0 commit comments

Comments
 (0)