diff --git a/packages/core/schematics/ng-generate/control-flow-migration/types.ts b/packages/core/schematics/ng-generate/control-flow-migration/types.ts index c354d0e4165..c04d8ab85b4 100644 --- a/packages/core/schematics/ng-generate/control-flow-migration/types.ts +++ b/packages/core/schematics/ng-generate/control-flow-migration/types.ts @@ -150,8 +150,19 @@ export class ElementToMigrate { this.aliasAttrs = aliasAttrs; } + normalizeConditionString(value: string): string { + value = this.insertSemicolon(value, value.indexOf(' else ')); + value = this.insertSemicolon(value, value.indexOf(' then ')); + value = this.insertSemicolon(value, value.indexOf(' let ')); + return value.replace(';;', ';'); + } + + insertSemicolon(str: string, ix: number): string { + return ix > -1 ? `${str.slice(0, ix)};${str.slice(ix)}` : str; + } + getCondition(): string { - const chunks = this.attr.value.split(';'); + const chunks = this.normalizeConditionString(this.attr.value).split(';'); let condition = chunks[0]; // checks for case of no usage of `;` in if else / if then else diff --git a/packages/core/schematics/test/control_flow_migration_spec.ts b/packages/core/schematics/test/control_flow_migration_spec.ts index ec6a3c0261d..9c9868de71a 100644 --- a/packages/core/schematics/test/control_flow_migration_spec.ts +++ b/packages/core/schematics/test/control_flow_migration_spec.ts @@ -1674,6 +1674,48 @@ describe('control flow migration', () => { ); }); + it('should migrate if/else with let variable in wrong place with no semicolons', async () => { + writeFile( + '/comp.ts', + ` + import {Component} from '@angular/core'; + import {NgIf} from '@angular/common'; + + @Component({ + templateUrl: './comp.html' + }) + class Comp { + user$ = of({ name: 'Jane' }}) + } + `, + ); + + writeFile( + '/comp.html', + [ + `