UNPKG

@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

90 lines 3.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RepositoryPatternDetector = void 0; const OrmTypeMatcher_1 = require("../strategies/OrmTypeMatcher"); const MethodNameValidator_1 = require("../strategies/MethodNameValidator"); const RepositoryFileAnalyzer_1 = require("../strategies/RepositoryFileAnalyzer"); const RepositoryViolationDetector_1 = require("../strategies/RepositoryViolationDetector"); /** * Detects Repository Pattern violations in the codebase * * This detector identifies violations where the Repository Pattern is not properly implemented: * 1. ORM-specific types in repository interfaces (domain should be ORM-agnostic) * 2. Concrete repository usage in use cases (violates dependency inversion) * 3. Repository instantiation with 'new' in use cases (should use DI) * 4. Non-domain method names in repositories (should use ubiquitous language) * * @example * ```typescript * const detector = new RepositoryPatternDetector() * * // Detect violations in a repository interface * const code = ` * interface IUserRepository { * findOne(query: Prisma.UserWhereInput): Promise<User> * } * ` * const violations = detector.detectViolations( * code, * 'src/domain/repositories/IUserRepository.ts', * 'domain' * ) * * // violations will contain ORM type violation * console.log(violations.length) // 1 * console.log(violations[0].violationType) // 'orm-type-in-interface' * ``` */ class RepositoryPatternDetector { ormMatcher; methodValidator; fileAnalyzer; violationDetector; constructor() { this.ormMatcher = new OrmTypeMatcher_1.OrmTypeMatcher(); this.methodValidator = new MethodNameValidator_1.MethodNameValidator(this.ormMatcher); this.fileAnalyzer = new RepositoryFileAnalyzer_1.RepositoryFileAnalyzer(); this.violationDetector = new RepositoryViolationDetector_1.RepositoryViolationDetector(this.ormMatcher, this.methodValidator); } /** * Detects all Repository Pattern violations in the given code */ detectViolations(code, filePath, layer) { const violations = []; if (this.fileAnalyzer.isRepositoryInterface(filePath, layer)) { violations.push(...this.violationDetector.detectOrmTypes(code, filePath, layer)); violations.push(...this.violationDetector.detectNonDomainMethods(code, filePath, layer)); } if (this.fileAnalyzer.isUseCase(filePath, layer)) { violations.push(...this.violationDetector.detectConcreteRepositoryUsage(code, filePath, layer)); violations.push(...this.violationDetector.detectNewInstantiation(code, filePath, layer)); } return violations; } /** * Checks if a type is an ORM-specific type */ isOrmType(typeName) { return this.ormMatcher.isOrmType(typeName); } /** * Checks if a method name follows domain language conventions */ isDomainMethodName(methodName) { return this.methodValidator.isDomainMethodName(methodName); } /** * Checks if a file is a repository interface */ isRepositoryInterface(filePath, layer) { return this.fileAnalyzer.isRepositoryInterface(filePath, layer); } /** * Checks if a file is a use case */ isUseCase(filePath, layer) { return this.fileAnalyzer.isUseCase(filePath, layer); } } exports.RepositoryPatternDetector = RepositoryPatternDetector; //# sourceMappingURL=RepositoryPatternDetector.js.map