From d98b1c3bc49eaa0fcc800b572fb210a164bfdc84 Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Mon, 2 Jul 2018 11:24:58 -0700 Subject: [PATCH] fix(ivy): strip newlines from selectors in .d.ts files (#24738) When writing selectors as string literal types in .d.ts files, strip newlines to avoid generating invalid code. Newlines carry no meaning in selectors anyway. PR Close #24738 --- packages/compiler/src/render3/view/compiler.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/compiler/src/render3/view/compiler.ts b/packages/compiler/src/render3/view/compiler.ts index 375c9e134b..96d7167adc 100644 --- a/packages/compiler/src/render3/view/compiler.ts +++ b/packages/compiler/src/render3/view/compiler.ts @@ -88,9 +88,14 @@ export function compileDirectiveFromMetadata( bindingParser: BindingParser): R3DirectiveDef { const definitionMap = baseDirectiveFields(meta, constantPool, bindingParser); const expression = o.importExpr(R3.defineDirective).callFn([definitionMap.toLiteralMap()]); + + // On the type side, remove newlines from the selector as it will need to fit into a TypeScript + // string literal, which must be on one line. + const selectorForType = (meta.selector || '').replace(/\n/g, ''); + const type = new o.ExpressionType(o.importExpr( R3.DirectiveDef, - [new o.ExpressionType(meta.type), new o.ExpressionType(o.literal(meta.selector || ''))])); + [new o.ExpressionType(meta.type), new o.ExpressionType(o.literal(selectorForType))])); return {expression, type}; } @@ -157,10 +162,14 @@ export function compileComponentFromMetadata( definitionMap.set('pipes', o.literalArr(Array.from(pipesUsed))); } + // On the type side, remove newlines from the selector as it will need to fit into a TypeScript + // string literal, which must be on one line. + const selectorForType = (meta.selector || '').replace(/\n/g, ''); + const expression = o.importExpr(R3.defineComponent).callFn([definitionMap.toLiteralMap()]); const type = new o.ExpressionType(o.importExpr( R3.ComponentDef, - [new o.ExpressionType(meta.type), new o.ExpressionType(o.literal(meta.selector || ''))])); + [new o.ExpressionType(meta.type), new o.ExpressionType(o.literal(selectorForType))])); return {expression, type}; }