fix(migrations): allows colons in ngIf else cases to migrate (#53076)

This makes sure colons after else and then cases get migrated properly.
fixes: #53150

PR Close #53076
This commit is contained in:
Jessica Janiuk
2023-11-27 10:33:00 -05:00
committed by Pawel Kozlowski
parent 8f6affdd64
commit dbd6f386ea
3 changed files with 43 additions and 11 deletions
@@ -119,9 +119,8 @@ function runControlFlowMigration(
}
function sortFilePaths(names: string[]): string[] {
const templateFiles = names.filter(n => n.endsWith('.html'));
const classFiles = names.filter(n => !n.endsWith('.html'));
return [...templateFiles, ...classFiles];
names.sort((a, _) => a.endsWith('.html') ? -1 : 0);
return names;
}
function generateErrorMessage(path: string, errors: MigrateError[]): string {
@@ -133,14 +133,12 @@ export class ElementToMigrate {
getTemplateName(targetStr: string, secondStr?: string): string {
const targetLocation = this.attr.value.indexOf(targetStr);
if (secondStr) {
const secondTargetLocation = this.attr.value.indexOf(secondStr);
return this.attr.value.slice(targetLocation + targetStr.length, secondTargetLocation)
.trim()
.split(';')[0]
.trim();
}
return this.attr.value.slice(targetLocation + targetStr.length).trim().split(';')[0].trim();
const secondTargetLocation = secondStr ? this.attr.value.indexOf(secondStr) : undefined;
return this.attr.value.slice(targetLocation + targetStr.length, secondTargetLocation)
.replace(':', '')
.trim()
.split(';')[0]
.trim();
}
start(offset: number): number {
@@ -723,6 +723,41 @@ describe('control flow migration', () => {
].join('\n'));
});
it('should migrate an if else case with a colon after else', async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';
import {NgIf} from '@angular/common';
@Component({
templateUrl: './comp.html'
})
class Comp {
show = false;
}
`);
writeFile('/comp.html', [
`<div>`,
`<span *ngIf="show; else: elseTmpl">Content here</span>`,
`<ng-template #elseTmpl>Else Content</ng-template>`,
`</div>`,
].join('\n'));
await runMigration();
const actual = tree.readContent('/comp.html');
const expected = [
`<div>`,
` @if (show) {`,
` <span>Content here</span>`,
` } @else {`,
` Else Content`,
` }`,
`</div>`,
].join('\n');
expect(actual).toBe(expected);
});
it('should migrate an if else case with no space after ;', async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';