website refactor
This commit is contained in:
@@ -1,21 +1,19 @@
|
||||
/**
|
||||
* ESLint Rule: Mutation Contract
|
||||
*
|
||||
* Ensures mutations implement the Mutation contract
|
||||
* Ensures mutations return Result type
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
type: 'problem',
|
||||
docs: {
|
||||
description: 'Ensure mutations implement the Mutation contract',
|
||||
description: 'Ensure mutations return Result type',
|
||||
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>',
|
||||
wrongReturnType: 'Mutations must return Promise<Result<void, string>> - see apps/website/lib/contracts/Result.ts',
|
||||
},
|
||||
schema: [],
|
||||
},
|
||||
@@ -28,103 +26,45 @@ module.exports = {
|
||||
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) {
|
||||
if (node.key.type === 'Identifier' &&
|
||||
node.key.name === 'execute' &&
|
||||
node.value.type === 'FunctionExpression') {
|
||||
|
||||
const returnType = node.value.returnType;
|
||||
|
||||
// Check if it returns Promise<Result<...>>
|
||||
if (!returnType ||
|
||||
!returnType.typeAnnotation ||
|
||||
!returnType.typeAnnotation.typeName ||
|
||||
returnType.typeAnnotation.typeName.name !== 'Promise') {
|
||||
context.report({
|
||||
node: executeMethodNode,
|
||||
messageId: 'missingExecute',
|
||||
node,
|
||||
messageId: 'wrongReturnType',
|
||||
});
|
||||
} 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;
|
||||
}
|
||||
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) {
|
||||
// Check for Result type
|
||||
const typeArgs = returnType.typeAnnotation.typeParameters;
|
||||
if (!typeArgs || !typeArgs.params || typeArgs.params.length === 0) {
|
||||
context.report({
|
||||
node: classNode,
|
||||
messageId: 'noMutationContract',
|
||||
node,
|
||||
messageId: 'wrongReturnType',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const resultType = typeArgs.params[0];
|
||||
if (resultType.type !== 'TSTypeReference' ||
|
||||
!resultType.typeName ||
|
||||
(resultType.typeName.type === 'Identifier' && resultType.typeName.name !== 'Result')) {
|
||||
context.report({
|
||||
node,
|
||||
messageId: 'wrongReturnType',
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Check execute method signature
|
||||
if (executeMethodNode && executeMethodNode.value.params.length === 0) {
|
||||
context.report({
|
||||
node: executeMethodNode,
|
||||
messageId: 'wrongExecuteSignature',
|
||||
});
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user