mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
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:
parent
f4a96a9160
commit
03e2f1bb25
2 changed files with 33 additions and 2 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
|
|
|
|||
Loading…
Reference in a new issue