From dd8a31838c8e7ac9bd175140b4058cb6f93eec54 Mon Sep 17 00:00:00 2001 From: JoostK Date: Sat, 5 Dec 2020 14:30:45 +0100 Subject: [PATCH] refactor(compiler-cli): extract parsing of interpolation config (#39961) Prior to this change the interpolation config value was cast to `[string, string]` without checking whether there really were two string values available. This commit extracts the logic of parsing the interpolation config into a separate function and adds a check that the array contains exactly two strings. PR Close #39961 --- .../partial_component_linker_1.ts | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/compiler-cli/linker/src/file_linker/partial_linkers/partial_component_linker_1.ts b/packages/compiler-cli/linker/src/file_linker/partial_linkers/partial_component_linker_1.ts index 0dafc02d48..6f5c9fa4b0 100644 --- a/packages/compiler-cli/linker/src/file_linker/partial_linkers/partial_component_linker_1.ts +++ b/packages/compiler-cli/linker/src/file_linker/partial_linkers/partial_component_linker_1.ts @@ -38,11 +38,7 @@ export class PartialComponentLinkerVersion1 implements PartialLinke export function toR3ComponentMeta( metaObj: AstObject, code: string, sourceUrl: string, options: LinkerOptions): R3ComponentMetadata { - let interpolation = DEFAULT_INTERPOLATION_CONFIG; - if (metaObj.has('interpolation')) { - interpolation = InterpolationConfig.fromArray( - metaObj.getArray('interpolation').map(entry => entry.getString()) as [string, string]); - } + const interpolation = parseInterpolationConfig(metaObj); const templateObj = metaObj.getObject('template'); const templateSource = templateObj.getValue('source'); const range = getTemplateRange(templateSource, code); @@ -130,6 +126,25 @@ export function toR3ComponentMeta( }; } +/** + * Extract an `InterpolationConfig` from the component declaration. + */ +function parseInterpolationConfig( + metaObj: AstObject): InterpolationConfig { + if (!metaObj.has('interpolation')) { + return DEFAULT_INTERPOLATION_CONFIG; + } + + const interpolationExpr = metaObj.getValue('interpolation'); + const values = interpolationExpr.getArray().map(entry => entry.getString()); + if (values.length !== 2) { + throw new FatalLinkerError( + interpolationExpr.expression, + 'Unsupported interpolation config, expected an array containing exactly two strings'); + } + return InterpolationConfig.fromArray(values as [string, string]); +} + /** * Determines the `ViewEncapsulation` mode from the AST value's symbol name. */