@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
127 lines • 4.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MethodNameValidator = void 0;
const detectorPatterns_1 = require("../constants/detectorPatterns");
/**
* Validates repository method names for domain language compliance
*
* Ensures repository methods use domain language instead of
* technical database terminology.
*/
class MethodNameValidator {
ormMatcher;
domainMethodPatterns = [
/^findBy[A-Z]/,
/^findAll$/,
/^find[A-Z]/,
/^save$/,
/^saveAll$/,
/^create$/,
/^update$/,
/^delete$/,
/^deleteBy[A-Z]/,
/^deleteAll$/,
/^remove$/,
/^removeBy[A-Z]/,
/^removeAll$/,
/^add$/,
/^add[A-Z]/,
/^get[A-Z]/,
/^getAll$/,
/^search/,
/^list/,
/^has[A-Z]/,
/^is[A-Z]/,
/^exists$/,
/^exists[A-Z]/,
/^existsBy[A-Z]/,
/^clear[A-Z]/,
/^clearAll$/,
/^store[A-Z]/,
/^initialize$/,
/^initializeCollection$/,
/^close$/,
/^connect$/,
/^disconnect$/,
/^count$/,
/^countBy[A-Z]/,
];
constructor(ormMatcher) {
this.ormMatcher = ormMatcher;
}
/**
* Checks if a method name follows domain language conventions
*/
isDomainMethodName(methodName) {
if (this.ormMatcher.isTechnicalMethod(methodName)) {
return false;
}
return this.domainMethodPatterns.some((pattern) => pattern.test(methodName));
}
/**
* Suggests better domain method names
*/
suggestDomainMethodName(methodName) {
const lowerName = methodName.toLowerCase();
const suggestions = [];
this.collectSuggestions(lowerName, suggestions);
if (lowerName.includes("get") && lowerName.includes("all")) {
suggestions.push(detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.FIND_ALL, detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.LIST_ALL);
}
if (suggestions.length === 0) {
return detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.DEFAULT_SUGGESTION;
}
return `Consider: ${suggestions.slice(0, 3).join(", ")}`;
}
/**
* Collects method name suggestions based on keywords
*/
collectSuggestions(lowerName, suggestions) {
const suggestionMap = {
query: [
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.SEARCH,
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.FIND_BY_PROPERTY,
],
select: [
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.FIND_BY_PROPERTY,
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.GET_ENTITY,
],
insert: [
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.CREATE,
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.ADD_ENTITY,
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.STORE_ENTITY,
],
update: [
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.UPDATE,
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.MODIFY_ENTITY,
],
upsert: [
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.SAVE,
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.STORE_ENTITY,
],
remove: [
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.DELETE,
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.REMOVE_BY_PROPERTY,
],
fetch: [
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.FIND_BY_PROPERTY,
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.GET_ENTITY,
],
retrieve: [
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.FIND_BY_PROPERTY,
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.GET_ENTITY,
],
load: [
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.FIND_BY_PROPERTY,
detectorPatterns_1.REPOSITORY_METHOD_SUGGESTIONS.GET_ENTITY,
],
};
for (const [keyword, keywords] of Object.entries(suggestionMap)) {
if (lowerName.includes(keyword)) {
suggestions.push(...keywords);
}
}
}
}
exports.MethodNameValidator = MethodNameValidator;
//# sourceMappingURL=MethodNameValidator.js.map