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 c3da13ed3208..bea65d7f3cfa 100644 --- a/packages/core/schematics/ng-generate/control-flow-migration/types.ts +++ b/packages/core/schematics/ng-generate/control-flow-migration/types.ts @@ -10,6 +10,7 @@ import { Attribute, Block, Element, + LetDeclaration, ParseTreeResult, RecursiveVisitor, Text, @@ -390,6 +391,7 @@ export class CommonCollector extends RecursiveVisitor { this.count++; } } + super.visitBlock(ast, null); } override visitText(ast: Text) { @@ -398,6 +400,13 @@ export class CommonCollector extends RecursiveVisitor { } } + override visitLetDeclaration(decl: LetDeclaration): void { + if (this.hasPipes(decl.value)) { + this.count++; + } + super.visitLetDeclaration(decl, null); + } + private hasDirectives(input: string): boolean { return commonModuleDirectives.has(input); } diff --git a/packages/core/schematics/test/control_flow_migration_spec.ts b/packages/core/schematics/test/control_flow_migration_spec.ts index db7cd4273bb2..2bb5fcdf8ed0 100644 --- a/packages/core/schematics/test/control_flow_migration_spec.ts +++ b/packages/core/schematics/test/control_flow_migration_spec.ts @@ -6479,6 +6479,72 @@ describe('control flow migration', () => { expect(actual).toBe(expected); }); + + it('should not remove common module if symbols are used inside new control flow', async () => { + writeFile( + '/comp.ts', + [ + `import {CommonModule} from '@angular/common';`, + `import {Component} from '@angular/core';\n`, + `@Component({`, + ` imports: [CommonModule],`, + ` template: \`@if (toggle) {