fix(language-service): Allow empty templates (#20651)

Fixes the bug where templates with empty strings show up as error in the editor.

PR Close #19406

PR Close #20651
This commit is contained in:
Keen Yee Liau 2017-11-22 12:09:49 -08:00 committed by Miško Hevery
parent 54bfe14313
commit 3203069d6c
2 changed files with 22 additions and 3 deletions

View File

@ -71,9 +71,12 @@ export function getDeclarationDiagnostics(
report(
`Component '${declaration.type.name}' is not included in a module and will not be available inside a template. Consider adding it to a NgModule declaration`);
}
if (!declaration.metadata.template !.template &&
!declaration.metadata.template !.templateUrl) {
report(`Component ${declaration.type.name} must have a template or templateUrl`);
const {template, templateUrl} = declaration.metadata.template !;
if (template === null && !templateUrl) {
report(`Component '${declaration.type.name}' must have a template or templateUrl`);
} else if (template && templateUrl) {
report(
`Component '${declaration.type.name}' must not have both template and templateUrl`);
}
} else {
if (!directives) {

View File

@ -175,6 +175,22 @@ describe('diagnostics', () => {
});
});
// Issue #19406
it('should allow empty template', () => {
const appComponent = `
import { Component } from '@angular/core';
@Component({
template : '',
})
export class AppComponent {}
`;
const fileName = '/app/app.component.ts';
mockHost.override(fileName, appComponent);
const diagnostics = ngService.getDiagnostics(fileName);
expect(diagnostics).toEqual([]);
});
// Issue #15460
it('should be able to find members defined on an ancestor type', () => {
const app_component = `