Skip to content

Commit 45064f1

Browse files
thePunderWomandylhunn
authored andcommitted
fix(migrations): CF migration - ensure NgIfElse attributes are properly removed (#53298)
the attribute in question was assumed to be at the start of the replaced content, but it could be later, too. fixes: #53288 PR Close #53298
1 parent a6275cf commit 45064f1

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,17 +472,18 @@ export function getMainBlock(etm: ElementToMigrate, tmpl: string, offset: number
472472

473473
// the beginning of the updated string in the main block, for example: <div some="attributes">
474474
let start = tmpl.slice(etm.start(offset), attrStart) + tmpl.slice(valEnd, childStart);
475+
// the middle is the actual contents of the element
476+
const middle = tmpl.slice(childStart, childEnd);
477+
// the end is the closing part of the element, example: </div>
478+
let end = tmpl.slice(childEnd, etm.end(offset));
475479

476480
if (etm.shouldRemoveElseAttr()) {
477481
// this removes a bound ngIfElse attribute that's no longer needed
482+
// this could be on the start or end
478483
start = start.replace(etm.getElseAttrStr(), '');
484+
end = end.replace(etm.getElseAttrStr(), '');
479485
}
480486

481-
// the middle is the actual contents of the element
482-
const middle = tmpl.slice(childStart, childEnd);
483-
// the end is the closing part of the element, example: </div>
484-
const end = tmpl.slice(childEnd, etm.end(offset));
485-
486487
return {start, middle, end};
487488
}
488489

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,45 @@ describe('control flow migration', () => {
645645
].join('\n'));
646646
});
647647

648+
it('should migrate a bound NgIfElse case with ng-templates and remove all unnecessary attributes',
649+
async () => {
650+
writeFile('/comp.ts', `
651+
import {Component} from '@angular/core';
652+
import {NgIf} from '@angular/common';
653+
654+
@Component({
655+
templateUrl: './comp.html'
656+
})
657+
class Comp {
658+
show = false;
659+
}
660+
`);
661+
662+
writeFile('/comp.html', [
663+
`<ng-template`,
664+
` [ngIf]="fooTemplate"`,
665+
` [ngIfElse]="barTemplate"`,
666+
` [ngTemplateOutlet]="fooTemplate"`,
667+
`></ng-template>`,
668+
`<ng-template #fooTemplate>Foo</ng-template>`,
669+
`<ng-template #barTemplate>Bar</ng-template>`,
670+
].join('\n'));
671+
672+
await runMigration();
673+
const content = tree.readContent('/comp.html');
674+
675+
expect(content).toBe([
676+
`@if (fooTemplate) {`,
677+
` <ng-template`,
678+
` [ngTemplateOutlet]="fooTemplate"`,
679+
` ></ng-template>`,
680+
`} @else {`,
681+
` Bar`,
682+
`}`,
683+
`<ng-template #fooTemplate>Foo</ng-template>\n`,
684+
].join('\n'));
685+
});
686+
648687
it('should migrate a bound NgIfThenElse case with ng-templates with i18n', async () => {
649688
writeFile('/comp.ts', `
650689
import {Component} from '@angular/core';

0 commit comments

Comments
 (0)