Skip to content

Commit 5179ce3

Browse files
fix(migrations): Fix cf migration let condition semicolon order (angular#56913)
In rare cases people may put let statements before else statements and omit semicolons, causing migration issues. PR Close angular#56913
1 parent 260531d commit 5179ce3

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,19 @@ export class ElementToMigrate {
150150
this.aliasAttrs = aliasAttrs;
151151
}
152152

153+
normalizeConditionString(value: string): string {
154+
value = this.insertSemicolon(value, value.indexOf(' else '));
155+
value = this.insertSemicolon(value, value.indexOf(' then '));
156+
value = this.insertSemicolon(value, value.indexOf(' let '));
157+
return value.replace(';;', ';');
158+
}
159+
160+
insertSemicolon(str: string, ix: number): string {
161+
return ix > -1 ? `${str.slice(0, ix)};${str.slice(ix)}` : str;
162+
}
163+
153164
getCondition(): string {
154-
const chunks = this.attr.value.split(';');
165+
const chunks = this.normalizeConditionString(this.attr.value).split(';');
155166
let condition = chunks[0];
156167

157168
// checks for case of no usage of `;` in if else / if then else

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,48 @@ describe('control flow migration', () => {
16741674
);
16751675
});
16761676

1677+
it('should migrate if/else with let variable in wrong place with no semicolons', async () => {
1678+
writeFile(
1679+
'/comp.ts',
1680+
`
1681+
import {Component} from '@angular/core';
1682+
import {NgIf} from '@angular/common';
1683+
1684+
@Component({
1685+
templateUrl: './comp.html'
1686+
})
1687+
class Comp {
1688+
user$ = of({ name: 'Jane' }})
1689+
}
1690+
`,
1691+
);
1692+
1693+
writeFile(
1694+
'/comp.html',
1695+
[
1696+
`<div>`,
1697+
`<div *ngIf="user of users; let tooltipContent else noUserBlock">{{ user.name }}</div>`,
1698+
`<ng-template #noUserBlock>No user</ng-template>`,
1699+
`</div>`,
1700+
].join('\n'),
1701+
);
1702+
1703+
await runMigration();
1704+
const content = tree.readContent('/comp.html');
1705+
1706+
expect(content).toBe(
1707+
[
1708+
`<div>`,
1709+
` @if (user of users; as tooltipContent) {`,
1710+
` <div>{{ user.name }}</div>`,
1711+
` } @else {`,
1712+
` No user`,
1713+
` }`,
1714+
`</div>`,
1715+
].join('\n'),
1716+
);
1717+
});
1718+
16771719
it('should migrate if/then/else with alias', async () => {
16781720
writeFile(
16791721
'/comp.ts',

0 commit comments

Comments
 (0)