53 lines
1.7 KiB
JavaScript
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,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
},
|
|
};
|
|
},
|
|
};
|