Skip to content

Commit a45e6b2

Browse files
lukasmattaatscott
authored andcommitted
fix(migrations): Prevent removal of templates referenced with preceding whitespace characters
In #64745, a fix was introduced for templates referenced with a trailing semicolon. However, templates are still incorrectly removed when there are whitespace characters before the template name. This commit updates the control flow migration logic to ensure templates referenced with preceding whitespace are not removed. Fixes #64854 (cherry picked from commit 5b210e9)
1 parent c374e80 commit a45e6b2

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

packages/core/schematics/migrations/control-flow-migration/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ function analyzeTemplateUsage(nodes: any[], templateName: string): TemplateUsage
551551
for (const attr of node.attrs) {
552552
if (
553553
(attr.name === '*ngTemplateOutlet' || attr.name === '[ngTemplateOutlet]') &&
554-
attr.value?.split(';')[0] === templateName
554+
attr.value?.split(';')[0]?.trim() === templateName
555555
) {
556556
isReferencedInTemplateOutlet = true;
557557
}

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,55 @@ describe('control flow migration (ng update)', () => {
14411441
);
14421442
});
14431443

1444+
it('should migrate but not remove ng-templates when referenced elsewhere with a trailing semicolon including leading whitespace character', async () => {
1445+
writeFile(
1446+
'/comp.ts',
1447+
`
1448+
import {Component} from '@angular/core';
1449+
import {NgIf} from '@angular/common';
1450+
1451+
@Component({
1452+
templateUrl: './comp.html'
1453+
})
1454+
class Comp {
1455+
show = false;
1456+
}
1457+
`,
1458+
);
1459+
1460+
writeFile(
1461+
'/comp.html',
1462+
[
1463+
`<div>`,
1464+
`<span *ngIf="show; then thenBlock; else elseBlock">Ignored</span>`,
1465+
`<ng-template #thenBlock><div>THEN Stuff</div></ng-template>`,
1466+
`<ng-template #elseBlock let-ctx>{{ ctx }} Else Content</ng-template>`,
1467+
`</div>`,
1468+
`<ng-container *ngTemplateOutlet="
1469+
elseBlock;
1470+
context: $implicit: 'Hello'"></ng-container>`,
1471+
].join('\n'),
1472+
);
1473+
1474+
await runMigration();
1475+
const content = tree.readContent('/comp.html');
1476+
expect(content).toBe(
1477+
[
1478+
`<div>`,
1479+
` @if (show) {`,
1480+
` <div>THEN Stuff</div>`,
1481+
` } @else {`,
1482+
` {{ ctx }} Else Content`,
1483+
` }`,
1484+
` <ng-template #elseBlock let-ctx>{{ ctx }} Else Content</ng-template>`,
1485+
`</div>`,
1486+
`<ng-container *ngTemplateOutlet="
1487+
elseBlock;
1488+
context: $implicit: 'Hello'"></ng-container>`,
1489+
].join('\n'),
1490+
);
1491+
});
1492+
14441493
it('should not remove ng-templates used by other directives', async () => {
14451494
writeFile(
14461495
'/comp.ts',

0 commit comments

Comments
 (0)