Skip to content

Commit c5980d6

Browse files
cexbrayatalxhub
authored andcommitted
fix(core): handle aliased index with no space in control flow migration (angular#52444)
The current regexp supposes that there is at least one space between the `=` and the aliased variable. As it is possible to write `let myIndex=index`, this commit updates the regexp to handle such a case. PR Close angular#52444
1 parent 53d9fd5 commit c5980d6

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ function buildIfThenElseBlock(
315315
}
316316

317317
function migrateNgFor(etm: ElementToMigrate, tmpl: string, offset: number): Result {
318-
const aliasWithEqualRegexp = /=\s+(count|index|first|last|even|odd)/gm;
318+
const aliasWithEqualRegexp = /=\s*(count|index|first|last|even|odd)/gm;
319319
const aliasWithAsRegexp = /(count|index|first|last|even|odd)\s+as/gm;
320320
const aliases = [];
321321
const lbString = etm.hasLineBreaks ? lb : '';

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,6 @@ describe('control flow migration', () => {
869869
'template: `<ul>@for (itm of items; track trackMeFn($index, itm)) {<li>{{itm.text}}</li>}</ul>`');
870870
});
871871

872-
873872
it('should migrate with an index alias', async () => {
874873
writeFile('/comp.ts', `
875874
import {Component} from '@angular/core';
@@ -895,6 +894,31 @@ describe('control flow migration', () => {
895894
'template: `<ul>@for (itm of items; track itm; let index = $index) {<li>{{itm.text}}</li>}</ul>`');
896895
});
897896

897+
it('should migrate with an index alias with no spaces', async () => {
898+
writeFile('/comp.ts', `
899+
import {Component} from '@angular/core';
900+
import {NgFor} from '@angular/common';
901+
interface Item {
902+
id: number;
903+
text: string;
904+
}
905+
906+
@Component({
907+
imports: [NgFor],
908+
template: \`<ul><li *ngFor="let itm of items; let index=index">{{itm.text}}</li></ul>\`
909+
})
910+
class Comp {
911+
items: Item[] = [{id: 1, text: 'blah'},{id: 2, text: 'stuff'}];
912+
}
913+
`);
914+
915+
await runMigration();
916+
const content = tree.readContent('/comp.ts');
917+
918+
expect(content).toContain(
919+
'template: `<ul>@for (itm of items; track itm; let index = $index) {<li>{{itm.text}}</li>}</ul>`');
920+
});
921+
898922
it('should migrate with alias declared with as', async () => {
899923
writeFile('/comp.ts', `
900924
import {Component} from '@angular/core';

0 commit comments

Comments
 (0)