/** * @angular-modernizer/plugin-angular - Dependency Injection Migration Transformation Rule * * Transform rule that modernizes Angular dependency injection patterns. * This is part of the modernization to improve code maintainability and follow Angular best practices. * * 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 { DependencyInjectionMigrationOrchestrator } from '../orchestrators/dependency-injection-migration-orchestrator.js'; /** * Transform rule for modernizing Angular dependency injection patterns. * * @remarks * This rule implements the {@link TransformRule} interface and serves as the * bridge between the Angular Modernizer's plugin system and the dependency * injection migration orchestrator. It follows the "Thin Rule, Thick Orchestrator" pattern * where the rule handles protocol compliance while delegating all business logic * to the {@link DependencyInjectionMigrationOrchestrator}. * * ## What It Does * * The rule orchestrates the modernization of: * - **String-based injection tokens**: Converts `@Inject('TOKEN')` to proper `InjectionToken` usage * - **Deprecated injector patterns**: Updates old injector usage patterns * - **Provider configurations**: Modernizes provider syntax where applicable * - **Injection token imports**: Ensures proper imports for modern DI patterns * * ## 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:dependency-injection-migration'); * const result = await rule.transform(context); * ``` * * ## Transformation Behavior * * **String-based Tokens**: * ```typescript * // Before * @Inject('API_URL') private apiUrl: string; * * // After * private apiUrl = inject(API_URL_TOKEN); * ``` * * **Deprecated Injector Usage**: * ```typescript * // Before * const injector = ReflectiveInjector.resolveAndCreate([SomeService]); * * // After * const injector = Injector.create({ providers: [SomeService] }); * ``` * * @see {@link DependencyInjectionMigrationOrchestrator} for transformation logic * @see {@link TransformRule} for the interface contract * @see {@link AngularPlugin} for plugin integration * * @public */ export class DependencyInjectionMigrationRule implements TransformRule { /** * Unique rule identifier. * Format: "plugin-name:rule-name" */ public readonly id = 'angular:dependency-injection-migration'; /** * Human-readable rule name. */ public readonly name = 'Dependency Injection Migration'; /** * Rule description. */ public readonly description = 'Modernizes Angular dependency injection patterns to follow current best practices'; /** * Rule category for grouping. */ public readonly category = 'modernization'; /** * Tags for filtering and search. */ public readonly tags = [ 'angular', 'dependency-injection', 'inject', 'tokens', 'providers', 'modernization', 'refactoring', ]; /** * The orchestrator that performs the actual transformation. */ private readonly orchestrator = new DependencyInjectionMigrationOrchestrator(); /** * Transform a source file by modernizing dependency injection patterns. * * @remarks * This method implements the {@link TransformRule.transform} contract. * It delegates to the {@link DependencyInjectionMigrationOrchestrator} 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 DependencyInjectionMigrationRule(); * const result = await rule.transform(context); * * if (result.modified) { * console.info(`Modernized DI patterns: ${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'] !== 'dependency-injection-migration' ) { 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 dependency injection patterns` : `No dependency injection patterns found to modernize`, filePath, }; } }