Skip to content

Commit

Permalink
fix(compiler): improve the error when template is not a string
Browse files Browse the repository at this point in the history
Closes #8708
Closes #13377
  • Loading branch information
Dzmitry Shylovich authored and hansl committed Dec 28, 2016
1 parent 5b4bea2 commit 2c0c86e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 9 additions & 1 deletion modules/@angular/compiler/src/directive_normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,18 @@ export class DirectiveNormalizer {
SyncAsyncResult<CompileTemplateMetadata> {
let normalizedTemplateSync: CompileTemplateMetadata = null;
let normalizedTemplateAsync: Promise<CompileTemplateMetadata>;
if (prenormData.template) {
if (prenormData.template != null) {
if (typeof prenormData.template !== 'string') {
throw new SyntaxError(
`The template specified for component ${stringify(prenormData.componentType)} is not a string`);
}
normalizedTemplateSync = this.normalizeTemplateSync(prenormData);
normalizedTemplateAsync = Promise.resolve(normalizedTemplateSync);
} else if (prenormData.templateUrl) {
if (typeof prenormData.templateUrl !== 'string') {
throw new SyntaxError(
`The templateUrl specified for component ${stringify(prenormData.componentType)} is not a string`);
}
normalizedTemplateAsync = this.normalizeTemplateAsync(prenormData);
} else {
throw new SyntaxError(
Expand Down
18 changes: 17 additions & 1 deletion modules/@angular/compiler/test/directive_normalizer_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ export function main() {
moduleUrl: SOME_MODULE_URL,
})).toThrowError(SyntaxError, 'No template specified for component SomeComp');
}));
it('should throw if template is not a string',
inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => {
expect(
() => normalizer.normalizeTemplate(
{componentType: SomeComp, moduleUrl: SOME_MODULE_URL, template: <any>{}}))
.toThrowError(
SyntaxError, 'The template specified for component SomeComp is not a string');
}));
it('should throw if templateUrl is not a string',
inject([DirectiveNormalizer], (normalizer: DirectiveNormalizer) => {
expect(
() => normalizer.normalizeTemplate(
{componentType: SomeComp, moduleUrl: SOME_MODULE_URL, templateUrl: <any>{}}))
.toThrowError(
SyntaxError, 'The templateUrl specified for component SomeComp is not a string');
}));
});

describe('normalizeTemplateSync', () => {
Expand Down Expand Up @@ -535,4 +551,4 @@ function programResourceLoaderSpy(spy: SpyResourceLoader, results: {[key: string
});
}

class SomeComp {}
class SomeComp {}

0 comments on commit 2c0c86e

Please sign in to comment.