/** * @angular-modernizer/plugin-angular - Form Modernization Transformation Rule * * Transform rule that modernizes Angular reactive forms to use current best practices. * This is part of the modernization to improve form handling and maintainability. * * Philosophy: "Thin Rule, Thick Orchestrator" * - The rule is a minimal wrapper * - All business logic lives in the orchestrator * - The rule handles the plugin protocol (TransformRule interface) */ import type { TransformRule, TransformContext, TransformResult, } from '@angular-modernizer/plugin-system'; import type { PublicApi } from '@angular-modernizer/api'; import { FormModernizationOrchestrator } from '../orchestrators/form-modernization-orchestrator.js'; /** * Transform rule for modernizing Angular reactive forms. * * @remarks * This rule implements the {@link TransformRule} interface and serves as the * bridge between the Angular Modernizer's plugin system and the form * modernization orchestrator. It follows the "Thin Rule, Thick Orchestrator" pattern * where the rule handles protocol compliance while delegating all business logic * to the {@link FormModernizationOrchestrator}. * * ## What It Does * * The rule orchestrates the modernization of: * - **FormBuilder usage**: Updates deprecated patterns to modern syntax * - **FormControl/FormGroup initialization**: Modernizes form control creation * - **Form validation**: Updates validation patterns to current best practices * - **Form submission**: Modernizes form submission handling * * ## Example Usage * * This rule is typically not used directly, but rather accessed through the * {@link AngularPlugin}: * * ```typescript * const plugin = new AngularPlugin(); * const rules = plugin.getTransformRules(); * const rule = rules.find(r => r.id === 'angular:form-modernization'); * const result = await rule.transform(context); * ``` * * ## Transformation Behavior * * **FormBuilder Patterns**: * ```typescript * // Before * constructor(private fb: FormBuilder) {} * ngOnInit() { * this.form = this.fb.group({ * name: ['', Validators.required], * email: ['', [Validators.required, Validators.email]] * }); * } * * // After * form = new FormGroup({ * name: new FormControl('', Validators.required), * email: new FormControl('', [Validators.required, Validators.email]) * }); * ``` * * **FormControl Initialization**: * ```typescript * // Before * this.control = new FormControl(null); * * // After * this.control = new FormControl(''); * ``` * * @see {@link FormModernizationOrchestrator} for transformation logic * @see {@link TransformRule} for the interface contract * @see {@link AngularPlugin} for plugin integration * * @public */ export class FormModernizationRule implements TransformRule { /** * Unique rule identifier. * Format: "plugin-name:rule-name" */ public readonly id = 'angular:form-modernization'; /** * Human-readable rule name. */ public readonly name = 'Form Modernization'; /** * Rule description. */ public readonly description = 'Modernizes Angular reactive forms to use current best practices and syntax'; /** * Rule category for grouping. */ public readonly category = 'modernization'; /** * Tags for filtering and search. */ public readonly tags = [ 'angular', 'forms', 'reactive-forms', 'formbuilder', 'validation', 'modernization', 'refactoring', ]; /** * The orchestrator that performs the actual transformation. */ private readonly orchestrator = new FormModernizationOrchestrator(); /** * Transform a source file by modernizing reactive forms patterns. * * @remarks * This method implements the {@link TransformRule.transform} contract. * It delegates to the {@link FormModernizationOrchestrator} for the actual * transformation work, then packages the result into a {@link TransformResult}. * * The method: * 1. Captures the file's initial state * 2. Invokes the orchestrator to perform the modernization * 3. Detects whether the file was modified * 4. Returns a standardized {@link TransformResult} * * ## Return Value * * The transform result includes: * - `modified`: Whether the file was changed * - `message`: A human-readable summary * - `filePath`: The path to the transformed file * - `ruleId`: This rule's identifier * * @param context - Transform context with file, project, and API access * * @returns Promise resolving to the transformation result * * @example * ```typescript * const rule = new FormModernizationRule(); * const result = await rule.transform(context); * * if (result.modified) { * console.info(`Modernized forms: ${result.filePath}`); * } * ``` * * @public */ async transform( context: TransformContext, ): Promise { const { sourceFile, filePath } = context; // Check if this transformation type is requested const pluginConfig = context.config[ '@angular-modernizer/plugin-angular' ] as Record | undefined; if (pluginConfig?.['transformationType'] !== 'form-modernization') { return { ruleId: this.id, modified: false, message: 'Transformation type not applicable', filePath, }; } // Capture initial state to detect modifications const initialText = sourceFile.getFullText(); // Run the orchestrator this.orchestrator.run(context); // Detect if the file was modified const modified = sourceFile.getFullText() !== initialText; // Return the result return { ruleId: this.id, modified, message: modified ? `Successfully modernized reactive forms patterns` : `No reactive forms patterns found to modernize`, filePath, }; } }