Skip to content

Commit 26e40c7

Browse files
thePunderWomandylhunn
authored andcommitted
fix(migrations): CF Migration fix missing alias for bound ngifs (#53296)
Empty aliases are considered the item in an ngFor, and ngIf was skipping that value. fixes: #53291 PR Close #53296
1 parent 493c412 commit 26e40c7

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ function migrateNgIf(etm: ElementToMigrate, tmpl: string, offset: number): Resul
9191
function buildIfBlock(etm: ElementToMigrate, tmpl: string, offset: number): Result {
9292
const aliasAttrs = etm.aliasAttrs!;
9393
const aliases = [...aliasAttrs.aliases.keys()];
94+
if (aliasAttrs.item) {
95+
aliases.push(aliasAttrs.item);
96+
}
9497

9598
// includes the mandatory semicolon before as
9699
const lbString = etm.hasLineBreaks ? '\n' : '';
@@ -137,6 +140,9 @@ function buildStandardIfElseBlock(
137140
function buildBoundIfElseBlock(etm: ElementToMigrate, tmpl: string, offset: number): Result {
138141
const aliasAttrs = etm.aliasAttrs!;
139142
const aliases = [...aliasAttrs.aliases.keys()];
143+
if (aliasAttrs.item) {
144+
aliases.push(aliasAttrs.item);
145+
}
140146

141147
// includes the mandatory semicolon before as
142148
let condition = etm.attr.value.replace(' as ', '; as ');

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,35 @@ describe('control flow migration', () => {
356356
].join('\n'));
357357
});
358358

359+
it('should migrate an if case as a binding with let variable with no value', async () => {
360+
writeFile('/comp.ts', `
361+
import {Component} from '@angular/core';
362+
import {NgIf} from '@angular/common';
363+
364+
@Component({
365+
templateUrl: './comp.html'
366+
})
367+
class Comp {
368+
show = false;
369+
}
370+
`);
371+
372+
writeFile('/comp.html', [
373+
`<ng-template [ngIf]="viewModel$ | async" let-vm>`,
374+
` {{vm | json}}`,
375+
`</ng-template>`,
376+
].join('\n'));
377+
378+
await runMigration();
379+
const content = tree.readContent('/comp.html');
380+
381+
expect(content).toBe([
382+
`@if (viewModel$ | async; as vm) {`,
383+
` {{vm | json}}`,
384+
`}`,
385+
].join('\n'));
386+
});
387+
359388
it('should migrate an if else case as bindings', async () => {
360389
writeFile('/comp.ts', `
361390
import {Component} from '@angular/core';

0 commit comments

Comments
 (0)