1 import { AST_NODE_TYPES, ESLintUtils, TSESTree } from "@typescript-eslint/utils"
2 import * as ts from "typescript"
3 import { createRule } from "../../create-rule"
4
5 type MessageIds = "missingContainer"
6
7 const WORKFLOW_ALIAS_NAME = "ReturnWorkflow"
8 const MAX_DEPTH = 4
9
10 /**
11 * Returns true when `type` (or any of its union/intersection constituents)
12 * looks like a Medusa workflow — either the canonical `ReturnWorkflow<...>`
13 * alias, or any type with the structural shape (a callable that also has
14 * `run`, `runAsStep`, and `getName`).
15 */
16 function isWorkflowType(type: ts.Type, depth = 0): boolean {
17 if (depth > MAX_DEPTH) {
18 return false
19 }
20
21 if (type.aliasSymbol?.name === WORKFLOW_ALIAS_NAME) {
22 return true
23 }
24 if (type.getSymbol()?.name === WORKFLOW_ALIAS_NAME) {
25 return true
26 }
27
28 if (type.isUnion() || type.isIntersection()) {
29 return type.types.some((t) => isWorkflowType(t, depth + 1))
30 }
31
32 const hasRun = type.getProperty("run")
33 const hasRunAsStep = type.getProperty("runAsStep")
34 const hasGetName = type.getProperty("getName")
35 const hasCallSignatures = type.getCallSignatures().length > 0
36
37 return !!hasRun && !!hasRunAsStep && !!hasGetName && !!hasCallSignatures
38 }
39
40 function getNameFromExpression(node: TSESTree.Node): string {
41 if (node.type === AST_NODE_TYPES.Identifier) {
42 return node.name
43 }
44 if (
45 node.type === AST_NODE_TYPES.MemberExpression &&
46 node.property.type === AST_NODE_TYPES.Identifier
47 ) {
48 return node.property.name
49 }
50 return "workflow"
51 }
52
53 export const rule = createRule<[], MessageIds>({
54 name: "no-workflow-call-without-container",
55 meta: {
56 type: "problem",
57 docs: {
58 description:
59 "Workflow invocations must pass the Medusa container — `workflow(container).run({ input })`. Calling the workflow with no arguments or accessing `.run` directly bypasses container resolution.",
60 requiresTypeChecking: true,
61 },
62 messages: {
63 missingContainer:
64 "Workflow `{{name}}` must be invoked with a container — use `{{name}}(container).run({ input })` instead.",
65 },
66 schema: [],
67 },
68 defaultOptions: [],
69 create(context) {
70 const services = ESLintUtils.getParserServices(context)
71 const checker = services.program.getTypeChecker()
72
73 function getType(node: TSESTree.Node): ts.Type {
74 const tsNode = services.esTreeNodeToTSNodeMap.get(node)
75 return checker.getTypeAtLocation(tsNode)
76 }
77
78 return {
79 // Flag `workflow()` — invoking a workflow value with no container argument.
80 CallExpression(node) {
81 if (node.arguments.length > 0) {
82 return
83 }
84 if (
85 node.callee.type !== AST_NODE_TYPES.Identifier &&
86 node.callee.type !== AST_NODE_TYPES.MemberExpression
87 ) {
88 return
89 }
90 if (!isWorkflowType(getType(node.callee as TSESTree.Node))) {
91 return
92 }
93
94 context.report({
95 node,
96 messageId: "missingContainer",
97 data: { name: getNameFromExpression(node.callee as TSESTree.Node) },
98 })
99 },
100
101 // Flag `workflow.run` — reaching for `.run` directly on the workflow
102 // value without first invoking it with a container.
103 MemberExpression(node) {
104 if (node.computed) {
105 return
106 }
107 if (node.property.type !== AST_NODE_TYPES.Identifier) {
108 return
109 }
110 if (node.property.name !== "run") {
111 return
112 }
113 if (!isWorkflowType(getType(node.object as TSESTree.Node))) {
114 return
115 }
116
117 context.report({
118 node,
119 messageId: "missingContainer",
120 data: { name: getNameFromExpression(node.object as TSESTree.Node) },
121 })
122 },
123 }
124 },
125 })
126
127 export default rule