From 739f1fc1679ce4bfab2ee51400ed8260865eeed7 Mon Sep 17 00:00:00 2001 From: Jessica Janiuk Date: Tue, 24 Oct 2023 14:20:30 -0400 Subject: [PATCH] fix(migrations): Fixes the root level template offset in control flow migration When migrating an ng-template later on in a file, the migrationResult was not being reset to zero and causing offsets to be double applied due to ng-template nodes being included in the migration loop. --- .../control-flow-migration/util.ts | 3 +- .../test/control_flow_migration_spec.ts | 61 ++++++++++++++++++- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/packages/core/schematics/ng-generate/control-flow-migration/util.ts b/packages/core/schematics/ng-generate/control-flow-migration/util.ts index 5a007d412577..b41faedbf89c 100644 --- a/packages/core/schematics/ng-generate/control-flow-migration/util.ts +++ b/packages/core/schematics/ng-generate/control-flow-migration/util.ts @@ -155,8 +155,8 @@ export function migrateTemplate(template: string): {migrated: string|null, error let offset = 0; let nestLevel = -1; let postOffsets: number[] = []; - let migrateResult: Result = {tmpl: result, offsets: {pre: 0, post: 0}}; for (const el of visitor.elements) { + let migrateResult: Result = {tmpl: result, offsets: {pre: 0, post: 0}}; // applies the post offsets after closing if (el.nestCount <= nestLevel) { const count = nestLevel - el.nestCount; @@ -189,7 +189,6 @@ export function migrateTemplate(template: string): {migrated: string|null, error result = migrateResult.tmpl; offset += migrateResult.offsets.pre; postOffsets.push(migrateResult.offsets.post); - const nm = el.el.name; nestLevel = el.nestCount; } diff --git a/packages/core/schematics/test/control_flow_migration_spec.ts b/packages/core/schematics/test/control_flow_migration_spec.ts index 7133351b7476..28762ac2fe7d 100644 --- a/packages/core/schematics/test/control_flow_migration_spec.ts +++ b/packages/core/schematics/test/control_flow_migration_spec.ts @@ -1798,7 +1798,66 @@ describe('control flow migration', () => { }); }); - describe('template removal', () => { + describe('template', () => { + it('should migrate a root level template thats not used in control flow', async () => { + writeFile('/comp.ts', ` + import {Component} from '@angular/core'; + import {NgIf} from '@angular/common'; + + @Component({ + selector: 'declare-comp', + templateUrl: './comp.html' + }) + class DeclareComp { + } + `); + + writeFile('/comp.html', [ + `
`, + ` `, + ` `, + `
`, + `
`, + `
`, + ``, + `
`, + ` `, + ` Wow...a button!`, + ` `, + `
`, + `
`, + ].join('\n')); + + await runMigration(); + + const content = tree.readContent('/comp.html'); + const result = [ + `
`, + ` `, + ` @if (content()) {\n`, + `
\n `, + `}`, + `
`, + ``, + `
`, + ` @if (shouldShowMe()) {`, + ``, + ` Wow...a button!`, + ` `, + `}`, + `
`, + `
`, + + ].join('\n'); + + expect(content).toBe(result); + }); + it('should not remove a template thats not used in control flow', async () => { writeFile('/comp.ts', ` import {Component} from '@angular/core';