/** * @angular-modernizer/plugin-angular - Any to Interface Transform Rule * * Replaces `any`-typed parameters and class properties with generated * TypeScript interfaces, inferred from downstream property accesses. * * This transformation follows the "Thin Rule, Thick Orchestrator" pattern: * - Rule: Thin wrapper (≤50 lines) delegating to orchestrator * - Orchestrator: Contains all business logic for transformation * * Guarantees: * - Idempotent: Safe to run multiple times (any annotations replaced first, * no any nodes remain on second run) * - Atomic: All or nothing per source file * - Reversible: Can be undone via git reset */ import type { TransformRule, TransformContext, TransformResult, } from '@angular-modernizer/plugin-system'; import type { PublicApi } from '@angular-modernizer/api'; import { AnyToInterfaceOrchestrator } from '../orchestrators/any-to-interface.orchestrator.js'; /** * Any to Interface Transform Rule - replaces any-typed nodes with interfaces. * * This is a thin wrapper that delegates all business logic to the * AnyToInterfaceOrchestrator following the recommended pattern. * * @example * ```typescript * const rule = new AnyToInterfaceTransformRule(); * const context = ContextFactory.createTransformContext({ sourceFile, project, api }); * const result = await rule.transform(context); * * if (result.modified) { * console.info('Generated interfaces for any-typed nodes'); * } * ``` */ export class AnyToInterfaceTransformRule implements TransformRule { public readonly id = 'angular:any-to-interface-transform'; public readonly name = 'Any to Interface Transform'; public readonly description = 'Replaces any-typed parameters and properties with generated interfaces'; public readonly category = 'modernization'; public readonly tags = ['angular', 'typescript', 'any', 'interface']; private readonly orchestrator = new AnyToInterfaceOrchestrator(); async transform( context: TransformContext, ): Promise { const { sourceFile, filePath } = context; if (filePath.endsWith('.d.ts')) { return { ruleId: this.id, modified: false, message: 'Skipped declaration file', filePath, }; } const initialText = sourceFile.getFullText(); await this.orchestrator.run(context); const modified = sourceFile.getFullText() !== initialText; return { ruleId: this.id, modified, message: modified ? 'Generated interfaces for any-typed nodes' : 'No any-to-interface changes needed', filePath, changeCount: modified ? 1 : 0, }; } }