Angular Modernizer
    Preparing search index...

    Transform rule that converts large switch statements into maintainable Factory Pattern implementations.

    Problem: Large switch statements (10+ cases) violate SOLID principles:

    • Open/Closed Principle: Adding new cases requires modifying the switch
    • Single Responsibility: Switch handles multiple concerns
    • Difficult to test, maintain, and extend

    Solution: Transforms God Switch into Factory Pattern using Strategy pattern:

    1. Extract each case into a separate Strategy class
    2. Generate a Factory class to create strategy instances
    3. Replace switch statement with factory call

    Before:

    switch (type) {
    case 'A':
    return handleA();
    case 'B':
    return handleB();
    // ... 10+ more cases
    }

    After:

    interface TypeStrategy {
    execute(): Result;
    }

    class AStrategy implements TypeStrategy {
    execute(): Result { return handleA(); }
    }

    class TypeFactory {
    createStrategy(type: string): TypeStrategy {
    const strategies = { A: AStrategy, B: BStrategy, ... };
    return new strategies[type]();
    }
    }

    const strategy = factory.createStrategy(type);
    return strategy.execute();

    Configuration:

    {
    "transformationType": "god-switch-transform",
    "godSwitch": {
    "minCaseCount": 10,
    "generateInterfaces": true,
    "generateFactory": true,
    "strategyNamingPattern": "{CaseValue}Strategy",
    "factoryNamingPattern": "{SwitchExpression}Factory"
    }
    }

    GodSwitchTransformOrchestrator for detailed transformation logic

    // Transformation triggered via MCP tool or programmatic API
    const rule = new GodSwitchTransformRule();
    const result = await rule.transform(context);

    if (result.modified) {
    console.info('Successfully transformed God Switch to Factory Pattern');
    }

    Implements

    Index

    Constructors

    Properties

    Methods

    Constructors

    Properties

    category: "refactoring" = 'refactoring'

    Category indicating this is a code restructuring transformation.

    description: "Transforms large switch statements into maintainable Factory Pattern implementations using the Strategy pattern" = 'Transforms large switch statements into maintainable Factory Pattern implementations using the Strategy pattern'

    Detailed description of the transformation's purpose and behavior.

    id: "solid:god-switch-transform" = 'solid:god-switch-transform'

    Unique identifier following plugin-name:rule-name convention.

    name: "God Switch to Factory Pattern" = 'God Switch to Factory Pattern'

    Human-readable name for the transformation.

    tags: string[] = ...

    Tags for discoverability and filtering.

    Methods

    • Transforms God Switch violations into Factory Pattern implementations.

      Process:

      1. Validates transformation type from configuration
      2. Captures initial source file state
      3. Delegates to orchestrator for transformation
      4. Detects modifications by comparing file state
      5. Returns structured result with modification status

      Idempotency: Running multiple times on the same file is safe - the orchestrator checks for existing factory implementations and skips already-transformed switches.

      Atomicity: The orchestrator uses backup/rollback mechanisms to ensure all-or-nothing semantics. If any step fails, the source file is restored to its original state.

      Parameters

      Returns Promise<TransformResult>

      Promise resolving to transformation result with modification status and message