diff --git a/packages/core/schematics/ng-generate/control-flow-migration/ifs.ts b/packages/core/schematics/ng-generate/control-flow-migration/ifs.ts index 34527b4a9b546..959623a4820bf 100644 --- a/packages/core/schematics/ng-generate/control-flow-migration/ifs.ts +++ b/packages/core/schematics/ng-generate/control-flow-migration/ifs.ts @@ -84,7 +84,10 @@ function migrateNgIf(etm: ElementToMigrate, tmpl: string, offset: number): Resul function buildIfBlock(etm: ElementToMigrate, tmpl: string, offset: number): Result { // includes the mandatory semicolon before as const lbString = etm.hasLineBreaks ? '\n' : ''; - const condition = etm.attr.value.replace(' as ', '; as '); + const condition = etm.attr.value + .replace(' as ', '; as ') + // replace 'let' with 'as' whatever spaces are between ; and 'let' + .replace(/;\s*let/g, '; as'); const originals = getOriginals(etm, tmpl, offset); @@ -106,7 +109,10 @@ function buildIfBlock(etm: ElementToMigrate, tmpl: string, offset: number): Resu function buildStandardIfElseBlock( etm: ElementToMigrate, tmpl: string, elseString: string, offset: number): Result { // includes the mandatory semicolon before as - const condition = etm.getCondition(elseString).replace(' as ', '; as '); + const condition = etm.getCondition(elseString) + .replace(' as ', '; as ') + // replace 'let' with 'as' whatever spaces are between ; and 'let' + .replace(/;\s*let/g, '; as'); const elsePlaceholder = `#${etm.getTemplateName(elseString)}|`; return buildIfElseBlock(etm, tmpl, condition, elsePlaceholder, offset); } @@ -151,7 +157,10 @@ function buildStandardIfThenElseBlock( etm: ElementToMigrate, tmpl: string, thenString: string, elseString: string, offset: number): Result { // includes the mandatory semicolon before as - const condition = etm.getCondition(thenString).replace(' as ', '; as '); + const condition = etm.getCondition(thenString) + .replace(' as ', '; as ') + // replace 'let' with 'as' whatever spaces are between ; and 'let' + .replace(/;\s*let/g, '; as'); const thenPlaceholder = `#${etm.getTemplateName(thenString, elseString)}|`; const elsePlaceholder = `#${etm.getTemplateName(elseString)}|`; return buildIfThenElseBlock(etm, tmpl, condition, thenPlaceholder, elsePlaceholder, offset); diff --git a/packages/core/schematics/test/control_flow_migration_spec.ts b/packages/core/schematics/test/control_flow_migration_spec.ts index 5452e32edae20..c06e74cd898ef 100644 --- a/packages/core/schematics/test/control_flow_migration_spec.ts +++ b/packages/core/schematics/test/control_flow_migration_spec.ts @@ -100,7 +100,6 @@ describe('control flow migration', () => { } catch (e: any) { error = e.message; } - expect(error).toBe('Cannot run control flow migration outside of the current project.'); }); @@ -3513,5 +3512,68 @@ describe('control flow migration', () => { expect(content).toContain( 'template: `