From 95344c19f3981d116546b0de3a72c8b5f66a5a87 Mon Sep 17 00:00:00 2001 From: Lukas Matta Date: Wed, 29 Oct 2025 00:14:57 +0100 Subject: [PATCH] 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 64cb08529d535591ee8f69bd0a3d7f0b66073e31) --- .../migrations/control-flow-migration/util.ts | 2 +- .../test/control_flow_migration_spec.ts | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/packages/core/schematics/migrations/control-flow-migration/util.ts b/packages/core/schematics/migrations/control-flow-migration/util.ts index 294aa2b2716..26516c9ef8b 100644 --- a/packages/core/schematics/migrations/control-flow-migration/util.ts +++ b/packages/core/schematics/migrations/control-flow-migration/util.ts @@ -520,7 +520,7 @@ function analyzeTemplateUsage(nodes: any[], templateName: string): TemplateUsage for (const attr of node.attrs) { if ( (attr.name === '*ngTemplateOutlet' || attr.name === '[ngTemplateOutlet]') && - attr.value === templateName + attr.value?.split(';')[0] === templateName ) { isReferencedInTemplateOutlet = true; } diff --git a/packages/core/schematics/test/control_flow_migration_spec.ts b/packages/core/schematics/test/control_flow_migration_spec.ts index 888b6374c6b..8d478a8b878 100644 --- a/packages/core/schematics/test/control_flow_migration_spec.ts +++ b/packages/core/schematics/test/control_flow_migration_spec.ts @@ -1395,6 +1395,52 @@ describe('control flow migration (ng update)', () => { ); }); + it('should migrate but not remove ng-templates when referenced elsewhere with a trailing semicolon', async () => { + writeFile( + '/comp.ts', + ` + import {Component} from '@angular/core'; + import {NgIf} from '@angular/common'; + + @Component({ + templateUrl: './comp.html' + }) + class Comp { + show = false; + } + `, + ); + + writeFile( + '/comp.html', + [ + `
`, + `Ignored`, + `
THEN Stuff
`, + `Else Content`, + `
`, + ``, + ].join('\n'), + ); + + await runMigration(); + const content = tree.readContent('/comp.html'); + + expect(content).toBe( + [ + `
`, + ` @if (show) {`, + `
THEN Stuff
`, + ` } @else {`, + ` Else Content`, + ` }`, + ` Else Content`, + `
`, + ``, + ].join('\n'), + ); + }); + it('should not remove ng-templates used by other directives', async () => { writeFile( '/comp.ts',