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
138 lines (136 loc) • 4.99 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtractSharedStrategy = void 0;
const types_1 = require("../../domain/models/types");
const path = __importStar(require("path"));
class ExtractSharedStrategy {
constructor() {
this.type = types_1.FixStrategy.EXTRACT_SHARED;
}
async canFix(cycle) {
const cycleLength = cycle.paths.length - 1;
return cycleLength >= 2 && cycleLength <= 4;
}
score(cycle, _modules) {
const cycleLength = cycle.paths.length - 1;
const directories = cycle.paths.slice(0, -1).map((p) => path.dirname(p));
const sameDirectory = new Set(directories).size === 1;
if (cycleLength === 2 && sameDirectory) {
return 70;
}
if (cycleLength <= 3 && sameDirectory) {
return 60;
}
return sameDirectory ? 40 : 20;
}
async fix(cycle, _modules, fileSystem, _dryRun) {
try {
const sharedModulePath = this.determineSharedModulePath(cycle);
const manualSteps = [
{
description: `Create a new shared module: ${sharedModulePath}`,
file: sharedModulePath,
code: this.generateSharedModuleTemplate(cycle),
},
{
description: 'Move shared types, interfaces, or utility functions to the new module',
file: sharedModulePath,
},
];
for (const modulePath of cycle.paths.slice(0, -1)) {
const relativePath = fileSystem.getRelativePath(modulePath, sharedModulePath);
manualSteps.push({
description: `Update imports in ${path.basename(modulePath)} to use shared module`,
file: modulePath,
code: `import { /* shared items */ } from '${relativePath}';`,
});
}
return {
cycle,
strategy: this.type,
success: false,
modifiedFiles: [],
createdFiles: [],
error: 'Extraction requires manual code movement',
manualSteps,
};
}
catch (error) {
return {
cycle,
strategy: this.type,
success: false,
modifiedFiles: [],
createdFiles: [],
error: error.message,
};
}
}
determineSharedModulePath(cycle) {
const firstPath = cycle.paths[0];
const dir = path.dirname(firstPath);
const ext = path.extname(firstPath);
return path.join(dir, `shared${ext}`);
}
generateSharedModuleTemplate(cycle) {
const ext = path.extname(cycle.paths[0]);
const isTypeScript = ext === '.ts' || ext === '.tsx';
if (isTypeScript) {
return `/**
* Shared module to break circular dependency
* Extracted from: ${cycle.paths
.slice(0, -1)
.map((p) => path.basename(p))
.join(', ')}
*/
// Move shared types, interfaces, and utility functions here
export {};
`;
}
return `/**
* Shared module to break circular dependency
* Extracted from: ${cycle.paths
.slice(0, -1)
.map((p) => path.basename(p))
.join(', ')}
*/
// Move shared functions and constants here
module.exports = {};
`;
}
}
exports.ExtractSharedStrategy = ExtractSharedStrategy;
//# sourceMappingURL=ExtractSharedStrategy.js.map