Skip to content

Commit

Permalink
fix(core): handle aliased index with no space in control flow migrati…
Browse files Browse the repository at this point in the history
…on (#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 #52444
  • Loading branch information
cexbrayat authored and alxhub committed Oct 31, 2023
1 parent abb4a9b commit b9ea2d6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Expand Up @@ -315,7 +315,7 @@ function buildIfThenElseBlock(
}

function migrateNgFor(etm: ElementToMigrate, tmpl: string, offset: number): Result {
const aliasWithEqualRegexp = /=\s+(count|index|first|last|even|odd)/gm;
const aliasWithEqualRegexp = /=\s*(count|index|first|last|even|odd)/gm;
const aliasWithAsRegexp = /(count|index|first|last|even|odd)\s+as/gm;
const aliases = [];
const lbString = etm.hasLineBreaks ? lb : '';
Expand Down
26 changes: 25 additions & 1 deletion packages/core/schematics/test/control_flow_migration_spec.ts
Expand Up @@ -869,7 +869,6 @@ describe('control flow migration', () => {
'template: `<ul>@for (itm of items; track trackMeFn($index, itm)) {<li>{{itm.text}}</li>}</ul>`');
});


it('should migrate with an index alias', async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';
Expand All @@ -895,6 +894,31 @@ 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 index alias with no spaces', async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';
import {NgFor} from '@angular/common';
interface Item {
id: number;
text: string;
}
@Component({
imports: [NgFor],
template: \`<ul><li *ngFor="let itm of items; let index=index">{{itm.text}}</li></ul>\`
})
class Comp {
items: Item[] = [{id: 1, text: 'blah'},{id: 2, text: 'stuff'}];
}
`);

await runMigration();
const content = tree.readContent('/comp.ts');

expect(content).toContain(
'template: `<ul>@for (itm of items; track itm; let index = $index) {<li>{{itm.text}}</li>}</ul>`');
});

it('should migrate with alias declared with as', async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';
Expand Down

0 comments on commit b9ea2d6

Please sign in to comment.