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
This commit is contained in:
JoostK 2020-12-05 14:30:45 +01:00 committed by Misko Hevery
parent c0bccc39db
commit dd8a31838c
1 changed files with 20 additions and 5 deletions

View File

@ -38,11 +38,7 @@ export class PartialComponentLinkerVersion1<TExpression> implements PartialLinke
export function toR3ComponentMeta<TExpression>(
metaObj: AstObject<R3DeclareComponentMetadata, TExpression>, 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<TExpression>(
};
}
/**
* Extract an `InterpolationConfig` from the component declaration.
*/
function parseInterpolationConfig<TExpression>(
metaObj: AstObject<R3DeclareComponentMetadata, TExpression>): 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.
*/