Skip to content

Commit a6275cf

Browse files
thePunderWomandylhunn
authored andcommitted
fix(migrations): CF Migration - Fix case of aliases on i18n ng-templates preventing removal (#53299)
i18n template removal expected no other attributes to be present, but if a bound ngIf is present with aliases and i18n, that is more than what was expected. Now it should safely remove them appropriately. fixes: #53289 PR Close #53299
1 parent 14e6653 commit a6275cf

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,9 @@ export function getOriginals(etm: ElementToMigrate, tmpl: string, offset: number
411411
}
412412

413413
function isI18nTemplate(etm: ElementToMigrate, i18nAttr: Attribute|undefined): boolean {
414-
return etm.el.name === 'ng-template' && i18nAttr !== undefined &&
415-
(etm.el.attrs.length === 2 || (etm.el.attrs.length === 3 && etm.elseAttr !== undefined));
414+
let attrCount = countAttributes(etm);
415+
const safeToRemove = etm.el.attrs.length === attrCount + (i18nAttr !== undefined ? 1 : 0);
416+
return etm.el.name === 'ng-template' && i18nAttr !== undefined && safeToRemove;
416417
}
417418

418419
function isRemovableContainer(etm: ElementToMigrate): boolean {

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,40 @@ describe('control flow migration', () => {
580580
].join('\n'));
581581
});
582582

583+
it('should migrate a bound if case on an ng-template with i18n', async () => {
584+
writeFile('/comp.ts', `
585+
import {Component} from '@angular/core';
586+
import {NgIf} from '@angular/common';
587+
588+
@Component({
589+
templateUrl: './comp.html'
590+
})
591+
class Comp {
592+
show = false;
593+
}
594+
`);
595+
596+
writeFile('/comp.html', [
597+
`<ng-template`,
598+
` [ngIf]="data$ | async"`,
599+
` let-data="ngIf"`,
600+
` i18n="@@i18n-label">`,
601+
` {{ data }}`,
602+
`</ng-template>`,
603+
].join('\n'));
604+
605+
await runMigration();
606+
const content = tree.readContent('/comp.html');
607+
608+
expect(content).toBe([
609+
`@if (data$ | async; as data) {`,
610+
` <ng-container i18n="@@i18n-label">`,
611+
` {{ data }}`,
612+
` </ng-container>`,
613+
`}`,
614+
].join('\n'));
615+
});
616+
583617
it('should migrate an if case with an ng-container with empty i18n', async () => {
584618
writeFile('/comp.ts', `
585619
import {Component} from '@angular/core';

0 commit comments

Comments
 (0)