Skip to content

Commit 2b3d3b0

Browse files
fix(migrations): CF migration log warning when collection aliasing detected in @for (#53238)
This logs a warning when an ngFor has a collection aliased, which is not supported with new syntax. fixes: #53233 PR Close #53238
1 parent 6be8804 commit 2b3d3b0

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ function migrateStandardNgFor(etm: ElementToMigrate, tmpl: string, offset: numbe
9292

9393
// first portion should always be the loop definition prefixed with `let`
9494
const condition = parts[0].replace('let ', '');
95+
if (condition.indexOf(' as ') > -1) {
96+
let errorMessage = `Found an aliased collection on an ngFor: "${condition}".` +
97+
' Collection aliasing is not supported with @for.' +
98+
' Refactor the code to remove the `as` alias and re-run the migration.';
99+
throw new Error(errorMessage);
100+
}
95101
const loopVar = condition.split(' of ')[0];
96102
let trackBy = loopVar;
97103
let aliasedIndex: string|null = null;

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3259,6 +3259,34 @@ describe('control flow migration', () => {
32593259
`A duplicate ng-template name "#elseTmpl" was found. ` +
32603260
`The control flow migration requires unique ng-template names within a component.`);
32613261
});
3262+
3263+
it('should log a migration error when collection aliasing is detected in ngFor', async () => {
3264+
writeFile('/comp.ts', `
3265+
import {Component} from '@angular/core';
3266+
import {NgIf} from '@angular/common';
3267+
3268+
@Component({
3269+
imports: [NgIf],
3270+
templateUrl: './comp.html'
3271+
})
3272+
class Comp {
3273+
toggle = false;
3274+
}
3275+
`);
3276+
3277+
writeFile('./comp.html', [
3278+
`<div *ngFor="let item of list$ | async as list;">Content</div>`,
3279+
].join('\n'));
3280+
3281+
await runMigration();
3282+
tree.readContent('/comp.ts');
3283+
3284+
expect(warnOutput.join(' '))
3285+
.toContain(
3286+
`Found an aliased collection on an ngFor: "item of list$ | async as list". ` +
3287+
`Collection aliasing is not supported with @for. ` +
3288+
`Refactor the code to remove the \`as\` alias and re-run the migration.`);
3289+
});
32623290
});
32633291

32643292
describe('template', () => {

0 commit comments

Comments
 (0)