UNPKG

reporemix

Version:

A opiniated repomix tool for Rust and NextJS projects.

47 lines (46 loc) 2.2 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.computeMetrics = computeMetrics; const tree_sitter_parser_1 = require("../tree-sitter-parser"); function computeMetrics(content, filePath) { return __awaiter(this, void 0, void 0, function* () { const tree = yield tree_sitter_parser_1.treeSitterParser.parseFile(content, filePath); if (!tree) return { complexity: 0, maxNesting: 0, commentRatio: 0 }; let complexity = 1; // Base let maxNesting = 0; let currentNesting = 0; let codeLength = 0; let commentLength = 0; function traverse(node, depth) { currentNesting = Math.max(currentNesting, depth); maxNesting = Math.max(maxNesting, currentNesting); if (['if_statement', 'for_statement', 'while_statement', 'switch_statement'].includes(node.type)) { complexity++; } if (node.type === 'comment') { commentLength += node.text.length; } else { codeLength += node.text.length; } for (let i = 0; i < node.childCount; i++) { const child = node.child(i); if (child) traverse(child, depth + 1); } } traverse(tree.rootNode, 0); const commentRatio = codeLength > 0 ? commentLength / codeLength : 0; return { complexity, maxNesting, commentRatio }; }); }