From 2c0c86e3ce36a301fdef62973c4133112e2761dc Mon Sep 17 00:00:00 2001 From: Dzmitry Shylovich Date: Sun, 11 Dec 2016 13:49:03 +0300 Subject: [PATCH] fix(compiler): improve the error when template is not a string Closes #8708 Closes #13377 --- .../compiler/src/directive_normalizer.ts | 10 +++++++++- .../compiler/test/directive_normalizer_spec.ts | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/modules/@angular/compiler/src/directive_normalizer.ts b/modules/@angular/compiler/src/directive_normalizer.ts index 60c34801a7342..99f936e3d5ec6 100644 --- a/modules/@angular/compiler/src/directive_normalizer.ts +++ b/modules/@angular/compiler/src/directive_normalizer.ts @@ -65,10 +65,18 @@ export class DirectiveNormalizer { SyncAsyncResult { let normalizedTemplateSync: CompileTemplateMetadata = null; let normalizedTemplateAsync: Promise; - 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( diff --git a/modules/@angular/compiler/test/directive_normalizer_spec.ts b/modules/@angular/compiler/test/directive_normalizer_spec.ts index e32dc1fba3a30..a44190c13f35b 100644 --- a/modules/@angular/compiler/test/directive_normalizer_spec.ts +++ b/modules/@angular/compiler/test/directive_normalizer_spec.ts @@ -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: {}})) + .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: {}})) + .toThrowError( + SyntaxError, 'The templateUrl specified for component SomeComp is not a string'); + })); }); describe('normalizeTemplateSync', () => { @@ -535,4 +551,4 @@ function programResourceLoaderSpy(spy: SpyResourceLoader, results: {[key: string }); } -class SomeComp {} \ No newline at end of file +class SomeComp {}