UNPKG

archunit

Version:

ArchUnit TypeScript is an architecture testing library, to specify and assert architecture rules in your TypeScript app

209 lines 6.98 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FunctionCountMetric = exports.InterfaceCountMetric = exports.ClassCountMetric = exports.ImportCountMetric = exports.StatementCountMetric = exports.LinesOfCodeMetric = exports.FieldCountMetric = exports.MethodCountMetric = void 0; const ts = __importStar(require("typescript")); /** * Counts the number of methods in a class */ class MethodCountMetric { constructor() { this.name = 'MethodCount'; this.description = 'Counts the number of methods in a class'; } calculate(classInfo) { return classInfo.methods.length; } } exports.MethodCountMetric = MethodCountMetric; /** * Counts the number of fields/properties in a class */ class FieldCountMetric { constructor() { this.name = 'FieldCount'; this.description = 'Counts the number of fields/properties in a class'; } calculate(classInfo) { return classInfo.fields.length; } } exports.FieldCountMetric = FieldCountMetric; /** * Counts the total lines of code in a file */ class LinesOfCodeMetric { constructor() { this.name = 'LinesOfCode'; this.description = 'Counts the total lines of code in a file (excluding empty lines and comments)'; } calculateFromFile(sourceFile) { const text = sourceFile.getFullText(); const lines = text.split('\n'); // Count non-empty lines that aren't just whitespace or comments return lines.filter((line) => { const trimmed = line.trim(); return (trimmed.length > 0 && !trimmed.startsWith('//') && !trimmed.startsWith('/*') && !trimmed.startsWith('*') && trimmed !== '*/'); }).length; } } exports.LinesOfCodeMetric = LinesOfCodeMetric; /** * Counts the total number of statements in a file */ class StatementCountMetric { constructor() { this.name = 'StatementCount'; this.description = 'Counts the total number of statements in a file'; } calculateFromFile(sourceFile) { let statementCount = 0; function visit(node) { // Count various types of statements if (ts.isExpressionStatement(node) || ts.isVariableStatement(node) || ts.isReturnStatement(node) || ts.isIfStatement(node) || ts.isForStatement(node) || ts.isWhileStatement(node) || ts.isDoStatement(node) || ts.isSwitchStatement(node) || ts.isBreakStatement(node) || ts.isContinueStatement(node) || ts.isThrowStatement(node) || ts.isTryStatement(node) || ts.isWithStatement(node) || ts.isLabeledStatement(node) || ts.isDebuggerStatement(node)) { statementCount++; } ts.forEachChild(node, visit); } visit(sourceFile); return statementCount; } } exports.StatementCountMetric = StatementCountMetric; /** * Counts the number of import statements in a file */ class ImportCountMetric { constructor() { this.name = 'ImportCount'; this.description = 'Counts the number of import statements in a file'; } calculateFromFile(sourceFile) { let importCount = 0; function visit(node) { if (ts.isImportDeclaration(node)) { importCount++; } ts.forEachChild(node, visit); } visit(sourceFile); return importCount; } } exports.ImportCountMetric = ImportCountMetric; /** * Counts the number of classes in a file */ class ClassCountMetric { constructor() { this.name = 'ClassCount'; this.description = 'Counts the number of classes in a file'; } calculateFromFile(sourceFile) { let classCount = 0; function visit(node) { if (ts.isClassDeclaration(node)) { classCount++; } ts.forEachChild(node, visit); } visit(sourceFile); return classCount; } } exports.ClassCountMetric = ClassCountMetric; /** * Counts the number of interfaces in a file */ class InterfaceCountMetric { constructor() { this.name = 'InterfaceCount'; this.description = 'Counts the number of interfaces in a file'; } calculateFromFile(sourceFile) { let interfaceCount = 0; function visit(node) { if (ts.isInterfaceDeclaration(node)) { interfaceCount++; } ts.forEachChild(node, visit); } visit(sourceFile); return interfaceCount; } } exports.InterfaceCountMetric = InterfaceCountMetric; /** * Counts the number of functions in a file (excluding methods) */ class FunctionCountMetric { constructor() { this.name = 'FunctionCount'; this.description = 'Counts the number of functions in a file (excluding class methods)'; } calculateFromFile(sourceFile) { let functionCount = 0; function visit(node) { // Count function declarations and function expressions at module level if ((ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node)) && !isInsideClass(node)) { functionCount++; } ts.forEachChild(node, visit); } function isInsideClass(node) { let parent = node.parent; while (parent) { if (ts.isClassDeclaration(parent)) { return true; } parent = parent.parent; } return false; } visit(sourceFile); return functionCount; } } exports.FunctionCountMetric = FunctionCountMetric; //# sourceMappingURL=count.js.map