Skip to content

Commit dbd6f38

Browse files
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
1 parent 8f6affd commit dbd6f38

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@ function runControlFlowMigration(
119119
}
120120

121121
function sortFilePaths(names: string[]): string[] {
122-
const templateFiles = names.filter(n => n.endsWith('.html'));
123-
const classFiles = names.filter(n => !n.endsWith('.html'));
124-
return [...templateFiles, ...classFiles];
122+
names.sort((a, _) => a.endsWith('.html') ? -1 : 0);
123+
return names;
125124
}
126125

127126
function generateErrorMessage(path: string, errors: MigrateError[]): string {

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,12 @@ export class ElementToMigrate {
133133

134134
getTemplateName(targetStr: string, secondStr?: string): string {
135135
const targetLocation = this.attr.value.indexOf(targetStr);
136-
if (secondStr) {
137-
const secondTargetLocation = this.attr.value.indexOf(secondStr);
138-
return this.attr.value.slice(targetLocation + targetStr.length, secondTargetLocation)
139-
.trim()
140-
.split(';')[0]
141-
.trim();
142-
}
143-
return this.attr.value.slice(targetLocation + targetStr.length).trim().split(';')[0].trim();
136+
const secondTargetLocation = secondStr ? this.attr.value.indexOf(secondStr) : undefined;
137+
return this.attr.value.slice(targetLocation + targetStr.length, secondTargetLocation)
138+
.replace(':', '')
139+
.trim()
140+
.split(';')[0]
141+
.trim();
144142
}
145143

146144
start(offset: number): number {

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,41 @@ describe('control flow migration', () => {
723723
].join('\n'));
724724
});
725725

726+
it('should migrate an if else case with a colon after else', async () => {
727+
writeFile('/comp.ts', `
728+
import {Component} from '@angular/core';
729+
import {NgIf} from '@angular/common';
730+
731+
@Component({
732+
templateUrl: './comp.html'
733+
})
734+
class Comp {
735+
show = false;
736+
}
737+
`);
738+
739+
writeFile('/comp.html', [
740+
`<div>`,
741+
`<span *ngIf="show; else: elseTmpl">Content here</span>`,
742+
`<ng-template #elseTmpl>Else Content</ng-template>`,
743+
`</div>`,
744+
].join('\n'));
745+
746+
await runMigration();
747+
const actual = tree.readContent('/comp.html');
748+
const expected = [
749+
`<div>`,
750+
` @if (show) {`,
751+
` <span>Content here</span>`,
752+
` } @else {`,
753+
` Else Content`,
754+
` }`,
755+
`</div>`,
756+
].join('\n');
757+
758+
expect(actual).toBe(expected);
759+
});
760+
726761
it('should migrate an if else case with no space after ;', async () => {
727762
writeFile('/comp.ts', `
728763
import {Component} from '@angular/core';

0 commit comments

Comments
 (0)