Skip to content

Commit

Permalink
fix(migrations): fix regexp for else and then in cf migration (#53257)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jessicajaniuk authored and dylhunn committed Nov 30, 2023
1 parent 7a2faca commit 72d22ba
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions packages/core/schematics/test/control_flow_migration_spec.ts
Expand Up @@ -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';
Expand Down

0 comments on commit 72d22ba

Please sign in to comment.