fix(migrations): fix regexp for else and then in cf migration (#53257)

The regexp for then and else did not ignore alphanumeric characters prior to the then and else. So if a string contained then, for example Authentication, it would incorrectly match as a then clause.

fixes: #53252

PR Close #53257
This commit is contained in:
Jessica Janiuk 2023-11-29 11:26:56 -05:00 committed by Dylan Hunn
parent f4a96a9160
commit 03e2f1bb25
2 changed files with 33 additions and 2 deletions

View file

@ -68,8 +68,8 @@ export function migrateIf(template: string):
}
function migrateNgIf(etm: ElementToMigrate, tmpl: string, offset: number): Result {
const matchThen = etm.attr.value.match(/;?\s*then/gm);
const matchElse = etm.attr.value.match(/;?\s*else/gm);
const matchThen = etm.attr.value.match(/[^\w\d];?\s*then/gm);
const matchElse = etm.attr.value.match(/[^\w\d];?\s*else/gm);
if (etm.thenAttr !== undefined || etm.elseAttr !== undefined) {
// bound if then / if then else

View file

@ -425,6 +425,37 @@ describe('control flow migration', () => {
].join('\n'));
});
it('should migrate an if else case with condition that has `then` in the string', async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';
import {NgIf,NgIfElse} from '@angular/common';
@Component({
templateUrl: './comp.html'
})
class Comp {
show = false;
}
`);
writeFile('/comp.html', [
`<div *ngIf="!(isAuthenticated$ | async) && !reauthRequired">`,
` Hello!`,
`</div>`,
].join('\n'));
await runMigration();
const content = tree.readContent('/comp.html');
expect(content).toBe([
`@if (!(isAuthenticated$ | async) && !reauthRequired) {`,
` <div>`,
` Hello!`,
` </div>`,
`}`,
].join('\n'));
});
it('should migrate an if case on a container', async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';