@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
@@ -1,45 +1,16 @@
1
- import type { WardenDiagnostic, WardenRule } from './types.js';
2
- import {
3
- isFrameworkInternalFile,
4
- isTestFile,
5
- stripQuotedContent,
6
- } from './scan.js';
7
-
8
- const RESULT_ACCESS_PATTERN =
9
- /\.(?:isOk|isErr|match|map)\s*\(|\.(?:value|error)\b/;
10
- const IMPLEMENTATION_CALL_PATTERN = /\.implementation\s*\(/;
11
-
12
- const isAwaitedImplementationCall = (line: string): boolean => {
13
- const callIndex = line.indexOf('.implementation(');
14
- if (callIndex === -1) {
15
- return false;
16
- }
17
-
18
- const awaitIndex = line.indexOf('await');
19
- return awaitIndex !== -1 && awaitIndex < callIndex;
20
- };
1
+ import { resultAccessorNames } from '@ontrails/core';
21
2
 
22
- const isDirectResultAccess = (line: string): boolean =>
23
- IMPLEMENTATION_CALL_PATTERN.test(line) &&
24
- RESULT_ACCESS_PATTERN.test(line) &&
25
- !isAwaitedImplementationCall(line);
26
-
27
- const isPendingUse = (line: string, variableName: string): boolean => {
28
- const escaped = variableName.replaceAll(/[.*+?^${}()|[\]\\]/g, '\\$&');
29
- const pendingPattern = new RegExp(
30
- `\\b${escaped}\\s*(?:\\.(?:isOk|isErr|match|map)\\s*\\(|\\.(?:value|error)\\b)`
31
- );
32
- return pendingPattern.test(line);
33
- };
3
+ import { identifierName, isBlazeCall, offsetToLine, parse } from './ast.js';
4
+ import type { AstNode } from './ast.js';
5
+ import { isFrameworkInternalFile, isTestFile } from './scan.js';
6
+ import type { WardenDiagnostic, WardenRule } from './types.js';
34
7
 
35
- interface PendingCall {
36
- line: number;
37
- remainingLines: number;
38
- variableName: string;
39
- }
8
+ const RESULT_ACCESSOR_PROPERTIES: ReadonlySet<string> = new Set(
9
+ resultAccessorNames
10
+ );
40
11
 
41
12
  const MISSING_AWAIT_MESSAGE =
42
- 'Missing await: .implementation() returns Promise<Result> after normalization. Use `const result = await trail.implementation(input, ctx)`.';
13
+ 'Missing await: .blaze() returns Promise<Result> after normalization. Use `const result = await trail.blaze(input, ctx)`.';
43
14
 
44
15
  const createMissingAwaitDiagnostic = (
45
16
  filePath: string,
@@ -52,105 +23,1169 @@ const createMissingAwaitDiagnostic = (
52
23
  severity: 'error',
53
24
  });
54
25
 
55
- const trackPendingCall = (line: string): string | undefined => {
56
- const match = line.match(
57
- /\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*([^;]*)/
58
- );
59
- if (!match?.[1] || !match[2] || !IMPLEMENTATION_CALL_PATTERN.test(match[2])) {
60
- return undefined;
26
+ const isAstLike = (value: unknown): value is AstNode =>
27
+ !!value && typeof value === 'object' && !!(value as AstNode).type;
28
+
29
+ /**
30
+ * Build parent map for a full AST.
31
+ *
32
+ * Populates a `WeakMap` directly during traversal so we never materialize a
33
+ * strong `Map` holding references to every AST node — the WeakMap lets parent
34
+ * entries be reclaimed alongside their nodes once the rule invocation ends.
35
+ */
36
+ const buildParentMap = (ast: AstNode): WeakMap<AstNode, AstNode> => {
37
+ const parents = new WeakMap<AstNode, AstNode>();
38
+
39
+ const recordAndVisit = (child: unknown, parent: AstNode): void => {
40
+ if (isAstLike(child)) {
41
+ parents.set(child, parent);
42
+ // eslint-disable-next-line no-use-before-define
43
+ visit(child);
44
+ }
45
+ };
46
+
47
+ const visit = (node: AstNode): void => {
48
+ for (const val of Object.values(node)) {
49
+ if (Array.isArray(val)) {
50
+ for (const item of val) {
51
+ recordAndVisit(item, node);
52
+ }
53
+ } else {
54
+ recordAndVisit(val, node);
55
+ }
56
+ }
57
+ };
58
+
59
+ visit(ast);
60
+ return parents;
61
+ };
62
+
63
+ /**
64
+ * Walk up the parent chain and return true when the expression is awaited
65
+ * before any result-accessing member access fires on it.
66
+ *
67
+ * `await x.blaze(...)` → awaited.
68
+ * `(await x.blaze(...)).isOk()` → awaited (await wraps before member access).
69
+ * `x.blaze(...).isOk()` → NOT awaited (member access on raw call).
70
+ */
71
+ const TRANSPARENT_WRAPPER_TYPES = new Set([
72
+ 'ParenthesizedExpression',
73
+ 'TSAsExpression',
74
+ 'TSSatisfiesExpression',
75
+ 'TSNonNullExpression',
76
+ 'TSTypeAssertion',
77
+ ]);
78
+
79
+ const skipParens = (
80
+ node: AstNode,
81
+ parents: WeakMap<AstNode, AstNode>
82
+ ): AstNode => {
83
+ let current = node;
84
+ let parent = parents.get(current);
85
+ while (parent?.type && TRANSPARENT_WRAPPER_TYPES.has(parent.type)) {
86
+ current = parent;
87
+ parent = parents.get(current);
88
+ }
89
+ return current;
90
+ };
91
+
92
+ /**
93
+ * Walk up through any wrapping parentheses and, when the current node sits
94
+ * in the `consequent` or `alternate` of a `ConditionalExpression`, through
95
+ * that conditional too. Returns the node whose parent should be inspected.
96
+ *
97
+ * Conservative: we only hop across a conditional when the node is one of
98
+ * its branches (not the `test` position). This lets us treat both
99
+ * `const r = cond ? x.blaze(...) : fallback` and
100
+ * `await (cond ? x.blaze(...) : fallback)` correctly without misattributing
101
+ * calls used as conditions.
102
+ */
103
+ const isBranchOfConditional = (outer: AstNode, parent: AstNode): boolean => {
104
+ if (parent.type !== 'ConditionalExpression') {
105
+ return false;
106
+ }
107
+ const cond = parent as unknown as {
108
+ consequent?: AstNode;
109
+ alternate?: AstNode;
110
+ };
111
+ return cond.consequent === outer || cond.alternate === outer;
112
+ };
113
+
114
+ /**
115
+ * Logical expressions (`&&`, `||`, `??`) carry the blaze result through either
116
+ * side. A `.blaze()` on either operand may be the value ultimately bound to a
117
+ * declarator (e.g. `const r = cond && trail.blaze(...)`), so we treat both
118
+ * operands as carriers.
119
+ */
120
+ const isOperandOfLogical = (outer: AstNode, parent: AstNode): boolean => {
121
+ if (parent.type !== 'LogicalExpression') {
122
+ return false;
123
+ }
124
+ const logical = parent as unknown as { left?: AstNode; right?: AstNode };
125
+ return logical.left === outer || logical.right === outer;
126
+ };
127
+
128
+ const skipParensAndBranchConditionals = (
129
+ node: AstNode,
130
+ parents: WeakMap<AstNode, AstNode>
131
+ ): AstNode => {
132
+ let outer = skipParens(node, parents);
133
+ while (true) {
134
+ const parent = parents.get(outer);
135
+ if (!parent) {
136
+ return outer;
137
+ }
138
+ if (
139
+ !(
140
+ isBranchOfConditional(outer, parent) ||
141
+ isOperandOfLogical(outer, parent)
142
+ )
143
+ ) {
144
+ return outer;
145
+ }
146
+ outer = skipParens(parent, parents);
147
+ }
148
+ };
149
+
150
+ const isAwaited = (
151
+ node: AstNode,
152
+ parents: WeakMap<AstNode, AstNode>
153
+ ): boolean => {
154
+ // Walk up through parens and any conditional whose branch is the blaze
155
+ // call. `await (c ? x.blaze(...) : fallback)` awaits the conditional as a
156
+ // whole, so the blaze call in a branch is effectively awaited.
157
+ const outer = skipParensAndBranchConditionals(node, parents);
158
+ return parents.get(outer)?.type === 'AwaitExpression';
159
+ };
160
+
161
+ const memberPropertyName = (node: AstNode): string | null => {
162
+ if (
163
+ node.type !== 'MemberExpression' &&
164
+ node.type !== 'StaticMemberExpression'
165
+ ) {
166
+ return null;
167
+ }
168
+ const prop = (node as unknown as { property?: AstNode }).property;
169
+ if (prop?.type !== 'Identifier') {
170
+ return null;
171
+ }
172
+ return (prop as unknown as { name?: string }).name ?? null;
173
+ };
174
+
175
+ /**
176
+ * Check if the blaze call is directly consumed by a result accessor
177
+ * (e.g. `foo.blaze(...).isOk()` or `foo.blaze(...).value`).
178
+ */
179
+ const hasDirectResultAccess = (
180
+ blazeCall: AstNode,
181
+ parents: WeakMap<AstNode, AstNode>
182
+ ): boolean => {
183
+ // Unwrap wrapping parentheses, conditional branches, and logical-operator
184
+ // operands so `(x.blaze(...)).isOk()`,
185
+ // `(cond ? x.blaze(...) : fb).isOk()`, and
186
+ // `(cond && x.blaze(...)).isOk()` are all detected the same way as the
187
+ // bare `x.blaze(...).isOk()` shape.
188
+ const outer = skipParensAndBranchConditionals(blazeCall, parents);
189
+ const parent = parents.get(outer);
190
+ if (!parent) {
191
+ return false;
192
+ }
193
+ const property = memberPropertyName(parent);
194
+ return property !== null && RESULT_ACCESSOR_PROPERTIES.has(property);
195
+ };
196
+
197
+ /**
198
+ * If the blaze call is the init of a VariableDeclarator (directly, through
199
+ * parens, or as a branch of a ConditionalExpression init), return the bound
200
+ * identifier name. Otherwise null.
201
+ */
202
+ const extractAssignedBinding = (
203
+ blazeCall: AstNode,
204
+ parents: WeakMap<AstNode, AstNode>
205
+ ): string | null => {
206
+ const outer = skipParensAndBranchConditionals(blazeCall, parents);
207
+ const parent = parents.get(outer);
208
+ if (!parent || parent.type !== 'VariableDeclarator') {
209
+ return null;
61
210
  }
211
+ const { id } = parent as unknown as { id?: AstNode };
212
+ return identifierName(id);
213
+ };
62
214
 
63
- if (isAwaitedImplementationCall(match[2])) {
64
- return undefined;
215
+ interface PendingBinding {
216
+ readonly name: string;
217
+ readonly declarationNode: AstNode;
218
+ /** Unique id of the scope frame that owns this binding. */
219
+ readonly scopeId: number;
220
+ }
221
+
222
+ const isResultAccessorMember = (node: AstNode): boolean => {
223
+ if (
224
+ node.type !== 'MemberExpression' &&
225
+ node.type !== 'StaticMemberExpression'
226
+ ) {
227
+ return false;
65
228
  }
229
+ const property = memberPropertyName(node);
230
+ return property !== null && RESULT_ACCESSOR_PROPERTIES.has(property);
231
+ };
232
+
233
+ const getIdentifierObjectName = (node: AstNode): string | null => {
234
+ const { object } = node as unknown as { object?: AstNode };
235
+ return object?.type === 'Identifier' ? identifierName(object) : null;
236
+ };
237
+
238
+ // ---------------------------------------------------------------------------
239
+ // Scope tracking
240
+ // ---------------------------------------------------------------------------
66
241
 
67
- return match[1];
242
+ const collectIdentifierBinding = (pattern: AstNode, out: Set<string>): void => {
243
+ const name = identifierName(pattern);
244
+ if (name) {
245
+ out.add(name);
246
+ }
68
247
  };
69
248
 
70
- const addPendingCall = (
71
- pendingCalls: PendingCall[],
72
- variableName: string,
73
- lineNumber: number
249
+ const collectAssignmentPatternBindings = (
250
+ pattern: AstNode,
251
+ out: Set<string>
74
252
  ): void => {
75
- pendingCalls.push({
76
- line: lineNumber,
77
- remainingLines: 6,
78
- variableName,
79
- });
253
+ const { left } = pattern as unknown as { left?: AstNode };
254
+ // eslint-disable-next-line no-use-before-define
255
+ collectPatternBindings(left, out);
80
256
  };
81
257
 
82
- const advancePendingCalls = (
83
- line: string,
84
- filePath: string,
85
- lineNumber: number,
86
- pendingCalls: PendingCall[],
87
- diagnostics: WardenDiagnostic[]
258
+ const collectRestElementBindings = (
259
+ pattern: AstNode,
260
+ out: Set<string>
88
261
  ): void => {
89
- for (let j = pendingCalls.length - 1; j >= 0; j -= 1) {
90
- const pendingCall = pendingCalls[j];
91
- if (pendingCall && isPendingUse(line, pendingCall.variableName)) {
92
- diagnostics.push(createMissingAwaitDiagnostic(filePath, lineNumber));
93
- pendingCalls.splice(j, 1);
94
- } else if (pendingCall) {
95
- pendingCall.remainingLines -= 1;
96
- if (pendingCall.remainingLines <= 0) {
97
- pendingCalls.splice(j, 1);
262
+ const { argument } = pattern as unknown as { argument?: AstNode };
263
+ // eslint-disable-next-line no-use-before-define
264
+ collectPatternBindings(argument, out);
265
+ };
266
+
267
+ type PatternHandler = (pattern: AstNode, out: Set<string>) => void;
268
+
269
+ const PATTERN_HANDLERS: Record<string, PatternHandler> = {
270
+ // eslint-disable-next-line no-use-before-define
271
+ ArrayPattern: (p, out) => collectArrayPatternBindings(p, out),
272
+ AssignmentPattern: collectAssignmentPatternBindings,
273
+ Identifier: collectIdentifierBinding,
274
+ // eslint-disable-next-line no-use-before-define
275
+ ObjectPattern: (p, out) => collectObjectPatternBindings(p, out),
276
+ RestElement: collectRestElementBindings,
277
+ };
278
+
279
+ /**
280
+ * Collect binding names introduced by a destructuring / parameter pattern.
281
+ * Handles Identifier, AssignmentPattern, ObjectPattern, ArrayPattern,
282
+ * and RestElement shapes.
283
+ *
284
+ * `function` declaration (instead of an arrow) so it can be hoisted for the
285
+ * mutually recursive calls from the array / object pattern helpers below.
286
+ */
287
+ // biome-ignore lint/style/useConst: hoisted for mutual recursion
288
+ // eslint-disable-next-line func-style
289
+ function collectPatternBindings(
290
+ pattern: AstNode | undefined,
291
+ out: Set<string>
292
+ ): void {
293
+ if (!pattern) {
294
+ return;
295
+ }
296
+ const handler = PATTERN_HANDLERS[pattern.type];
297
+ if (handler) {
298
+ handler(pattern, out);
299
+ }
300
+ }
301
+
302
+ const collectArrayPatternBindings = (
303
+ pattern: AstNode,
304
+ out: Set<string>
305
+ ): void => {
306
+ const { elements } = pattern as unknown as {
307
+ elements?: readonly (AstNode | null)[];
308
+ };
309
+ if (!elements) {
310
+ return;
311
+ }
312
+ for (const element of elements) {
313
+ if (element) {
314
+ // eslint-disable-next-line no-use-before-define
315
+ collectPatternBindings(element, out);
316
+ }
317
+ }
318
+ };
319
+
320
+ const collectObjectPatternBindings = (
321
+ pattern: AstNode,
322
+ out: Set<string>
323
+ ): void => {
324
+ const { properties } = pattern as unknown as {
325
+ properties?: readonly AstNode[];
326
+ };
327
+ if (!properties) {
328
+ return;
329
+ }
330
+ for (const prop of properties) {
331
+ if (prop.type === 'RestElement') {
332
+ // eslint-disable-next-line no-use-before-define
333
+ collectPatternBindings(prop, out);
334
+ } else {
335
+ // Property node: value holds the binding pattern.
336
+ const { value } = prop as unknown as { value?: AstNode };
337
+ // eslint-disable-next-line no-use-before-define
338
+ collectPatternBindings(value, out);
339
+ }
340
+ }
341
+ };
342
+
343
+ const SCOPE_NODE_TYPES = new Set([
344
+ 'FunctionDeclaration',
345
+ 'FunctionExpression',
346
+ 'ArrowFunctionExpression',
347
+ 'BlockStatement',
348
+ 'StaticBlock',
349
+ 'CatchClause',
350
+ 'ForStatement',
351
+ 'ForInStatement',
352
+ 'ForOfStatement',
353
+ ]);
354
+
355
+ const isScopeBoundary = (node: AstNode): boolean =>
356
+ SCOPE_NODE_TYPES.has(node.type);
357
+
358
+ /**
359
+ * Collect the local binding names introduced directly in this scope's own
360
+ * declarations (params + var/let/const/catch/for declarations), without
361
+ * descending into nested function or block scopes.
362
+ *
363
+ * For function-like scopes, the body (a BlockStatement) is its own child
364
+ * scope — we do not merge params into it. Params and body bindings are
365
+ * treated as sibling frames via the scope walk: when entering the function,
366
+ * we push a frame with params; when entering its body block, we push another
367
+ * frame with the block's declarations. Nearest-scope resolution treats them
368
+ * as a single effective scope chain.
369
+ */
370
+ const FUNCTION_SCOPE_TYPES = new Set([
371
+ 'FunctionDeclaration',
372
+ 'FunctionExpression',
373
+ 'ArrowFunctionExpression',
374
+ ]);
375
+
376
+ const collectVariableDeclarationBindings = (
377
+ declNode: AstNode | undefined,
378
+ out: Set<string>
379
+ ): void => {
380
+ if (!declNode || declNode.type !== 'VariableDeclaration') {
381
+ return;
382
+ }
383
+ const declarators = (
384
+ declNode as unknown as {
385
+ declarations?: readonly AstNode[];
386
+ }
387
+ ).declarations;
388
+ if (!declarators) {
389
+ return;
390
+ }
391
+ for (const d of declarators) {
392
+ const { id } = d as unknown as { id?: AstNode };
393
+ collectPatternBindings(id, out);
394
+ }
395
+ };
396
+
397
+ const getVariableDeclarationKind = (
398
+ declNode: AstNode | undefined
399
+ ): string | null => {
400
+ if (!declNode || declNode.type !== 'VariableDeclaration') {
401
+ return null;
402
+ }
403
+ return (declNode as unknown as { kind?: string }).kind ?? null;
404
+ };
405
+
406
+ /** True if declaration is `var` (function/program-scoped, hoistable). */
407
+ const isVarDeclaration = (declNode: AstNode | undefined): boolean =>
408
+ getVariableDeclarationKind(declNode) === 'var';
409
+
410
+ /** Collect only `let`/`const` declarator bindings (block-scoped). */
411
+ const collectBlockScopedDeclaratorBindings = (
412
+ declNode: AstNode | undefined,
413
+ out: Set<string>
414
+ ): void => {
415
+ const kind = getVariableDeclarationKind(declNode);
416
+ if (!kind || kind === 'var') {
417
+ return;
418
+ }
419
+ collectVariableDeclarationBindings(declNode, out);
420
+ };
421
+
422
+ interface FunctionScopeBindings {
423
+ readonly bindings: Set<string>;
424
+ readonly paramBindings: Set<string>;
425
+ }
426
+
427
+ const collectParamBindings = (scope: AstNode): Set<string> => {
428
+ const paramBindings = new Set<string>();
429
+ const { params } = scope as unknown as { params?: readonly AstNode[] };
430
+ if (params) {
431
+ for (const param of params) {
432
+ collectPatternBindings(param, paramBindings);
433
+ }
434
+ }
435
+ return paramBindings;
436
+ };
437
+
438
+ const addHoistedVarsFromBody = (scope: AstNode, out: Set<string>): void => {
439
+ const { body } = scope as unknown as { body?: AstNode };
440
+ if (!(body && isAstLike(body))) {
441
+ return;
442
+ }
443
+ const hoisted = new Set<string>();
444
+ // eslint-disable-next-line no-use-before-define
445
+ collectHoistedVarBindings(body, hoisted);
446
+ for (const name of hoisted) {
447
+ out.add(name);
448
+ }
449
+ };
450
+
451
+ const collectFunctionScopeBindingsEx = (
452
+ scope: AstNode
453
+ ): FunctionScopeBindings => {
454
+ const paramBindings = collectParamBindings(scope);
455
+ const bindings = new Set<string>(paramBindings);
456
+ addHoistedVarsFromBody(scope, bindings);
457
+ return { bindings, paramBindings };
458
+ };
459
+
460
+ const collectFunctionScopeBindings = (scope: AstNode): Set<string> =>
461
+ collectFunctionScopeBindingsEx(scope).bindings;
462
+
463
+ const collectCatchScopeBindings = (scope: AstNode): Set<string> => {
464
+ const bindings = new Set<string>();
465
+ const { param } = scope as unknown as { param?: AstNode };
466
+ collectPatternBindings(param, bindings);
467
+ return bindings;
468
+ };
469
+
470
+ const collectForScopeBindings = (scope: AstNode): Set<string> => {
471
+ const bindings = new Set<string>();
472
+ if (scope.type === 'ForStatement') {
473
+ const { init } = scope as unknown as { init?: AstNode };
474
+ collectBlockScopedDeclaratorBindings(init, bindings);
475
+ } else {
476
+ const { left } = scope as unknown as { left?: AstNode };
477
+ collectBlockScopedDeclaratorBindings(left, bindings);
478
+ }
479
+ return bindings;
480
+ };
481
+
482
+ const addFunctionDeclarationName = (stmt: AstNode, out: Set<string>): void => {
483
+ if (stmt.type !== 'FunctionDeclaration') {
484
+ return;
485
+ }
486
+ const { id } = stmt as unknown as { id?: AstNode };
487
+ const fnName = identifierName(id);
488
+ if (fnName) {
489
+ out.add(fnName);
490
+ }
491
+ };
492
+
493
+ const addClassDeclarationName = (stmt: AstNode, out: Set<string>): void => {
494
+ if (stmt.type !== 'ClassDeclaration') {
495
+ return;
496
+ }
497
+ const { id } = stmt as unknown as { id?: AstNode };
498
+ const className = identifierName(id);
499
+ if (className) {
500
+ out.add(className);
501
+ }
502
+ };
503
+
504
+ const collectBlockScopedStatementListBindings = (
505
+ statements: readonly AstNode[] | undefined,
506
+ out: Set<string>
507
+ ): void => {
508
+ if (!statements) {
509
+ return;
510
+ }
511
+ for (const stmt of statements) {
512
+ collectBlockScopedDeclaratorBindings(stmt, out);
513
+ addFunctionDeclarationName(stmt, out);
514
+ addClassDeclarationName(stmt, out);
515
+ }
516
+ };
517
+
518
+ const collectBlockStatementBindings = (scope: AstNode): Set<string> => {
519
+ const bindings = new Set<string>();
520
+ const { body } = scope as unknown as { body?: readonly AstNode[] };
521
+ collectBlockScopedStatementListBindings(body, bindings);
522
+ // Static initializer blocks own their own VariableEnvironment (per ES spec),
523
+ // so `var` declarations inside them do not escape into the enclosing class
524
+ // or function scope. `collectHoistedVarBindings` correctly refuses to compose
525
+ // a `StaticBlock` boundary from the outside, which means nothing else will
526
+ // register these bindings. Hoist them here so `var result = trail.blaze(...)`
527
+ // inside a `static { ... }` block is tracked against the block itself.
528
+ if (scope.type === 'StaticBlock') {
529
+ // `collectHoistedVarBindings` is called with the StaticBlock as the root,
530
+ // so the own-VariableEnvironment check (which refuses to descend *into* a
531
+ // nested StaticBlock) does not short-circuit traversal of the node itself.
532
+ // eslint-disable-next-line no-use-before-define
533
+ collectHoistedVarBindings(scope, bindings);
534
+ }
535
+ return bindings;
536
+ };
537
+
538
+ /**
539
+ * Collect the local binding names introduced directly in this scope's own
540
+ * declarations (params + var/let/const/catch/for declarations), without
541
+ * descending into nested function or block scopes.
542
+ */
543
+ const collectScopeBindings = (scope: AstNode): Set<string> => {
544
+ if (FUNCTION_SCOPE_TYPES.has(scope.type)) {
545
+ return collectFunctionScopeBindings(scope);
546
+ }
547
+ if (scope.type === 'CatchClause') {
548
+ return collectCatchScopeBindings(scope);
549
+ }
550
+ if (
551
+ scope.type === 'ForStatement' ||
552
+ scope.type === 'ForInStatement' ||
553
+ scope.type === 'ForOfStatement'
554
+ ) {
555
+ return collectForScopeBindings(scope);
556
+ }
557
+ if (scope.type === 'BlockStatement' || scope.type === 'StaticBlock') {
558
+ return collectBlockStatementBindings(scope);
559
+ }
560
+ return new Set();
561
+ };
562
+
563
+ type ScopeKind = 'program' | 'function' | 'block' | 'for' | 'catch';
564
+
565
+ interface ScopeFrame {
566
+ readonly id: number;
567
+ readonly kind: ScopeKind;
568
+ readonly bindings: Set<string>;
569
+ /**
570
+ * For function frames: names that came from parameters (not hoisted `var`s).
571
+ * A `var` declaration with the same name as a parameter is redundant in JS —
572
+ * the parameter is the real binding. We track params separately so we don't
573
+ * register a pending `.blaze()` binding that is actually shadowed by a param.
574
+ */
575
+ readonly paramBindings?: Set<string>;
576
+ }
577
+
578
+ const scopeKindForNode = (node: AstNode): ScopeKind => {
579
+ if (FUNCTION_SCOPE_TYPES.has(node.type)) {
580
+ return 'function';
581
+ }
582
+ if (node.type === 'CatchClause') {
583
+ return 'catch';
584
+ }
585
+ if (
586
+ node.type === 'ForStatement' ||
587
+ node.type === 'ForInStatement' ||
588
+ node.type === 'ForOfStatement'
589
+ ) {
590
+ return 'for';
591
+ }
592
+ return 'block';
593
+ };
594
+
595
+ /**
596
+ * True when a nested node owns its own VariableEnvironment and therefore stops
597
+ * `var` hoisting from composing into the enclosing function/program scope.
598
+ * Covers function-like nodes and `StaticBlock` (ECMAScript: static blocks
599
+ * introduce their own LexicalEnvironment and VariableEnvironment).
600
+ */
601
+ const ownsVariableEnvironment = (node: AstNode): boolean =>
602
+ FUNCTION_SCOPE_TYPES.has(node.type) || node.type === 'StaticBlock';
603
+
604
+ const collectHoistedVarBindings = (root: AstNode, out: Set<string>): void => {
605
+ const visit = (node: AstNode, isRoot: boolean): void => {
606
+ // Nested var-environment owners (functions, static blocks) do not leak
607
+ // their `var`s to the enclosing scope.
608
+ if (!isRoot && ownsVariableEnvironment(node)) {
609
+ return;
610
+ }
611
+ if (node.type === 'VariableDeclaration' && isVarDeclaration(node)) {
612
+ collectVariableDeclarationBindings(node, out);
613
+ }
614
+ for (const val of Object.values(node)) {
615
+ if (Array.isArray(val)) {
616
+ for (const item of val) {
617
+ if (isAstLike(item)) {
618
+ visit(item, false);
619
+ }
620
+ }
621
+ } else if (isAstLike(val)) {
622
+ visit(val, false);
98
623
  }
99
624
  }
625
+ };
626
+ visit(root, true);
627
+ };
628
+
629
+ interface AnalyzeState {
630
+ readonly parents: WeakMap<AstNode, AstNode>;
631
+ readonly diagnostics: WardenDiagnostic[];
632
+ readonly sourceCode: string;
633
+ readonly filePath: string;
634
+ /** Pending `.blaze()` bindings seen so far, keyed by scope id + name. */
635
+ readonly pendingByScopeAndName: Map<string, PendingBinding>;
636
+ readonly scopeStack: ScopeFrame[];
637
+ readonly reportedAt: Set<number>;
638
+ /**
639
+ * Monotonic counter for scope frame ids. Intentionally mutable — every other
640
+ * field on `AnalyzeState` is `readonly`, but this one is incremented with
641
+ * `state.nextScopeId += 1` each time a scope frame is pushed so sibling
642
+ * scopes get distinct ids. Keeping it as a plain number (rather than a
643
+ * boxed `{ current: number }`) avoids an extra allocation and indirection
644
+ * on a hot path; the mutability is local to `pushScopeIfBoundary`.
645
+ */
646
+ nextScopeId: number;
647
+ }
648
+
649
+ const pendingKey = (scopeId: number, name: string): string =>
650
+ `${scopeId}\u0000${name}`;
651
+
652
+ /**
653
+ * Resolve an identifier use to the nearest enclosing scope frame that binds
654
+ * the name. Returns `null` if no frame binds it.
655
+ */
656
+ const resolveNearestScope = (
657
+ name: string,
658
+ stack: readonly ScopeFrame[]
659
+ ): ScopeFrame | null => {
660
+ for (let i = stack.length - 1; i >= 0; i -= 1) {
661
+ const frame = stack[i];
662
+ if (frame && frame.bindings.has(name)) {
663
+ return frame;
664
+ }
100
665
  }
666
+ return null;
101
667
  };
102
668
 
103
- const processLine = (
104
- line: string,
105
- filePath: string,
106
- lineNumber: number,
107
- pendingCalls: PendingCall[],
108
- diagnostics: WardenDiagnostic[]
669
+ /**
670
+ * Resolve the blaze call to a `{ name, declarator }` pair when it is the init
671
+ * of a `VariableDeclarator` (directly, through parens, or as a branch of a
672
+ * `ConditionalExpression` init). Returns null otherwise.
673
+ */
674
+ const resolveBlazeBinding = (
675
+ blazeCall: AstNode,
676
+ parents: WeakMap<AstNode, AstNode>
677
+ ): { readonly name: string; readonly declarator: AstNode } | null => {
678
+ const name = extractAssignedBinding(blazeCall, parents);
679
+ if (!name) {
680
+ return null;
681
+ }
682
+ // Mirror `extractAssignedBinding`: unwrap parens and branch-position
683
+ // conditionals so the stored declaration node points at the
684
+ // `VariableDeclarator`, not at an intermediate `ParenthesizedExpression`
685
+ // or `ConditionalExpression`.
686
+ const outer = skipParensAndBranchConditionals(blazeCall, parents);
687
+ const declarator = parents.get(outer);
688
+ return declarator ? { declarator, name } : null;
689
+ };
690
+
691
+ /**
692
+ * Resolve the blaze call to a `{ name, assignment }` pair when it is the RHS
693
+ * of a plain `=` `AssignmentExpression` with an `Identifier` LHS (directly,
694
+ * through parens, or as a branch of a conditional/logical expression).
695
+ *
696
+ * Covers patterns like:
697
+ * let result;
698
+ * result = trail.blaze(input, ctx);
699
+ * result.isOk();
700
+ *
701
+ * Member-expression LHS (`obj.result = blaze(...)`) is intentionally skipped —
702
+ * those are property writes, not bare bindings we can track by name.
703
+ */
704
+ const extractPlainIdentifierAssignmentName = (
705
+ parent: AstNode | undefined
706
+ ): string | null => {
707
+ if (!parent || parent.type !== 'AssignmentExpression') {
708
+ return null;
709
+ }
710
+ const { operator, left } = parent as unknown as {
711
+ operator?: string;
712
+ left?: AstNode;
713
+ };
714
+ // Only plain `=` assignments to a bare identifier. Member-expression LHS
715
+ // (`obj.result = blaze(...)`) is a property write, not a bare binding we
716
+ // can track by name.
717
+ if (operator !== '=' || !left || left.type !== 'Identifier') {
718
+ return null;
719
+ }
720
+ return identifierName(left);
721
+ };
722
+
723
+ const resolveBlazeAssignment = (
724
+ blazeCall: AstNode,
725
+ parents: WeakMap<AstNode, AstNode>
726
+ ): { readonly name: string; readonly assignment: AstNode } | null => {
727
+ const outer = skipParensAndBranchConditionals(blazeCall, parents);
728
+ const parent = parents.get(outer);
729
+ const name = extractPlainIdentifierAssignmentName(parent);
730
+ return name && parent ? { assignment: parent, name } : null;
731
+ };
732
+
733
+ /**
734
+ * True when `declarator` is a `VariableDeclarator` whose parent
735
+ * `VariableDeclaration` uses the `var` kind. Such declarators re-initialize
736
+ * a same-named function parameter rather than shadowing it, because `var`
737
+ * and parameters share the function's VariableEnvironment.
738
+ */
739
+ const isVarDeclaratorOfParamName = (
740
+ declarator: AstNode,
741
+ parents: WeakMap<AstNode, AstNode>
742
+ ): boolean => {
743
+ if (declarator.type !== 'VariableDeclarator') {
744
+ return false;
745
+ }
746
+ const decl = parents.get(declarator);
747
+ return isVarDeclaration(decl);
748
+ };
749
+
750
+ /**
751
+ * True when `node` is a plain `=` `AssignmentExpression` with an `Identifier`
752
+ * LHS. Such an assignment writes to the existing binding for that name — if
753
+ * that name is a function parameter, the assignment re-initializes the
754
+ * parameter's slot in the VariableEnvironment, just like `var <name> = ...`.
755
+ * Compound assignments (`+=`, `??=`, etc.) are excluded because they do not
756
+ * unconditionally replace the slot with the blaze result.
757
+ */
758
+ const isAssignmentToParamName = (node: AstNode): boolean => {
759
+ if (node.type !== 'AssignmentExpression') {
760
+ return false;
761
+ }
762
+ const { operator, left } = node as unknown as {
763
+ operator?: string;
764
+ left?: AstNode;
765
+ };
766
+ return operator === '=' && left?.type === 'Identifier';
767
+ };
768
+
769
+ const recordPendingBinding = (
770
+ blazeCall: AstNode,
771
+ state: AnalyzeState
772
+ ): void => {
773
+ const binding =
774
+ resolveBlazeBinding(blazeCall, state.parents) ??
775
+ (() => {
776
+ const asn = resolveBlazeAssignment(blazeCall, state.parents);
777
+ return asn ? { declarator: asn.assignment, name: asn.name } : null;
778
+ })();
779
+ if (!binding) {
780
+ return;
781
+ }
782
+ const { name, declarator } = binding;
783
+ // The pending binding lives in the nearest scope that declares `name`.
784
+ // That is always the innermost scope in the current stack, because the
785
+ // variable declaration's id was contributed to its enclosing scope's
786
+ // bindings when that scope was entered.
787
+ const owningFrame = resolveNearestScope(name, state.scopeStack);
788
+ if (!owningFrame) {
789
+ return;
790
+ }
791
+ // If the name resolves to a function parameter, the `var` that visually
792
+ // appears to declare it is redundant — the parameter is the real binding,
793
+ // and parameters are not pending `.blaze()` results.
794
+ //
795
+ // Carve-out: a `var <name> = blaze(...)` *initializer* inside the same
796
+ // function body legitimately re-binds the parameter at that point. `var`
797
+ // and parameters share the function's VariableEnvironment, so the `var`
798
+ // writes to the existing parameter slot and the subsequent use resolves
799
+ // to the freshly-assigned `.blaze()` result. Treat that as a pending
800
+ // binding.
801
+ //
802
+ // The same logic applies to a bare `result = blaze(...)` assignment: it
803
+ // writes to the parameter's existing slot in the same VariableEnvironment,
804
+ // so the subsequent `result.isOk()` observes the blaze result. Only
805
+ // compound assignments (`+=`, `??=`, etc.) and member-expression LHS fall
806
+ // through the param-shadow suppression, because they do not
807
+ // unconditionally replace the parameter slot with the blaze result.
808
+ if (
809
+ owningFrame.paramBindings?.has(name) &&
810
+ !isVarDeclaratorOfParamName(declarator, state.parents) &&
811
+ !isAssignmentToParamName(declarator)
812
+ ) {
813
+ return;
814
+ }
815
+ state.pendingByScopeAndName.set(pendingKey(owningFrame.id, name), {
816
+ declarationNode: declarator,
817
+ name,
818
+ scopeId: owningFrame.id,
819
+ });
820
+ };
821
+
822
+ /**
823
+ * True when `expr`, descended through wrapping parens, conditional branches,
824
+ * and logical-operator operands, contains a `.blaze()` call that would be
825
+ * registered by `recordPendingBinding` for this assignment.
826
+ *
827
+ * This mirrors the *upward* carrier walk done by
828
+ * `skipParensAndBranchConditionals` — if a blaze call is anywhere along a
829
+ * carrier path descending from `expr`, then visiting that blaze call will
830
+ * re-register the pending binding, so we must not clear it on the way in.
831
+ */
832
+ type CarrierChildExtractor = (
833
+ expr: AstNode
834
+ ) => readonly (AstNode | undefined)[];
835
+
836
+ const CARRIER_CHILDREN: Record<string, CarrierChildExtractor> = {
837
+ ConditionalExpression: (expr) => {
838
+ const { consequent, alternate } = expr as unknown as {
839
+ consequent?: AstNode;
840
+ alternate?: AstNode;
841
+ };
842
+ return [consequent, alternate];
843
+ },
844
+ LogicalExpression: (expr) => {
845
+ const { left, right } = expr as unknown as {
846
+ left?: AstNode;
847
+ right?: AstNode;
848
+ };
849
+ return [left, right];
850
+ },
851
+ };
852
+
853
+ const unwrapTransparentWrapper = (expr: AstNode): AstNode | undefined =>
854
+ (expr as unknown as { expression?: AstNode }).expression;
855
+
856
+ // biome-ignore lint/style/useConst: hoisted for recursive call
857
+ // eslint-disable-next-line func-style
858
+ function rhsCarriesBlazeReinit(expr: AstNode | undefined): boolean {
859
+ if (!expr) {
860
+ return false;
861
+ }
862
+ if (TRANSPARENT_WRAPPER_TYPES.has(expr.type)) {
863
+ return rhsCarriesBlazeReinit(unwrapTransparentWrapper(expr));
864
+ }
865
+ const extractor = CARRIER_CHILDREN[expr.type];
866
+ if (extractor) {
867
+ return extractor(expr).some(rhsCarriesBlazeReinit);
868
+ }
869
+ return isBlazeCall(expr);
870
+ }
871
+
872
+ /**
873
+ * Nullish/falsy-skip compound assignments (`??=`, `||=`) only write to the slot
874
+ * when the LHS is nullish or falsy. A pending `.blaze()` binding holds a
875
+ * truthy `Promise<Result>`, so the RHS never runs and the pending binding must
876
+ * survive them.
877
+ *
878
+ * `&&=` is intentionally excluded: it writes when the LHS is truthy, so a
879
+ * pending `Promise<Result>` is *always* overwritten by the RHS. That matches
880
+ * the clearing behavior of mathematical compound operators (`+=`, `-=`, ...).
881
+ */
882
+ const NULLISH_SKIP_OPERATORS = new Set(['??=', '||=']);
883
+
884
+ interface IdentifierAssignment {
885
+ readonly operator: string;
886
+ readonly name: string;
887
+ readonly right: AstNode | undefined;
888
+ }
889
+
890
+ const extractIdentifierAssignment = (
891
+ node: AstNode
892
+ ): IdentifierAssignment | null => {
893
+ if (node.type !== 'AssignmentExpression') {
894
+ return null;
895
+ }
896
+ const { operator, left, right } = node as unknown as {
897
+ operator?: string;
898
+ left?: AstNode;
899
+ right?: AstNode;
900
+ };
901
+ if (!(operator && left) || left.type !== 'Identifier') {
902
+ return null;
903
+ }
904
+ const name = identifierName(left);
905
+ return name ? { name, operator, right } : null;
906
+ };
907
+
908
+ const resolvePendingKeyFor = (
909
+ name: string,
910
+ state: AnalyzeState
911
+ ): string | null => {
912
+ const frame = resolveNearestScope(name, state.scopeStack);
913
+ if (!frame) {
914
+ return null;
915
+ }
916
+ const key = pendingKey(frame.id, name);
917
+ return state.pendingByScopeAndName.has(key) ? key : null;
918
+ };
919
+
920
+ /**
921
+ * Handle a plain `=` assignment (or clearing compound assignment) to a bare
922
+ * identifier whose name currently has a pending `.blaze()` binding in scope.
923
+ *
924
+ * A plain `=` whose RHS carries another blaze call leaves the pending entry
925
+ * alone — `recordPendingBinding` will re-register it when the blaze call
926
+ * itself is visited. Otherwise, clear the pending entry: the identifier has
927
+ * been overwritten with a non-Result value, so the original
928
+ * `result.isOk()`-style diagnostic no longer applies.
929
+ *
930
+ * Nullish/falsy-skip compound assignments (`??=`, `||=`) are ignored — a
931
+ * truthy pending `Promise<Result>` causes the RHS to be skipped, so the
932
+ * pending binding is preserved. `&&=` is *not* in this set: a truthy LHS
933
+ * causes the RHS to always run, overwriting the pending slot, so it falls
934
+ * through to the clearing path alongside `+=`, `-=`, etc. Member-expression
935
+ * LHS is ignored because it writes a property, not the tracked identifier.
936
+ */
937
+ const handleAssignmentReassignment = (
938
+ node: AstNode,
939
+ state: AnalyzeState
109
940
  ): void => {
110
- if (isDirectResultAccess(line)) {
111
- diagnostics.push(createMissingAwaitDiagnostic(filePath, lineNumber));
941
+ const assignment = extractIdentifierAssignment(node);
942
+ if (!assignment || NULLISH_SKIP_OPERATORS.has(assignment.operator)) {
943
+ return;
944
+ }
945
+ const key = resolvePendingKeyFor(assignment.name, state);
946
+ if (!key) {
947
+ return;
948
+ }
949
+ // Plain `=` with a blaze-carrying RHS will re-register via
950
+ // `recordPendingBinding` when the blaze call itself is visited. Other
951
+ // compound operators (`+=`, `-=`, `*=`, etc.) produce a primitive value
952
+ // from the existing slot, so they always clear.
953
+ if (assignment.operator === '=' && rhsCarriesBlazeReinit(assignment.right)) {
954
+ return;
955
+ }
956
+ state.pendingByScopeAndName.delete(key);
957
+ };
958
+
959
+ const reportMissingAwait = (node: AstNode, state: AnalyzeState): void => {
960
+ if (state.reportedAt.has(node.start)) {
112
961
  return;
113
962
  }
963
+ state.reportedAt.add(node.start);
964
+ state.diagnostics.push(
965
+ createMissingAwaitDiagnostic(
966
+ state.filePath,
967
+ offsetToLine(state.sourceCode, node.start)
968
+ )
969
+ );
970
+ };
971
+
972
+ const findPendingBindingForUse = (
973
+ node: AstNode,
974
+ state: AnalyzeState
975
+ ): PendingBinding | null => {
976
+ if (!isResultAccessorMember(node)) {
977
+ return null;
978
+ }
979
+ const name = getIdentifierObjectName(node);
980
+ if (!name) {
981
+ return null;
982
+ }
983
+ const frame = resolveNearestScope(name, state.scopeStack);
984
+ if (!frame) {
985
+ return null;
986
+ }
987
+ return state.pendingByScopeAndName.get(pendingKey(frame.id, name)) ?? null;
988
+ };
989
+
990
+ const checkPendingAccess = (node: AstNode, state: AnalyzeState): void => {
991
+ const binding = findPendingBindingForUse(node, state);
992
+ if (!binding) {
993
+ return;
994
+ }
995
+ // Declaration must precede the use. Use source offsets for ordering.
996
+ if (node.start < binding.declarationNode.end) {
997
+ return;
998
+ }
999
+ reportMissingAwait(node, state);
1000
+ };
114
1001
 
115
- const variableName = trackPendingCall(line);
116
- if (variableName) {
117
- addPendingCall(pendingCalls, variableName, lineNumber);
1002
+ /**
1003
+ * If the blaze call is the init of a VariableDeclarator whose id is an
1004
+ * ObjectPattern that destructures any known Result accessor property,
1005
+ * return the declarator node. Otherwise null.
1006
+ *
1007
+ * Catches the core missing-await shape when written as destructuring:
1008
+ * `const { isOk } = entityShow.blaze(input, ctx)` — no await, immediate
1009
+ * access to a Result accessor, should fire.
1010
+ */
1011
+ const propertyDestructuresResultAccessor = (prop: AstNode): boolean => {
1012
+ if (prop.type === 'RestElement') {
1013
+ return false;
118
1014
  }
1015
+ const { key } = prop as unknown as { key?: AstNode };
1016
+ const keyName = identifierName(key);
1017
+ return keyName !== null && RESULT_ACCESSOR_PROPERTIES.has(keyName);
1018
+ };
119
1019
 
120
- advancePendingCalls(line, filePath, lineNumber, pendingCalls, diagnostics);
1020
+ const objectPatternHasResultAccessorKey = (pattern: AstNode): boolean => {
1021
+ const { properties } = pattern as unknown as {
1022
+ properties?: readonly AstNode[];
1023
+ };
1024
+ return properties?.some(propertyDestructuresResultAccessor) ?? false;
121
1025
  };
122
1026
 
123
- const scanSourceCode = (
1027
+ const getDestructuredResultAccessorDeclarator = (
1028
+ blazeCall: AstNode,
1029
+ parents: WeakMap<AstNode, AstNode>
1030
+ ): AstNode | null => {
1031
+ // Unwrap any wrapping parentheses and branch-position conditionals so
1032
+ // `const { isOk } = (trail.blaze(...));` and
1033
+ // `const { isOk } = cond ? trail.blaze(...) : fallback;` are treated as
1034
+ // `const { isOk } = trail.blaze(...);`.
1035
+ const outer = skipParensAndBranchConditionals(blazeCall, parents);
1036
+ const parent = parents.get(outer);
1037
+ if (!parent || parent.type !== 'VariableDeclarator') {
1038
+ return null;
1039
+ }
1040
+ const { id } = parent as unknown as { id?: AstNode };
1041
+ if (!id || id.type !== 'ObjectPattern') {
1042
+ return null;
1043
+ }
1044
+ return objectPatternHasResultAccessorKey(id) ? parent : null;
1045
+ };
1046
+
1047
+ const visitBlazeCall = (node: AstNode, state: AnalyzeState): void => {
1048
+ if (!isBlazeCall(node) || isAwaited(node, state.parents)) {
1049
+ return;
1050
+ }
1051
+ if (hasDirectResultAccess(node, state.parents)) {
1052
+ reportMissingAwait(node, state);
1053
+ return;
1054
+ }
1055
+ const destructuredDeclarator = getDestructuredResultAccessorDeclarator(
1056
+ node,
1057
+ state.parents
1058
+ );
1059
+ if (destructuredDeclarator) {
1060
+ reportMissingAwait(destructuredDeclarator, state);
1061
+ return;
1062
+ }
1063
+ recordPendingBinding(node, state);
1064
+ };
1065
+
1066
+ const visitNode = (node: AstNode, state: AnalyzeState): void => {
1067
+ visitBlazeCall(node, state);
1068
+ checkPendingAccess(node, state);
1069
+ };
1070
+
1071
+ /**
1072
+ * Post-order visitor for assignment re-assignment clearing.
1073
+ *
1074
+ * `handleAssignmentReassignment` must run *after* the RHS subtree has been
1075
+ * walked. Otherwise a self-referential `result = result.value` would clear
1076
+ * the pending entry before the RHS `result.value` access is observed — the
1077
+ * missing-await diagnostic would disappear even though the write produced
1078
+ * a non-Result value from the same pending slot.
1079
+ */
1080
+ const visitNodePost = (node: AstNode, state: AnalyzeState): void => {
1081
+ handleAssignmentReassignment(node, state);
1082
+ };
1083
+
1084
+ const pushScopeIfBoundary = (node: AstNode, state: AnalyzeState): boolean => {
1085
+ if (!isScopeBoundary(node)) {
1086
+ return false;
1087
+ }
1088
+ const kind = scopeKindForNode(node);
1089
+ if (kind === 'function') {
1090
+ const { bindings, paramBindings } = collectFunctionScopeBindingsEx(node);
1091
+ state.scopeStack.push({
1092
+ bindings,
1093
+ id: state.nextScopeId,
1094
+ kind,
1095
+ paramBindings,
1096
+ });
1097
+ } else {
1098
+ state.scopeStack.push({
1099
+ bindings: collectScopeBindings(node),
1100
+ id: state.nextScopeId,
1101
+ kind,
1102
+ });
1103
+ }
1104
+ state.nextScopeId += 1;
1105
+ return true;
1106
+ };
1107
+
1108
+ const walkChild = (child: unknown, state: AnalyzeState): void => {
1109
+ if (child && typeof child === 'object' && (child as AstNode).type) {
1110
+ // eslint-disable-next-line no-use-before-define
1111
+ walkWithScopes(child as AstNode, state);
1112
+ }
1113
+ };
1114
+
1115
+ const walkChildren = (node: AstNode, state: AnalyzeState): void => {
1116
+ for (const val of Object.values(node)) {
1117
+ if (Array.isArray(val)) {
1118
+ for (const item of val) {
1119
+ walkChild(item, state);
1120
+ }
1121
+ } else {
1122
+ walkChild(val, state);
1123
+ }
1124
+ }
1125
+ };
1126
+
1127
+ // biome-ignore lint/style/useConst: hoisted for mutual recursion with walkChildren
1128
+ // eslint-disable-next-line func-style
1129
+ function walkWithScopes(node: AstNode, state: AnalyzeState): void {
1130
+ const pushed = pushScopeIfBoundary(node, state);
1131
+ visitNode(node, state);
1132
+ walkChildren(node, state);
1133
+ visitNodePost(node, state);
1134
+ if (pushed) {
1135
+ state.scopeStack.pop();
1136
+ }
1137
+ }
1138
+
1139
+ const collectProgramBindings = (ast: AstNode): Set<string> => {
1140
+ const bindings = new Set<string>();
1141
+ const programBody = (ast as unknown as { body?: readonly AstNode[] }).body;
1142
+ // Top-level `let`/`const`/function declarations.
1143
+ collectBlockScopedStatementListBindings(programBody, bindings);
1144
+ // Top-level `var`s are program-scoped; also hoist any `var`s nested
1145
+ // inside blocks/loops at program level.
1146
+ collectHoistedVarBindings(ast, bindings);
1147
+ return bindings;
1148
+ };
1149
+
1150
+ const analyze = (
1151
+ ast: AstNode,
124
1152
  sourceCode: string,
125
1153
  filePath: string
126
1154
  ): readonly WardenDiagnostic[] => {
127
- const diagnostics: WardenDiagnostic[] = [];
128
- const lines = sourceCode.split('\n');
129
- const pendingCalls: PendingCall[] = [];
130
-
131
- for (let i = 0; i < lines.length; i += 1) {
132
- const line = lines[i];
133
- if (!line) {
134
- continue;
135
- }
136
- processLine(line, filePath, i + 1, pendingCalls, diagnostics);
137
- }
1155
+ const state: AnalyzeState = {
1156
+ diagnostics: [],
1157
+ filePath,
1158
+ nextScopeId: 1,
1159
+ parents: buildParentMap(ast),
1160
+ pendingByScopeAndName: new Map(),
1161
+ reportedAt: new Set(),
1162
+ scopeStack: [
1163
+ { bindings: collectProgramBindings(ast), id: 0, kind: 'program' },
1164
+ ],
1165
+ sourceCode,
1166
+ };
1167
+
1168
+ walkWithScopes(ast, state);
138
1169
 
139
- return diagnostics;
1170
+ return state.diagnostics;
140
1171
  };
141
1172
 
142
1173
  /**
143
- * Flags code that assumes `.implementation()` returns a synchronous result.
1174
+ * Flags code that assumes `.blaze()` returns a synchronous result.
144
1175
  */
145
1176
  export const noSyncResultAssumption: WardenRule = {
146
1177
  check(sourceCode: string, filePath: string): readonly WardenDiagnostic[] {
147
1178
  if (isTestFile(filePath) || isFrameworkInternalFile(filePath)) {
148
1179
  return [];
149
1180
  }
150
- return scanSourceCode(stripQuotedContent(sourceCode), filePath);
1181
+ const ast = parse(filePath, sourceCode);
1182
+ if (!ast) {
1183
+ return [];
1184
+ }
1185
+ return analyze(ast, sourceCode, filePath);
151
1186
  },
152
1187
  description:
153
- 'Disallow treating .implementation() as synchronous after normalization. Always await the returned Promise<Result>.',
1188
+ 'Disallow treating .blaze() as synchronous after normalization. Always await the returned Promise<Result>.',
154
1189
  name: 'no-sync-result-assumption',
155
1190
  severity: 'error',
156
1191
  };