@ontrails/warden 1.0.0-beta.3 → 1.0.0-beta.32

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 (260) hide show
  1. package/CHANGELOG.md +674 -9
  2. package/README.md +133 -26
  3. package/bin/warden.ts +51 -0
  4. package/package.json +28 -5
  5. package/src/adapter-check.ts +136 -0
  6. package/src/ast.ts +137 -0
  7. package/src/cli.ts +1451 -104
  8. package/src/command.ts +966 -0
  9. package/src/config.ts +193 -0
  10. package/src/draft.ts +22 -0
  11. package/src/drift.ts +122 -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 +217 -14
  16. package/src/project-context.ts +192 -0
  17. package/src/project-rules.ts +290 -0
  18. package/src/resolve.ts +531 -0
  19. package/src/rules/activation-orphan.ts +97 -0
  20. package/src/rules/ast.ts +4008 -86
  21. package/src/rules/circular-refs.ts +154 -0
  22. package/src/rules/cli-command-route-coherence.ts +177 -0
  23. package/src/rules/composes-declarations.ts +837 -0
  24. package/src/rules/context-no-surface-types.ts +78 -15
  25. package/src/rules/contour-exists.ts +251 -0
  26. package/src/rules/contour-ids.ts +15 -0
  27. package/src/rules/dead-internal-trail.ts +161 -0
  28. package/src/rules/dead-public-trail.ts +258 -0
  29. package/src/rules/draft-file-marking.ts +160 -0
  30. package/src/rules/draft-visible-debt.ts +87 -0
  31. package/src/rules/duplicate-public-contract.ts +91 -0
  32. package/src/rules/error-mapping-completeness.ts +290 -0
  33. package/src/rules/example-valid.ts +395 -0
  34. package/src/rules/fires-declarations.ts +735 -0
  35. package/src/rules/implementation-returns-result.ts +1409 -166
  36. package/src/rules/incomplete-accessor-for-standard-op.ts +272 -0
  37. package/src/rules/incomplete-crud.ts +581 -0
  38. package/src/rules/index.ts +235 -18
  39. package/src/rules/intent-propagation.ts +127 -0
  40. package/src/rules/layer-field-name-drift.ts +102 -0
  41. package/src/rules/library-projection-coherence.ts +100 -0
  42. package/src/rules/metadata.ts +755 -0
  43. package/src/rules/missing-reconcile.ts +98 -0
  44. package/src/rules/missing-visibility.ts +110 -0
  45. package/src/rules/no-destructured-compose.ts +194 -0
  46. package/src/rules/no-dev-permit-in-source.ts +99 -0
  47. package/src/rules/no-direct-implementation-call.ts +7 -7
  48. package/src/rules/no-legacy-layer-imports.ts +211 -0
  49. package/src/rules/no-native-error-result.ts +118 -0
  50. package/src/rules/no-redundant-result-error-wrap.ts +384 -0
  51. package/src/rules/no-retired-cross-vocabulary.ts +194 -0
  52. package/src/rules/no-sync-result-assumption.ts +1135 -98
  53. package/src/rules/no-throw-in-detour-recover.ts +225 -0
  54. package/src/rules/no-throw-in-implementation.ts +10 -9
  55. package/src/rules/no-top-level-surface.ts +371 -0
  56. package/src/rules/on-references-exist.ts +194 -0
  57. package/src/rules/orphaned-signal.ts +150 -0
  58. package/src/rules/owner-projection-parity.ts +143 -0
  59. package/src/rules/permit-governance.ts +25 -0
  60. package/src/rules/public-export-example-coverage.ts +562 -0
  61. package/src/rules/public-internal-deep-imports.ts +457 -0
  62. package/src/rules/public-output-schema.ts +29 -0
  63. package/src/rules/public-union-output-discriminants.ts +150 -0
  64. package/src/rules/read-intent-fires.ts +187 -0
  65. package/src/rules/reference-exists.ts +98 -0
  66. package/src/rules/registry-names.ts +155 -0
  67. package/src/rules/resolved-import-boundary.ts +146 -0
  68. package/src/rules/resource-declarations.ts +693 -0
  69. package/src/rules/resource-exists.ts +179 -0
  70. package/src/rules/resource-id-grammar.ts +65 -0
  71. package/src/rules/resource-mock-coverage.ts +115 -0
  72. package/src/rules/scan.ts +38 -25
  73. package/src/rules/scheduled-destroy-intent.ts +44 -0
  74. package/src/rules/signal-graph-coaching.ts +191 -0
  75. package/src/rules/specs.ts +9 -5
  76. package/src/rules/static-resource-accessor-preference.ts +649 -0
  77. package/src/rules/surface-facet-coherence.ts +362 -0
  78. package/src/rules/trail-fork-coaching.ts +616 -0
  79. package/src/rules/trail-versioning-source.ts +1072 -0
  80. package/src/rules/trail-versioning-topo.ts +172 -0
  81. package/src/rules/types.ts +297 -8
  82. package/src/rules/unmaterialized-activation-source.ts +84 -0
  83. package/src/rules/unreachable-detour-shadowing.ts +339 -0
  84. package/src/rules/valid-describe-refs.ts +162 -32
  85. package/src/rules/valid-detour-contract.ts +78 -0
  86. package/src/rules/warden-export-symmetry.ts +540 -0
  87. package/src/rules/warden-rules-use-ast.ts +1114 -0
  88. package/src/rules/webhook-route-collision.ts +243 -0
  89. package/src/trails/activation-orphan.trail.ts +84 -0
  90. package/src/trails/circular-refs.trail.ts +29 -0
  91. package/src/trails/cli-command-route-coherence.trail.ts +47 -0
  92. package/src/trails/composes-declarations.trail.ts +22 -0
  93. package/src/trails/context-no-surface-types.trail.ts +21 -0
  94. package/src/trails/contour-exists.trail.ts +21 -0
  95. package/src/trails/dead-internal-trail.trail.ts +26 -0
  96. package/src/trails/dead-public-trail.trail.ts +31 -0
  97. package/src/trails/deprecation-without-guidance.trail.ts +21 -0
  98. package/src/trails/draft-file-marking.trail.ts +16 -0
  99. package/src/trails/draft-visible-debt.trail.ts +16 -0
  100. package/src/trails/duplicate-public-contract.trail.ts +47 -0
  101. package/src/trails/error-mapping-completeness.trail.ts +30 -0
  102. package/src/trails/example-valid.trail.ts +25 -0
  103. package/src/trails/fires-declarations.trail.ts +23 -0
  104. package/src/trails/fork-without-preserved-blaze.trail.ts +31 -0
  105. package/src/trails/implementation-returns-result.trail.ts +20 -0
  106. package/src/trails/incomplete-accessor-for-standard-op.trail.ts +76 -0
  107. package/src/trails/incomplete-crud.trail.ts +39 -0
  108. package/src/trails/index.ts +83 -0
  109. package/src/trails/intent-propagation.trail.ts +30 -0
  110. package/src/trails/layer-field-name-drift.trail.ts +39 -0
  111. package/src/trails/library-projection-coherence.trail.ts +43 -0
  112. package/src/trails/marker-schema-unsupported.trail.ts +23 -0
  113. package/src/trails/missing-reconcile.trail.ts +33 -0
  114. package/src/trails/missing-visibility.trail.ts +22 -0
  115. package/src/trails/no-destructured-compose.trail.ts +44 -0
  116. package/src/trails/no-dev-permit-in-source.trail.ts +16 -0
  117. package/src/trails/no-direct-implementation-call.trail.ts +16 -0
  118. package/src/trails/no-legacy-layer-imports.trail.ts +41 -0
  119. package/src/trails/no-native-error-result.trail.ts +18 -0
  120. package/src/trails/no-redundant-result-error-wrap.trail.ts +55 -0
  121. package/src/trails/no-retired-cross-vocabulary.trail.ts +42 -0
  122. package/src/trails/no-sync-result-assumption.trail.ts +19 -0
  123. package/src/trails/no-throw-in-detour-recover.trail.ts +24 -0
  124. package/src/trails/no-throw-in-implementation.trail.ts +20 -0
  125. package/src/trails/no-top-level-surface.trail.ts +43 -0
  126. package/src/trails/on-references-exist.trail.ts +21 -0
  127. package/src/trails/orphaned-signal.trail.ts +36 -0
  128. package/src/trails/owner-projection-parity.trail.ts +26 -0
  129. package/src/trails/pending-force.trail.ts +21 -0
  130. package/src/trails/permit-governance.trail.ts +51 -0
  131. package/src/trails/prefer-schema-inference.trail.ts +21 -0
  132. package/src/trails/public-export-example-coverage.trail.ts +16 -0
  133. package/src/trails/public-internal-deep-imports.trail.ts +94 -0
  134. package/src/trails/public-output-schema.trail.ts +55 -0
  135. package/src/trails/public-union-output-discriminants.trail.ts +33 -0
  136. package/src/trails/read-intent-fires.trail.ts +20 -0
  137. package/src/trails/reference-exists.trail.ts +25 -0
  138. package/src/trails/resolved-import-boundary.trail.ts +109 -0
  139. package/src/trails/resource-declarations.trail.ts +25 -0
  140. package/src/trails/resource-exists.trail.ts +27 -0
  141. package/src/trails/resource-id-grammar.trail.ts +39 -0
  142. package/src/trails/resource-mock-coverage.trail.ts +40 -0
  143. package/src/trails/run.ts +162 -0
  144. package/src/trails/scheduled-destroy-intent.trail.ts +56 -0
  145. package/src/trails/schema.ts +198 -0
  146. package/src/trails/signal-graph-coaching.trail.ts +77 -0
  147. package/src/trails/static-resource-accessor-preference.trail.ts +25 -0
  148. package/src/trails/surface-facet-coherence.trail.ts +25 -0
  149. package/src/trails/topo.ts +6 -0
  150. package/src/trails/trail-fork-coaching.trail.ts +42 -0
  151. package/src/trails/unmaterialized-activation-source.trail.ts +72 -0
  152. package/src/trails/unreachable-detour-shadowing.trail.ts +45 -0
  153. package/src/trails/valid-describe-refs.trail.ts +18 -0
  154. package/src/trails/valid-detour-contract.trail.ts +71 -0
  155. package/src/trails/version-gap.trail.ts +35 -0
  156. package/src/trails/version-pinned-compose.trail.ts +23 -0
  157. package/src/trails/version-without-examples.trail.ts +38 -0
  158. package/src/trails/warden-export-symmetry.trail.ts +16 -0
  159. package/src/trails/warden-rules-use-ast.trail.ts +64 -0
  160. package/src/trails/webhook-route-collision.trail.ts +50 -0
  161. package/src/trails/wrap-rule.ts +214 -0
  162. package/src/workspaces.ts +199 -0
  163. package/.turbo/turbo-build.log +0 -1
  164. package/.turbo/turbo-lint.log +0 -3
  165. package/.turbo/turbo-typecheck.log +0 -1
  166. package/dist/cli.d.ts +0 -46
  167. package/dist/cli.d.ts.map +0 -1
  168. package/dist/cli.js +0 -221
  169. package/dist/cli.js.map +0 -1
  170. package/dist/drift.d.ts +0 -26
  171. package/dist/drift.d.ts.map +0 -1
  172. package/dist/drift.js +0 -27
  173. package/dist/drift.js.map +0 -1
  174. package/dist/formatters.d.ts +0 -29
  175. package/dist/formatters.d.ts.map +0 -1
  176. package/dist/formatters.js +0 -87
  177. package/dist/formatters.js.map +0 -1
  178. package/dist/index.d.ts +0 -26
  179. package/dist/index.d.ts.map +0 -1
  180. package/dist/index.js +0 -26
  181. package/dist/index.js.map +0 -1
  182. package/dist/rules/ast.d.ts +0 -41
  183. package/dist/rules/ast.d.ts.map +0 -1
  184. package/dist/rules/ast.js +0 -163
  185. package/dist/rules/ast.js.map +0 -1
  186. package/dist/rules/context-no-surface-types.d.ts +0 -12
  187. package/dist/rules/context-no-surface-types.d.ts.map +0 -1
  188. package/dist/rules/context-no-surface-types.js +0 -96
  189. package/dist/rules/context-no-surface-types.js.map +0 -1
  190. package/dist/rules/implementation-returns-result.d.ts +0 -13
  191. package/dist/rules/implementation-returns-result.d.ts.map +0 -1
  192. package/dist/rules/implementation-returns-result.js +0 -277
  193. package/dist/rules/implementation-returns-result.js.map +0 -1
  194. package/dist/rules/index.d.ts +0 -22
  195. package/dist/rules/index.d.ts.map +0 -1
  196. package/dist/rules/index.js +0 -41
  197. package/dist/rules/index.js.map +0 -1
  198. package/dist/rules/no-direct-impl-in-route.d.ts +0 -12
  199. package/dist/rules/no-direct-impl-in-route.d.ts.map +0 -1
  200. package/dist/rules/no-direct-impl-in-route.js +0 -46
  201. package/dist/rules/no-direct-impl-in-route.js.map +0 -1
  202. package/dist/rules/no-direct-implementation-call.d.ts +0 -12
  203. package/dist/rules/no-direct-implementation-call.d.ts.map +0 -1
  204. package/dist/rules/no-direct-implementation-call.js +0 -39
  205. package/dist/rules/no-direct-implementation-call.js.map +0 -1
  206. package/dist/rules/no-sync-result-assumption.d.ts +0 -6
  207. package/dist/rules/no-sync-result-assumption.d.ts.map +0 -1
  208. package/dist/rules/no-sync-result-assumption.js +0 -98
  209. package/dist/rules/no-sync-result-assumption.js.map +0 -1
  210. package/dist/rules/no-throw-in-detour-target.d.ts +0 -12
  211. package/dist/rules/no-throw-in-detour-target.d.ts.map +0 -1
  212. package/dist/rules/no-throw-in-detour-target.js +0 -87
  213. package/dist/rules/no-throw-in-detour-target.js.map +0 -1
  214. package/dist/rules/no-throw-in-implementation.d.ts +0 -9
  215. package/dist/rules/no-throw-in-implementation.d.ts.map +0 -1
  216. package/dist/rules/no-throw-in-implementation.js +0 -34
  217. package/dist/rules/no-throw-in-implementation.js.map +0 -1
  218. package/dist/rules/prefer-schema-inference.d.ts +0 -7
  219. package/dist/rules/prefer-schema-inference.d.ts.map +0 -1
  220. package/dist/rules/prefer-schema-inference.js +0 -86
  221. package/dist/rules/prefer-schema-inference.js.map +0 -1
  222. package/dist/rules/scan.d.ts +0 -8
  223. package/dist/rules/scan.d.ts.map +0 -1
  224. package/dist/rules/scan.js +0 -32
  225. package/dist/rules/scan.js.map +0 -1
  226. package/dist/rules/specs.d.ts +0 -29
  227. package/dist/rules/specs.d.ts.map +0 -1
  228. package/dist/rules/specs.js +0 -192
  229. package/dist/rules/specs.js.map +0 -1
  230. package/dist/rules/structure.d.ts +0 -13
  231. package/dist/rules/structure.d.ts.map +0 -1
  232. package/dist/rules/structure.js +0 -142
  233. package/dist/rules/structure.js.map +0 -1
  234. package/dist/rules/types.d.ts +0 -52
  235. package/dist/rules/types.d.ts.map +0 -1
  236. package/dist/rules/types.js +0 -2
  237. package/dist/rules/types.js.map +0 -1
  238. package/dist/rules/valid-describe-refs.d.ts +0 -7
  239. package/dist/rules/valid-describe-refs.d.ts.map +0 -1
  240. package/dist/rules/valid-describe-refs.js +0 -51
  241. package/dist/rules/valid-describe-refs.js.map +0 -1
  242. package/dist/rules/valid-detour-refs.d.ts +0 -6
  243. package/dist/rules/valid-detour-refs.d.ts.map +0 -1
  244. package/dist/rules/valid-detour-refs.js +0 -116
  245. package/dist/rules/valid-detour-refs.js.map +0 -1
  246. package/src/__tests__/cli.test.ts +0 -198
  247. package/src/__tests__/drift.test.ts +0 -74
  248. package/src/__tests__/formatters.test.ts +0 -157
  249. package/src/__tests__/implementation-returns-result.test.ts +0 -128
  250. package/src/__tests__/no-direct-implementation-call.test.ts +0 -83
  251. package/src/__tests__/no-sync-result-assumption.test.ts +0 -85
  252. package/src/__tests__/no-throw-in-detour-target.test.ts +0 -78
  253. package/src/__tests__/prefer-schema-inference.test.ts +0 -84
  254. package/src/__tests__/rules.test.ts +0 -215
  255. package/src/__tests__/valid-describe-refs.test.ts +0 -60
  256. package/src/rules/no-direct-impl-in-route.ts +0 -77
  257. package/src/rules/no-throw-in-detour-target.ts +0 -150
  258. package/src/rules/valid-detour-refs.ts +0 -187
  259. package/tsconfig.json +0 -9
  260. package/tsconfig.tsbuildinfo +0 -1
@@ -0,0 +1,735 @@
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
+ deriveConstString,
17
+ extractStringLiteral,
18
+ findBlazeBodies,
19
+ findConfigProperty,
20
+ findTrailDefinitions,
21
+ getNodeBodyNode,
22
+ getNodeBodyStatements,
23
+ getNodeDeclarations,
24
+ getNodeId,
25
+ getNodeInit,
26
+ getNodeKey,
27
+ getNodeKind,
28
+ getNodeLeft,
29
+ getNodeObject,
30
+ getNodeProperties,
31
+ getNodeProperty,
32
+ getNodeValueNode,
33
+ identifierName,
34
+ offsetToLine,
35
+ parse,
36
+ walkScope,
37
+ } from './ast.js';
38
+ import type { AstNode, SignalIdentifierResolver } from './ast.js';
39
+ import { isTestFile } from './scan.js';
40
+ import type { WardenDiagnostic, WardenRule } from './types.js';
41
+
42
+ // ---------------------------------------------------------------------------
43
+ // Const identifier resolution
44
+ // ---------------------------------------------------------------------------
45
+
46
+ /**
47
+ * Resolve an array element to a static signal ID when possible.
48
+ *
49
+ * Returns null for entries the rule can't statically resolve. Unresolved entries
50
+ * are not proof of missingness, but they also do not suppress diagnostics for
51
+ * statically resolved `ctx.fire()` calls that target different signals.
52
+ */
53
+ const resolveFireElementId = (
54
+ element: AstNode,
55
+ sourceCode: string,
56
+ signalIds: SignalIdentifierResolver
57
+ ): string | null => {
58
+ const literalValue = extractStringLiteral(element);
59
+ if (literalValue !== null) {
60
+ return literalValue;
61
+ }
62
+
63
+ if (element.type === 'Identifier') {
64
+ const name = identifierName(element);
65
+ if (name) {
66
+ const resolved = signalIds.resolve(element);
67
+ if (resolved.kind === 'signal' || resolved.kind === 'string') {
68
+ return resolved.id;
69
+ }
70
+ if (resolved.kind === 'shadowed') {
71
+ return null;
72
+ }
73
+ return deriveConstString(name, sourceCode);
74
+ }
75
+ }
76
+
77
+ return null;
78
+ };
79
+
80
+ // ---------------------------------------------------------------------------
81
+ // Declared fires extraction
82
+ // ---------------------------------------------------------------------------
83
+
84
+ /** Extract the ArrayExpression elements from a config's `fires` property. */
85
+ const getFiresElements = (config: AstNode): readonly AstNode[] | null => {
86
+ const firesProp = findConfigProperty(config, 'fires');
87
+ if (!firesProp) {
88
+ return null;
89
+ }
90
+
91
+ const arrayNode = firesProp.value;
92
+ if (!arrayNode || (arrayNode as AstNode).type !== 'ArrayExpression') {
93
+ return null;
94
+ }
95
+
96
+ const elements = (arrayNode as AstNode)['elements'] as
97
+ | readonly AstNode[]
98
+ | undefined;
99
+ return elements ?? null;
100
+ };
101
+
102
+ interface DeclaredFires {
103
+ /** Statically resolved signal ids from string literals / const identifiers. */
104
+ readonly ids: ReadonlySet<string>;
105
+ /** True if any element could not be statically resolved (e.g. Signal value). */
106
+ readonly hasUnresolved: boolean;
107
+ }
108
+
109
+ /**
110
+ * Extract declared fires from a `fires: [...]` array.
111
+ *
112
+ * Object-form entries (`fires: [someSignal]`) can be unresolved at lint time
113
+ * when they come from imports the rule cannot prove. Those unresolved entries
114
+ * do not soften concrete undeclared `ctx.fire()` calls; this mirrors
115
+ * `composes-declarations` and keeps real drift hard-failing.
116
+ */
117
+ const resolveDeclaredFiresElements = (
118
+ elements: readonly AstNode[],
119
+ sourceCode: string,
120
+ signalIds: SignalIdentifierResolver
121
+ ): DeclaredFires => {
122
+ const ids = new Set<string>();
123
+ let hasUnresolved = false;
124
+ for (const element of elements) {
125
+ const resolved = resolveFireElementId(element, sourceCode, signalIds);
126
+ if (resolved) {
127
+ ids.add(resolved);
128
+ } else {
129
+ hasUnresolved = true;
130
+ }
131
+ }
132
+ return { hasUnresolved, ids };
133
+ };
134
+
135
+ const extractDeclaredFires = (
136
+ config: AstNode,
137
+ sourceCode: string,
138
+ signalIds: SignalIdentifierResolver
139
+ ): DeclaredFires => {
140
+ const elements = getFiresElements(config);
141
+ return elements
142
+ ? resolveDeclaredFiresElements(elements, sourceCode, signalIds)
143
+ : { hasUnresolved: false, ids: new Set() };
144
+ };
145
+
146
+ // ---------------------------------------------------------------------------
147
+ // Called fires extraction — member expression helpers
148
+ // ---------------------------------------------------------------------------
149
+
150
+ const MEMBER_TYPES = new Set(['StaticMemberExpression', 'MemberExpression']);
151
+
152
+ /** Extract object and property Identifier names from a MemberExpression. */
153
+ const extractMemberPair = (
154
+ callee: AstNode
155
+ ): { objName: string; propName: string } | null => {
156
+ if (!MEMBER_TYPES.has(callee.type)) {
157
+ return null;
158
+ }
159
+
160
+ const objName = identifierName(getNodeObject(callee));
161
+ const propName = identifierName(getNodeProperty(callee));
162
+
163
+ return objName && propName ? { objName, propName } : null;
164
+ };
165
+
166
+ /**
167
+ * Extract the second parameter node from a blaze function node.
168
+ *
169
+ * Handles `(input, ctx) => ...`, `async (input, context) => ...`,
170
+ * `function(input, ctx) { ... }`, and parameter-level destructuring
171
+ * like `(input, { fire }) => ...`.
172
+ */
173
+ const extractContextParamNode = (blazeBody: AstNode): AstNode | null => {
174
+ const params = blazeBody['params'] as readonly AstNode[] | undefined;
175
+ if (!params || params.length < 2) {
176
+ return null;
177
+ }
178
+ return params[1] ?? null;
179
+ };
180
+
181
+ /** Extract the local name bound to `fire` inside an ObjectPattern Property. */
182
+ const extractFireLocalName = (prop: AstNode): string | null => {
183
+ if (prop.type !== 'Property') {
184
+ return null;
185
+ }
186
+ const key = getNodeKey(prop);
187
+ const value = getNodeValueNode(prop);
188
+ const keyName = identifierName(key);
189
+ if (keyName !== 'fire') {
190
+ return null;
191
+ }
192
+ // `{ fire }` → key and value are the same Identifier (shorthand).
193
+ // `{ fire: emit }` → value is a distinct Identifier.
194
+ return identifierName(value) ?? keyName;
195
+ };
196
+
197
+ /** Collect `fire` local names from an ObjectPattern's properties into `names`. */
198
+ const collectFireNamesFromPattern = (
199
+ pattern: AstNode,
200
+ names: Set<string>
201
+ ): void => {
202
+ const properties = getNodeProperties(pattern);
203
+ if (!properties) {
204
+ return;
205
+ }
206
+ for (const prop of properties) {
207
+ const localName = extractFireLocalName(prop);
208
+ if (localName) {
209
+ names.add(localName);
210
+ }
211
+ }
212
+ };
213
+
214
+ /**
215
+ * Extract the second parameter name from a blaze function node.
216
+ *
217
+ * Returns null when the parameter is not a plain Identifier (e.g. when the
218
+ * author destructures `{ fire }` in the parameter list). Parameter-level
219
+ * destructuring is handled separately by `collectParamFireNames`.
220
+ *
221
+ * Also handles defaulted parameters like `(input, ctx = fallback) => ...`
222
+ * (AssignmentPattern whose `.left` is the Identifier). Without this, valid
223
+ * signatures would silently drop out of ctx-access analysis.
224
+ */
225
+ const extractContextParamName = (blazeBody: AstNode): string | null => {
226
+ const param = extractContextParamNode(blazeBody);
227
+ if (!param) {
228
+ return null;
229
+ }
230
+ if (param.type === 'AssignmentPattern') {
231
+ const left = getNodeLeft(param);
232
+ return identifierName(left);
233
+ }
234
+ return identifierName(param);
235
+ };
236
+
237
+ /**
238
+ * Collect `fire` local names bound via parameter-level destructuring.
239
+ *
240
+ * Recognizes `(input, { fire }) => ...` and `(input, { fire: emit }) => ...`.
241
+ * When the blaze author destructures in the parameter list, there is no
242
+ * enclosing `ctx` identifier to track — we seed the fire local set directly
243
+ * from the ObjectPattern in `params[1]`.
244
+ */
245
+ const collectParamFireNames = (body: AstNode): ReadonlySet<string> => {
246
+ const param = extractContextParamNode(body);
247
+ if (!param || param.type !== 'ObjectPattern') {
248
+ return new Set();
249
+ }
250
+ const names = new Set<string>();
251
+ collectFireNamesFromPattern(param, names);
252
+ return names;
253
+ };
254
+
255
+ /** Check if a callee is a member-style fire call: <ctxName>.fire(...). */
256
+ const isMemberFireCall = (
257
+ callee: AstNode,
258
+ ctxNames: ReadonlySet<string>
259
+ ): boolean => {
260
+ const pair = extractMemberPair(callee);
261
+ return !!pair && ctxNames.has(pair.objName) && pair.propName === 'fire';
262
+ };
263
+
264
+ /**
265
+ * Check if a node is a `<ctxName>.fire(...)` call.
266
+ *
267
+ * Also matches bare `<fireLocalName>(...)` calls, but only when the local name
268
+ * was verifiably destructured from the trail context (e.g. `const { fire } = ctx`
269
+ * or `const { fire: emit } = ctx`). Unrelated local `fire()` helpers are
270
+ * ignored — see `collectDestructuredFireNames`.
271
+ */
272
+ const isTrackedFireCallee = (
273
+ callee: AstNode,
274
+ ctxNames: ReadonlySet<string>,
275
+ fireLocalNames: ReadonlySet<string>
276
+ ): boolean => {
277
+ if (isMemberFireCall(callee, ctxNames)) {
278
+ return true;
279
+ }
280
+ const calleeName = identifierName(callee);
281
+ return !!calleeName && fireLocalNames.has(calleeName);
282
+ };
283
+
284
+ interface FireCallArg {
285
+ readonly id: string | null;
286
+ readonly stringId: string | null;
287
+ readonly unresolved: boolean;
288
+ }
289
+
290
+ const firstCallArg = (node: AstNode): AstNode | null => {
291
+ const args = node['arguments'] as readonly AstNode[] | undefined;
292
+ return args?.[0] ?? null;
293
+ };
294
+
295
+ const resolveFireCallArg = (
296
+ arg: AstNode | null,
297
+ sourceCode: string,
298
+ signalIds: SignalIdentifierResolver
299
+ ): FireCallArg => {
300
+ if (!arg) {
301
+ return { id: null, stringId: null, unresolved: true };
302
+ }
303
+
304
+ const stringId = extractStringLiteral(arg);
305
+ if (stringId !== null) {
306
+ return { id: stringId, stringId, unresolved: false };
307
+ }
308
+
309
+ if (arg.type === 'Identifier') {
310
+ const name = identifierName(arg);
311
+ if (!name) {
312
+ return { id: null, stringId: null, unresolved: true };
313
+ }
314
+ const resolved = signalIds.resolve(arg);
315
+ if (resolved.kind === 'signal') {
316
+ return { id: resolved.id, stringId: null, unresolved: false };
317
+ }
318
+ if (resolved.kind === 'string') {
319
+ return { id: resolved.id, stringId: resolved.id, unresolved: false };
320
+ }
321
+ if (resolved.kind === 'shadowed') {
322
+ return { id: null, stringId: null, unresolved: true };
323
+ }
324
+ const constStringId = deriveConstString(name, sourceCode);
325
+ if (constStringId) {
326
+ return { id: constStringId, stringId: constStringId, unresolved: false };
327
+ }
328
+ }
329
+
330
+ return { id: null, stringId: null, unresolved: true };
331
+ };
332
+
333
+ const extractFireCallId = (
334
+ node: AstNode,
335
+ ctxNames: ReadonlySet<string>,
336
+ fireLocalNames: ReadonlySet<string>,
337
+ sourceCode: string,
338
+ signalIds: SignalIdentifierResolver
339
+ ): FireCallArg | null => {
340
+ if (node.type !== 'CallExpression') {
341
+ return null;
342
+ }
343
+ const callee = node['callee'] as AstNode | undefined;
344
+ if (!callee) {
345
+ return null;
346
+ }
347
+ return isTrackedFireCallee(callee, ctxNames, fireLocalNames)
348
+ ? resolveFireCallArg(firstCallArg(node), sourceCode, signalIds)
349
+ : null;
350
+ };
351
+
352
+ /**
353
+ * Walk a blaze body and collect local names bound to `ctx.fire` via destructure.
354
+ *
355
+ * Recognizes:
356
+ * - `const { fire } = ctx;` → adds `fire`
357
+ * - `const { fire: emit } = context;` → adds `emit`
358
+ *
359
+ * Only destructures whose init is one of the tracked ctx parameter names are
360
+ * accepted. This prevents unrelated local `fire` helpers from being treated as
361
+ * calls into the trail context.
362
+ */
363
+ /** Check if a VariableDeclarator destructures from a known ctx identifier. */
364
+ const getCtxDestructurePattern = (
365
+ node: AstNode,
366
+ ctxNames: ReadonlySet<string>
367
+ ): AstNode | null => {
368
+ if (node.type !== 'VariableDeclarator') {
369
+ return null;
370
+ }
371
+ const id = getNodeId(node);
372
+ const init = getNodeInit(node);
373
+ if (!id || id.type !== 'ObjectPattern' || !init) {
374
+ return null;
375
+ }
376
+ const initName = identifierName(init);
377
+ if (!initName || !ctxNames.has(initName)) {
378
+ return null;
379
+ }
380
+ return id;
381
+ };
382
+
383
+ /**
384
+ * Collect `fire` local names destructured from ctx at the TOP LEVEL of the
385
+ * blaze body. Destructures inside nested functions are intentionally ignored
386
+ * to avoid leaking nested-scope bindings into the outer blaze scope — a
387
+ * `const { fire } = ctx` inside a nested helper should not cause an outer
388
+ * bare `fire('x')` to be treated as a ctx-bound call.
389
+ *
390
+ * Tradeoff: nested-scope destructures lose tracking entirely. Calls inside
391
+ * nested functions that rely on their own destructure will not be analyzed.
392
+ * This is a conservative precision loss; a full scope walker is a follow-up.
393
+ *
394
+ * Tradeoff: only `const` destructures are tracked. `let` and `var` bindings
395
+ * allow reassignment (`let { fire } = ctx; fire = other; fire('x')`) which
396
+ * this flow-insensitive walker cannot follow. Skipping them trades a small
397
+ * amount of precision — `let { fire } = ctx` is rare — for eliminating a
398
+ * class of false positives. The runtime + signal-id compose-check still
399
+ * validate real undeclared fires.
400
+ */
401
+ /** Get the top-level statements of a blaze function's BlockStatement body. */
402
+ const getTopLevelStatements = (body: AstNode): readonly AstNode[] => {
403
+ const blockBody = getNodeBodyNode(body);
404
+ if (!blockBody || blockBody.type !== 'BlockStatement') {
405
+ return [];
406
+ }
407
+ return getNodeBodyStatements(blockBody);
408
+ };
409
+
410
+ /** Collect fire-local names from a single top-level VariableDeclaration. */
411
+ const collectFireNamesFromDeclaration = (
412
+ stmt: AstNode,
413
+ ctxNames: ReadonlySet<string>,
414
+ names: Set<string>
415
+ ): void => {
416
+ if (stmt.type !== 'VariableDeclaration') {
417
+ return;
418
+ }
419
+ // Only track `const` destructures. `let` and `var` allow reassignment that
420
+ // a single-pass walker cannot track, so `let { fire } = ctx; fire = other;
421
+ // fire('x')` would otherwise be a false positive. Skipping non-const is a
422
+ // small precision loss (see TSDoc on `collectDestructuredFireNames`) in
423
+ // exchange for eliminating that class of false positives.
424
+ const kind = getNodeKind(stmt);
425
+ if (kind !== 'const') {
426
+ return;
427
+ }
428
+ const declarations = getNodeDeclarations(stmt) ?? [];
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
+ ): WardenDiagnostic => ({
581
+ filePath,
582
+ line,
583
+ message: `Trail "${trailId}": ctx.fire('${signalId}') called but '${signalId}' is not declared in fires`,
584
+ rule: 'fires-declarations',
585
+ severity: 'error',
586
+ });
587
+
588
+ const buildStringFireDiagnostic = (
589
+ trailId: string,
590
+ signalId: string,
591
+ filePath: string,
592
+ line: number
593
+ ): WardenDiagnostic => ({
594
+ filePath,
595
+ line,
596
+ message: `Trail "${trailId}": ctx.fire('${signalId}') uses a string signal id; pass the Signal value to ctx.fire(signal, payload)`,
597
+ rule: 'fires-declarations',
598
+ severity: 'error',
599
+ });
600
+
601
+ const buildUnusedDiagnostic = (
602
+ trailId: string,
603
+ signalId: string,
604
+ filePath: string,
605
+ line: number
606
+ ): WardenDiagnostic => ({
607
+ filePath,
608
+ line,
609
+ message: `Trail "${trailId}": '${signalId}' declared in fires but ctx.fire('${signalId}') never called`,
610
+ rule: 'fires-declarations',
611
+ severity: 'warn',
612
+ });
613
+
614
+ // ---------------------------------------------------------------------------
615
+ // Comparison
616
+ // ---------------------------------------------------------------------------
617
+
618
+ /** Emit error for each called ID not present in declared set. */
619
+ const reportUndeclared = (
620
+ called: ReadonlySet<string>,
621
+ declared: ReadonlySet<string>,
622
+ ctx: {
623
+ trailId: string;
624
+ filePath: string;
625
+ line: number;
626
+ },
627
+ diagnostics: WardenDiagnostic[]
628
+ ): void => {
629
+ for (const id of called) {
630
+ if (!declared.has(id)) {
631
+ diagnostics.push(
632
+ buildUndeclaredDiagnostic(ctx.trailId, id, ctx.filePath, ctx.line)
633
+ );
634
+ }
635
+ }
636
+ };
637
+
638
+ /**
639
+ * Emit warning for each declared ID not present in called set.
640
+ *
641
+ * Unused string-literal declarations remain warnings: a declared string that is
642
+ * never called is visible cleanup debt even when other object-form declarations
643
+ * are unresolved.
644
+ */
645
+ const reportUnused = (
646
+ declared: ReadonlySet<string>,
647
+ called: ReadonlySet<string>,
648
+ ctx: { trailId: string; filePath: string; line: number },
649
+ diagnostics: WardenDiagnostic[]
650
+ ): void => {
651
+ for (const id of declared) {
652
+ if (!called.has(id)) {
653
+ diagnostics.push(
654
+ buildUnusedDiagnostic(ctx.trailId, id, ctx.filePath, ctx.line)
655
+ );
656
+ }
657
+ }
658
+ };
659
+
660
+ const reportStringFireCalls = (
661
+ stringIds: ReadonlySet<string>,
662
+ ctx: { trailId: string; filePath: string; line: number },
663
+ diagnostics: WardenDiagnostic[]
664
+ ): void => {
665
+ for (const id of stringIds) {
666
+ diagnostics.push(
667
+ buildStringFireDiagnostic(ctx.trailId, id, ctx.filePath, ctx.line)
668
+ );
669
+ }
670
+ };
671
+
672
+ const checkTrailDefinition = (
673
+ def: { id: string; config: AstNode; start: number },
674
+ filePath: string,
675
+ sourceCode: string,
676
+ signalIds: SignalIdentifierResolver,
677
+ diagnostics: WardenDiagnostic[]
678
+ ): void => {
679
+ const declared = extractDeclaredFires(def.config, sourceCode, signalIds);
680
+ const called = extractCalledFires(def.config, sourceCode, signalIds);
681
+
682
+ if (
683
+ declared.ids.size === 0 &&
684
+ !declared.hasUnresolved &&
685
+ called.ids.size === 0 &&
686
+ !called.hasUnresolved
687
+ ) {
688
+ return;
689
+ }
690
+
691
+ const line = offsetToLine(sourceCode, def.start);
692
+ const ctx = { filePath, line, trailId: def.id };
693
+ const signalValueCalledIds = new Set(
694
+ [...called.ids].filter((id) => !called.stringIds.has(id))
695
+ );
696
+
697
+ reportStringFireCalls(called.stringIds, ctx, diagnostics);
698
+ reportUndeclared(signalValueCalledIds, declared.ids, ctx, diagnostics);
699
+ reportUnused(declared.ids, called.ids, ctx, diagnostics);
700
+ };
701
+
702
+ // ---------------------------------------------------------------------------
703
+ // Rule
704
+ // ---------------------------------------------------------------------------
705
+
706
+ /**
707
+ * Validates that `ctx.fire()` calls align with declared `fires` arrays.
708
+ */
709
+ export const firesDeclarations: WardenRule = {
710
+ check(sourceCode: string, filePath: string): readonly WardenDiagnostic[] {
711
+ if (isTestFile(filePath)) {
712
+ return [];
713
+ }
714
+
715
+ const ast = parse(filePath, sourceCode);
716
+ if (!ast) {
717
+ return [];
718
+ }
719
+ const signalIds = buildSignalIdentifierResolver(ast);
720
+
721
+ const diagnostics: WardenDiagnostic[] = [];
722
+
723
+ for (const def of findTrailDefinitions(ast)) {
724
+ if (def.kind === 'trail') {
725
+ checkTrailDefinition(def, filePath, sourceCode, signalIds, diagnostics);
726
+ }
727
+ }
728
+
729
+ return diagnostics;
730
+ },
731
+ description:
732
+ 'Ensure ctx.fire() calls match the declared fires array in trail definitions.',
733
+ name: 'fires-declarations',
734
+ severity: 'error',
735
+ };