UNPKG

refakts

Version:

TypeScript refactoring tool built for AI coding agents to perform precise refactoring operations via command line instead of requiring complete code regeneration.

125 lines 4.71 kB
"use strict"; 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.checkFileSizes = checkFileSizes; exports.checkFunctionSizes = checkFunctionSizes; const ts_morph_1 = require("ts-morph"); const path = __importStar(require("path")); function checkFileSizes(srcDir) { const project = createTsMorphProject(srcDir); const issues = []; for (const sourceFile of project.getSourceFiles()) { processSourceFileForSizes(sourceFile, issues); } return issues; } function checkFunctionSizes(srcDir) { const project = createTsMorphProject(srcDir); const issues = []; for (const sourceFile of project.getSourceFiles()) { processSourceFileForFunctions(sourceFile, issues); } return issues; } function createTsMorphProject(srcDir) { const project = new ts_morph_1.Project(); project.addSourceFilesAtPaths(`${srcDir}/**/*.ts`); return project; } function processSourceFileForSizes(sourceFile, issues) { const filePath = path.relative(process.cwd(), sourceFile.getFilePath()); if (shouldSkipFile(filePath)) { return; } addFileSizeIssueIfNeeded(sourceFile, filePath, issues); } function addFileSizeIssueIfNeeded(sourceFile, filePath, issues) { const lineCount = sourceFile.getEndLineNumber(); const issue = createFileSizeIssue(filePath, lineCount); if (issue) { issues.push(issue); } } function processSourceFileForFunctions(sourceFile, issues) { const filePath = path.relative(process.cwd(), sourceFile.getFilePath()); if (shouldSkipFile(filePath)) { return; } collectFunctionIssues(sourceFile, filePath, issues); } function shouldSkipFile(filePath) { return filePath.includes('.test.') || filePath.includes('.spec.') || filePath.endsWith('.d.ts'); } function createFileSizeIssue(filePath, lineCount) { if (lineCount > 300) { return { file: filePath, lines: lineCount, severity: 'critical' }; } else if (lineCount > 200) { return { file: filePath, lines: lineCount, severity: 'warn' }; } return null; } function collectFunctionIssues(sourceFile, filePath, issues) { const allFunctions = getAllFunctions(sourceFile); processFunctionSizes(allFunctions, filePath, issues); } function getAllFunctions(sourceFile) { const functions = sourceFile.getFunctions(); const methods = sourceFile.getClasses().flatMap((c) => c.getMethods()); return [...functions, ...methods]; } function processFunctionSizes(functions, filePath, issues) { functions.forEach(func => { const issue = createFunctionSizeIssue(filePath, func); if (issue) { issues.push(issue); } }); } function createFunctionSizeIssue(filePath, func) { const lineCount = func.getEndLineNumber() - func.getStartLineNumber() + 1; const functionName = func.getName() || 'anonymous'; return determineFunctionSizeIssue(filePath, functionName, lineCount); } function determineFunctionSizeIssue(filePath, functionName, lineCount) { if (lineCount > 12) { return { file: filePath, function: functionName, lines: lineCount, severity: 'critical' }; } else if (lineCount > 10) { return { file: filePath, function: functionName, lines: lineCount, severity: 'warn' }; } return null; } //# sourceMappingURL=file-size-checker.js.map