@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
71 lines • 3.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AstNamingTraverser = void 0;
const constants_1 = require("../../../shared/constants");
/**
* AST tree traverser for detecting naming convention violations
*
* Walks through the Abstract Syntax Tree and uses analyzers
* to detect naming violations in classes, interfaces, functions, and variables.
*/
class AstNamingTraverser {
classAnalyzer;
interfaceAnalyzer;
functionAnalyzer;
variableAnalyzer;
nodeHandlers;
constructor(classAnalyzer, interfaceAnalyzer, functionAnalyzer, variableAnalyzer) {
this.classAnalyzer = classAnalyzer;
this.interfaceAnalyzer = interfaceAnalyzer;
this.functionAnalyzer = functionAnalyzer;
this.variableAnalyzer = variableAnalyzer;
this.nodeHandlers = this.buildNodeHandlers();
}
/**
* Traverses the AST tree and collects naming violations
*/
traverse(tree, sourceCode, layer, filePath) {
const results = [];
const lines = sourceCode.split("\n");
const cursor = tree.walk();
this.visit(cursor, lines, layer, filePath, results);
return results;
}
buildNodeHandlers() {
const handlers = new Map();
handlers.set(constants_1.AST_CLASS_TYPES.CLASS_DECLARATION, (node, layer, filePath, lines) => this.classAnalyzer.analyze(node, layer, filePath, lines));
handlers.set(constants_1.AST_CLASS_TYPES.INTERFACE_DECLARATION, (node, layer, filePath, lines) => this.interfaceAnalyzer.analyze(node, layer, filePath, lines));
const functionHandler = (node, layer, filePath, lines) => this.functionAnalyzer.analyze(node, layer, filePath, lines);
handlers.set(constants_1.AST_FUNCTION_TYPES.FUNCTION_DECLARATION, functionHandler);
handlers.set(constants_1.AST_FUNCTION_TYPES.METHOD_DEFINITION, functionHandler);
handlers.set(constants_1.AST_FUNCTION_TYPES.FUNCTION_SIGNATURE, functionHandler);
const variableHandler = (node, layer, filePath, lines) => this.variableAnalyzer.analyze(node, layer, filePath, lines);
handlers.set(constants_1.AST_VARIABLE_TYPES.VARIABLE_DECLARATOR, variableHandler);
handlers.set(constants_1.AST_VARIABLE_TYPES.REQUIRED_PARAMETER, variableHandler);
handlers.set(constants_1.AST_VARIABLE_TYPES.OPTIONAL_PARAMETER, variableHandler);
handlers.set(constants_1.AST_VARIABLE_TYPES.PUBLIC_FIELD_DEFINITION, variableHandler);
handlers.set(constants_1.AST_VARIABLE_TYPES.PROPERTY_SIGNATURE, variableHandler);
return handlers;
}
/**
* Recursively visits AST nodes
*/
visit(cursor, lines, layer, filePath, results) {
const node = cursor.currentNode;
const handler = this.nodeHandlers.get(node.type);
if (handler) {
const violation = handler(node, layer, filePath, lines);
if (violation) {
results.push(violation);
}
}
if (cursor.gotoFirstChild()) {
do {
this.visit(cursor, lines, layer, filePath, results);
} while (cursor.gotoNextSibling());
cursor.gotoParent();
}
}
}
exports.AstNamingTraverser = AstNamingTraverser;
//# sourceMappingURL=AstNamingTraverser.js.map