1 import { AST_NODE_TYPES } from "@typescript-eslint/utils"
2 import { createRule } from "../../create-rule"
3 import {
4 createWorkflowSdkBindings,
5 isInWorkflowDefinitionScope,
6 trackWorkflowSdkImports,
7 } from "../../util/workflow-scope"
8
9 type MessageIds = "consoleInWorkflow"
10
11 const CONSOLE_METHODS = new Set([
12 "log",
13 "info",
14 "warn",
15 "error",
16 "debug",
17 "trace",
18 "dir",
19 ])
20
21 export const rule = createRule<[], MessageIds>({
22 name: "no-console-log-in-workflow",
23 meta: {
24 type: "suggestion",
25 docs: {
26 description:
27 "Disallow `console.log` (and other `console.*` calls) directly inside a `createWorkflow` constructor or a `when().then()` callback. Both run at workflow-definition time, not execution time — move logs into a `createStep` callback so they run when the workflow actually executes.",
28 },
29 messages: {
30 consoleInWorkflow:
31 "`console.{{method}}` inside a workflow constructor or a `when().then()` callback runs at workflow-definition time, not execution time, so it only logs when the workflow is registered. Move logging into a `createStep(...)` callback if you want it to run on each workflow execution.",
32 },
33 schema: [],
34 },
35 defaultOptions: [],
36 create(context) {
37 const bindings = createWorkflowSdkBindings()
38
39 return {
40 ImportDeclaration(node) {
41 trackWorkflowSdkImports(node, bindings)
42 },
43
44 CallExpression(node) {
45 if (bindings.createWorkflow.size === 0) {
46 return
47 }
48 const callee = node.callee
49 if (callee.type !== AST_NODE_TYPES.MemberExpression) {
50 return
51 }
52 if (callee.computed) {
53 return
54 }
55 if (
56 callee.object.type !== AST_NODE_TYPES.Identifier ||
57 callee.object.name !== "console"
58 ) {
59 return
60 }
61 if (callee.property.type !== AST_NODE_TYPES.Identifier) {
62 return
63 }
64 const method = callee.property.name
65 if (!CONSOLE_METHODS.has(method)) {
66 return
67 }
68 if (!isInWorkflowDefinitionScope(node, bindings)) {
69 return
70 }
71 context.report({
72 node,
73 messageId: "consoleInWorkflow",
74 data: { method },
75 })
76 },
77 }
78 },
79 })
80
81 export default rule