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',