Skip to content

Commit ffe9b1f

Browse files
cexbrayatpkozlowski-opensource
authored andcommitted
fix(core): handle for alias with as in control flow migration (#52183)
This adds the support of `let index as myIndex` in `*ngFor` for the control flow migration. Before: `@for (itm of items; track itm)` After: `@for (itm of items; track itm; let myIndex = $index)` PR Close #52183
1 parent 0792424 commit ffe9b1f

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ export function migrateTemplate(template: string): string|null {
169169

170170
function migrateNgFor(
171171
etm: ElementToMigrate, tmpl: string, offset: number): {tmpl: string, offset: number} {
172-
const aliasRegexp = /=\s+(count|index|first|last|even|odd)/gm;
172+
const aliasWithEqualRegexp = /=\s+(count|index|first|last|even|odd)/gm;
173+
const aliasWithAsRegexp = /(count|index|first|last|even|odd)\s+as/gm;
173174
const aliases = [];
174175

175176
const parts = etm.attr.value.split(';');
@@ -187,10 +188,20 @@ function migrateNgFor(
187188
trackBy = `${trackByFn}($index, ${loopVar})`;
188189
}
189190
// aliases
190-
if (part.match(aliasRegexp)) {
191+
// declared with `let myIndex = index`
192+
if (part.match(aliasWithEqualRegexp)) {
193+
// 'let myIndex = index' -> ['let myIndex', 'index']
191194
const aliasParts = part.split('=');
195+
// -> 'let myIndex = $index'
192196
aliases.push(` ${aliasParts[0].trim()} = $${aliasParts[1].trim()}`);
193197
}
198+
// declared with `index as myIndex`
199+
if (part.match(aliasWithAsRegexp)) {
200+
// 'index as myIndex' -> ['index', 'myIndex']
201+
const aliasParts = part.split(/\s+as\s+/);
202+
// -> 'let myIndex = $index'
203+
aliases.push(` let ${aliasParts[1].trim()} = $${aliasParts[0].trim()}`);
204+
}
194205
}
195206

196207
const aliasStr = (aliases.length > 0) ? `;${aliases.join(';')}` : '';

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,31 @@ describe('control flow migration', () => {
521521
'template: `<ul>@for (itm of items; track itm; let index = $index) {<li>{{itm.text}}</li>}</ul>`');
522522
});
523523

524+
it('should migrate with alias declared with as', async () => {
525+
writeFile('/comp.ts', `
526+
import {Component} from '@angular/core';
527+
import {NgFor} from '@angular/common';
528+
interface Item {
529+
id: number;
530+
text: string;
531+
}
532+
533+
@Component({
534+
imports: [NgFor],
535+
template: \`<ul><li *ngFor="let itm of items; index as myIndex">{{itm.text}}</li></ul>\`
536+
})
537+
class Comp {
538+
items: Item[] = [{id: 1, text: 'blah'},{id: 2, text: 'stuff'}];
539+
}
540+
`);
541+
542+
await runMigration();
543+
const content = tree.readContent('/comp.ts');
544+
545+
expect(content).toContain(
546+
'template: `<ul>@for (itm of items; track itm; let myIndex = $index) {<li>{{itm.text}}</li>}</ul>`');
547+
});
548+
524549
it('should migrate with multiple aliases', async () => {
525550
writeFile('/comp.ts', `
526551
import {Component} from '@angular/core';

0 commit comments

Comments
 (0)