Skip to content

Commit 7764c5c

Browse files
chuckjazalxhub
authored andcommitted
fix(language-service): avoid throwing exceptions when reporting metadata errors
1 parent c6917d9 commit 7764c5c

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

packages/compiler/src/metadata_resolver.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ export class CompileMetadataResolver {
529529
syntaxError(
530530
`Can't export ${this._getTypeDescriptor(exportedId.reference)} ${stringifyType(exportedId.reference)} from ${stringifyType(moduleType)} as it was neither declared nor imported!`),
531531
moduleType);
532+
return;
532533
}
533534
});
534535

@@ -627,6 +628,7 @@ export class CompileMetadataResolver {
627628
`Please consider moving ${stringifyType(type)} to a higher module that imports ${stringifyType(oldModule)} and ${stringifyType(moduleType)}. ` +
628629
`You can also create a new NgModule that exports and includes ${stringifyType(type)} then import that NgModule in ${stringifyType(oldModule)} and ${stringifyType(moduleType)}.`),
629630
moduleType);
631+
return;
630632
}
631633
this._ngModuleOfTypes.set(type, moduleType);
632634
}
@@ -860,6 +862,7 @@ export class CompileMetadataResolver {
860862
} else if (provider === void 0) {
861863
this._reportError(syntaxError(
862864
`Encountered undefined provider! Usually this means you have a circular dependencies (might be caused by using 'barrel' index.ts files.`));
865+
return;
863866
} else {
864867
const providersInfo =
865868
(<string[]>providers.reduce(
@@ -879,6 +882,7 @@ export class CompileMetadataResolver {
879882
syntaxError(
880883
`Invalid ${debugInfo ? debugInfo : 'provider'} - only instances of Provider and Type are allowed, got: [${providersInfo}]`),
881884
type);
885+
return;
882886
}
883887
if (providerMeta.token === resolveIdentifier(Identifiers.ANALYZE_FOR_ENTRY_COMPONENTS)) {
884888
targetEntryComponents.push(...this._getEntryComponentsFromProvider(providerMeta, type));
@@ -1002,8 +1006,10 @@ export class CompileMetadataResolver {
10021006
syntaxError(
10031007
`Can't construct a query for the property "${propertyName}" of "${stringifyType(typeOrFunc)}" since the query selector wasn't defined.`),
10041008
typeOrFunc);
1009+
selectors = [];
1010+
} else {
1011+
selectors = [this._getTokenMetadata(q.selector)];
10051012
}
1006-
selectors = [this._getTokenMetadata(q.selector)];
10071013
}
10081014

10091015
return {

packages/language-service/test/diagnostics_spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,25 @@ describe('diagnostics', () => {
201201
expect(diagnostic).toEqual([]);
202202
});
203203

204+
205+
it('should report an error for invalid providers', () => {
206+
addCode(
207+
`
208+
@Component({
209+
template: '',
210+
providers: [null]
211+
})
212+
export class MyComponent {}
213+
`,
214+
fileName => {
215+
const diagnostics = ngService.getDiagnostics(fileName);
216+
const expected = diagnostics.find(d => d.message.startsWith('Invalid providers for'));
217+
const notExpected = diagnostics.find(d => d.message.startsWith('Cannot read property'));
218+
expect(expected).toBeDefined();
219+
expect(notExpected).toBeUndefined();
220+
});
221+
});
222+
204223
function addCode(code: string, cb: (fileName: string, content?: string) => void) {
205224
const fileName = '/app/app.component.ts';
206225
const originalContent = mockHost.getFileContent(fileName);

0 commit comments

Comments
 (0)