@diullei/codeguardian
Version:
Open-source developer tool to validate and enforce architectural rules, especially for AI-generated code
46 lines • 1.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectLinesRule = void 0;
const core_1 = require("../core");
class SelectLinesRule extends core_1.SelectorRule {
pattern;
includeContext;
constructor(id, pattern, includeContext = 0) {
super(id);
this.pattern = pattern;
this.includeContext = includeContext;
}
async select(context) {
const content = context.currentItem;
if (!content || typeof content !== 'string') {
return [];
}
const lines = content.split('\n');
const matches = [];
lines.forEach((line, index) => {
if (this.pattern.test(line)) {
matches.push({
lineNumber: index + 1,
content: line,
context: this.getContext(lines, index, this.includeContext),
});
}
});
return matches;
}
getContext(lines, index, contextSize) {
if (contextSize === 0)
return [];
const context = [];
const start = Math.max(0, index - contextSize);
const end = Math.min(lines.length - 1, index + contextSize);
for (let i = start; i <= end; i++) {
if (i !== index) {
context.push(lines[i]);
}
}
return context;
}
}
exports.SelectLinesRule = SelectLinesRule;
//# sourceMappingURL=SelectLinesRule.js.map