/** * @angular-modernizer/plugin-angular - Type Safety Transform Rule * * Automatically adds missing type annotations to parameters, return types, * and class properties, replacing implicit `any` with `unknown`. * * 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 (guards via getTypeNode() checks) * - 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 { TypeSafetyTransformOrchestrator } from '../orchestrators/type-safety-transform.orchestrator.js'; /** * Type Safety Transform Rule - adds missing type annotations. * * This is a thin wrapper that delegates all business logic to the * TypeSafetyTransformOrchestrator following the recommended pattern. * * @example * ```typescript * const rule = new TypeSafetyTransformRule(); * const context = ContextFactory.createTransformContext({ sourceFile, project, api }); * const result = await rule.transform(context); * * if (result.modified) { * console.info('Added missing type annotations'); * console.info(`Changes: ${result.changeCount}`); * } * ``` */ export class TypeSafetyTransformRule implements TransformRule { public readonly id = 'angular:type-safety-transform'; public readonly name = 'Type Safety Transform'; public readonly description = 'Adds missing type annotations to parameters, return types, and class properties'; public readonly category = 'modernization'; public readonly tags = [ 'angular', 'typescript', 'type-safety', 'modernization', ]; /** * Orchestrator instance containing all transformation business logic. * @private */ private readonly orchestrator = new TypeSafetyTransformOrchestrator(); /** * Transforms source file by adding missing type annotations. * * @param context - Transform context with source file and PublicApi * @returns Transform result with modification status */ 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, }; } // Capture initial state for change detection const initialText = sourceFile.getFullText(); // Delegate ALL business logic to orchestrator await this.orchestrator.run(context); // Detect if modifications were made const modified = sourceFile.getFullText() !== initialText; return { ruleId: this.id, modified, message: modified ? 'Added missing type annotations' : 'No type annotation changes needed', filePath, changeCount: modified ? 1 : 0, }; } }