133 lines
4.0 KiB
JavaScript
133 lines
4.0 KiB
JavaScript
/**
|
|
* ESLint Rule: Mutation Contract
|
|
*
|
|
* Ensures mutations implement the Mutation contract
|
|
*/
|
|
|
|
module.exports = {
|
|
meta: {
|
|
type: 'problem',
|
|
docs: {
|
|
description: 'Ensure mutations implement the Mutation contract',
|
|
category: 'Mutation Contract',
|
|
recommended: true,
|
|
},
|
|
messages: {
|
|
noMutationContract: 'Mutations must implement the Mutation interface from lib/contracts/mutations/Mutation.ts',
|
|
missingExecute: 'Mutations must have an execute method that takes input and returns Promise',
|
|
wrongExecuteSignature: 'Execute method must have signature: execute(input: TInput): Promise<TOutput>',
|
|
},
|
|
schema: [],
|
|
},
|
|
|
|
create(context) {
|
|
const filename = context.getFilename();
|
|
|
|
// Only apply to mutation files
|
|
if (!filename.includes('/lib/mutations/') || !filename.endsWith('.ts')) {
|
|
return {};
|
|
}
|
|
|
|
let hasMutationImport = false;
|
|
let hasExecuteMethod = false;
|
|
let implementsMutation = false;
|
|
let executeMethodNode = null;
|
|
|
|
return {
|
|
// Check for Mutation import
|
|
ImportDeclaration(node) {
|
|
if (node.source.value === '@/lib/contracts/mutations/Mutation') {
|
|
hasMutationImport = true;
|
|
}
|
|
},
|
|
|
|
// Check for implements clause
|
|
ClassDeclaration(node) {
|
|
if (node.implements) {
|
|
node.implements.forEach(impl => {
|
|
if (impl.type === 'Identifier' && impl.name === 'Mutation') {
|
|
implementsMutation = true;
|
|
}
|
|
if (impl.type === 'TSExpressionWithTypeArguments' &&
|
|
impl.expression.type === 'Identifier' &&
|
|
impl.expression.name === 'Mutation') {
|
|
implementsMutation = true;
|
|
}
|
|
});
|
|
}
|
|
},
|
|
|
|
// Check for execute method
|
|
MethodDefinition(node) {
|
|
if (node.key.type === 'Identifier' && node.key.name === 'execute') {
|
|
hasExecuteMethod = true;
|
|
executeMethodNode = node;
|
|
}
|
|
},
|
|
|
|
'Program:exit'() {
|
|
// Skip if file doesn't look like a mutation
|
|
const isMutationFile = filename.includes('/lib/mutations/') &&
|
|
filename.endsWith('.ts') &&
|
|
!filename.endsWith('.test.ts');
|
|
|
|
if (!isMutationFile) return;
|
|
|
|
// Check if it's a class-based mutation
|
|
const hasClass = filename.includes('/lib/mutations/') &&
|
|
!filename.endsWith('.test.ts');
|
|
|
|
if (hasClass && !hasExecuteMethod) {
|
|
if (executeMethodNode) {
|
|
context.report({
|
|
node: executeMethodNode,
|
|
messageId: 'missingExecute',
|
|
});
|
|
} else {
|
|
// Find the class node to report on
|
|
const sourceCode = context.getSourceCode();
|
|
const classNode = sourceCode.ast.body.find(node =>
|
|
node.type === 'ClassDeclaration' &&
|
|
node.id &&
|
|
node.id.name.endsWith('Mutation')
|
|
);
|
|
if (classNode) {
|
|
context.report({
|
|
node: classNode,
|
|
messageId: 'missingExecute',
|
|
});
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Check for contract implementation (if it's a class)
|
|
if (hasClass && !implementsMutation && hasMutationImport) {
|
|
// Find the class node to report on
|
|
const sourceCode = context.getSourceCode();
|
|
const classNode = sourceCode.ast.body.find(node =>
|
|
node.type === 'ClassDeclaration' &&
|
|
node.id &&
|
|
node.id.name.endsWith('Mutation')
|
|
);
|
|
if (classNode) {
|
|
context.report({
|
|
node: classNode,
|
|
messageId: 'noMutationContract',
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Check execute method signature
|
|
if (executeMethodNode && executeMethodNode.value.params.length === 0) {
|
|
context.report({
|
|
node: executeMethodNode,
|
|
messageId: 'wrongExecuteSignature',
|
|
});
|
|
}
|
|
},
|
|
};
|
|
},
|
|
};
|