UNPKG

@mutasim77/loc-counter

Version:

A powerful lines of code counter with detailed statistics

184 lines (183 loc) 6.77 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.countLines = countLines; exports.aggregateStats = aggregateStats; const path = __importStar(require("path")); const languages_1 = require("../constants/languages"); const fileUtils_1 = require("./fileUtils"); async function countLines(filePath) { const extension = path.extname(filePath); const language = (0, languages_1.getLanguageByExtension)(extension); if (!language) { return null; } const content = await (0, fileUtils_1.readFileContent)(filePath); if (!content) { return null; } const stats = analyzeContent(content, language); return { path: filePath, language: language.name, size: content.length, ...stats }; } function analyzeContent(content, language) { const lines = content.split('\n'); const stats = { total: lines.length, code: 0, comment: 0, blank: 0 }; let inMultiLineComment = false; let currentMultiLineCommentEnd = ''; lines.forEach(line => { var _a, _b, _c; const trimmedLine = line.trim(); // if it's a blank line if (trimmedLine === '') { stats.blank++; return; } // if we're inside a multi-line comment if (inMultiLineComment) { stats.comment++; // if the multi-line comment ends on this line if (trimmedLine.includes(currentMultiLineCommentEnd)) { inMultiLineComment = false; currentMultiLineCommentEnd = ''; // And if there's code after the comment end; ++ code count const codeAfterComment = (_a = trimmedLine.split(currentMultiLineCommentEnd)[1]) === null || _a === void 0 ? void 0 : _a.trim(); if (codeAfterComment && !isLineStartingWithComment(codeAfterComment, language)) { stats.code++; stats.comment--; } } return; } // if line starts with a single-line comment if ((_b = language.commentPatterns.singleLine) === null || _b === void 0 ? void 0 : _b.some(pattern => trimmedLine.startsWith(pattern))) { stats.comment++; return; } // if line contains a multi-line comment start const multiLineCommentStart = (_c = language.commentPatterns.multiLine) === null || _c === void 0 ? void 0 : _c.find(pattern => trimmedLine.includes(pattern.start)); if (multiLineCommentStart) { const startIndex = trimmedLine.indexOf(multiLineCommentStart.start); // if there's code before the comment if (startIndex > 0 && trimmedLine.substring(0, startIndex).trim() !== '') { stats.code++; } else { stats.comment++; } // if the multi-line comment ends on the same line if (trimmedLine.includes(multiLineCommentStart.end)) { // if there's code after the comment const endIndex = trimmedLine.indexOf(multiLineCommentStart.end) + multiLineCommentStart.end.length; if (endIndex < trimmedLine.length && trimmedLine.substring(endIndex).trim() !== '') { stats.code++; } } else { inMultiLineComment = true; currentMultiLineCommentEnd = multiLineCommentStart.end; } return; } // here --> it's a code line stats.code++; }); return stats; } function isLineStartingWithComment(line, language) { var _a, _b; // single-line comment if ((_a = language.commentPatterns.singleLine) === null || _a === void 0 ? void 0 : _a.some(pattern => line.startsWith(pattern))) { return true; } // multi-line comment if ((_b = language.commentPatterns.multiLine) === null || _b === void 0 ? void 0 : _b.some(pattern => line.startsWith(pattern.start))) { return true; } return false; } function aggregateStats(fileStats) { const total = { total: 0, code: 0, comment: 0, blank: 0 }; const languageMap = {}; fileStats.forEach(stat => { total.total += stat.total; total.code += stat.code; total.comment += stat.comment; total.blank += stat.blank; if (!languageMap[stat.language]) { languageMap[stat.language] = { files: 0, total: 0, code: 0, comment: 0, blank: 0 }; } languageMap[stat.language].files += 1; languageMap[stat.language].total += stat.total; languageMap[stat.language].code += stat.code; languageMap[stat.language].comment += stat.comment; languageMap[stat.language].blank += stat.blank; }); const languages = Object.entries(languageMap).map(([language, stats]) => ({ language, files: stats.files, total: stats.total, code: stats.code, comment: stats.comment, blank: stats.blank, percentage: (stats.code / total.code) * 100 })); languages.sort((a, b) => b.code - a.code); return { files: fileStats, languages, total }; }