refakts
Version:
TypeScript refactoring tool built for AI coding agents to perform precise refactoring operations via command line instead of requiring complete code regeneration.
74 lines • 3.6 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.commentCheck = void 0;
const ts_morph_1 = require("ts-morph");
const path = __importStar(require("path"));
exports.commentCheck = {
name: 'comment',
check: (sourceDir) => {
const project = new ts_morph_1.Project();
project.addSourceFilesAtPaths(`${sourceDir}/**/*.ts`);
return project.getSourceFiles().flatMap(findCommentsInFile);
},
getGroupDefinition: (groupKey) => groupKey === 'comment' ? {
title: 'COMMENTS DETECTED',
description: 'Comments indicate code that is not self-documenting.',
actionGuidance: 'Extract complex logic into well-named functions instead of explaining with comments. Remove ALL comments unless they impact functionality.'
} : undefined
};
const findCommentsInFile = (sourceFile) => {
const filePath = path.relative(process.cwd(), sourceFile.getFilePath());
if (shouldSkipFile(filePath))
return [];
const singleComments = sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.SingleLineCommentTrivia)
.filter((comment) => isValidSingleComment(comment.getText().trim()))
.map((comment) => createCommentIssue(filePath, comment, 'single'));
const multiComments = sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.MultiLineCommentTrivia)
.filter((comment) => isValidMultiComment(comment.getText().trim()))
.map((comment) => createCommentIssue(filePath, comment, 'multi'));
return [...singleComments, ...multiComments];
};
const createCommentIssue = (filePath, comment, type) => ({
type: 'comment',
message: `${type}-line comment: "${truncateText(comment.getText().trim())}"`,
file: filePath,
line: comment.getStartLineNumber()
});
const isValidSingleComment = (text) => text.startsWith('//') && !text.startsWith('// @') && text.length >= 10;
const isValidMultiComment = (text) => !text.startsWith('/**') && text.length >= 15;
const truncateText = (text) => text.length > 50 ? text.substring(0, 50) + '...' : text;
const shouldSkipFile = (filePath) => filePath.includes('.test.') || filePath.includes('.spec.') || filePath.endsWith('.d.ts');
//# sourceMappingURL=comment-check.js.map