Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(migrations): Fixes a bug in the ngFor pre-v5 alias translation #52531

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ function migrateNgFor(etm: ElementToMigrate, tmpl: string, offset: number): Resu
// if the aliased variable is the index, then we store it
if (aliasParts[1].trim() === 'index') {
// 'let myIndex' -> 'myIndex'
aliasedIndex = aliasParts[0].trim().split(/\s+as\s+/)[1];
aliasedIndex = aliasParts[0].replace('let', '').trim();
}
}
// declared with `index as myIndex`
Expand Down
47 changes: 47 additions & 0 deletions packages/core/schematics/test/control_flow_migration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,53 @@ describe('control flow migration', () => {
'template: `<ul>@for (itm of items; track itm; let index = $index) {<li>{{itm.text}}</li>}</ul>`');
});

it('should migrate with an old style alias', async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';
import {NgFor} from '@angular/common';
interface Item {
id: number;
text: string;
}

@Component({
imports: [NgFor],
templateUrl: 'comp.html',
})
class Comp {
items: Item[] = [{id: 1, text: 'blah'},{id: 2, text: 'stuff'}];
}
`);

writeFile('/comp.html', [
`<tbody>`,
` <tr *ngFor="let row of field(); let y = index; trackBy: trackByIndex">`,
` <td`,
` *ngFor="let cell of row; let x = index; trackBy: trackByIndex"`,
` ></td>`,
` </tr>`,
`</tbody>`,
].join('\n'));

await runMigration();
const actual = tree.readContent('/comp.html');

const expected = [
`<tbody>`,
` @for (row of field(); track trackByIndex(y, row); let y = $index) {`,
` <tr>`,
` @for (cell of row; track trackByIndex(x, cell); let x = $index) {`,
` <td\n `,
` ></td>`,
`}`,
` </tr>`,
`}`,
`</tbody>`,
].join('\n');

expect(actual).toBe(expected);
});

it('should migrate an index alias after an expression containing commas', async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';
Expand Down