Files
gridpilot.gg/apps/api/eslint-rules/api-controller-rules.js
2026-01-16 11:51:12 +01:00

53 lines
1.7 KiB
JavaScript

module.exports = {
meta: {
type: 'problem',
docs: {
description: 'Enforce that API controllers only call Use Cases or Application Services',
category: 'Architecture',
recommended: true,
},
fixable: null,
schema: [],
messages: {
forbiddenCall: 'Controllers should only call Use Cases or Application Services. Forbidden call to "{{name}}".',
},
},
create(context) {
return {
ClassDeclaration(node) {
const isController = node.decorators && node.decorators.some(d =>
d.expression.type === 'CallExpression' &&
d.expression.callee.name === 'Controller'
);
if (!isController) return;
// Check constructor for injected dependencies
const constructor = node.body.body.find(m => m.kind === 'constructor');
if (constructor) {
constructor.value.params.forEach(param => {
if (param.type === 'TSParameterProperty') {
const typeAnnotation = param.parameter.typeAnnotation;
if (typeAnnotation && typeAnnotation.typeAnnotation.type === 'TSTypeReference') {
const typeName = typeAnnotation.typeAnnotation.typeName.name;
const isValidDependency = /UseCase$/.test(typeName) || /Service$/.test(typeName) || /Logger$/.test(typeName) || /Module$/.test(typeName);
if (!isValidDependency) {
context.report({
node: param,
messageId: 'forbiddenCall',
data: {
name: typeName,
},
});
}
}
}
});
}
},
};
},
};