import { defineRule } from "@oxlint/plugins"; import type { ESTree } from "@oxlint/plugins"; import { createTypeAliasEnvironment, resolvedTypeMatches, type TypeAliasEnvironment, } from "../shared/type-alias-resolution.ts"; type FunctionWithReturnType = | ESTree.ArrowFunctionExpression | ESTree.Function | ESTree.TSCallSignatureDeclaration | ESTree.TSConstructSignatureDeclaration | ESTree.TSConstructorType | ESTree.TSFunctionType | ESTree.TSMethodSignature; /** Ban function contracts that return unknown instead of a parsed domain type. */ export const noUnknownReturnsRule = defineRule({ meta: { type: "problem", docs: { description: "Disallow functions whose explicit return contract is unknown or Promise.", }, messages: { unknownReturn: "This function exposes `unknown` to its caller. Parse the value at its boundary and return a named domain type.", }, }, createOnce(context) { let environment: TypeAliasEnvironment | null = null; const resolvesToUnknown = (type: ESTree.TSType): boolean => environment !== null && resolvedTypeMatches(type, environment, (resolved, matches) => { if (resolved.type === "TSUnknownKeyword") return true; if (resolved.type === "TSParenthesizedType") { return matches(resolved.typeAnnotation); } if (resolved.type === "TSUnionType") return resolved.types.some(matches); if ( resolved.type !== "TSTypeReference" || resolved.typeName.type !== "Identifier" || (resolved.typeName.name !== "Promise" && resolved.typeName.name !== "PromiseLike") ) { return false; } const value = resolved.typeArguments?.params[0]; return value !== undefined && matches(value); }); const checkReturnType = (node: FunctionWithReturnType) => { const annotation = node.returnType; if (annotation === null || annotation === undefined) return; if (!resolvesToUnknown(annotation.typeAnnotation)) return; context.report({ node: annotation.typeAnnotation, messageId: "unknownReturn" }); }; return { Program(node) { environment = createTypeAliasEnvironment( node, context.sourceCode.visitorKeys, ); }, ArrowFunctionExpression: checkReturnType, FunctionDeclaration: checkReturnType, FunctionExpression: checkReturnType, TSCallSignatureDeclaration: checkReturnType, TSConstructSignatureDeclaration: checkReturnType, TSConstructorType: checkReturnType, TSDeclareFunction: checkReturnType, TSEmptyBodyFunctionExpression: checkReturnType, TSFunctionType: checkReturnType, TSMethodSignature: checkReturnType, }; }, });