cyclic-dependency-fixer
Version: 
AI-powered tool to detect and fix circular dependencies in JavaScript/TypeScript projects. Features intelligent refactoring with Claude/GPT-4, codebase pattern learning, and context-aware fix recommendations
90 lines • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FixCyclesUseCase = void 0;
class FixCyclesUseCase {
    constructor(fileSystem, strategies) {
        this.fileSystem = fileSystem;
        this.strategies = strategies;
    }
    async execute(cycles, modules, options) {
        const results = [];
        for (const cycle of cycles) {
            const result = await this.fixCycle(cycle, modules, options);
            results.push(result);
        }
        return results;
    }
    async fixCycle(cycle, modules, options) {
        const applicableStrategies = await this.findApplicableStrategies(cycle, modules, options);
        if (applicableStrategies.length === 0) {
            return this.createNoStrategyResult(cycle);
        }
        applicableStrategies.sort((a, b) => b.score - a.score);
        for (const { strategy } of applicableStrategies) {
            try {
                const result = await strategy.fix(cycle, modules, this.fileSystem, options.dryRun);
                if (result.success || result.manualSteps) {
                    return result;
                }
            }
            catch (error) {
                continue;
            }
        }
        return this.createFailureResult(cycle, applicableStrategies);
    }
    async findApplicableStrategies(cycle, modules, options) {
        const applicable = [];
        for (const strategy of this.strategies) {
            if (options.strategies.length > 0 && !options.strategies.includes(strategy.type)) {
                continue;
            }
            const canFix = await strategy.canFix(cycle, modules);
            if (!canFix) {
                continue;
            }
            const score = strategy.score(cycle, modules);
            applicable.push({ strategy, score });
        }
        return applicable;
    }
    createNoStrategyResult(cycle) {
        return {
            cycle,
            strategy: cycle.paths.length === 2 ? cycle.paths[0] : null,
            success: false,
            modifiedFiles: [],
            createdFiles: [],
            error: 'No applicable fix strategy found',
            manualSteps: [
                {
                    description: 'Review the circular dependency and refactor manually',
                    file: cycle.paths[0],
                },
                {
                    description: 'Consider extracting shared code or using dependency injection',
                    file: cycle.paths[0],
                },
            ],
        };
    }
    createFailureResult(cycle, attemptedStrategies) {
        const strategyNames = attemptedStrategies.map((s) => s.strategy.type).join(', ');
        return {
            cycle,
            strategy: attemptedStrategies[0]?.strategy.type || '',
            success: false,
            modifiedFiles: [],
            createdFiles: [],
            error: `All strategies failed: ${strategyNames}`,
            manualSteps: [
                {
                    description: 'Manual intervention required to fix this cycle',
                    file: cycle.paths[0],
                },
            ],
        };
    }
}
exports.FixCyclesUseCase = FixCyclesUseCase;
//# sourceMappingURL=FixCyclesUseCase.js.map