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.

106 lines 4.05 kB
#!/usr/bin/env node "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.findComments = findComments; const ts_morph_1 = require("ts-morph"); const path = __importStar(require("path")); function findComments(srcDir) { const project = createProject(srcDir); return processAllSourceFiles(project); } function processAllSourceFiles(project) { const issues = []; for (const sourceFile of project.getSourceFiles()) { processSourceFile(sourceFile, issues); } return issues; } function processSourceFile(sourceFile, issues) { const filePath = getRelativeFilePath(sourceFile); if (shouldSkipFile(filePath)) { return; } collectAllComments(sourceFile, filePath, issues); } function createProject(srcDir) { const project = new ts_morph_1.Project(); project.addSourceFilesAtPaths(`${srcDir}/**/*.ts`); return project; } function collectAllComments(sourceFile, filePath, issues) { collectSingleLineComments(sourceFile, filePath, issues); collectMultiLineComments(sourceFile, filePath, issues); } function getRelativeFilePath(sourceFile) { return path.relative(process.cwd(), sourceFile.getFilePath()); } function shouldSkipFile(filePath) { return filePath.includes('.test.') || filePath.includes('.spec.') || filePath.endsWith('.d.ts'); } function collectSingleLineComments(sourceFile, filePath, issues) { const singleComments = sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.SingleLineCommentTrivia); for (const comment of singleComments) { const text = comment.getText().trim(); if (isValidSingleLineComment(text)) { issues.push(createCommentIssue(filePath, comment, text, 'single')); } } } function collectMultiLineComments(sourceFile, filePath, issues) { const multiComments = sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.MultiLineCommentTrivia); for (const comment of multiComments) { const text = comment.getText().trim(); if (isValidMultiLineComment(text)) { const truncatedText = text.substring(0, 50) + '...'; issues.push(createCommentIssue(filePath, comment, truncatedText, 'multi')); } } } function isValidSingleLineComment(text) { return text.startsWith('//') && !text.startsWith('// @') && text.length >= 10; } function isValidMultiLineComment(text) { return !text.startsWith('/**') && text.length >= 15; } function createCommentIssue(filePath, comment, text, type) { return { file: filePath, line: comment.getStartLineNumber(), text: text, type: type }; } //# sourceMappingURL=comment-detector.js.map