Skip to content

Commit 1f5c8bf

Browse files
thePunderWomanalxhub
authored andcommitted
fix(migrations): ensure we do not overwrite prior template replacements in migration (#53393)
This fixes a bug where only the last replacement of a template placeholder would apply. fixes: #53383 PR Close #53393
1 parent 70a442e commit 1f5c8bf

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,14 +369,13 @@ export function processNgTemplates(template: string): {migrated: string, err: Er
369369
function replaceRemainingPlaceholders(template: string): string {
370370
const replaceRegex = new RegExp(`θ.*δ`, 'g');
371371
const placeholders = [...template.matchAll(replaceRegex)];
372-
let migrated = template;
373372
for (let ph of placeholders) {
374373
const placeholder = ph[0];
375374
const name = placeholder.slice(1, placeholder.length - 1);
376-
migrated =
375+
template =
377376
template.replace(placeholder, `<ng-template [ngTemplateOutlet]="${name}"></ng-template>`);
378377
}
379-
return migrated;
378+
return template;
380379
}
381380

382381
/**

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4149,6 +4149,58 @@ describe('control flow migration', () => {
41494149
`<ng-template #empty>Empty</ng-template>`,
41504150
].join('\n'));
41514151
});
4152+
4153+
it('should replace all instances of template placeholders', async () => {
4154+
writeFile('/comp.ts', `
4155+
import {Component} from '@angular/core';
4156+
import {NgIf} from '@angular/common';
4157+
4158+
@Component({
4159+
templateUrl: './comp.html'
4160+
})
4161+
class Comp {
4162+
show = false;
4163+
}
4164+
`);
4165+
4166+
writeFile('/comp.html', [
4167+
`<div *ngIf="condition; else otherTemplate">`,
4168+
` <ng-container *ngIf="!defaultTemplate; else defaultTemplate">`,
4169+
` Hello!`,
4170+
` </ng-container>`,
4171+
`</div>`,
4172+
`<ng-template #otherTemplate>`,
4173+
` <div>`,
4174+
` <ng-container *ngIf="!defaultTemplate; else defaultTemplate">`,
4175+
` Hello again!`,
4176+
` </ng-container>`,
4177+
` </div>`,
4178+
`</ng-template>`,
4179+
].join('\n'));
4180+
4181+
await runMigration();
4182+
const content = tree.readContent('/comp.html');
4183+
4184+
expect(content).toBe([
4185+
`@if (condition) {`,
4186+
` <div>`,
4187+
` @if (!defaultTemplate) {`,
4188+
` Hello!`,
4189+
` } @else {`,
4190+
` <ng-template [ngTemplateOutlet]="defaultTemplate"></ng-template>`,
4191+
` }`,
4192+
` </div>`,
4193+
`} @else {`,
4194+
` <div>`,
4195+
` @if (!defaultTemplate) {`,
4196+
` Hello again!`,
4197+
` } @else {`,
4198+
` <ng-template [ngTemplateOutlet]="defaultTemplate"></ng-template>`,
4199+
` }`,
4200+
` </div>`,
4201+
`}\n`,
4202+
].join('\n'));
4203+
});
41524204
});
41534205

41544206
describe('formatting', () => {

0 commit comments

Comments
 (0)