UNPKG

eslint-plugin-goblock-custom-rules

Version:

ESLint plugin to enforce pattern rules in Clean Architecture (goBlockchain)

73 lines (72 loc) 2.53 kB
"use strict"; /* * goAssets - API * Copyright (C) 2024 goBlockchain * All rights reserved. * * This software is the confidential and proprietary information of goBlockchain. * You shall not disclose such confidential information and shall use it only in * accordance with the terms of the license agreement you entered into with goBlockchain. * * Unauthorized copying of this file, via any medium, is strictly prohibited. * Licensed under the goBlockchain license agreement. */ Object.defineProperty(exports, "__esModule", { value: true }); const noFindOutsideRepositoryRule = { defaultOptions: [], meta: { type: "problem", docs: { description: "Disallow find functions outside repository layer", recommended: "recommended", }, messages: { noFindOutsideRepository: 'Functions starting with "find" are only allowed in repository layer', }, schema: [], }, create(context) { const checkFindFunction = (node) => { const filePath = context.filename; const functionName = extractFunctionName(node); if (!(functionName === null || functionName === void 0 ? void 0 : functionName.startsWith("find"))) { return; } if (!filePath.includes("repository") && (filePath.includes("service") || filePath.includes("controller"))) { context.report({ node, messageId: "noFindOutsideRepository", }); } }; return { FunctionDeclaration: (node) => { checkFindFunction(node); }, ArrowFunctionExpression: (node) => { checkFindFunction(node); }, FunctionExpression: (node) => { checkFindFunction(node); }, }; }, }; const extractFunctionName = (node) => { var _a, _b, _c; if (node.type === "FunctionDeclaration") { return (_a = node.id) === null || _a === void 0 ? void 0 : _a.name; } if (!node.parent) { return undefined; } if (node.parent.type === "VariableDeclarator") { return (_b = node.parent.id) === null || _b === void 0 ? void 0 : _b.name; } if (node.parent.type === "MethodDefinition") { return (_c = node.parent.key) === null || _c === void 0 ? void 0 : _c.name; } return undefined; }; exports.default = noFindOutsideRepositoryRule;