@ontrails/warden 1.0.0-beta.2 → 1.0.0-beta.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (249) hide show
  1. package/CHANGELOG.md +497 -6
  2. package/README.md +77 -26
  3. package/bin/warden.ts +50 -0
  4. package/package.json +27 -5
  5. package/src/adapter-check.ts +136 -0
  6. package/src/ast.ts +28 -0
  7. package/src/cli.ts +1374 -103
  8. package/src/command.ts +953 -0
  9. package/src/config.ts +184 -0
  10. package/src/draft.ts +22 -0
  11. package/src/drift.ts +106 -22
  12. package/src/fix.ts +120 -0
  13. package/src/formatters.ts +79 -9
  14. package/src/guide.ts +245 -0
  15. package/src/index.ts +206 -14
  16. package/src/project-context.ts +163 -0
  17. package/src/resolve.ts +530 -0
  18. package/src/rules/activation-orphan.ts +97 -0
  19. package/src/rules/ast.ts +3176 -85
  20. package/src/rules/circular-refs.ts +154 -0
  21. package/src/rules/composes-declarations.ts +704 -0
  22. package/src/rules/context-no-surface-types.ts +68 -8
  23. package/src/rules/contour-exists.ts +251 -0
  24. package/src/rules/contour-ids.ts +15 -0
  25. package/src/rules/dead-internal-trail.ts +154 -0
  26. package/src/rules/draft-file-marking.ts +160 -0
  27. package/src/rules/draft-visible-debt.ts +87 -0
  28. package/src/rules/error-mapping-completeness.ts +288 -0
  29. package/src/rules/example-valid.ts +401 -0
  30. package/src/rules/fires-declarations.ts +758 -0
  31. package/src/rules/implementation-returns-result.ts +1265 -95
  32. package/src/rules/incomplete-accessor-for-standard-op.ts +272 -0
  33. package/src/rules/incomplete-crud.ts +580 -0
  34. package/src/rules/index.ts +219 -18
  35. package/src/rules/intent-propagation.ts +127 -0
  36. package/src/rules/layer-field-name-drift.ts +96 -0
  37. package/src/rules/metadata.ts +654 -0
  38. package/src/rules/missing-reconcile.ts +98 -0
  39. package/src/rules/missing-visibility.ts +110 -0
  40. package/src/rules/no-destructured-compose.ts +192 -0
  41. package/src/rules/no-dev-permit-in-source.ts +99 -0
  42. package/src/rules/no-direct-implementation-call.ts +7 -7
  43. package/src/rules/no-legacy-layer-imports.ts +211 -0
  44. package/src/rules/no-native-error-result.ts +111 -0
  45. package/src/rules/no-redundant-result-error-wrap.ts +331 -0
  46. package/src/rules/no-retired-cross-vocabulary.ts +194 -0
  47. package/src/rules/no-sync-result-assumption.ts +1134 -99
  48. package/src/rules/no-throw-in-detour-recover.ts +225 -0
  49. package/src/rules/no-throw-in-implementation.ts +10 -9
  50. package/src/rules/no-top-level-surface.ts +389 -0
  51. package/src/rules/on-references-exist.ts +194 -0
  52. package/src/rules/orphaned-signal.ts +150 -0
  53. package/src/rules/owner-projection-parity.ts +146 -0
  54. package/src/rules/permit-governance.ts +25 -0
  55. package/src/rules/public-export-example-coverage.ts +553 -0
  56. package/src/rules/public-internal-deep-imports.ts +517 -0
  57. package/src/rules/public-output-schema.ts +29 -0
  58. package/src/rules/public-union-output-discriminants.ts +150 -0
  59. package/src/rules/read-intent-fires.ts +187 -0
  60. package/src/rules/reference-exists.ts +98 -0
  61. package/src/rules/registry-names.ts +145 -0
  62. package/src/rules/resolved-import-boundary.ts +146 -0
  63. package/src/rules/resource-declarations.ts +704 -0
  64. package/src/rules/resource-exists.ts +179 -0
  65. package/src/rules/resource-id-grammar.ts +65 -0
  66. package/src/rules/resource-mock-coverage.ts +115 -0
  67. package/src/rules/scan.ts +38 -25
  68. package/src/rules/scheduled-destroy-intent.ts +44 -0
  69. package/src/rules/signal-graph-coaching.ts +191 -0
  70. package/src/rules/specs.ts +9 -5
  71. package/src/rules/static-resource-accessor-preference.ts +657 -0
  72. package/src/rules/surface-facet-coherence.ts +370 -0
  73. package/src/rules/trail-versioning-source.ts +1094 -0
  74. package/src/rules/trail-versioning-topo.ts +172 -0
  75. package/src/rules/types.ts +270 -6
  76. package/src/rules/unmaterialized-activation-source.ts +84 -0
  77. package/src/rules/unreachable-detour-shadowing.ts +344 -0
  78. package/src/rules/valid-describe-refs.ts +160 -32
  79. package/src/rules/valid-detour-contract.ts +78 -0
  80. package/src/rules/warden-export-symmetry.ts +533 -0
  81. package/src/rules/warden-rules-use-ast.ts +996 -0
  82. package/src/rules/webhook-route-collision.ts +243 -0
  83. package/src/trails/activation-orphan.trail.ts +84 -0
  84. package/src/trails/circular-refs.trail.ts +29 -0
  85. package/src/trails/composes-declarations.trail.ts +22 -0
  86. package/src/trails/context-no-surface-types.trail.ts +21 -0
  87. package/src/trails/contour-exists.trail.ts +21 -0
  88. package/src/trails/dead-internal-trail.trail.ts +26 -0
  89. package/src/trails/deprecation-without-guidance.trail.ts +21 -0
  90. package/src/trails/draft-file-marking.trail.ts +16 -0
  91. package/src/trails/draft-visible-debt.trail.ts +16 -0
  92. package/src/trails/error-mapping-completeness.trail.ts +29 -0
  93. package/src/trails/example-valid.trail.ts +25 -0
  94. package/src/trails/fires-declarations.trail.ts +23 -0
  95. package/src/trails/fork-without-preserved-blaze.trail.ts +31 -0
  96. package/src/trails/implementation-returns-result.trail.ts +20 -0
  97. package/src/trails/incomplete-accessor-for-standard-op.trail.ts +76 -0
  98. package/src/trails/incomplete-crud.trail.ts +39 -0
  99. package/src/trails/index.ts +78 -0
  100. package/src/trails/intent-propagation.trail.ts +30 -0
  101. package/src/trails/layer-field-name-drift.trail.ts +39 -0
  102. package/src/trails/marker-schema-unsupported.trail.ts +23 -0
  103. package/src/trails/missing-reconcile.trail.ts +33 -0
  104. package/src/trails/missing-visibility.trail.ts +22 -0
  105. package/src/trails/no-destructured-compose.trail.ts +44 -0
  106. package/src/trails/no-dev-permit-in-source.trail.ts +16 -0
  107. package/src/trails/no-direct-implementation-call.trail.ts +16 -0
  108. package/src/trails/no-legacy-layer-imports.trail.ts +41 -0
  109. package/src/trails/no-native-error-result.trail.ts +18 -0
  110. package/src/trails/no-redundant-result-error-wrap.trail.ts +55 -0
  111. package/src/trails/no-retired-cross-vocabulary.trail.ts +42 -0
  112. package/src/trails/no-sync-result-assumption.trail.ts +19 -0
  113. package/src/trails/no-throw-in-detour-recover.trail.ts +24 -0
  114. package/src/trails/no-throw-in-implementation.trail.ts +20 -0
  115. package/src/trails/no-top-level-surface.trail.ts +43 -0
  116. package/src/trails/on-references-exist.trail.ts +21 -0
  117. package/src/trails/orphaned-signal.trail.ts +36 -0
  118. package/src/trails/owner-projection-parity.trail.ts +26 -0
  119. package/src/trails/pending-force.trail.ts +21 -0
  120. package/src/trails/permit-governance.trail.ts +51 -0
  121. package/src/trails/prefer-schema-inference.trail.ts +21 -0
  122. package/src/trails/public-export-example-coverage.trail.ts +16 -0
  123. package/src/trails/public-internal-deep-imports.trail.ts +94 -0
  124. package/src/trails/public-output-schema.trail.ts +55 -0
  125. package/src/trails/public-union-output-discriminants.trail.ts +33 -0
  126. package/src/trails/read-intent-fires.trail.ts +20 -0
  127. package/src/trails/reference-exists.trail.ts +25 -0
  128. package/src/trails/resolved-import-boundary.trail.ts +109 -0
  129. package/src/trails/resource-declarations.trail.ts +25 -0
  130. package/src/trails/resource-exists.trail.ts +27 -0
  131. package/src/trails/resource-id-grammar.trail.ts +39 -0
  132. package/src/trails/resource-mock-coverage.trail.ts +40 -0
  133. package/src/trails/run.ts +162 -0
  134. package/src/trails/scheduled-destroy-intent.trail.ts +56 -0
  135. package/src/trails/schema.ts +194 -0
  136. package/src/trails/signal-graph-coaching.trail.ts +77 -0
  137. package/src/trails/static-resource-accessor-preference.trail.ts +25 -0
  138. package/src/trails/surface-facet-coherence.trail.ts +25 -0
  139. package/src/trails/topo.ts +6 -0
  140. package/src/trails/unmaterialized-activation-source.trail.ts +72 -0
  141. package/src/trails/unreachable-detour-shadowing.trail.ts +45 -0
  142. package/src/trails/valid-describe-refs.trail.ts +18 -0
  143. package/src/trails/valid-detour-contract.trail.ts +71 -0
  144. package/src/trails/version-gap.trail.ts +35 -0
  145. package/src/trails/version-pinned-compose.trail.ts +23 -0
  146. package/src/trails/version-without-examples.trail.ts +38 -0
  147. package/src/trails/warden-export-symmetry.trail.ts +16 -0
  148. package/src/trails/warden-rules-use-ast.trail.ts +45 -0
  149. package/src/trails/webhook-route-collision.trail.ts +50 -0
  150. package/src/trails/wrap-rule.ts +213 -0
  151. package/src/workspaces.ts +238 -0
  152. package/.turbo/turbo-build.log +0 -1
  153. package/.turbo/turbo-lint.log +0 -3
  154. package/.turbo/turbo-typecheck.log +0 -1
  155. package/dist/cli.d.ts +0 -46
  156. package/dist/cli.d.ts.map +0 -1
  157. package/dist/cli.js +0 -221
  158. package/dist/cli.js.map +0 -1
  159. package/dist/drift.d.ts +0 -26
  160. package/dist/drift.d.ts.map +0 -1
  161. package/dist/drift.js +0 -27
  162. package/dist/drift.js.map +0 -1
  163. package/dist/formatters.d.ts +0 -29
  164. package/dist/formatters.d.ts.map +0 -1
  165. package/dist/formatters.js +0 -87
  166. package/dist/formatters.js.map +0 -1
  167. package/dist/index.d.ts +0 -26
  168. package/dist/index.d.ts.map +0 -1
  169. package/dist/index.js +0 -26
  170. package/dist/index.js.map +0 -1
  171. package/dist/rules/ast.d.ts +0 -41
  172. package/dist/rules/ast.d.ts.map +0 -1
  173. package/dist/rules/ast.js +0 -163
  174. package/dist/rules/ast.js.map +0 -1
  175. package/dist/rules/context-no-surface-types.d.ts +0 -12
  176. package/dist/rules/context-no-surface-types.d.ts.map +0 -1
  177. package/dist/rules/context-no-surface-types.js +0 -96
  178. package/dist/rules/context-no-surface-types.js.map +0 -1
  179. package/dist/rules/implementation-returns-result.d.ts +0 -13
  180. package/dist/rules/implementation-returns-result.d.ts.map +0 -1
  181. package/dist/rules/implementation-returns-result.js +0 -231
  182. package/dist/rules/implementation-returns-result.js.map +0 -1
  183. package/dist/rules/index.d.ts +0 -22
  184. package/dist/rules/index.d.ts.map +0 -1
  185. package/dist/rules/index.js +0 -41
  186. package/dist/rules/index.js.map +0 -1
  187. package/dist/rules/no-direct-impl-in-route.d.ts +0 -12
  188. package/dist/rules/no-direct-impl-in-route.d.ts.map +0 -1
  189. package/dist/rules/no-direct-impl-in-route.js +0 -46
  190. package/dist/rules/no-direct-impl-in-route.js.map +0 -1
  191. package/dist/rules/no-direct-implementation-call.d.ts +0 -12
  192. package/dist/rules/no-direct-implementation-call.d.ts.map +0 -1
  193. package/dist/rules/no-direct-implementation-call.js +0 -39
  194. package/dist/rules/no-direct-implementation-call.js.map +0 -1
  195. package/dist/rules/no-sync-result-assumption.d.ts +0 -6
  196. package/dist/rules/no-sync-result-assumption.d.ts.map +0 -1
  197. package/dist/rules/no-sync-result-assumption.js +0 -98
  198. package/dist/rules/no-sync-result-assumption.js.map +0 -1
  199. package/dist/rules/no-throw-in-detour-target.d.ts +0 -12
  200. package/dist/rules/no-throw-in-detour-target.d.ts.map +0 -1
  201. package/dist/rules/no-throw-in-detour-target.js +0 -87
  202. package/dist/rules/no-throw-in-detour-target.js.map +0 -1
  203. package/dist/rules/no-throw-in-implementation.d.ts +0 -9
  204. package/dist/rules/no-throw-in-implementation.d.ts.map +0 -1
  205. package/dist/rules/no-throw-in-implementation.js +0 -34
  206. package/dist/rules/no-throw-in-implementation.js.map +0 -1
  207. package/dist/rules/prefer-schema-inference.d.ts +0 -7
  208. package/dist/rules/prefer-schema-inference.d.ts.map +0 -1
  209. package/dist/rules/prefer-schema-inference.js +0 -86
  210. package/dist/rules/prefer-schema-inference.js.map +0 -1
  211. package/dist/rules/scan.d.ts +0 -8
  212. package/dist/rules/scan.d.ts.map +0 -1
  213. package/dist/rules/scan.js +0 -32
  214. package/dist/rules/scan.js.map +0 -1
  215. package/dist/rules/specs.d.ts +0 -29
  216. package/dist/rules/specs.d.ts.map +0 -1
  217. package/dist/rules/specs.js +0 -192
  218. package/dist/rules/specs.js.map +0 -1
  219. package/dist/rules/structure.d.ts +0 -13
  220. package/dist/rules/structure.d.ts.map +0 -1
  221. package/dist/rules/structure.js +0 -142
  222. package/dist/rules/structure.js.map +0 -1
  223. package/dist/rules/types.d.ts +0 -52
  224. package/dist/rules/types.d.ts.map +0 -1
  225. package/dist/rules/types.js +0 -2
  226. package/dist/rules/types.js.map +0 -1
  227. package/dist/rules/valid-describe-refs.d.ts +0 -7
  228. package/dist/rules/valid-describe-refs.d.ts.map +0 -1
  229. package/dist/rules/valid-describe-refs.js +0 -51
  230. package/dist/rules/valid-describe-refs.js.map +0 -1
  231. package/dist/rules/valid-detour-refs.d.ts +0 -6
  232. package/dist/rules/valid-detour-refs.d.ts.map +0 -1
  233. package/dist/rules/valid-detour-refs.js +0 -116
  234. package/dist/rules/valid-detour-refs.js.map +0 -1
  235. package/src/__tests__/cli.test.ts +0 -198
  236. package/src/__tests__/drift.test.ts +0 -74
  237. package/src/__tests__/formatters.test.ts +0 -157
  238. package/src/__tests__/implementation-returns-result.test.ts +0 -75
  239. package/src/__tests__/no-direct-implementation-call.test.ts +0 -83
  240. package/src/__tests__/no-sync-result-assumption.test.ts +0 -85
  241. package/src/__tests__/no-throw-in-detour-target.test.ts +0 -78
  242. package/src/__tests__/prefer-schema-inference.test.ts +0 -84
  243. package/src/__tests__/rules.test.ts +0 -188
  244. package/src/__tests__/valid-describe-refs.test.ts +0 -60
  245. package/src/rules/no-direct-impl-in-route.ts +0 -77
  246. package/src/rules/no-throw-in-detour-target.ts +0 -150
  247. package/src/rules/valid-detour-refs.ts +0 -187
  248. package/tsconfig.json +0 -9
  249. package/tsconfig.tsbuildinfo +0 -1
@@ -0,0 +1,758 @@
1
+ /**
2
+ * Validates that `ctx.fire()` calls match the declared `fires` array.
3
+ *
4
+ * Statically analyzes trail `blaze` functions to find `ctx.fire(signal, ...)`
5
+ * calls and compares locally-resolved `Signal` values against the `fires: [...]`
6
+ * declaration in the trail config. Reports errors for undeclared fires, string
7
+ * fire calls that no longer match the public runtime API, and warnings for
8
+ * unused declarations.
9
+ *
10
+ * Mirrors `composes-declarations` structurally — same extraction, same reporting
11
+ * shape, same const-identifier resolution, same context-parameter handling.
12
+ */
13
+
14
+ import {
15
+ buildSignalIdentifierResolver,
16
+ extractStringLiteral,
17
+ findConfigProperty,
18
+ findBlazeBodies,
19
+ findTrailDefinitions,
20
+ identifierName,
21
+ offsetToLine,
22
+ parse,
23
+ deriveConstString,
24
+ walkScope,
25
+ } from './ast.js';
26
+ import type { AstNode, SignalIdentifierResolver } from './ast.js';
27
+ import { isTestFile } from './scan.js';
28
+ import type { WardenDiagnostic, WardenRule } from './types.js';
29
+
30
+ // ---------------------------------------------------------------------------
31
+ // Const identifier resolution
32
+ // ---------------------------------------------------------------------------
33
+
34
+ /**
35
+ * Resolve an array element to a static signal ID when possible.
36
+ *
37
+ * Returns null for entries the rule can't statically resolve — callers should
38
+ * treat "unresolved" as "trust the runtime" rather than a missing declaration.
39
+ * In particular, object-form references (e.g. `fires: [orderPlaced]` where
40
+ * `orderPlaced` is a `Signal` imported from elsewhere) resolve via runtime
41
+ * normalization in `trail()`, not at lint time.
42
+ */
43
+ const resolveFireElementId = (
44
+ element: AstNode,
45
+ sourceCode: string,
46
+ signalIds: SignalIdentifierResolver
47
+ ): string | null => {
48
+ const literalValue = extractStringLiteral(element);
49
+ if (literalValue !== null) {
50
+ return literalValue;
51
+ }
52
+
53
+ if (element.type === 'Identifier') {
54
+ const name = identifierName(element);
55
+ if (name) {
56
+ const resolved = signalIds.resolve(element);
57
+ if (resolved.kind === 'signal' || resolved.kind === 'string') {
58
+ return resolved.id;
59
+ }
60
+ if (resolved.kind === 'shadowed') {
61
+ return null;
62
+ }
63
+ return deriveConstString(name, sourceCode);
64
+ }
65
+ }
66
+
67
+ return null;
68
+ };
69
+
70
+ // ---------------------------------------------------------------------------
71
+ // Declared fires extraction
72
+ // ---------------------------------------------------------------------------
73
+
74
+ /** Extract the ArrayExpression elements from a config's `fires` property. */
75
+ const getFiresElements = (config: AstNode): readonly AstNode[] | null => {
76
+ const firesProp = findConfigProperty(config, 'fires');
77
+ if (!firesProp) {
78
+ return null;
79
+ }
80
+
81
+ const arrayNode = firesProp.value;
82
+ if (!arrayNode || (arrayNode as AstNode).type !== 'ArrayExpression') {
83
+ return null;
84
+ }
85
+
86
+ const elements = (arrayNode as AstNode)['elements'] as
87
+ | readonly AstNode[]
88
+ | undefined;
89
+ return elements ?? null;
90
+ };
91
+
92
+ interface DeclaredFires {
93
+ /** Statically resolved signal ids from string literals / const identifiers. */
94
+ readonly ids: ReadonlySet<string>;
95
+ /** True if any element could not be statically resolved (e.g. Signal value). */
96
+ readonly hasUnresolved: boolean;
97
+ }
98
+
99
+ /**
100
+ * Extract declared fires from a `fires: [...]` array.
101
+ *
102
+ * Object-form entries (`fires: [someSignal]`) cannot be resolved at lint time;
103
+ * they're normalized at runtime by `trail()`. When any entry is unresolved,
104
+ * the rule reports `hasUnresolved: true`, and callers should suppress the
105
+ * "undeclared" diagnostic since the declared set is incomplete from our view.
106
+ */
107
+ const resolveDeclaredFiresElements = (
108
+ elements: readonly AstNode[],
109
+ sourceCode: string,
110
+ signalIds: SignalIdentifierResolver
111
+ ): DeclaredFires => {
112
+ const ids = new Set<string>();
113
+ let hasUnresolved = false;
114
+ for (const element of elements) {
115
+ const resolved = resolveFireElementId(element, sourceCode, signalIds);
116
+ if (resolved) {
117
+ ids.add(resolved);
118
+ } else {
119
+ hasUnresolved = true;
120
+ }
121
+ }
122
+ return { hasUnresolved, ids };
123
+ };
124
+
125
+ const extractDeclaredFires = (
126
+ config: AstNode,
127
+ sourceCode: string,
128
+ signalIds: SignalIdentifierResolver
129
+ ): DeclaredFires => {
130
+ const elements = getFiresElements(config);
131
+ return elements
132
+ ? resolveDeclaredFiresElements(elements, sourceCode, signalIds)
133
+ : { hasUnresolved: false, ids: new Set() };
134
+ };
135
+
136
+ // ---------------------------------------------------------------------------
137
+ // Called fires extraction — member expression helpers
138
+ // ---------------------------------------------------------------------------
139
+
140
+ const MEMBER_TYPES = new Set(['StaticMemberExpression', 'MemberExpression']);
141
+
142
+ /** Extract object and property Identifier names from a MemberExpression. */
143
+ const extractMemberPair = (
144
+ callee: AstNode
145
+ ): { objName: string; propName: string } | null => {
146
+ if (!MEMBER_TYPES.has(callee.type)) {
147
+ return null;
148
+ }
149
+
150
+ const objName = identifierName(
151
+ (callee as unknown as { object?: AstNode }).object
152
+ );
153
+ const propName = identifierName(
154
+ (callee as unknown as { property?: AstNode }).property
155
+ );
156
+
157
+ return objName && propName ? { objName, propName } : null;
158
+ };
159
+
160
+ /**
161
+ * Extract the second parameter node from a blaze function node.
162
+ *
163
+ * Handles `(input, ctx) => ...`, `async (input, context) => ...`,
164
+ * `function(input, ctx) { ... }`, and parameter-level destructuring
165
+ * like `(input, { fire }) => ...`.
166
+ */
167
+ const extractContextParamNode = (blazeBody: AstNode): AstNode | null => {
168
+ const params = blazeBody['params'] as readonly AstNode[] | undefined;
169
+ if (!params || params.length < 2) {
170
+ return null;
171
+ }
172
+ return params[1] ?? null;
173
+ };
174
+
175
+ /** Extract the local name bound to `fire` inside an ObjectPattern Property. */
176
+ const extractFireLocalName = (prop: AstNode): string | null => {
177
+ if (prop.type !== 'Property') {
178
+ return null;
179
+ }
180
+ const { key } = prop as unknown as { key?: AstNode };
181
+ const { value } = prop as unknown as { value?: AstNode };
182
+ const keyName = identifierName(key);
183
+ if (keyName !== 'fire') {
184
+ return null;
185
+ }
186
+ // `{ fire }` → key and value are the same Identifier (shorthand).
187
+ // `{ fire: emit }` → value is a distinct Identifier.
188
+ return identifierName(value) ?? keyName;
189
+ };
190
+
191
+ /** Collect `fire` local names from an ObjectPattern's properties into `names`. */
192
+ const collectFireNamesFromPattern = (
193
+ pattern: AstNode,
194
+ names: Set<string>
195
+ ): void => {
196
+ const { properties } = pattern as unknown as {
197
+ properties?: readonly AstNode[];
198
+ };
199
+ if (!properties) {
200
+ return;
201
+ }
202
+ for (const prop of properties) {
203
+ const localName = extractFireLocalName(prop);
204
+ if (localName) {
205
+ names.add(localName);
206
+ }
207
+ }
208
+ };
209
+
210
+ /**
211
+ * Extract the second parameter name from a blaze function node.
212
+ *
213
+ * Returns null when the parameter is not a plain Identifier (e.g. when the
214
+ * author destructures `{ fire }` in the parameter list). Parameter-level
215
+ * destructuring is handled separately by `collectParamFireNames`.
216
+ *
217
+ * Also handles defaulted parameters like `(input, ctx = fallback) => ...`
218
+ * (AssignmentPattern whose `.left` is the Identifier). Without this, valid
219
+ * signatures would silently drop out of ctx-access analysis.
220
+ */
221
+ const extractContextParamName = (blazeBody: AstNode): string | null => {
222
+ const param = extractContextParamNode(blazeBody);
223
+ if (!param) {
224
+ return null;
225
+ }
226
+ if (param.type === 'AssignmentPattern') {
227
+ const { left } = param as unknown as { left?: AstNode };
228
+ return identifierName(left);
229
+ }
230
+ return identifierName(param);
231
+ };
232
+
233
+ /**
234
+ * Collect `fire` local names bound via parameter-level destructuring.
235
+ *
236
+ * Recognizes `(input, { fire }) => ...` and `(input, { fire: emit }) => ...`.
237
+ * When the blaze author destructures in the parameter list, there is no
238
+ * enclosing `ctx` identifier to track — we seed the fire local set directly
239
+ * from the ObjectPattern in `params[1]`.
240
+ */
241
+ const collectParamFireNames = (body: AstNode): ReadonlySet<string> => {
242
+ const param = extractContextParamNode(body);
243
+ if (!param || param.type !== 'ObjectPattern') {
244
+ return new Set();
245
+ }
246
+ const names = new Set<string>();
247
+ collectFireNamesFromPattern(param, names);
248
+ return names;
249
+ };
250
+
251
+ /** Check if a callee is a member-style fire call: <ctxName>.fire(...). */
252
+ const isMemberFireCall = (
253
+ callee: AstNode,
254
+ ctxNames: ReadonlySet<string>
255
+ ): boolean => {
256
+ const pair = extractMemberPair(callee);
257
+ return !!pair && ctxNames.has(pair.objName) && pair.propName === 'fire';
258
+ };
259
+
260
+ /**
261
+ * Check if a node is a `<ctxName>.fire(...)` call.
262
+ *
263
+ * Also matches bare `<fireLocalName>(...)` calls, but only when the local name
264
+ * was verifiably destructured from the trail context (e.g. `const { fire } = ctx`
265
+ * or `const { fire: emit } = ctx`). Unrelated local `fire()` helpers are
266
+ * ignored — see `collectDestructuredFireNames`.
267
+ */
268
+ const isTrackedFireCallee = (
269
+ callee: AstNode,
270
+ ctxNames: ReadonlySet<string>,
271
+ fireLocalNames: ReadonlySet<string>
272
+ ): boolean => {
273
+ if (isMemberFireCall(callee, ctxNames)) {
274
+ return true;
275
+ }
276
+ const calleeName = identifierName(callee);
277
+ return !!calleeName && fireLocalNames.has(calleeName);
278
+ };
279
+
280
+ interface FireCallArg {
281
+ readonly id: string | null;
282
+ readonly stringId: string | null;
283
+ readonly unresolved: boolean;
284
+ }
285
+
286
+ const firstCallArg = (node: AstNode): AstNode | null => {
287
+ const args = node['arguments'] as readonly AstNode[] | undefined;
288
+ return args?.[0] ?? null;
289
+ };
290
+
291
+ const resolveFireCallArg = (
292
+ arg: AstNode | null,
293
+ sourceCode: string,
294
+ signalIds: SignalIdentifierResolver
295
+ ): FireCallArg => {
296
+ if (!arg) {
297
+ return { id: null, stringId: null, unresolved: true };
298
+ }
299
+
300
+ const stringId = extractStringLiteral(arg);
301
+ if (stringId !== null) {
302
+ return { id: stringId, stringId, unresolved: false };
303
+ }
304
+
305
+ if (arg.type === 'Identifier') {
306
+ const name = identifierName(arg);
307
+ if (!name) {
308
+ return { id: null, stringId: null, unresolved: true };
309
+ }
310
+ const resolved = signalIds.resolve(arg);
311
+ if (resolved.kind === 'signal') {
312
+ return { id: resolved.id, stringId: null, unresolved: false };
313
+ }
314
+ if (resolved.kind === 'string') {
315
+ return { id: resolved.id, stringId: resolved.id, unresolved: false };
316
+ }
317
+ if (resolved.kind === 'shadowed') {
318
+ return { id: null, stringId: null, unresolved: true };
319
+ }
320
+ const constStringId = deriveConstString(name, sourceCode);
321
+ if (constStringId) {
322
+ return { id: constStringId, stringId: constStringId, unresolved: false };
323
+ }
324
+ }
325
+
326
+ return { id: null, stringId: null, unresolved: true };
327
+ };
328
+
329
+ const extractFireCallId = (
330
+ node: AstNode,
331
+ ctxNames: ReadonlySet<string>,
332
+ fireLocalNames: ReadonlySet<string>,
333
+ sourceCode: string,
334
+ signalIds: SignalIdentifierResolver
335
+ ): FireCallArg | null => {
336
+ if (node.type !== 'CallExpression') {
337
+ return null;
338
+ }
339
+ const callee = node['callee'] as AstNode | undefined;
340
+ if (!callee) {
341
+ return null;
342
+ }
343
+ return isTrackedFireCallee(callee, ctxNames, fireLocalNames)
344
+ ? resolveFireCallArg(firstCallArg(node), sourceCode, signalIds)
345
+ : null;
346
+ };
347
+
348
+ /**
349
+ * Walk a blaze body and collect local names bound to `ctx.fire` via destructure.
350
+ *
351
+ * Recognizes:
352
+ * - `const { fire } = ctx;` → adds `fire`
353
+ * - `const { fire: emit } = context;` → adds `emit`
354
+ *
355
+ * Only destructures whose init is one of the tracked ctx parameter names are
356
+ * accepted. This prevents unrelated local `fire` helpers from being treated as
357
+ * calls into the trail context.
358
+ */
359
+ /** Check if a VariableDeclarator destructures from a known ctx identifier. */
360
+ const getCtxDestructurePattern = (
361
+ node: AstNode,
362
+ ctxNames: ReadonlySet<string>
363
+ ): AstNode | null => {
364
+ if (node.type !== 'VariableDeclarator') {
365
+ return null;
366
+ }
367
+ const { id, init } = node as unknown as {
368
+ readonly id?: AstNode;
369
+ readonly init?: AstNode;
370
+ };
371
+ if (!id || id.type !== 'ObjectPattern' || !init) {
372
+ return null;
373
+ }
374
+ const initName = identifierName(init);
375
+ if (!initName || !ctxNames.has(initName)) {
376
+ return null;
377
+ }
378
+ return id;
379
+ };
380
+
381
+ /**
382
+ * Collect `fire` local names destructured from ctx at the TOP LEVEL of the
383
+ * blaze body. Destructures inside nested functions are intentionally ignored
384
+ * to avoid leaking nested-scope bindings into the outer blaze scope — a
385
+ * `const { fire } = ctx` inside a nested helper should not cause an outer
386
+ * bare `fire('x')` to be treated as a ctx-bound call.
387
+ *
388
+ * Tradeoff: nested-scope destructures lose tracking entirely. Calls inside
389
+ * nested functions that rely on their own destructure will not be analyzed.
390
+ * This is a conservative precision loss; a full scope walker is a follow-up.
391
+ *
392
+ * Tradeoff: only `const` destructures are tracked. `let` and `var` bindings
393
+ * allow reassignment (`let { fire } = ctx; fire = other; fire('x')`) which
394
+ * this flow-insensitive walker cannot follow. Skipping them trades a small
395
+ * amount of precision — `let { fire } = ctx` is rare — for eliminating a
396
+ * class of false positives. The runtime + signal-id compose-check still
397
+ * validate real undeclared fires.
398
+ */
399
+ /** Get the top-level statements of a blaze function's BlockStatement body. */
400
+ const getTopLevelStatements = (body: AstNode): readonly AstNode[] => {
401
+ const blockBody = (body as unknown as { body?: AstNode }).body;
402
+ if (!blockBody || blockBody.type !== 'BlockStatement') {
403
+ return [];
404
+ }
405
+ return (blockBody as unknown as { body?: readonly AstNode[] }).body ?? [];
406
+ };
407
+
408
+ /** Collect fire-local names from a single top-level VariableDeclaration. */
409
+ const collectFireNamesFromDeclaration = (
410
+ stmt: AstNode,
411
+ ctxNames: ReadonlySet<string>,
412
+ names: Set<string>
413
+ ): void => {
414
+ if (stmt.type !== 'VariableDeclaration') {
415
+ return;
416
+ }
417
+ // Only track `const` destructures. `let` and `var` allow reassignment that
418
+ // a single-pass walker cannot track, so `let { fire } = ctx; fire = other;
419
+ // fire('x')` would otherwise be a false positive. Skipping non-const is a
420
+ // small precision loss (see TSDoc on `collectDestructuredFireNames`) in
421
+ // exchange for eliminating that class of false positives.
422
+ const { kind } = stmt as unknown as { kind?: string };
423
+ if (kind !== 'const') {
424
+ return;
425
+ }
426
+ const declarations =
427
+ (stmt as unknown as { declarations?: readonly AstNode[] }).declarations ??
428
+ [];
429
+ for (const decl of declarations) {
430
+ const pattern = getCtxDestructurePattern(decl, ctxNames);
431
+ if (pattern) {
432
+ collectFireNamesFromPattern(pattern, names);
433
+ }
434
+ }
435
+ };
436
+
437
+ const collectDestructuredFireNames = (
438
+ body: AstNode,
439
+ ctxNames: ReadonlySet<string>
440
+ ): ReadonlySet<string> => {
441
+ const names = new Set<string>();
442
+ for (const stmt of getTopLevelStatements(body)) {
443
+ collectFireNamesFromDeclaration(stmt, ctxNames, names);
444
+ }
445
+ return names;
446
+ };
447
+
448
+ /**
449
+ * Build the set of context parameter names to match against.
450
+ *
451
+ * Returns ONLY the actual second-parameter name from the blaze signature.
452
+ * No seeded defaults: if the blaze has no second parameter, the returned set
453
+ * is empty and no `ctx.fire(...)` / `context.fire(...)` calls are tracked
454
+ * for that blaze. An unrelated closure-scoped `ctx` identifier is not the
455
+ * trail context and must not be treated as one.
456
+ */
457
+ const buildCtxNames = (body: AstNode): ReadonlySet<string> => {
458
+ const ctxNames = new Set<string>();
459
+ const paramName = extractContextParamName(body);
460
+ if (paramName) {
461
+ ctxNames.add(paramName);
462
+ }
463
+ return ctxNames;
464
+ };
465
+
466
+ /**
467
+ * Walk blaze bodies and collect all statically resolvable ctx.fire() signal IDs.
468
+ *
469
+ * Traversal uses `walkScope`, which stops at nested function boundaries
470
+ * (FunctionDeclaration, FunctionExpression, ArrowFunctionExpression). This
471
+ * mirrors the top-level-only behavior of `collectDestructuredFireNames` and
472
+ * avoids false positives when a nested function parameter shadows `ctx` or a
473
+ * destructured `fire` local:
474
+ *
475
+ * ```ts
476
+ * blaze: async (_, ctx) => {
477
+ * const { fire } = ctx;
478
+ * function nested(fire) { fire(orderPlaced); } // ignored — shadowed
479
+ * function other(ctx) { ctx.fire(orderPlaced); } // ignored — shadowed
480
+ * return Result.ok({});
481
+ * }
482
+ * ```
483
+ *
484
+ * Tradeoff: legitimate helper-scoped fire calls are not statically analyzed
485
+ * today. This includes both direct `ctx.fire(...)` inside a nested helper and
486
+ * helper-local destructures like `const { fire } = ctx` inside that helper.
487
+ * The runtime + signal-id compose-check still validate them; the warden just
488
+ * can't prove them at lint time. A fuller helper-aware scope walker remains
489
+ * follow-up work if this precision loss becomes meaningful in practice.
490
+ */
491
+ interface CalledFires {
492
+ readonly hasUnresolved: boolean;
493
+ readonly ids: ReadonlySet<string>;
494
+ readonly stringIds: ReadonlySet<string>;
495
+ }
496
+
497
+ const mergeCalledFires = (
498
+ target: {
499
+ hasUnresolved: boolean;
500
+ ids: Set<string>;
501
+ stringIds: Set<string>;
502
+ },
503
+ source: CalledFires
504
+ ): void => {
505
+ for (const id of source.ids) {
506
+ target.ids.add(id);
507
+ }
508
+ for (const id of source.stringIds) {
509
+ target.stringIds.add(id);
510
+ }
511
+ target.hasUnresolved = target.hasUnresolved || source.hasUnresolved;
512
+ };
513
+
514
+ const extractCalledFiresFromBody = (
515
+ body: AstNode,
516
+ sourceCode: string,
517
+ signalIds: SignalIdentifierResolver
518
+ ): CalledFires => {
519
+ const ids = new Set<string>();
520
+ const stringIds = new Set<string>();
521
+ let hasUnresolved = false;
522
+ const ctxNames = buildCtxNames(body);
523
+ const bodyFireNames = collectDestructuredFireNames(body, ctxNames);
524
+ const paramFireNames = collectParamFireNames(body);
525
+ const fireLocalNames = new Set<string>([...bodyFireNames, ...paramFireNames]);
526
+
527
+ walkScope(body, (node) => {
528
+ const call = extractFireCallId(
529
+ node,
530
+ ctxNames,
531
+ fireLocalNames,
532
+ sourceCode,
533
+ signalIds
534
+ );
535
+ if (!call) {
536
+ return;
537
+ }
538
+ if (call.id) {
539
+ ids.add(call.id);
540
+ }
541
+ if (call.stringId) {
542
+ stringIds.add(call.stringId);
543
+ }
544
+ if (call.unresolved) {
545
+ hasUnresolved = true;
546
+ }
547
+ });
548
+
549
+ return { hasUnresolved, ids, stringIds };
550
+ };
551
+
552
+ const extractCalledFires = (
553
+ config: AstNode,
554
+ sourceCode: string,
555
+ signalIds: SignalIdentifierResolver
556
+ ): CalledFires => {
557
+ const ids = new Set<string>();
558
+ const stringIds = new Set<string>();
559
+ const merged = { hasUnresolved: false, ids, stringIds };
560
+
561
+ for (const body of findBlazeBodies(config)) {
562
+ mergeCalledFires(
563
+ merged,
564
+ extractCalledFiresFromBody(body, sourceCode, signalIds)
565
+ );
566
+ }
567
+
568
+ return { hasUnresolved: merged.hasUnresolved, ids, stringIds };
569
+ };
570
+
571
+ // ---------------------------------------------------------------------------
572
+ // Diagnostic builders
573
+ // ---------------------------------------------------------------------------
574
+
575
+ const buildUndeclaredDiagnostic = (
576
+ trailId: string,
577
+ signalId: string,
578
+ filePath: string,
579
+ line: number,
580
+ softened = false
581
+ ): WardenDiagnostic => ({
582
+ filePath,
583
+ line,
584
+ message: softened
585
+ ? `Trail "${trailId}": ctx.fire('${signalId}') called but '${signalId}' is not declared in fires (may be declared via object-form fires entries)`
586
+ : `Trail "${trailId}": ctx.fire('${signalId}') called but '${signalId}' is not declared in fires`,
587
+ rule: 'fires-declarations',
588
+ severity: softened ? 'warn' : 'error',
589
+ });
590
+
591
+ const buildStringFireDiagnostic = (
592
+ trailId: string,
593
+ signalId: string,
594
+ filePath: string,
595
+ line: number
596
+ ): WardenDiagnostic => ({
597
+ filePath,
598
+ line,
599
+ message: `Trail "${trailId}": ctx.fire('${signalId}') uses a string signal id; pass the Signal value to ctx.fire(signal, payload)`,
600
+ rule: 'fires-declarations',
601
+ severity: 'error',
602
+ });
603
+
604
+ const buildUnusedDiagnostic = (
605
+ trailId: string,
606
+ signalId: string,
607
+ filePath: string,
608
+ line: number
609
+ ): WardenDiagnostic => ({
610
+ filePath,
611
+ line,
612
+ message: `Trail "${trailId}": '${signalId}' declared in fires but ctx.fire('${signalId}') never called`,
613
+ rule: 'fires-declarations',
614
+ severity: 'warn',
615
+ });
616
+
617
+ // ---------------------------------------------------------------------------
618
+ // Comparison
619
+ // ---------------------------------------------------------------------------
620
+
621
+ /** Emit error for each called ID not present in declared set. */
622
+ const reportUndeclared = (
623
+ called: ReadonlySet<string>,
624
+ declared: ReadonlySet<string>,
625
+ ctx: {
626
+ trailId: string;
627
+ filePath: string;
628
+ line: number;
629
+ softened?: boolean;
630
+ },
631
+ diagnostics: WardenDiagnostic[]
632
+ ): void => {
633
+ for (const id of called) {
634
+ if (!declared.has(id)) {
635
+ diagnostics.push(
636
+ buildUndeclaredDiagnostic(
637
+ ctx.trailId,
638
+ id,
639
+ ctx.filePath,
640
+ ctx.line,
641
+ ctx.softened
642
+ )
643
+ );
644
+ }
645
+ }
646
+ };
647
+
648
+ /**
649
+ * Emit warning for each declared ID not present in called set.
650
+ *
651
+ * Note: unlike `reportUndeclared`, this function does NOT soften its
652
+ * diagnostics when `hasUnresolved` is true. The asymmetry is intentional —
653
+ * softening only applies to the undeclared direction because unresolved
654
+ * Signal-value entries might cover an unknown set of called IDs. In the
655
+ * unused direction, a declared string-literal that is never called is
656
+ * genuinely unused regardless of whether other entries are unresolved.
657
+ */
658
+ const reportUnused = (
659
+ declared: ReadonlySet<string>,
660
+ called: ReadonlySet<string>,
661
+ ctx: { trailId: string; filePath: string; line: number },
662
+ diagnostics: WardenDiagnostic[]
663
+ ): void => {
664
+ for (const id of declared) {
665
+ if (!called.has(id)) {
666
+ diagnostics.push(
667
+ buildUnusedDiagnostic(ctx.trailId, id, ctx.filePath, ctx.line)
668
+ );
669
+ }
670
+ }
671
+ };
672
+
673
+ const reportStringFireCalls = (
674
+ stringIds: ReadonlySet<string>,
675
+ ctx: { trailId: string; filePath: string; line: number },
676
+ diagnostics: WardenDiagnostic[]
677
+ ): void => {
678
+ for (const id of stringIds) {
679
+ diagnostics.push(
680
+ buildStringFireDiagnostic(ctx.trailId, id, ctx.filePath, ctx.line)
681
+ );
682
+ }
683
+ };
684
+
685
+ const checkTrailDefinition = (
686
+ def: { id: string; config: AstNode; start: number },
687
+ filePath: string,
688
+ sourceCode: string,
689
+ signalIds: SignalIdentifierResolver,
690
+ diagnostics: WardenDiagnostic[]
691
+ ): void => {
692
+ const declared = extractDeclaredFires(def.config, sourceCode, signalIds);
693
+ const called = extractCalledFires(def.config, sourceCode, signalIds);
694
+
695
+ if (
696
+ declared.ids.size === 0 &&
697
+ !declared.hasUnresolved &&
698
+ called.ids.size === 0 &&
699
+ !called.hasUnresolved
700
+ ) {
701
+ return;
702
+ }
703
+
704
+ const line = offsetToLine(sourceCode, def.start);
705
+ const ctx = { filePath, line, trailId: def.id };
706
+ const signalValueCalledIds = new Set(
707
+ [...called.ids].filter((id) => !called.stringIds.has(id))
708
+ );
709
+
710
+ reportStringFireCalls(called.stringIds, ctx, diagnostics);
711
+ // When the declared array contains object-form references we can't resolve,
712
+ // downgrade "undeclared" diagnostics from error to warn with a disclaimer
713
+ // instead of suppressing entirely. The developer still sees genuinely
714
+ // undeclared calls, but we can't statically prove the call isn't covered by
715
+ // a Signal-value entry the runtime will normalize.
716
+ reportUndeclared(
717
+ signalValueCalledIds,
718
+ declared.ids,
719
+ { ...ctx, softened: declared.hasUnresolved },
720
+ diagnostics
721
+ );
722
+ reportUnused(declared.ids, called.ids, ctx, diagnostics);
723
+ };
724
+
725
+ // ---------------------------------------------------------------------------
726
+ // Rule
727
+ // ---------------------------------------------------------------------------
728
+
729
+ /**
730
+ * Validates that `ctx.fire()` calls align with declared `fires` arrays.
731
+ */
732
+ export const firesDeclarations: WardenRule = {
733
+ check(sourceCode: string, filePath: string): readonly WardenDiagnostic[] {
734
+ if (isTestFile(filePath)) {
735
+ return [];
736
+ }
737
+
738
+ const ast = parse(filePath, sourceCode);
739
+ if (!ast) {
740
+ return [];
741
+ }
742
+ const signalIds = buildSignalIdentifierResolver(ast);
743
+
744
+ const diagnostics: WardenDiagnostic[] = [];
745
+
746
+ for (const def of findTrailDefinitions(ast)) {
747
+ if (def.kind === 'trail') {
748
+ checkTrailDefinition(def, filePath, sourceCode, signalIds, diagnostics);
749
+ }
750
+ }
751
+
752
+ return diagnostics;
753
+ },
754
+ description:
755
+ 'Ensure ctx.fire() calls match the declared fires array in trail definitions.',
756
+ name: 'fires-declarations',
757
+ severity: 'error',
758
+ };