@diullei/codeguardian
Version:
Open-source developer tool to validate and enforce architectural rules, especially for AI-generated code
109 lines • 3.88 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AssertLineCountRule = void 0;
const core_1 = require("../core");
class AssertLineCountRule extends core_1.AssertionRule {
operator;
expectedValue;
message;
suggestion;
documentation;
constructor(id, operator, expectedValue, message, suggestion, documentation) {
super(id);
this.operator = operator;
this.expectedValue = expectedValue;
this.message = message;
this.suggestion = suggestion;
this.documentation = documentation;
}
async assert(item, _context) {
const lineCount = this.getLineCount(item);
return this.compareValues(lineCount, this.expectedValue, this.operator);
}
async assertWithDetails(item, _context) {
const lineCount = this.getLineCount(item);
const passed = this.compareValues(lineCount, this.expectedValue, this.operator);
if (!passed) {
const itemType = this.getItemType(item);
const filePath = this.getFilePath(item);
const defaultMessage = this.message ||
`${itemType} has ${lineCount} lines, expected ${this.operator} ${this.expectedValue}`;
return {
passed: false,
message: defaultMessage,
context: {
code: filePath ? `File: ${filePath} (${lineCount} lines)` : `Lines: ${lineCount}`,
suggestion: this.suggestion ||
(lineCount > this.expectedValue ? 'Consider breaking this file into smaller modules' : undefined),
documentation: this.documentation,
},
};
}
return { passed: true };
}
getLineCount(item) {
if (typeof item === 'string') {
return this.countLines(item);
}
if (item && typeof item === 'object') {
if ('content' in item && typeof item.content === 'string') {
return this.countLines(item.content);
}
if ('text' in item && typeof item.text === 'string') {
return this.countLines(item.text);
}
if ('lineCount' in item && typeof item.lineCount === 'number') {
return item.lineCount;
}
}
return 0;
}
countLines(text) {
if (!text)
return 0;
const lines = text.split(/\r?\n/);
while (lines.length > 0 && lines[lines.length - 1] === '') {
lines.pop();
}
return lines.length;
}
compareValues(actual, expected, operator) {
switch (operator) {
case '==':
return actual === expected;
case '!=':
return actual !== expected;
case '>':
return actual > expected;
case '>=':
return actual >= expected;
case '<':
return actual < expected;
case '<=':
return actual <= expected;
default:
throw new Error(`Unsupported operator for line count comparison: ${operator}`);
}
}
getItemType(item) {
if (typeof item === 'string')
return 'text';
if (item && typeof item === 'object') {
if ('content' in item && 'path' in item)
return 'file';
if ('path' in item)
return 'file path';
if ('text' in item)
return 'text content';
}
return 'item';
}
getFilePath(item) {
if (item && typeof item === 'object' && 'path' in item) {
return String(item.path);
}
return undefined;
}
}
exports.AssertLineCountRule = AssertLineCountRule;
//# sourceMappingURL=AssertLineCountRule.js.map