Skip to content

Commit 2edc82a

Browse files
lukasmattacrisbeto
authored andcommitted
fix(migrations): Do not remove a template if it is referenced even with a trailing semilocon
This commit fixes a behavior where under certain conditions, the migration script ignored a template reference with a trailing semicolon and incorrectly removed the definition of a referenced template. Fixes #64741 (cherry picked from commit 64cb085)
1 parent adae565 commit 2edc82a

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-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 === templateName
554+
attr.value?.split(';')[0] === templateName
555555
) {
556556
isReferencedInTemplateOutlet = true;
557557
}

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,52 @@ describe('control flow migration (ng update)', () => {
13951395
);
13961396
});
13971397

1398+
it('should migrate but not remove ng-templates when referenced elsewhere with a trailing semicolon', async () => {
1399+
writeFile(
1400+
'/comp.ts',
1401+
`
1402+
import {Component} from '@angular/core';
1403+
import {NgIf} from '@angular/common';
1404+
1405+
@Component({
1406+
templateUrl: './comp.html'
1407+
})
1408+
class Comp {
1409+
show = false;
1410+
}
1411+
`,
1412+
);
1413+
1414+
writeFile(
1415+
'/comp.html',
1416+
[
1417+
`<div>`,
1418+
`<span *ngIf="show; then thenBlock; else elseBlock">Ignored</span>`,
1419+
`<ng-template #thenBlock><div>THEN Stuff</div></ng-template>`,
1420+
`<ng-template #elseBlock>Else Content</ng-template>`,
1421+
`</div>`,
1422+
`<ng-container *ngTemplateOutlet="elseBlock;"></ng-container>`,
1423+
].join('\n'),
1424+
);
1425+
1426+
await runMigration();
1427+
const content = tree.readContent('/comp.html');
1428+
1429+
expect(content).toBe(
1430+
[
1431+
`<div>`,
1432+
` @if (show) {`,
1433+
` <div>THEN Stuff</div>`,
1434+
` } @else {`,
1435+
` Else Content`,
1436+
` }`,
1437+
` <ng-template #elseBlock>Else Content</ng-template>`,
1438+
`</div>`,
1439+
`<ng-container *ngTemplateOutlet="elseBlock;"></ng-container>`,
1440+
].join('\n'),
1441+
);
1442+
});
1443+
13981444
it('should not remove ng-templates used by other directives', async () => {
13991445
writeFile(
14001446
'/comp.ts',

0 commit comments

Comments
 (0)