|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Build-time lint for flow authoring ANTI-PATTERNS — metadata that is valid |
| 5 | + * (passes schema + expression checks) but is semantically a footgun at runtime. |
| 6 | + * These are emitted as WARNINGS: they guide the author (very often an AI |
| 7 | + * generating templates) toward the robust pattern without failing the build on |
| 8 | + * a technically-legal construct. |
| 9 | + * |
| 10 | + * #1874 — time-relative rules via record-change date-EQUALITY. A start-node |
| 11 | + * trigger condition like `end_date == daysFromNow(60)` on a `record-*` trigger |
| 12 | + * only fires if the record happens to be written on that exact day; the robust |
| 13 | + * shape is a daily SCHEDULE trigger + a range query. We flag the equality form |
| 14 | + * specifically (range operators `>=`/`<=` are not flagged — they're the building |
| 15 | + * block of the correct pattern), keeping false positives near zero. |
| 16 | + */ |
| 17 | + |
| 18 | +export interface FlowLintFinding { |
| 19 | + where: string; |
| 20 | + message: string; |
| 21 | + hint: string; |
| 22 | + rule: string; |
| 23 | +} |
| 24 | + |
| 25 | +type AnyRec = Record<string, unknown>; |
| 26 | + |
| 27 | +function asArray(v: unknown): AnyRec[] { |
| 28 | + if (Array.isArray(v)) return v as AnyRec[]; |
| 29 | + if (v && typeof v === 'object') return Object.entries(v as AnyRec).map(([name, def]) => ({ name, ...(def as AnyRec) })); |
| 30 | + return []; |
| 31 | +} |
| 32 | + |
| 33 | +/** Extract the raw predicate source from a `condition` (string or Expression envelope). */ |
| 34 | +function conditionSource(raw: unknown): string { |
| 35 | + if (typeof raw === 'string') return raw; |
| 36 | + if (raw && typeof raw === 'object' && typeof (raw as AnyRec).source === 'string') return (raw as AnyRec).source as string; |
| 37 | + return ''; |
| 38 | +} |
| 39 | + |
| 40 | +const TIME_FNS = 'daysFromNow|daysAgo|today|now|date|datetime'; |
| 41 | +// A time function adjacent to an equality operator, either side: |
| 42 | +// `end_date == daysFromNow(60)` / `today() != record.start` |
| 43 | +const DATE_EQ = new RegExp( |
| 44 | + `(?:(?:${TIME_FNS})\\s*\\([^)]*\\)\\s*(?:==|!=))|(?:(?:==|!=)\\s*(?:${TIME_FNS})\\s*\\()`, |
| 45 | +); |
| 46 | + |
| 47 | +export const FLOW_TIME_RELATIVE_ANTIPATTERN = 'flow-time-relative-antipattern'; |
| 48 | + |
| 49 | +/** |
| 50 | + * Lint every flow's start node for known authoring anti-patterns. Returns a |
| 51 | + * (possibly empty) list of advisory findings — never throws, never fails a build. |
| 52 | + */ |
| 53 | +export function lintFlowPatterns(stack: AnyRec): FlowLintFinding[] { |
| 54 | + const findings: FlowLintFinding[] = []; |
| 55 | + for (const flow of asArray(stack.flows)) { |
| 56 | + const flowName = typeof flow.name === 'string' ? flow.name : '(unnamed flow)'; |
| 57 | + const nodes = Array.isArray(flow.nodes) ? (flow.nodes as AnyRec[]) : []; |
| 58 | + const start = nodes.find((n) => n.type === 'start'); |
| 59 | + if (!start) continue; |
| 60 | + const cfg = (start.config ?? {}) as AnyRec; |
| 61 | + const triggerType = typeof cfg.triggerType === 'string' ? cfg.triggerType : ''; |
| 62 | + if (!triggerType.startsWith('record-')) continue; |
| 63 | + |
| 64 | + const src = conditionSource(cfg.condition).trim(); |
| 65 | + if (src && DATE_EQ.test(src)) { |
| 66 | + findings.push({ |
| 67 | + where: `flow '${flowName}' · start condition`, |
| 68 | + message: |
| 69 | + `record-change trigger uses a date-EQUALITY time condition (\`${src}\`) — it only fires if the ` + |
| 70 | + `record happens to be written on that exact day, so unattended "N days before" rules never run.`, |
| 71 | + hint: |
| 72 | + `Use a SCHEDULE trigger (daily cron) + a range query instead — e.g. a scheduled flow whose ` + |
| 73 | + `get_record filters \`end_date\` BETWEEN {TODAY()} and {TODAY()+N}. (#1874)`, |
| 74 | + rule: FLOW_TIME_RELATIVE_ANTIPATTERN, |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + return findings; |
| 79 | +} |
0 commit comments