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 c04d8ab85b4..49175f6a766 100644 --- a/packages/core/schematics/ng-generate/control-flow-migration/types.ts +++ b/packages/core/schematics/ng-generate/control-flow-migration/types.ts @@ -6,7 +6,14 @@ * found in the LICENSE file at https://angular.io/license */ -import {Attribute, Element, ParseTreeResult, RecursiveVisitor, Text} from '@angular/compiler'; +import { + Attribute, + Block, + Element, + ParseTreeResult, + RecursiveVisitor, + Text, +} from '@angular/compiler'; import ts from 'typescript'; import {lookupIdentifiersInSourceFile} from './identifier-lookup'; @@ -377,6 +384,14 @@ export class CommonCollector extends RecursiveVisitor { super.visitElement(el, null); } + override visitBlock(ast: Block): void { + for (const blockParam of ast.parameters) { + if (this.hasPipes(blockParam.expression)) { + this.count++; + } + } + } + override visitText(ast: Text) { if (this.hasPipes(ast.value)) { this.count++; diff --git a/packages/core/schematics/test/control_flow_migration_spec.ts b/packages/core/schematics/test/control_flow_migration_spec.ts index 9c9868de71a..cc77f95667f 100644 --- a/packages/core/schematics/test/control_flow_migration_spec.ts +++ b/packages/core/schematics/test/control_flow_migration_spec.ts @@ -6243,6 +6243,54 @@ describe('control flow migration', () => { expect(actual).toBe(expected); }); + it('should not remove common module when second run of migration and common module symbols are found', async () => { + writeFile( + '/comp.ts', + [ + `import {Component} from '@angular/core';`, + `import {CommonModule} from '@angular/common';\n`, + `@Component({`, + ` standalone: true`, + ` selector: 'example-cmp',`, + ` templateUrl: './comp.html',`, + ` imports: [CommonModule],`, + `})`, + `export class ExampleCmp {`, + `}`, + ].join('\n'), + ); + + writeFile( + '/comp.html', + [ + `
`, + ` @if (state$ | async; as state) {`, + `
`, + ` Content here {{state}}`, + `
`, + ` }`, + `
`, + ].join('\n'), + ); + + await runMigration(); + const actual = tree.readContent('/comp.ts'); + const expected = [ + `import {Component} from '@angular/core';`, + `import {CommonModule} from '@angular/common';\n`, + `@Component({`, + ` standalone: true`, + ` selector: 'example-cmp',`, + ` templateUrl: './comp.html',`, + ` imports: [CommonModule],`, + `})`, + `export class ExampleCmp {`, + `}`, + ].join('\n'); + + expect(actual).toBe(expected); + }); + it('should not remove imports when mismatch in counts', async () => { writeFile( '/comp.ts',