1 import type { TSESTree } from "@typescript-eslint/utils"
2 import { createRule } from "../../create-rule"
3 import {
4 createWorkflowSdkBindings,
5 isInWorkflowDefinitionScope,
6 trackWorkflowSdkImports,
7 WorkflowSdkBindings,
8 } from "../../util/workflow-scope"
9
10 type MessageIds = "newDateInWorkflow" | "dateMethodInWorkflow"
11
12 const isDateIdentifier = (node: TSESTree.Node): boolean =>
13 node.type === "Identifier" && node.name === "Date"
14
15 export const rule = createRule<[], MessageIds>({
16 name: "no-new-date-in-workflow",
17 meta: {
18 type: "problem",
19 docs: {
20 description:
21 "Disallow `new Date(...)` and any `Date.*(...)` static-method call directly inside a `createWorkflow` constructor or a `when().then()` callback. They evaluate at workflow-definition time, not execution time — wrap them in `transform` or compute inside a `createStep`.",
22 },
23 messages: {
24 newDateInWorkflow:
25 "Do not call `new Date(...)` inside a workflow constructor or a `when().then()` callback — it evaluates at definition time, not execution time. Wrap it in `transform({}, () => new Date())` or compute it inside a `createStep`.",
26 dateMethodInWorkflow:
27 "Do not call `Date.{{method}}(...)` inside a workflow constructor or a `when().then()` callback — it evaluates at definition time, not execution time. Wrap it in `transform({}, () => Date.{{method}}(...))` or compute it inside a `createStep`.",
28 },
29 schema: [],
30 },
31 defaultOptions: [],
32 create(context) {
33 const bindings: WorkflowSdkBindings = createWorkflowSdkBindings()
34
35 return {
36 ImportDeclaration(node) {
37 trackWorkflowSdkImports(node, bindings)
38 },
39 NewExpression(node) {
40 if (bindings.createWorkflow.size === 0) {
41 return
42 }
43 if (!isDateIdentifier(node.callee)) {
44 return
45 }
46 if (!isInWorkflowDefinitionScope(node, bindings)) {
47 return
48 }
49 context.report({ node, messageId: "newDateInWorkflow" })
50 },
51 CallExpression(node) {
52 if (bindings.createWorkflow.size === 0) {
53 return
54 }
55 const callee = node.callee
56 if (
57 callee.type !== "MemberExpression" ||
58 callee.computed ||
59 !isDateIdentifier(callee.object) ||
60 callee.property.type !== "Identifier"
61 ) {
62 return
63 }
64 if (!isInWorkflowDefinitionScope(node, bindings)) {
65 return
66 }
67 context.report({
68 node,
69 messageId: "dateMethodInWorkflow",
70 data: { method: callee.property.name },
71 })
72 },
73 }
74 },
75 })
76
77 export default rule