Skip to content

Commit 22b95de

Browse files
thePunderWomanatscott
authored andcommitted
fix(migrations): cf migration - fix bug in attribute formatting (#53636)
The formatting that would preserve attribute indents completely missed attributes that start on new lines rather than the same line as the opening element. PR Close #53636
1 parent 8e21787 commit 22b95de

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

packages/core/schematics/ng-generate/control-flow-migration/util.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -671,10 +671,12 @@ export function formatTemplate(tmpl: string, templateType: string): string {
671671
}
672672

673673
// if a line ends in an unclosed attribute, we need to note that and close it later
674-
if (!inAttribute && openAttrDoubleRegex.test(line)) {
674+
const isOpenDoubleAttr = openAttrDoubleRegex.test(line);
675+
const isOpenSingleAttr = openAttrSingleRegex.test(line);
676+
if (!inAttribute && isOpenDoubleAttr) {
675677
inAttribute = true;
676678
isDoubleQuotes = true;
677-
} else if (!inAttribute && openAttrSingleRegex.test(line)) {
679+
} else if (!inAttribute && isOpenSingleAttr) {
678680
inAttribute = true;
679681
isDoubleQuotes = false;
680682
}
@@ -684,8 +686,9 @@ export function formatTemplate(tmpl: string, templateType: string): string {
684686
mindent + (line.trim() !== '' ? indent : '') + line.trim();
685687
formatted.push(newLine);
686688

687-
if ((inAttribute && isDoubleQuotes && closeAttrDoubleRegex.test(line)) ||
688-
(inAttribute && !isDoubleQuotes && closeAttrSingleRegex.test(line))) {
689+
if (!isOpenDoubleAttr && !isOpenSingleAttr &&
690+
((inAttribute && isDoubleQuotes && closeAttrDoubleRegex.test(line)) ||
691+
(inAttribute && !isDoubleQuotes && closeAttrSingleRegex.test(line)))) {
689692
inAttribute = false;
690693
}
691694

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4776,6 +4776,13 @@ describe('control flow migration', () => {
47764776
` with cool things">`,
47774777
` Content here`,
47784778
`</span>`,
4779+
`<span`,
4780+
` i18n-message="this is a multi-`,
4781+
` line attribute`,
4782+
` that starts`,
4783+
` on a newline">`,
4784+
` Different Content`,
4785+
`</span>`,
47794786
].join('\n'));
47804787

47814788
await runMigration();
@@ -4789,6 +4796,13 @@ describe('control flow migration', () => {
47894796
` with cool things">`,
47904797
` Content here`,
47914798
`</span>`,
4799+
`<span`,
4800+
` i18n-message="this is a multi-`,
4801+
` line attribute`,
4802+
` that starts`,
4803+
` on a newline">`,
4804+
` Different Content`,
4805+
`</span>`,
47924806
].join('\n');
47934807

47944808
expect(actual).toBe(expected);
@@ -4815,6 +4829,13 @@ describe('control flow migration', () => {
48154829
` with cool things'>`,
48164830
` Content here`,
48174831
`</span>`,
4832+
`<span`,
4833+
` i18n-message='this is a multi-`,
4834+
` line attribute`,
4835+
` that starts`,
4836+
` on a newline'>`,
4837+
` Different here`,
4838+
`</span>`,
48184839
].join('\n'));
48194840

48204841
await runMigration();
@@ -4828,6 +4849,13 @@ describe('control flow migration', () => {
48284849
` with cool things'>`,
48294850
` Content here`,
48304851
`</span>`,
4852+
`<span`,
4853+
` i18n-message='this is a multi-`,
4854+
` line attribute`,
4855+
` that starts`,
4856+
` on a newline'>`,
4857+
` Different here`,
4858+
`</span>`,
48314859
].join('\n');
48324860

48334861
expect(actual).toBe(expected);

0 commit comments

Comments
 (0)