UNPKG

@addon24/eslint-config

Version:

ESLint configuration rules for WorldOfTextcraft projects - Centralized configuration for all project types

82 lines (69 loc) 2.56 kB
/** * @fileoverview Verhindert die direkte Verwendung von Entities in Controller-Responses und erzwingt DTOs */ "use strict"; const requireDtoResponseRule = { meta: { type: "problem", docs: { description: "Verhindert die direkte Verwendung von Entities in Controller-Responses und erzwingt DTOs", category: "Security", recommended: true, }, fixable: null, schema: [], messages: { entityInResponse: "Entity-Variable '{{variableName}}' darf nicht direkt in sendSuccess() verwendet werden. Controller müssen Entities zu DTOs konvertieren bevor sie als API-Response zurückgegeben werden.", }, }, create(context) { const filename = context.getFilename(); // Nur Controller-Dateien prüfen, außer Tests if (!filename.includes("Controller.ts") || filename.includes("test") || filename.includes("spec")) { return {}; } // Entity-Pattern erkennen function isEntityPattern(variableName) { return ( variableName.endsWith("Definition") || variableName.endsWith("Entity") || variableName.endsWith("Model") || variableName.endsWith("Record") || variableName.endsWith("Instance") || variableName.startsWith("new") || variableName.includes("Entity") || variableName.includes("Definition") ); } return { CallExpression(node) { // Prüfe auf this.sendSuccess() Aufrufe if (node.callee?.type === "MemberExpression" && node.callee.object?.type === "ThisExpression" && node.callee.property?.name === "sendSuccess") { // Prüfe das zweite Argument (Response-Objekt) if (node.arguments.length >= 2 && node.arguments[1]?.type === "ObjectExpression") { const responseObject = node.arguments[1]; // Prüfe alle Properties im Response-Objekt responseObject.properties.forEach(property => { if (property.type === "Property" && property.value?.type === "Identifier") { const variableName = property.value.name; // Melde Entity-Patterns if (isEntityPattern(variableName)) { context.report({ node: property.value, messageId: "entityInResponse", data: { variableName } }); } } }); } } } }; } }; export default requireDtoResponseRule;