From 6b2ddef70d84e546e620c961e338cb32b7897f61 Mon Sep 17 00:00:00 2001 From: lilbeqiri Date: Fri, 7 Jun 2024 13:49:01 +0200 Subject: [PATCH] fix(input-migration): consider whitespaces when matching against template --- .../__snapshots__/generator.spec.ts.snap | 30 +++++++++++++++++++ .../convert-signal-inputs/generator.spec.ts | 18 +++++++++++ .../migrate-signals-in-template.ts | 2 +- 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/libs/plugin/src/generators/convert-signal-inputs/__snapshots__/generator.spec.ts.snap b/libs/plugin/src/generators/convert-signal-inputs/__snapshots__/generator.spec.ts.snap index 5c2793b11..356078f0b 100644 --- a/libs/plugin/src/generators/convert-signal-inputs/__snapshots__/generator.spec.ts.snap +++ b/libs/plugin/src/generators/convert-signal-inputs/__snapshots__/generator.spec.ts.snap @@ -4,6 +4,11 @@ exports[`convertSignalInputsGenerator should convert properly 1`] = ` " import { Component, Input, input } from '@angular/core'; +interface Person { + name: string; + lastname: string; +} + @Component({ template: \` @@ -51,6 +56,7 @@ import { Component, Input, input } from '@angular/core'; + {{ 'someNormalTextnormalInput' | translate: 'normalInput' }} @@ -69,6 +75,17 @@ import { Component, Input, input } from '@angular/core';

{{ data().normalInput }}

+

{{person()?.name ? person().name : ""}}

+

{{ + person().name +}}

+ + + {{title}} + + + + \` }) export class MyCmp { @@ -85,6 +102,7 @@ export class MyCmp { requiredWithAliasAndTransform = input.required({ alias: 'transformedRequiredAlias', transform: numberAttribute }); withTransformWithoutType = input(booleanAttribute(false), { transform: booleanAttribute }); requiredWithTransformWithoutType = input(numberAttribute(1), { transform: numberAttribute }); + person = input(); @Input() set leaveMeAlone(value: number) { console.log('setter', value); @@ -396,6 +414,7 @@ exports[`convertSignalInputsGenerator should convert properly for templateUrl 2` +
{{ 'someNormalTextnormalInput' | translate: 'normalInput' }} @@ -413,6 +432,17 @@ exports[`convertSignalInputsGenerator should convert properly for templateUrl 2`

{{ data().normalInput }}

+ +

{{person?.name ? person.name : ""}}

+

{{ + person.name +}}

+ + + {{title}} + + + " `; diff --git a/libs/plugin/src/generators/convert-signal-inputs/generator.spec.ts b/libs/plugin/src/generators/convert-signal-inputs/generator.spec.ts index da032ef10..20f61c472 100644 --- a/libs/plugin/src/generators/convert-signal-inputs/generator.spec.ts +++ b/libs/plugin/src/generators/convert-signal-inputs/generator.spec.ts @@ -48,6 +48,7 @@ const template = `
{{ inputWithoutType }}
+
{{ 'someNormalTextnormalInput' | translate: 'normalInput' }} @@ -65,6 +66,17 @@ const template = `
{{ inputWithoutType }}

{{ data().normalInput }}

+ +

{{person?.name ? person.name : ""}}

+

{{ + person.name +}}

+ + + {{title}} + + + `; const filesMap = { @@ -91,6 +103,11 @@ export class MyCmp { component: ` import { Component, Input } from '@angular/core'; +interface Person { + name: string; + lastname: string; +} + @Component({ template: \` ${template} @@ -110,6 +127,7 @@ export class MyCmp { @Input({ required: true, alias: 'transformedRequiredAlias', transform: numberAttribute }) requiredWithAliasAndTransform!: number; @Input({ transform: booleanAttribute }) withTransformWithoutType = false; @Input({ transform: numberAttribute }) requiredWithTransformWithoutType = 1; + @Input() person: Person; @Input() set leaveMeAlone(value: number) { console.log('setter', value); diff --git a/libs/plugin/src/generators/shared-utils/migrate-signals-in-template.ts b/libs/plugin/src/generators/shared-utils/migrate-signals-in-template.ts index 19bc931e4..e49f4eb0a 100644 --- a/libs/plugin/src/generators/shared-utils/migrate-signals-in-template.ts +++ b/libs/plugin/src/generators/shared-utils/migrate-signals-in-template.ts @@ -291,7 +291,7 @@ function replaceVariablesInsideInterpolation( variables: string[], ): string { // find all interpolations and replace the variables for each interpolation - value = value.replaceAll(/{{(.*?)}}/g, (match) => { + value = value.replaceAll(/{{\s*(.*?)\s*}}/g, (match) => { const usedVars = []; for (const variable of variables) { if (match.includes(variable)) {