Skip to content

Commit 72d22ba

Browse files
thePunderWomandylhunn
authored andcommitted
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
1 parent 7a2faca commit 72d22ba

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ export function migrateIf(template: string):
6868
}
6969

7070
function migrateNgIf(etm: ElementToMigrate, tmpl: string, offset: number): Result {
71-
const matchThen = etm.attr.value.match(/;?\s*then/gm);
72-
const matchElse = etm.attr.value.match(/;?\s*else/gm);
71+
const matchThen = etm.attr.value.match(/[^\w\d];?\s*then/gm);
72+
const matchElse = etm.attr.value.match(/[^\w\d];?\s*else/gm);
7373

7474
if (etm.thenAttr !== undefined || etm.elseAttr !== undefined) {
7575
// bound if then / if then else

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,37 @@ describe('control flow migration', () => {
425425
].join('\n'));
426426
});
427427

428+
it('should migrate an if else case with condition that has `then` in the string', async () => {
429+
writeFile('/comp.ts', `
430+
import {Component} from '@angular/core';
431+
import {NgIf,NgIfElse} from '@angular/common';
432+
433+
@Component({
434+
templateUrl: './comp.html'
435+
})
436+
class Comp {
437+
show = false;
438+
}
439+
`);
440+
441+
writeFile('/comp.html', [
442+
`<div *ngIf="!(isAuthenticated$ | async) && !reauthRequired">`,
443+
` Hello!`,
444+
`</div>`,
445+
].join('\n'));
446+
447+
await runMigration();
448+
const content = tree.readContent('/comp.html');
449+
450+
expect(content).toBe([
451+
`@if (!(isAuthenticated$ | async) && !reauthRequired) {`,
452+
` <div>`,
453+
` Hello!`,
454+
` </div>`,
455+
`}`,
456+
].join('\n'));
457+
});
458+
428459
it('should migrate an if case on a container', async () => {
429460
writeFile('/comp.ts', `
430461
import {Component} from '@angular/core';

0 commit comments

Comments
 (0)