@samiyev/guardian
Version:
Research-backed code quality guardian for AI-assisted development. Detects hardcodes, secrets, circular deps, framework leaks, entity exposure, and 9 architecture violations. Enforces Clean Architecture/DDD principles. Works with GitHub Copilot, Cursor, W
73 lines • 2.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AstBooleanAnalyzer = void 0;
const HardcodedValue_1 = require("../../domain/value-objects/HardcodedValue");
const rules_1 = require("../../shared/constants/rules");
/**
* AST-based analyzer for detecting magic booleans
*
* Detects boolean literals used as arguments without clear meaning.
* Example: doSomething(true, false, true) - hard to understand
* Better: doSomething({ sync: true, validate: false, cache: true })
*/
class AstBooleanAnalyzer {
contextChecker;
constructor(contextChecker) {
this.contextChecker = contextChecker;
}
/**
* Analyzes a boolean node and returns a violation if it's a magic boolean
*/
analyze(node, lines) {
if (!this.shouldDetect(node)) {
return null;
}
const value = node.text === rules_1.DETECTION_VALUES.BOOLEAN_TRUE;
return this.createViolation(node, value, lines);
}
/**
* Checks if boolean should be detected
*/
shouldDetect(node) {
if (this.contextChecker.isInExportedConstant(node)) {
return false;
}
if (this.contextChecker.isInTypeContext(node)) {
return false;
}
if (this.contextChecker.isInTestDescription(node)) {
return false;
}
const parent = node.parent;
if (!parent) {
return false;
}
if (parent.type === "arguments") {
return this.isInFunctionCallWithMultipleBooleans(parent);
}
return false;
}
/**
* Checks if function call has multiple boolean arguments
*/
isInFunctionCallWithMultipleBooleans(argsNode) {
let booleanCount = 0;
for (const child of argsNode.children) {
if (child.type === "true" || child.type === "false") {
booleanCount++;
}
}
return booleanCount >= 2;
}
/**
* Creates a HardcodedValue violation from a boolean node
*/
createViolation(node, value, lines) {
const lineNumber = node.startPosition.row + 1;
const column = node.startPosition.column;
const context = lines[node.startPosition.row]?.trim() ?? "";
return HardcodedValue_1.HardcodedValue.create(value, rules_1.HARDCODE_TYPES.MAGIC_BOOLEAN, lineNumber, column, context);
}
}
exports.AstBooleanAnalyzer = AstBooleanAnalyzer;
//# sourceMappingURL=AstBooleanAnalyzer.js.map