From 58b3a51e64cda45719e89e2764bc2c6e23f099fa Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Tue, 22 Oct 2019 15:05:44 +0100 Subject: [PATCH] refactor(compiler): use type guard rather than type cast (#33318) The code will now fail if the `i18n` property is of the wrong type. PR Close #33318 --- .../src/render3/r3_template_transform.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/compiler/src/render3/r3_template_transform.ts b/packages/compiler/src/render3/r3_template_transform.ts index 7a10c596ba..41c2452089 100644 --- a/packages/compiler/src/render3/r3_template_transform.ts +++ b/packages/compiler/src/render3/r3_template_transform.ts @@ -231,19 +231,23 @@ class HtmlAstToIvyAst implements html.Visitor { } visitExpansion(expansion: html.Expansion): t.Icu|null { - const meta = expansion.i18n as i18n.Message; - // do not generate Icu in case it was created - // outside of i18n block in a template - if (!meta) { + if (!expansion.i18n) { + // do not generate Icu in case it was created + // outside of i18n block in a template return null; } + if (!isI18nRootNode(expansion.i18n)) { + throw new Error( + `Invalid type "${expansion.i18n.constructor}" for "i18n" property of ${expansion.sourceSpan.toString()}. Expected a "Message"`); + } + const message = expansion.i18n; const vars: {[name: string]: t.BoundText} = {}; const placeholders: {[name: string]: t.Text | t.BoundText} = {}; // extract VARs from ICUs - we process them separately while // assembling resulting message via goog.getMsg function, since // we need to pass them to top-level goog.getMsg call - Object.keys(meta.placeholders).forEach(key => { - const value = meta.placeholders[key]; + Object.keys(message.placeholders).forEach(key => { + const value = message.placeholders[key]; if (key.startsWith(I18N_ICU_VAR_PREFIX)) { const config = this.bindingParser.interpolationConfig; // ICU expression is a plain string, not wrapped into start @@ -254,7 +258,7 @@ class HtmlAstToIvyAst implements html.Visitor { placeholders[key] = this._visitTextWithInterpolation(value, expansion.sourceSpan); } }); - return new t.Icu(vars, placeholders, expansion.sourceSpan, meta); + return new t.Icu(vars, placeholders, expansion.sourceSpan, message); } visitExpansionCase(expansionCase: html.ExpansionCase): null { return null; }