@ontrails/warden 1.0.0-beta.13 → 1.0.0-beta.15

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 (474) hide show
  1. package/.turbo/turbo-lint.log +1 -1
  2. package/CHANGELOG.md +30 -0
  3. package/README.md +31 -20
  4. package/dist/cli.d.ts +19 -2
  5. package/dist/cli.d.ts.map +1 -1
  6. package/dist/cli.js +261 -64
  7. package/dist/cli.js.map +1 -1
  8. package/dist/draft.d.ts +5 -0
  9. package/dist/draft.d.ts.map +1 -0
  10. package/dist/draft.js +16 -0
  11. package/dist/draft.js.map +1 -0
  12. package/dist/drift.d.ts +10 -7
  13. package/dist/drift.d.ts.map +1 -1
  14. package/dist/drift.js +50 -16
  15. package/dist/drift.js.map +1 -1
  16. package/dist/formatters.d.ts +2 -1
  17. package/dist/formatters.d.ts.map +1 -1
  18. package/dist/formatters.js +15 -4
  19. package/dist/formatters.js.map +1 -1
  20. package/dist/index.d.ts +9 -17
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +10 -17
  23. package/dist/index.js.map +1 -1
  24. package/dist/rules/ast.d.ts +412 -7
  25. package/dist/rules/ast.d.ts.map +1 -1
  26. package/dist/rules/ast.js +1847 -102
  27. package/dist/rules/ast.js.map +1 -1
  28. package/dist/rules/circular-refs.d.ts +6 -0
  29. package/dist/rules/circular-refs.d.ts.map +1 -0
  30. package/dist/rules/circular-refs.js +83 -0
  31. package/dist/rules/circular-refs.js.map +1 -0
  32. package/dist/rules/context-no-surface-types.d.ts.map +1 -1
  33. package/dist/rules/context-no-surface-types.js +59 -3
  34. package/dist/rules/context-no-surface-types.js.map +1 -1
  35. package/dist/rules/contour-exists.d.ts +7 -0
  36. package/dist/rules/contour-exists.d.ts.map +1 -0
  37. package/dist/rules/contour-exists.js +113 -0
  38. package/dist/rules/contour-exists.js.map +1 -0
  39. package/dist/rules/contour-ids.d.ts +10 -0
  40. package/dist/rules/contour-ids.d.ts.map +1 -0
  41. package/dist/rules/contour-ids.js +12 -0
  42. package/dist/rules/contour-ids.js.map +1 -0
  43. package/dist/rules/cross-declarations.d.ts.map +1 -1
  44. package/dist/rules/cross-declarations.js +171 -57
  45. package/dist/rules/cross-declarations.js.map +1 -1
  46. package/dist/rules/dead-internal-trail.d.ts +3 -0
  47. package/dist/rules/dead-internal-trail.d.ts.map +1 -0
  48. package/dist/rules/dead-internal-trail.js +80 -0
  49. package/dist/rules/dead-internal-trail.js.map +1 -0
  50. package/dist/rules/draft-file-marking.d.ts +6 -0
  51. package/dist/rules/draft-file-marking.d.ts.map +1 -0
  52. package/dist/rules/draft-file-marking.js +87 -0
  53. package/dist/rules/draft-file-marking.js.map +1 -0
  54. package/dist/rules/draft-visible-debt.d.ts +12 -0
  55. package/dist/rules/draft-visible-debt.d.ts.map +1 -0
  56. package/dist/rules/draft-visible-debt.js +50 -0
  57. package/dist/rules/draft-visible-debt.js.map +1 -0
  58. package/dist/rules/error-mapping-completeness.d.ts +13 -0
  59. package/dist/rules/error-mapping-completeness.d.ts.map +1 -0
  60. package/dist/rules/error-mapping-completeness.js +160 -0
  61. package/dist/rules/error-mapping-completeness.js.map +1 -0
  62. package/dist/rules/example-valid.d.ts +6 -0
  63. package/dist/rules/example-valid.d.ts.map +1 -0
  64. package/dist/rules/example-valid.js +203 -0
  65. package/dist/rules/example-valid.js.map +1 -0
  66. package/dist/rules/fires-declarations.d.ts +16 -0
  67. package/dist/rules/fires-declarations.d.ts.map +1 -0
  68. package/dist/rules/fires-declarations.js +444 -0
  69. package/dist/rules/fires-declarations.js.map +1 -0
  70. package/dist/rules/implementation-returns-result.d.ts +9 -0
  71. package/dist/rules/implementation-returns-result.d.ts.map +1 -1
  72. package/dist/rules/implementation-returns-result.js +638 -76
  73. package/dist/rules/implementation-returns-result.js.map +1 -1
  74. package/dist/rules/incomplete-accessor-for-standard-op.d.ts +30 -0
  75. package/dist/rules/incomplete-accessor-for-standard-op.d.ts.map +1 -0
  76. package/dist/rules/incomplete-accessor-for-standard-op.js +226 -0
  77. package/dist/rules/incomplete-accessor-for-standard-op.js.map +1 -0
  78. package/dist/rules/incomplete-crud.d.ts +21 -0
  79. package/dist/rules/incomplete-crud.d.ts.map +1 -0
  80. package/dist/rules/incomplete-crud.js +368 -0
  81. package/dist/rules/incomplete-crud.js.map +1 -0
  82. package/dist/rules/index.d.ts +40 -7
  83. package/dist/rules/index.d.ts.map +1 -1
  84. package/dist/rules/index.js +91 -15
  85. package/dist/rules/index.js.map +1 -1
  86. package/dist/rules/intent-propagation.d.ts +3 -0
  87. package/dist/rules/intent-propagation.d.ts.map +1 -0
  88. package/dist/rules/intent-propagation.js +57 -0
  89. package/dist/rules/intent-propagation.js.map +1 -0
  90. package/dist/rules/missing-reconcile.d.ts +3 -0
  91. package/dist/rules/missing-reconcile.d.ts.map +1 -0
  92. package/dist/rules/missing-reconcile.js +44 -0
  93. package/dist/rules/missing-reconcile.js.map +1 -0
  94. package/dist/rules/missing-visibility.d.ts +3 -0
  95. package/dist/rules/missing-visibility.d.ts.map +1 -0
  96. package/dist/rules/missing-visibility.js +63 -0
  97. package/dist/rules/missing-visibility.js.map +1 -0
  98. package/dist/rules/no-direct-impl-in-route.d.ts.map +1 -1
  99. package/dist/rules/no-direct-impl-in-route.js +0 -3
  100. package/dist/rules/no-direct-impl-in-route.js.map +1 -1
  101. package/dist/rules/no-direct-implementation-call.js +1 -1
  102. package/dist/rules/no-direct-implementation-call.js.map +1 -1
  103. package/dist/rules/no-sync-result-assumption.d.ts.map +1 -1
  104. package/dist/rules/no-sync-result-assumption.js +870 -61
  105. package/dist/rules/no-sync-result-assumption.js.map +1 -1
  106. package/dist/rules/no-throw-in-detour-recover.d.ts +3 -0
  107. package/dist/rules/no-throw-in-detour-recover.d.ts.map +1 -0
  108. package/dist/rules/no-throw-in-detour-recover.js +147 -0
  109. package/dist/rules/no-throw-in-detour-recover.js.map +1 -0
  110. package/dist/rules/no-throw-in-detour-target.d.ts +4 -1
  111. package/dist/rules/no-throw-in-detour-target.d.ts.map +1 -1
  112. package/dist/rules/no-throw-in-detour-target.js +6 -3
  113. package/dist/rules/no-throw-in-detour-target.js.map +1 -1
  114. package/dist/rules/no-throw-in-implementation.d.ts +4 -2
  115. package/dist/rules/no-throw-in-implementation.d.ts.map +1 -1
  116. package/dist/rules/no-throw-in-implementation.js +6 -4
  117. package/dist/rules/no-throw-in-implementation.js.map +1 -1
  118. package/dist/rules/on-references-exist.d.ts +14 -0
  119. package/dist/rules/on-references-exist.d.ts.map +1 -0
  120. package/dist/rules/on-references-exist.js +109 -0
  121. package/dist/rules/on-references-exist.js.map +1 -0
  122. package/dist/rules/orphaned-signal.d.ts +3 -0
  123. package/dist/rules/orphaned-signal.d.ts.map +1 -0
  124. package/dist/rules/orphaned-signal.js +67 -0
  125. package/dist/rules/orphaned-signal.js.map +1 -0
  126. package/dist/rules/permit-governance.d.ts +3 -0
  127. package/dist/rules/permit-governance.d.ts.map +1 -0
  128. package/dist/rules/permit-governance.js +15 -0
  129. package/dist/rules/permit-governance.js.map +1 -0
  130. package/dist/rules/reference-exists.d.ts +6 -0
  131. package/dist/rules/reference-exists.d.ts.map +1 -0
  132. package/dist/rules/reference-exists.js +47 -0
  133. package/dist/rules/reference-exists.js.map +1 -0
  134. package/dist/rules/registry-names.d.ts +8 -0
  135. package/dist/rules/registry-names.d.ts.map +1 -0
  136. package/dist/rules/registry-names.js +83 -0
  137. package/dist/rules/registry-names.js.map +1 -0
  138. package/dist/rules/resource-declarations.d.ts +14 -0
  139. package/dist/rules/resource-declarations.d.ts.map +1 -0
  140. package/dist/rules/resource-declarations.js +413 -0
  141. package/dist/rules/resource-declarations.js.map +1 -0
  142. package/dist/rules/resource-exists.d.ts +6 -0
  143. package/dist/rules/resource-exists.d.ts.map +1 -0
  144. package/dist/rules/resource-exists.js +90 -0
  145. package/dist/rules/resource-exists.js.map +1 -0
  146. package/dist/rules/resource-id-grammar.d.ts +3 -0
  147. package/dist/rules/resource-id-grammar.d.ts.map +1 -0
  148. package/dist/rules/resource-id-grammar.js +39 -0
  149. package/dist/rules/resource-id-grammar.js.map +1 -0
  150. package/dist/rules/specs.d.ts.map +1 -1
  151. package/dist/rules/specs.js +5 -1
  152. package/dist/rules/specs.js.map +1 -1
  153. package/dist/rules/types.d.ts +53 -4
  154. package/dist/rules/types.d.ts.map +1 -1
  155. package/dist/rules/unreachable-detour-shadowing.d.ts +3 -0
  156. package/dist/rules/unreachable-detour-shadowing.d.ts.map +1 -0
  157. package/dist/rules/unreachable-detour-shadowing.js +202 -0
  158. package/dist/rules/unreachable-detour-shadowing.js.map +1 -0
  159. package/dist/rules/valid-describe-refs.d.ts.map +1 -1
  160. package/dist/rules/valid-describe-refs.js +132 -16
  161. package/dist/rules/valid-describe-refs.js.map +1 -1
  162. package/dist/rules/valid-detour-contract.d.ts +3 -0
  163. package/dist/rules/valid-detour-contract.d.ts.map +1 -0
  164. package/dist/rules/valid-detour-contract.js +47 -0
  165. package/dist/rules/valid-detour-contract.js.map +1 -0
  166. package/dist/rules/valid-detour-refs.d.ts.map +1 -1
  167. package/dist/rules/valid-detour-refs.js +73 -82
  168. package/dist/rules/valid-detour-refs.js.map +1 -1
  169. package/dist/rules/warden-export-symmetry.d.ts +7 -0
  170. package/dist/rules/warden-export-symmetry.d.ts.map +1 -0
  171. package/dist/rules/warden-export-symmetry.js +352 -0
  172. package/dist/rules/warden-export-symmetry.js.map +1 -0
  173. package/dist/rules/warden-rules-use-ast.d.ts +17 -0
  174. package/dist/rules/warden-rules-use-ast.d.ts.map +1 -0
  175. package/dist/rules/warden-rules-use-ast.js +778 -0
  176. package/dist/rules/warden-rules-use-ast.js.map +1 -0
  177. package/dist/trails/circular-refs.trail.d.ts +24 -0
  178. package/dist/trails/circular-refs.trail.d.ts.map +1 -0
  179. package/dist/trails/circular-refs.trail.js +29 -0
  180. package/dist/trails/circular-refs.trail.js.map +1 -0
  181. package/dist/trails/context-no-surface-types.trail.d.ts +2 -2
  182. package/dist/trails/context-no-surface-types.trail.d.ts.map +1 -1
  183. package/dist/trails/context-no-trailhead-types.trail.d.ts +2 -2
  184. package/dist/trails/context-no-trailhead-types.trail.d.ts.map +1 -1
  185. package/dist/trails/contour-exists.trail.d.ts +24 -0
  186. package/dist/trails/contour-exists.trail.d.ts.map +1 -0
  187. package/dist/trails/contour-exists.trail.js +21 -0
  188. package/dist/trails/contour-exists.trail.js.map +1 -0
  189. package/dist/trails/cross-declarations.trail.d.ts +2 -2
  190. package/dist/trails/cross-declarations.trail.d.ts.map +1 -1
  191. package/dist/trails/dead-internal-trail.trail.d.ts +24 -0
  192. package/dist/trails/dead-internal-trail.trail.d.ts.map +1 -0
  193. package/dist/trails/dead-internal-trail.trail.js +26 -0
  194. package/dist/trails/dead-internal-trail.trail.js.map +1 -0
  195. package/dist/trails/{provision-declarations.trail.d.ts → draft-file-marking.trail.d.ts} +3 -3
  196. package/dist/trails/draft-file-marking.trail.d.ts.map +1 -0
  197. package/dist/trails/draft-file-marking.trail.js +16 -0
  198. package/dist/trails/draft-file-marking.trail.js.map +1 -0
  199. package/dist/trails/draft-visible-debt.trail.d.ts +13 -0
  200. package/dist/trails/draft-visible-debt.trail.d.ts.map +1 -0
  201. package/dist/trails/draft-visible-debt.trail.js +16 -0
  202. package/dist/trails/draft-visible-debt.trail.js.map +1 -0
  203. package/dist/trails/error-mapping-completeness.trail.d.ts +13 -0
  204. package/dist/trails/error-mapping-completeness.trail.d.ts.map +1 -0
  205. package/dist/trails/error-mapping-completeness.trail.js +29 -0
  206. package/dist/trails/error-mapping-completeness.trail.js.map +1 -0
  207. package/dist/trails/{follow-declarations.trail.d.ts → example-valid.trail.d.ts} +3 -3
  208. package/dist/trails/example-valid.trail.d.ts.map +1 -0
  209. package/dist/trails/example-valid.trail.js +25 -0
  210. package/dist/trails/example-valid.trail.js.map +1 -0
  211. package/dist/trails/fires-declarations.trail.d.ts +13 -0
  212. package/dist/trails/fires-declarations.trail.d.ts.map +1 -0
  213. package/dist/trails/fires-declarations.trail.js +22 -0
  214. package/dist/trails/fires-declarations.trail.js.map +1 -0
  215. package/dist/trails/implementation-returns-result.trail.d.ts +2 -2
  216. package/dist/trails/implementation-returns-result.trail.d.ts.map +1 -1
  217. package/dist/trails/incomplete-accessor-for-standard-op.trail.d.ts +12 -0
  218. package/dist/trails/incomplete-accessor-for-standard-op.trail.d.ts.map +1 -0
  219. package/dist/trails/incomplete-accessor-for-standard-op.trail.js +60 -0
  220. package/dist/trails/incomplete-accessor-for-standard-op.trail.js.map +1 -0
  221. package/dist/trails/incomplete-crud.trail.d.ts +24 -0
  222. package/dist/trails/incomplete-crud.trail.d.ts.map +1 -0
  223. package/dist/trails/incomplete-crud.trail.js +39 -0
  224. package/dist/trails/incomplete-crud.trail.js.map +1 -0
  225. package/dist/trails/index.d.ts +29 -7
  226. package/dist/trails/index.d.ts.map +1 -1
  227. package/dist/trails/index.js +28 -6
  228. package/dist/trails/index.js.map +1 -1
  229. package/dist/trails/intent-propagation.trail.d.ts +24 -0
  230. package/dist/trails/intent-propagation.trail.d.ts.map +1 -0
  231. package/dist/trails/intent-propagation.trail.js +30 -0
  232. package/dist/trails/intent-propagation.trail.js.map +1 -0
  233. package/dist/trails/missing-reconcile.trail.d.ts +24 -0
  234. package/dist/trails/missing-reconcile.trail.d.ts.map +1 -0
  235. package/dist/trails/missing-reconcile.trail.js +33 -0
  236. package/dist/trails/missing-reconcile.trail.js.map +1 -0
  237. package/dist/trails/missing-visibility.trail.d.ts +24 -0
  238. package/dist/trails/missing-visibility.trail.d.ts.map +1 -0
  239. package/dist/trails/missing-visibility.trail.js +22 -0
  240. package/dist/trails/missing-visibility.trail.js.map +1 -0
  241. package/dist/trails/no-direct-impl-in-route.trail.d.ts +2 -2
  242. package/dist/trails/no-direct-impl-in-route.trail.d.ts.map +1 -1
  243. package/dist/trails/no-direct-implementation-call.trail.d.ts +2 -2
  244. package/dist/trails/no-direct-implementation-call.trail.d.ts.map +1 -1
  245. package/dist/trails/no-sync-result-assumption.trail.d.ts +2 -2
  246. package/dist/trails/no-sync-result-assumption.trail.d.ts.map +1 -1
  247. package/dist/trails/no-throw-in-detour-recover.trail.d.ts +13 -0
  248. package/dist/trails/no-throw-in-detour-recover.trail.d.ts.map +1 -0
  249. package/dist/trails/no-throw-in-detour-recover.trail.js +24 -0
  250. package/dist/trails/no-throw-in-detour-recover.trail.js.map +1 -0
  251. package/dist/trails/no-throw-in-detour-target.trail.d.ts +13 -3
  252. package/dist/trails/no-throw-in-detour-target.trail.d.ts.map +1 -1
  253. package/dist/trails/no-throw-in-implementation.trail.d.ts +2 -2
  254. package/dist/trails/no-throw-in-implementation.trail.d.ts.map +1 -1
  255. package/dist/trails/on-references-exist.trail.d.ts +24 -0
  256. package/dist/trails/on-references-exist.trail.d.ts.map +1 -0
  257. package/dist/trails/on-references-exist.trail.js +21 -0
  258. package/dist/trails/on-references-exist.trail.js.map +1 -0
  259. package/dist/trails/orphaned-signal.trail.d.ts +24 -0
  260. package/dist/trails/orphaned-signal.trail.d.ts.map +1 -0
  261. package/dist/trails/orphaned-signal.trail.js +36 -0
  262. package/dist/trails/orphaned-signal.trail.js.map +1 -0
  263. package/dist/trails/permit-governance.trail.d.ts +12 -0
  264. package/dist/trails/permit-governance.trail.d.ts.map +1 -0
  265. package/dist/trails/permit-governance.trail.js +47 -0
  266. package/dist/trails/permit-governance.trail.js.map +1 -0
  267. package/dist/trails/prefer-schema-inference.trail.d.ts +2 -2
  268. package/dist/trails/prefer-schema-inference.trail.d.ts.map +1 -1
  269. package/dist/trails/reference-exists.trail.d.ts +24 -0
  270. package/dist/trails/reference-exists.trail.d.ts.map +1 -0
  271. package/dist/trails/reference-exists.trail.js +25 -0
  272. package/dist/trails/reference-exists.trail.js.map +1 -0
  273. package/dist/trails/resource-declarations.trail.d.ts +13 -0
  274. package/dist/trails/resource-declarations.trail.d.ts.map +1 -0
  275. package/dist/trails/{provision-declarations.trail.js → resource-declarations.trail.js} +7 -7
  276. package/dist/trails/resource-declarations.trail.js.map +1 -0
  277. package/dist/trails/resource-exists.trail.d.ts +24 -0
  278. package/dist/trails/resource-exists.trail.d.ts.map +1 -0
  279. package/dist/trails/{provision-exists.trail.js → resource-exists.trail.js} +8 -8
  280. package/dist/trails/resource-exists.trail.js.map +1 -0
  281. package/dist/trails/resource-id-grammar.trail.d.ts +13 -0
  282. package/dist/trails/resource-id-grammar.trail.d.ts.map +1 -0
  283. package/dist/trails/resource-id-grammar.trail.js +38 -0
  284. package/dist/trails/resource-id-grammar.trail.js.map +1 -0
  285. package/dist/trails/run.d.ts +25 -9
  286. package/dist/trails/run.d.ts.map +1 -1
  287. package/dist/trails/run.js +63 -19
  288. package/dist/trails/run.js.map +1 -1
  289. package/dist/trails/schema.d.ts +28 -3
  290. package/dist/trails/schema.d.ts.map +1 -1
  291. package/dist/trails/schema.js +57 -4
  292. package/dist/trails/schema.js.map +1 -1
  293. package/dist/trails/unreachable-detour-shadowing.trail.d.ts +13 -0
  294. package/dist/trails/unreachable-detour-shadowing.trail.d.ts.map +1 -0
  295. package/dist/trails/unreachable-detour-shadowing.trail.js +44 -0
  296. package/dist/trails/unreachable-detour-shadowing.trail.js.map +1 -0
  297. package/dist/trails/valid-describe-refs.trail.d.ts +12 -3
  298. package/dist/trails/valid-describe-refs.trail.d.ts.map +1 -1
  299. package/dist/trails/valid-detour-contract.trail.d.ts +12 -0
  300. package/dist/trails/valid-detour-contract.trail.d.ts.map +1 -0
  301. package/dist/trails/valid-detour-contract.trail.js +66 -0
  302. package/dist/trails/valid-detour-contract.trail.js.map +1 -0
  303. package/dist/trails/valid-detour-refs.trail.d.ts +13 -3
  304. package/dist/trails/valid-detour-refs.trail.d.ts.map +1 -1
  305. package/dist/trails/warden-export-symmetry.trail.d.ts +13 -0
  306. package/dist/trails/warden-export-symmetry.trail.d.ts.map +1 -0
  307. package/dist/trails/warden-export-symmetry.trail.js +16 -0
  308. package/dist/trails/warden-export-symmetry.trail.js.map +1 -0
  309. package/dist/trails/warden-rules-use-ast.trail.d.ts +13 -0
  310. package/dist/trails/warden-rules-use-ast.trail.d.ts.map +1 -0
  311. package/dist/trails/warden-rules-use-ast.trail.js +41 -0
  312. package/dist/trails/warden-rules-use-ast.trail.js.map +1 -0
  313. package/dist/trails/wrap-rule.d.ts +16 -2
  314. package/dist/trails/wrap-rule.d.ts.map +1 -1
  315. package/dist/trails/wrap-rule.js +71 -11
  316. package/dist/trails/wrap-rule.js.map +1 -1
  317. package/package.json +7 -4
  318. package/src/__tests__/ast.test.ts +613 -0
  319. package/src/__tests__/circular-refs.test.ts +121 -0
  320. package/src/__tests__/cli.test.ts +360 -32
  321. package/src/__tests__/contour-exists.test.ts +203 -0
  322. package/src/__tests__/cross-declarations.test.ts +245 -0
  323. package/src/__tests__/dead-internal-trail.test.ts +81 -0
  324. package/src/__tests__/draft-rules-context.test.ts +150 -0
  325. package/src/__tests__/drift.test.ts +75 -5
  326. package/src/__tests__/error-mapping-completeness.test.ts +56 -0
  327. package/src/__tests__/example-valid.test.ts +101 -0
  328. package/src/__tests__/fires-declarations-param-destructure.test.ts +54 -0
  329. package/src/__tests__/fires-declarations.test.ts +652 -0
  330. package/src/__tests__/formatters.test.ts +2 -2
  331. package/src/__tests__/implementation-returns-result.test.ts +1016 -2
  332. package/src/__tests__/incomplete-accessor-for-standard-op.test.ts +337 -0
  333. package/src/__tests__/incomplete-crud.test.ts +498 -0
  334. package/src/__tests__/intent-propagation.test.ts +116 -0
  335. package/src/__tests__/missing-reconcile.test.ts +154 -0
  336. package/src/__tests__/missing-visibility.test.ts +108 -0
  337. package/src/__tests__/no-sync-result-assumption.test.ts +870 -39
  338. package/src/__tests__/no-throw-in-detour-recover.test.ts +93 -0
  339. package/src/__tests__/no-throw-in-implementation.test.ts +88 -0
  340. package/src/__tests__/on-references-exist.test.ts +151 -0
  341. package/src/__tests__/orphaned-signal.test.ts +137 -0
  342. package/src/__tests__/permit-governance.test.ts +66 -0
  343. package/src/__tests__/reference-exists.test.ts +281 -0
  344. package/src/__tests__/resource-declarations.test.ts +448 -0
  345. package/src/__tests__/resource-exists.test.ts +122 -0
  346. package/src/__tests__/resource-id-grammar.test.ts +50 -0
  347. package/src/__tests__/rules.test.ts +17 -77
  348. package/src/__tests__/topo-aware-rule.test.ts +257 -0
  349. package/src/__tests__/trails.test.ts +2 -2
  350. package/src/__tests__/unreachable-detour-shadowing.test.ts +128 -0
  351. package/src/__tests__/valid-describe-refs.test.ts +183 -0
  352. package/src/__tests__/valid-detour-contract.test.ts +86 -0
  353. package/src/__tests__/warden-export-symmetry.test.ts +251 -0
  354. package/src/__tests__/warden-rules-use-ast.test.ts +468 -0
  355. package/src/__tests__/wrap-rule.test.ts +3 -3
  356. package/src/cli.ts +458 -91
  357. package/src/draft.ts +22 -0
  358. package/src/drift.ts +63 -21
  359. package/src/formatters.ts +15 -4
  360. package/src/index.ts +62 -23
  361. package/src/rules/ast.ts +2715 -119
  362. package/src/rules/circular-refs.ts +154 -0
  363. package/src/rules/{context-no-trailhead-types.ts → context-no-surface-types.ts} +72 -12
  364. package/src/rules/contour-exists.ts +251 -0
  365. package/src/rules/contour-ids.ts +15 -0
  366. package/src/rules/cross-declarations.ts +277 -69
  367. package/src/rules/dead-internal-trail.ts +141 -0
  368. package/src/rules/draft-file-marking.ts +160 -0
  369. package/src/rules/draft-visible-debt.ts +87 -0
  370. package/src/rules/error-mapping-completeness.ts +273 -0
  371. package/src/rules/example-valid.ts +401 -0
  372. package/src/rules/fires-declarations.ts +609 -0
  373. package/src/rules/implementation-returns-result.ts +1042 -122
  374. package/src/rules/incomplete-accessor-for-standard-op.ts +315 -0
  375. package/src/rules/incomplete-crud.ts +579 -0
  376. package/src/rules/index.ts +95 -16
  377. package/src/rules/intent-propagation.ts +142 -0
  378. package/src/rules/missing-reconcile.ts +98 -0
  379. package/src/rules/missing-visibility.ts +110 -0
  380. package/src/rules/no-direct-impl-in-route.ts +0 -4
  381. package/src/rules/no-direct-implementation-call.ts +1 -1
  382. package/src/rules/no-sync-result-assumption.ts +1134 -96
  383. package/src/rules/no-throw-in-detour-recover.ts +225 -0
  384. package/src/rules/no-throw-in-implementation.ts +6 -4
  385. package/src/rules/on-references-exist.ts +194 -0
  386. package/src/rules/orphaned-signal.ts +150 -0
  387. package/src/rules/permit-governance.ts +25 -0
  388. package/src/rules/reference-exists.ts +98 -0
  389. package/src/rules/registry-names.ts +83 -0
  390. package/src/rules/{provision-declarations.ts → resource-declarations.ts} +208 -138
  391. package/src/rules/{provision-exists.ts → resource-exists.ts} +48 -51
  392. package/src/rules/resource-id-grammar.ts +65 -0
  393. package/src/rules/specs.ts +5 -1
  394. package/src/rules/types.ts +57 -4
  395. package/src/rules/unreachable-detour-shadowing.ts +375 -0
  396. package/src/rules/valid-describe-refs.ts +160 -32
  397. package/src/rules/valid-detour-contract.ts +78 -0
  398. package/src/rules/warden-export-symmetry.ts +533 -0
  399. package/src/rules/warden-rules-use-ast.ts +996 -0
  400. package/src/trails/circular-refs.trail.ts +29 -0
  401. package/src/trails/{context-no-trailhead-types.trail.ts → context-no-surface-types.trail.ts} +4 -4
  402. package/src/trails/contour-exists.trail.ts +21 -0
  403. package/src/trails/dead-internal-trail.trail.ts +26 -0
  404. package/src/trails/draft-file-marking.trail.ts +16 -0
  405. package/src/trails/draft-visible-debt.trail.ts +16 -0
  406. package/src/trails/error-mapping-completeness.trail.ts +29 -0
  407. package/src/trails/example-valid.trail.ts +25 -0
  408. package/src/trails/fires-declarations.trail.ts +22 -0
  409. package/src/trails/incomplete-accessor-for-standard-op.trail.ts +76 -0
  410. package/src/trails/incomplete-crud.trail.ts +39 -0
  411. package/src/trails/index.ts +40 -7
  412. package/src/trails/intent-propagation.trail.ts +30 -0
  413. package/src/trails/missing-reconcile.trail.ts +33 -0
  414. package/src/trails/missing-visibility.trail.ts +22 -0
  415. package/src/trails/no-throw-in-detour-recover.trail.ts +24 -0
  416. package/src/trails/on-references-exist.trail.ts +21 -0
  417. package/src/trails/orphaned-signal.trail.ts +36 -0
  418. package/src/trails/permit-governance.trail.ts +51 -0
  419. package/src/trails/reference-exists.trail.ts +25 -0
  420. package/src/trails/{provision-declarations.trail.ts → resource-declarations.trail.ts} +6 -6
  421. package/src/trails/{provision-exists.trail.ts → resource-exists.trail.ts} +7 -7
  422. package/src/trails/resource-id-grammar.trail.ts +39 -0
  423. package/src/trails/run.ts +121 -24
  424. package/src/trails/schema.ts +66 -4
  425. package/src/trails/unreachable-detour-shadowing.trail.ts +45 -0
  426. package/src/trails/valid-detour-contract.trail.ts +71 -0
  427. package/src/trails/warden-export-symmetry.trail.ts +16 -0
  428. package/src/trails/warden-rules-use-ast.trail.ts +45 -0
  429. package/src/trails/wrap-rule.ts +104 -12
  430. package/tsconfig.tests.json +10 -0
  431. package/tsconfig.tsbuildinfo +1 -1
  432. package/dist/rules/follow-declarations.d.ts +0 -13
  433. package/dist/rules/follow-declarations.d.ts.map +0 -1
  434. package/dist/rules/follow-declarations.js +0 -264
  435. package/dist/rules/follow-declarations.js.map +0 -1
  436. package/dist/rules/provision-declarations.d.ts +0 -14
  437. package/dist/rules/provision-declarations.d.ts.map +0 -1
  438. package/dist/rules/provision-declarations.js +0 -344
  439. package/dist/rules/provision-declarations.js.map +0 -1
  440. package/dist/rules/provision-exists.d.ts +0 -6
  441. package/dist/rules/provision-exists.d.ts.map +0 -1
  442. package/dist/rules/provision-exists.js +0 -89
  443. package/dist/rules/provision-exists.js.map +0 -1
  444. package/dist/rules/service-declarations.d.ts +0 -16
  445. package/dist/rules/service-declarations.d.ts.map +0 -1
  446. package/dist/rules/service-declarations.js +0 -346
  447. package/dist/rules/service-declarations.js.map +0 -1
  448. package/dist/rules/service-exists.d.ts +0 -8
  449. package/dist/rules/service-exists.d.ts.map +0 -1
  450. package/dist/rules/service-exists.js +0 -91
  451. package/dist/rules/service-exists.js.map +0 -1
  452. package/dist/trails/follow-declarations.trail.d.ts.map +0 -1
  453. package/dist/trails/follow-declarations.trail.js +0 -22
  454. package/dist/trails/follow-declarations.trail.js.map +0 -1
  455. package/dist/trails/provision-declarations.trail.d.ts.map +0 -1
  456. package/dist/trails/provision-declarations.trail.js.map +0 -1
  457. package/dist/trails/provision-exists.trail.d.ts +0 -15
  458. package/dist/trails/provision-exists.trail.d.ts.map +0 -1
  459. package/dist/trails/provision-exists.trail.js.map +0 -1
  460. package/dist/trails/service-declarations.trail.d.ts +0 -26
  461. package/dist/trails/service-declarations.trail.d.ts.map +0 -1
  462. package/dist/trails/service-declarations.trail.js +0 -27
  463. package/dist/trails/service-declarations.trail.js.map +0 -1
  464. package/dist/trails/service-exists.trail.d.ts +0 -32
  465. package/dist/trails/service-exists.trail.d.ts.map +0 -1
  466. package/dist/trails/service-exists.trail.js +0 -29
  467. package/dist/trails/service-exists.trail.js.map +0 -1
  468. package/src/__tests__/no-throw-in-detour-target.test.ts +0 -78
  469. package/src/__tests__/provision-declarations.test.ts +0 -318
  470. package/src/__tests__/provision-exists.test.ts +0 -122
  471. package/src/rules/no-throw-in-detour-target.ts +0 -150
  472. package/src/rules/valid-detour-refs.ts +0 -187
  473. package/src/trails/no-throw-in-detour-target.trail.ts +0 -20
  474. package/src/trails/valid-detour-refs.trail.ts +0 -24
package/src/rules/ast.ts CHANGED
@@ -5,6 +5,10 @@
5
5
  * walker and helpers for finding trail implementation bodies.
6
6
  */
7
7
 
8
+ import { resolve } from 'node:path';
9
+ import { fileURLToPath } from 'node:url';
10
+
11
+ import { DRAFT_ID_PREFIX } from '@ontrails/core';
8
12
  import { parseSync } from 'oxc-parser';
9
13
 
10
14
  // ---------------------------------------------------------------------------
@@ -16,11 +20,19 @@ export interface AstNode {
16
20
  readonly start: number;
17
21
  readonly end: number;
18
22
  readonly key?: { readonly name?: string };
19
- readonly value?: AstNode;
23
+ readonly value?: unknown;
20
24
  readonly body?: AstNode | readonly AstNode[];
21
25
  readonly [key: string]: unknown;
22
26
  }
23
27
 
28
+ interface StringLiteralNode extends AstNode {
29
+ readonly type: 'Literal' | 'StringLiteral';
30
+ readonly value?: unknown;
31
+ }
32
+
33
+ const isAstNode = (value: unknown): value is AstNode =>
34
+ Boolean(value && typeof value === 'object' && (value as AstNode).type);
35
+
24
36
  // ---------------------------------------------------------------------------
25
37
  // Parser
26
38
  // ---------------------------------------------------------------------------
@@ -92,7 +104,7 @@ const walkScopeInner: WalkFn = (node, visit) => {
92
104
  /**
93
105
  * Walk an AST node tree without descending into nested function scopes.
94
106
  * The root node is always traversed; only inner function boundaries are skipped.
95
- * Useful for provision-access analysis where inner functions may shadow
107
+ * Useful for resource-access analysis where inner functions may shadow
96
108
  * the trail context parameter name.
97
109
  */
98
110
  export const walkScope: WalkFn = (node, visit) => {
@@ -130,7 +142,9 @@ export const identifierName = (node: AstNode | undefined): string | null => {
130
142
  };
131
143
 
132
144
  /** Check if a node is a string literal. */
133
- export const isStringLiteral = (node: AstNode | undefined): node is AstNode => {
145
+ export const isStringLiteral = (
146
+ node: AstNode | undefined
147
+ ): node is StringLiteralNode => {
134
148
  if (!node) {
135
149
  return false;
136
150
  }
@@ -149,12 +163,246 @@ export const getStringValue = (node: AstNode): string | null => {
149
163
  return typeof val === 'string' ? val : null;
150
164
  };
151
165
 
166
+ /**
167
+ * Best-effort resolution of `const NAME = 'value'` declarations via regex.
168
+ *
169
+ * Returns the string value if a simple `const <name> = '...'` or `"..."` is
170
+ * found in the source. Returns null for anything more complex. Shared between
171
+ * warden rules that need to resolve identifier references to signal / trail
172
+ * IDs at lint time.
173
+ */
174
+ export const deriveConstString = (
175
+ name: string,
176
+ sourceCode: string
177
+ ): string | null => {
178
+ const pattern = new RegExp(
179
+ `const\\s+${name}\\s*=\\s*(?:'([^']*)'|"([^"]*)")`
180
+ );
181
+ const match = pattern.exec(sourceCode);
182
+ if (!match) {
183
+ return null;
184
+ }
185
+ return match[1] ?? match[2] ?? null;
186
+ };
187
+
152
188
  /** Extract a string literal value, or null when the node is not a string. */
153
189
  export const extractStringLiteral = (
154
190
  node: AstNode | undefined
155
191
  ): string | null =>
156
192
  node && isStringLiteral(node) ? getStringValue(node) : null;
157
193
 
194
+ /**
195
+ * Extract the cooked value from a `TemplateLiteral` with no interpolations
196
+ * (e.g. `` `entity.fallback` ``). Template literals with `${...}` expressions
197
+ * cannot be resolved at lint time and return null.
198
+ *
199
+ * Shared helper used by rules that accept both string literals and simple
200
+ * backtick-literal IDs (e.g. `valid-describe-refs`).
201
+ */
202
+ const getSingleQuasi = (node: AstNode): AstNode | null => {
203
+ const expressions =
204
+ (node['expressions'] as readonly AstNode[] | undefined) ?? [];
205
+ if (expressions.length > 0) {
206
+ return null;
207
+ }
208
+ const quasis = (node['quasis'] as readonly AstNode[] | undefined) ?? [];
209
+ return quasis.length === 1 ? (quasis[0] ?? null) : null;
210
+ };
211
+
212
+ export const extractPlainTemplateLiteral = (
213
+ node: AstNode | undefined
214
+ ): string | null => {
215
+ if (!node || node.type !== 'TemplateLiteral') {
216
+ return null;
217
+ }
218
+ const quasi = getSingleQuasi(node);
219
+ if (!quasi) {
220
+ return null;
221
+ }
222
+ const cooked = (quasi as unknown as { value?: { cooked?: unknown } }).value
223
+ ?.cooked;
224
+ return typeof cooked === 'string' ? cooked : null;
225
+ };
226
+
227
+ /**
228
+ * Extract a string value from either a string literal or a plain template
229
+ * literal (no `${...}` expressions). Returns null for anything else.
230
+ */
231
+ export const extractStringOrTemplateLiteral = (
232
+ node: AstNode | undefined
233
+ ): string | null =>
234
+ extractStringLiteral(node) ?? extractPlainTemplateLiteral(node);
235
+
236
+ export interface StringLiteralMatch {
237
+ readonly end: number;
238
+ readonly node: AstNode;
239
+ readonly start: number;
240
+ readonly value: string;
241
+ }
242
+
243
+ /**
244
+ * Names of framework constants whose value is a draft-marker prefix literal.
245
+ *
246
+ * String literals that initialize a `const` declaration with one of these
247
+ * names are treated as the framework's own draft-marker declarations, not as
248
+ * draft-id usage. This list is intentionally small and explicit — adding a
249
+ * new framework draft-prefix constant requires updating this set.
250
+ */
251
+ export const FRAMEWORK_DRAFT_PREFIX_CONSTANT_NAMES: ReadonlySet<string> =
252
+ new Set(['DRAFT_ID_PREFIX', 'DRAFT_FILE_PREFIX']);
253
+
254
+ /**
255
+ * Exact string literal value allowed for framework draft-prefix constant
256
+ * declarations. Tightens the exemption so a future framework file cannot
257
+ * redeclare `DRAFT_ID_PREFIX = '_draft.something-else'` and accidentally
258
+ * suppress its own draft-id diagnostic.
259
+ */
260
+ const FRAMEWORK_DRAFT_PREFIX_LITERAL = DRAFT_ID_PREFIX;
261
+
262
+ /**
263
+ * Absolute paths of the two framework files allowed to declare the
264
+ * draft-prefix constants. Anchored against the rule module's own URL so the
265
+ * exemption is scoped to this package's real on-disk location — a consumer
266
+ * repository that happens to declare `const DRAFT_ID_PREFIX = '_draft.leak'`
267
+ * anywhere else cannot hide a genuine leak by matching the identifier name.
268
+ *
269
+ * The two framework files are:
270
+ * - `packages/core/src/draft.ts` (defines `DRAFT_ID_PREFIX`)
271
+ * - `packages/warden/src/draft.ts` (defines `DRAFT_FILE_PREFIX`)
272
+ */
273
+ const FRAMEWORK_DRAFT_CONSTANT_FILES: ReadonlySet<string> = new Set([
274
+ resolve(
275
+ fileURLToPath(new URL('../../../core/src/draft.ts', import.meta.url))
276
+ ),
277
+ resolve(fileURLToPath(new URL('../draft.ts', import.meta.url))),
278
+ ]);
279
+
280
+ /**
281
+ * Collect the source offsets of string literals that initialize a framework
282
+ * draft-prefix constant declaration (e.g. `export const DRAFT_ID_PREFIX =
283
+ * '_draft.'`). Used by draft-awareness rules to skip their own marker
284
+ * constants.
285
+ *
286
+ * Exemption is gated on all three of:
287
+ * 1. The file's absolute path matches one of the two framework files that
288
+ * actually define these constants.
289
+ * 2. The declaration name is `DRAFT_ID_PREFIX` or `DRAFT_FILE_PREFIX`.
290
+ * 3. The string literal value is exactly `'_draft.'`.
291
+ *
292
+ * A consumer file that reuses one of these identifier names cannot hide a
293
+ * `_draft.*` leak — the path gate rejects it outright.
294
+ */
295
+ export const collectFrameworkDraftPrefixConstantOffsets = (
296
+ ast: AstNode,
297
+ filePath: string
298
+ ): ReadonlySet<number> => {
299
+ const offsets = new Set<number>();
300
+
301
+ if (!FRAMEWORK_DRAFT_CONSTANT_FILES.has(resolve(filePath))) {
302
+ return offsets;
303
+ }
304
+
305
+ walk(ast, (node) => {
306
+ if (node.type !== 'VariableDeclarator') {
307
+ return;
308
+ }
309
+
310
+ const { id, init } = node as unknown as {
311
+ readonly id?: AstNode;
312
+ readonly init?: AstNode;
313
+ };
314
+ const name = identifierName(id);
315
+ if (
316
+ !name ||
317
+ !FRAMEWORK_DRAFT_PREFIX_CONSTANT_NAMES.has(name) ||
318
+ !init ||
319
+ !isStringLiteral(init)
320
+ ) {
321
+ return;
322
+ }
323
+
324
+ if (getStringValue(init) !== FRAMEWORK_DRAFT_PREFIX_LITERAL) {
325
+ return;
326
+ }
327
+
328
+ offsets.add(init.start);
329
+ });
330
+
331
+ return offsets;
332
+ };
333
+
334
+ const WARDEN_IGNORE_NEXT_LINE_PRAGMA = '// warden-ignore-next-line';
335
+
336
+ /**
337
+ * Split source code into lines for pragma lookups. Callers should split once
338
+ * per `check` invocation and thread the result through to
339
+ * {@link hasIgnoreCommentOnLine} so we avoid re-splitting the full source on
340
+ * every match in files with many draft-like string literals.
341
+ */
342
+ export const splitSourceLines = (sourceCode: string): readonly string[] =>
343
+ sourceCode.split('\n');
344
+
345
+ /**
346
+ * Check whether the line immediately preceding `line` contains a
347
+ * `// warden-ignore-next-line` pragma (leading/trailing whitespace tolerated).
348
+ * Pragma scope is strictly one line — an intervening blank line breaks it.
349
+ *
350
+ * Takes a pre-split `lines` array so callers can split the source once per
351
+ * invocation instead of re-splitting for every literal they check.
352
+ *
353
+ * @example
354
+ * ```ts
355
+ * // warden-ignore-next-line
356
+ * const x = '_draft.intentional'; // suppressed
357
+ * ```
358
+ */
359
+ export const hasIgnoreCommentOnLine = (
360
+ lines: readonly string[],
361
+ line: number
362
+ ): boolean => {
363
+ if (line <= 1) {
364
+ return false;
365
+ }
366
+
367
+ const previous = lines[line - 2];
368
+ if (previous === undefined) {
369
+ return false;
370
+ }
371
+
372
+ return previous.trim() === WARDEN_IGNORE_NEXT_LINE_PRAGMA;
373
+ };
374
+
375
+ export const findStringLiterals = (
376
+ ast: AstNode,
377
+ predicate?: (value: string, node: AstNode) => boolean
378
+ ): StringLiteralMatch[] => {
379
+ const matches: StringLiteralMatch[] = [];
380
+
381
+ walk(ast, (node) => {
382
+ if (!isStringLiteral(node)) {
383
+ return;
384
+ }
385
+
386
+ const value = getStringValue(node);
387
+ if (value === null) {
388
+ return;
389
+ }
390
+
391
+ if (predicate && !predicate(value, node)) {
392
+ return;
393
+ }
394
+
395
+ matches.push({
396
+ end: node.end,
397
+ node,
398
+ start: node.start,
399
+ value,
400
+ });
401
+ });
402
+
403
+ return matches;
404
+ };
405
+
158
406
  /** Extract the first string argument from a CallExpression. */
159
407
  export const extractFirstStringArg = (node: AstNode): string | null => {
160
408
  if (node.type !== 'CallExpression') {
@@ -166,11 +414,11 @@ export const extractFirstStringArg = (node: AstNode): string | null => {
166
414
  return extractStringLiteral(firstArg);
167
415
  };
168
416
 
169
- const isProvisionCall = (node: AstNode | undefined): boolean =>
417
+ const isResourceCall = (node: AstNode | undefined): boolean =>
170
418
  !!node &&
171
419
  node.type === 'CallExpression' &&
172
420
  identifierName((node as unknown as { callee?: AstNode }).callee) ===
173
- 'provision';
421
+ 'resource';
174
422
 
175
423
  const extractBindingName = (node: AstNode | undefined): string | null => {
176
424
  if (!node) {
@@ -185,8 +433,8 @@ const extractBindingName = (node: AstNode | undefined): string | null => {
185
433
  return null;
186
434
  };
187
435
 
188
- /** Collect `const foo = provision('id', ...)` bindings from a parsed file. */
189
- export const collectNamedProvisionIds = (
436
+ /** Collect `const foo = resource('id', ...)` bindings from a parsed file. */
437
+ export const collectNamedResourceIds = (
190
438
  ast: AstNode
191
439
  ): ReadonlyMap<string, string> => {
192
440
  const ids = new Map<string, string>();
@@ -200,28 +448,28 @@ export const collectNamedProvisionIds = (
200
448
  readonly id?: AstNode;
201
449
  readonly init?: AstNode;
202
450
  };
203
- if (!isProvisionCall(init)) {
451
+ if (!isResourceCall(init)) {
204
452
  return;
205
453
  }
206
454
 
207
455
  const name = extractBindingName(id);
208
- const provisionId = init ? extractFirstStringArg(init) : null;
209
- if (name && provisionId) {
210
- ids.set(name, provisionId);
456
+ const resourceId = init ? extractFirstStringArg(init) : null;
457
+ if (name && resourceId) {
458
+ ids.set(name, resourceId);
211
459
  }
212
460
  });
213
461
 
214
462
  return ids;
215
463
  };
216
464
 
217
- /** Collect all inline `provision('id', ...)` definition IDs from a parsed file. */
218
- export const collectProvisionDefinitionIds = (
465
+ /** Collect all inline `resource('id', ...)` definition IDs from a parsed file. */
466
+ export const collectResourceDefinitionIds = (
219
467
  ast: AstNode
220
468
  ): ReadonlySet<string> => {
221
469
  const ids = new Set<string>();
222
470
 
223
471
  walk(ast, (node) => {
224
- if (!isProvisionCall(node)) {
472
+ if (!isResourceCall(node)) {
225
473
  return;
226
474
  }
227
475
 
@@ -235,14 +483,40 @@ export const collectProvisionDefinitionIds = (
235
483
  };
236
484
 
237
485
  /** Backward-compatible aliases while the migration is in flight. */
238
- export const collectNamedServiceIds = collectNamedProvisionIds;
486
+ export const collectNamedServiceIds = collectNamedResourceIds;
239
487
  /** Backward-compatible aliases while the migration is in flight. */
240
- export const collectServiceDefinitionIds = collectProvisionDefinitionIds;
488
+ export const collectServiceDefinitionIds = collectResourceDefinitionIds;
241
489
 
242
490
  // ---------------------------------------------------------------------------
243
491
  // Config property extraction helpers
244
492
  // ---------------------------------------------------------------------------
245
493
 
494
+ /**
495
+ * Extract the identifying name of a `Property` key, supporting both
496
+ * identifier keys (`{ foo: 1 }`) and string-literal keys
497
+ * (`{ "foo": 1 }`). Computed keys are intentionally not resolved — a
498
+ * computed expression could evaluate to anything and we only want to
499
+ * match keys that are statically equivalent to a plain identifier.
500
+ */
501
+ const staticPropertyKeyName = (key: AstNode): string | null => {
502
+ if (key.type === 'Identifier') {
503
+ return (key as unknown as { name?: string }).name ?? null;
504
+ }
505
+ return isStringLiteral(key) ? getStringValue(key) : null;
506
+ };
507
+
508
+ const propertyKeyName = (prop: AstNode): string | null => {
509
+ if (prop.type !== 'Property') {
510
+ return null;
511
+ }
512
+ const { computed } = prop as unknown as { computed?: boolean };
513
+ if (computed) {
514
+ return null;
515
+ }
516
+ const key = prop.key as AstNode | undefined;
517
+ return key ? staticPropertyKeyName(key) : null;
518
+ };
519
+
246
520
  /** Find a Property node by key name inside an ObjectExpression config. */
247
521
  export const findConfigProperty = (
248
522
  config: AstNode,
@@ -256,7 +530,7 @@ export const findConfigProperty = (
256
530
  return null;
257
531
  }
258
532
  for (const prop of properties) {
259
- if (prop.type === 'Property' && prop.key?.name === propertyName) {
533
+ if (propertyKeyName(prop) === propertyName) {
260
534
  return prop;
261
535
  }
262
536
  }
@@ -284,144 +558,2466 @@ export interface TrailDefinition {
284
558
  *
285
559
  * Returns the trail ID, kind, and config object node for each definition.
286
560
  */
287
- const TRAIL_CALLEE_NAMES = new Set(['trail', 'signal']);
561
+ const TRAIL_CALLEE_NAMES = new Set(['signal', 'trail']);
288
562
 
289
- const getTrailCalleeName = (node: AstNode): string | null => {
290
- if (node.type !== 'CallExpression') {
291
- return null;
292
- }
293
- const callee = node['callee'] as AstNode | undefined;
294
- if (!callee || callee.type !== 'Identifier') {
295
- return null;
296
- }
297
- const { name } = callee as unknown as { name?: string };
298
- return name && TRAIL_CALLEE_NAMES.has(name) ? name : null;
299
- };
563
+ /**
564
+ * Source prefix for the Trails framework package whose namespace imports are
565
+ * recognized as carriers of `trail()` / `signal()` / `contour()` primitives.
566
+ *
567
+ * A namespaced callee like `core.trail(...)` is only treated as a framework
568
+ * call when the receiver identifier resolves to an `import * as core from
569
+ * '@ontrails/...'` in the same file. An unrelated `analytics.trail(...)`
570
+ * whose `analytics` comes from a different module (or no import at all)
571
+ * is ignored.
572
+ */
573
+ const FRAMEWORK_NAMESPACE_SOURCE_PREFIX = '@ontrails/';
300
574
 
301
- /** Extract args from a trail() call, handling both two-arg and single-object forms. */
302
- const extractTrailArgs = (
303
- node: AstNode
304
- ): { idArg: AstNode | null; configArg: AstNode } | null => {
305
- const args = node['arguments'] as readonly AstNode[] | undefined;
306
- if (!args || args.length === 0) {
307
- return null;
308
- }
575
+ const isFrameworkNamespaceSource = (value: unknown): boolean =>
576
+ typeof value === 'string' &&
577
+ value.startsWith(FRAMEWORK_NAMESPACE_SOURCE_PREFIX);
309
578
 
310
- const [firstArg, secondArg] = args;
311
- if (!firstArg) {
312
- return null;
313
- }
579
+ /**
580
+ * Collect local binding names introduced by `import * as <name> from
581
+ * '@ontrails/...'` declarations. Used to gate namespaced framework-primitive
582
+ * calls so an unrelated `analytics.trail(...)` doesn't match.
583
+ */
584
+ const getImportSourceValue = (node: AstNode): unknown => {
585
+ const sourceNode = (node as unknown as { source?: AstNode }).source;
586
+ return sourceNode
587
+ ? (sourceNode as unknown as { value?: unknown }).value
588
+ : undefined;
589
+ };
314
590
 
315
- // Two-arg form: trail('id', { ... })
316
- if (secondArg && firstArg.type !== 'ObjectExpression') {
317
- return { configArg: secondArg, idArg: firstArg };
591
+ const addNamespaceImportBindings = (
592
+ node: AstNode,
593
+ names: Set<string>
594
+ ): void => {
595
+ const specifiers =
596
+ (node['specifiers'] as readonly AstNode[] | undefined) ?? [];
597
+ for (const spec of specifiers) {
598
+ if (spec.type !== 'ImportNamespaceSpecifier') {
599
+ continue;
600
+ }
601
+ const { local } = spec as unknown as { local?: AstNode };
602
+ const localName = identifierName(local);
603
+ if (localName) {
604
+ names.add(localName);
605
+ }
318
606
  }
319
-
320
- // Single-object form: trail({ id: 'x', ... })
321
- return firstArg.type === 'ObjectExpression'
322
- ? { configArg: firstArg, idArg: null }
323
- : null;
324
607
  };
325
608
 
326
- /** Extract the string value from an `id` property inside a config ObjectExpression. */
327
- const extractIdFromConfig = (config: AstNode): string | null => {
328
- const idProp = findConfigProperty(config, 'id');
329
- if (!idProp || !idProp.value) {
330
- return null;
609
+ const TOP_LEVEL_NAMED_DECL_TYPES = new Set([
610
+ 'ClassDeclaration',
611
+ 'FunctionDeclaration',
612
+ 'TSEnumDeclaration',
613
+ 'TSModuleDeclaration',
614
+ ]);
615
+
616
+ const removeVarDeclarationShadowedNames = (
617
+ stmt: AstNode,
618
+ names: Set<string>
619
+ ): void => {
620
+ const declarations =
621
+ (stmt as unknown as { declarations?: readonly AstNode[] }).declarations ??
622
+ [];
623
+ for (const d of declarations) {
624
+ const { id } = d as unknown as { id?: AstNode };
625
+ const n = identifierName(id);
626
+ if (n) {
627
+ names.delete(n);
628
+ }
331
629
  }
332
- const val = (idProp.value as unknown as { value?: unknown }).value;
333
- return typeof val === 'string' ? val : null;
334
630
  };
335
631
 
336
- const extractTrailId = (trailArgs: {
337
- idArg: AstNode | null;
338
- configArg: AstNode;
339
- }): string | null => {
340
- if (trailArgs.idArg) {
341
- return (trailArgs.idArg as unknown as { value?: string }).value ?? null;
632
+ const removeNamedDeclShadowedName = (
633
+ stmt: AstNode,
634
+ names: Set<string>
635
+ ): void => {
636
+ const { id } = stmt as unknown as { id?: AstNode };
637
+ const n = identifierName(id);
638
+ if (n) {
639
+ names.delete(n);
342
640
  }
343
- return extractIdFromConfig(trailArgs.configArg);
344
641
  };
345
642
 
346
- const extractTrailDefinition = (node: AstNode): TrailDefinition | null => {
347
- const calleeName = getTrailCalleeName(node);
348
- if (!calleeName) {
349
- return null;
643
+ const removeTopLevelShadowedNames = (
644
+ stmt: AstNode,
645
+ names: Set<string>
646
+ ): void => {
647
+ if (
648
+ stmt.type === 'ExportNamedDeclaration' ||
649
+ stmt.type === 'ExportDefaultDeclaration'
650
+ ) {
651
+ const { declaration } = stmt as unknown as { declaration?: AstNode };
652
+ if (declaration) {
653
+ removeTopLevelShadowedNames(declaration, names);
654
+ }
655
+ return;
350
656
  }
351
-
352
- const trailArgs = extractTrailArgs(node);
353
- if (!trailArgs) {
354
- return null;
657
+ if (stmt.type === 'VariableDeclaration') {
658
+ removeVarDeclarationShadowedNames(stmt, names);
659
+ return;
355
660
  }
356
-
357
- const trailId = extractTrailId(trailArgs);
358
- if (!trailId) {
359
- return null;
661
+ if (TOP_LEVEL_NAMED_DECL_TYPES.has(stmt.type)) {
662
+ removeNamedDeclShadowedName(stmt, names);
360
663
  }
361
-
362
- return {
363
- config: trailArgs.configArg,
364
- id: trailId,
365
- kind: calleeName,
366
- start: node.start,
367
- };
368
664
  };
369
665
 
370
- export const findTrailDefinitions = (ast: AstNode): TrailDefinition[] => {
371
- const definitions: TrailDefinition[] = [];
372
-
666
+ const collectFrameworkNamespaceBindings = (
667
+ ast: AstNode
668
+ ): ReadonlySet<string> => {
669
+ const names = new Set<string>();
373
670
  walk(ast, (node) => {
374
- const def = extractTrailDefinition(node);
375
- if (def) {
376
- definitions.push(def);
671
+ if (node.type !== 'ImportDeclaration') {
672
+ return;
673
+ }
674
+ if (!isFrameworkNamespaceSource(getImportSourceValue(node))) {
675
+ return;
377
676
  }
677
+ addNamespaceImportBindings(node, names);
378
678
  });
379
-
380
- return definitions;
679
+ if (names.size === 0) {
680
+ return names;
681
+ }
682
+ // A same-named top-level declaration (class / enum / namespace / var /
683
+ // function / lexical binding) shadows the namespace import at module scope.
684
+ // The scope walker treats Program as the outermost frame and skips it when
685
+ // testing for inner shadows, so we have to strip these collisions here.
686
+ if (ast.type === 'Program') {
687
+ const body = (ast as unknown as { body?: readonly AstNode[] }).body ?? [];
688
+ for (const stmt of body) {
689
+ removeTopLevelShadowedNames(stmt, names);
690
+ }
691
+ }
692
+ return names;
381
693
  };
382
694
 
383
695
  // ---------------------------------------------------------------------------
384
- // Blaze body extraction
696
+ // Scope-aware framework-namespace resolution
385
697
  // ---------------------------------------------------------------------------
698
+ //
699
+ // A module-level `import * as core from '@ontrails/core'` makes `core` a
700
+ // framework-namespace binding, but a function-local `const core = {...}` (or
701
+ // param, `let`, `var`, `function`, class, catch param) shadows the import for
702
+ // the duration of that scope. A name-only check is not enough to trust
703
+ // `core.trail(...)` — we have to walk scopes outward from each call site and
704
+ // verify the first declaration of the receiver IS the namespace import.
705
+ //
706
+ // {@link collectFrameworkNamespacedCallStarts} performs that walk once per
707
+ // AST and returns the set of `CallExpression` start offsets whose receiver is
708
+ // provably the framework binding. Downstream helpers gate on this set instead
709
+ // of the bare names, so a local shadow cannot sneak through.
386
710
 
387
- /**
388
- * Extract top-level `blaze:` property values from an ObjectExpression's direct properties.
389
- *
390
- * Does not recurse into nested objects, so `meta: { blaze: ... }` is ignored.
391
- */
392
- const extractBlazeFromConfig = (config: AstNode): AstNode[] => {
393
- const bodies: AstNode[] = [];
394
- const properties = config['properties'] as readonly AstNode[] | undefined;
395
- if (!properties) {
396
- return bodies;
711
+ type PatternExpander = (node: AstNode) => readonly AstNode[];
712
+
713
+ const expandAssignmentPattern: PatternExpander = (node) => {
714
+ const { left } = node as unknown as { left?: AstNode };
715
+ return left ? [left] : [];
716
+ };
717
+
718
+ const expandRestElement: PatternExpander = (node) => {
719
+ const { argument } = node as unknown as { argument?: AstNode };
720
+ return argument ? [argument] : [];
721
+ };
722
+
723
+ const expandArrayPattern: PatternExpander = (node) => {
724
+ const elements =
725
+ (node as unknown as { elements?: readonly (AstNode | null)[] }).elements ??
726
+ [];
727
+ return elements.filter((e): e is AstNode => e !== null);
728
+ };
729
+
730
+ const expandObjectPatternProperty = (prop: AstNode): AstNode | null => {
731
+ if (prop.type === 'RestElement') {
732
+ return prop;
397
733
  }
398
- for (const prop of properties) {
399
- if (prop.type === 'Property' && prop.key?.name === 'blaze' && prop.value) {
400
- bodies.push(prop.value);
734
+ const { value } = prop as unknown as { value?: AstNode };
735
+ return value ?? null;
736
+ };
737
+
738
+ const expandObjectPattern: PatternExpander = (node) => {
739
+ const properties =
740
+ (node as unknown as { properties?: readonly AstNode[] }).properties ?? [];
741
+ return properties
742
+ .map(expandObjectPatternProperty)
743
+ .filter((n): n is AstNode => n !== null);
744
+ };
745
+
746
+ const PATTERN_EXPANDERS: Record<string, PatternExpander> = {
747
+ ArrayPattern: expandArrayPattern,
748
+ AssignmentPattern: expandAssignmentPattern,
749
+ ObjectPattern: expandObjectPattern,
750
+ RestElement: expandRestElement,
751
+ };
752
+
753
+ const processPatternNode = (
754
+ node: AstNode,
755
+ into: Set<string>,
756
+ stack: AstNode[]
757
+ ): void => {
758
+ if (node.type === 'Identifier') {
759
+ const { name } = node as unknown as { name?: string };
760
+ if (name) {
761
+ into.add(name);
401
762
  }
763
+ return;
764
+ }
765
+ const expand = PATTERN_EXPANDERS[node.type];
766
+ if (expand) {
767
+ stack.push(...expand(node));
402
768
  }
403
- return bodies;
404
769
  };
405
770
 
406
- /**
407
- * Find `blaze:` property values.
408
- *
409
- * When given an ObjectExpression (trail config), returns only its direct `blaze:`
410
- * properties. When given a full AST, finds trail definitions first and extracts
411
- * `blaze:` from each config — in both cases ignoring nested `blaze:` properties
412
- * (e.g. `meta: { blaze: ... }`).
413
- */
414
- export const findBlazeBodies = (node: AstNode): AstNode[] => {
415
- if (node.type === 'ObjectExpression') {
416
- return extractBlazeFromConfig(node);
771
+ const addPatternBindingNames = (
772
+ pattern: AstNode | undefined,
773
+ into: Set<string>
774
+ ): void => {
775
+ if (!pattern) {
776
+ return;
417
777
  }
418
-
419
- // Full AST find trail definitions and extract blaze from their configs
778
+ const stack: AstNode[] = [pattern];
779
+ while (stack.length > 0) {
780
+ const node = stack.pop();
781
+ if (node) {
782
+ processPatternNode(node, into, stack);
783
+ }
784
+ }
785
+ };
786
+
787
+ const addVarDeclarationBindingNames = (
788
+ decl: AstNode,
789
+ into: Set<string>
790
+ ): void => {
791
+ const declarations =
792
+ (decl as unknown as { declarations?: readonly AstNode[] }).declarations ??
793
+ [];
794
+ for (const d of declarations) {
795
+ addPatternBindingNames((d as unknown as { id?: AstNode }).id, into);
796
+ }
797
+ };
798
+
799
+ const addFunctionOrClassBindingName = (
800
+ node: AstNode,
801
+ into: Set<string>
802
+ ): void => {
803
+ const { id } = node as unknown as { id?: AstNode };
804
+ const name = identifierName(id);
805
+ if (name) {
806
+ into.add(name);
807
+ }
808
+ };
809
+
810
+ const addBlockStatementBindings = (stmt: AstNode, into: Set<string>): void => {
811
+ if (stmt.type === 'VariableDeclaration') {
812
+ addVarDeclarationBindingNames(stmt, into);
813
+ return;
814
+ }
815
+ if (
816
+ stmt.type === 'FunctionDeclaration' ||
817
+ stmt.type === 'ClassDeclaration' ||
818
+ stmt.type === 'TSEnumDeclaration' ||
819
+ stmt.type === 'TSModuleDeclaration'
820
+ ) {
821
+ addFunctionOrClassBindingName(stmt, into);
822
+ }
823
+ };
824
+
825
+ const collectTopLevelStatementBindings = (
826
+ stmt: AstNode,
827
+ into: Set<string>
828
+ ): void => {
829
+ if (
830
+ stmt.type === 'ExportNamedDeclaration' ||
831
+ stmt.type === 'ExportDefaultDeclaration'
832
+ ) {
833
+ const { declaration } = stmt as unknown as { declaration?: AstNode };
834
+ if (declaration) {
835
+ collectTopLevelStatementBindings(declaration, into);
836
+ }
837
+ return;
838
+ }
839
+ addBlockStatementBindings(stmt, into);
840
+ };
841
+
842
+ const FUNCTION_BOUNDARY_TYPES = new Set([
843
+ 'ArrowFunctionExpression',
844
+ 'FunctionDeclaration',
845
+ 'FunctionExpression',
846
+ 'StaticBlock',
847
+ ]);
848
+
849
+ const forEachAstChild = (
850
+ node: AstNode,
851
+ visit: (child: AstNode) => void
852
+ ): void => {
853
+ for (const val of Object.values(node)) {
854
+ if (Array.isArray(val)) {
855
+ for (const item of val) {
856
+ if (item && typeof item === 'object' && (item as AstNode).type) {
857
+ visit(item as AstNode);
858
+ }
859
+ }
860
+ } else if (val && typeof val === 'object' && (val as AstNode).type) {
861
+ visit(val as AstNode);
862
+ }
863
+ }
864
+ };
865
+
866
+ const recordHoistedBinding = (
867
+ node: AstNode,
868
+ into: Set<string>,
869
+ inNestedBlock: boolean
870
+ ): void => {
871
+ if (node.type === 'VariableDeclaration') {
872
+ const { kind } = node as unknown as { kind?: string };
873
+ if (kind === 'var') {
874
+ addVarDeclarationBindingNames(node, into);
875
+ }
876
+ return;
877
+ }
878
+ // In strict/module code, function/class/enum/module declarations inside a
879
+ // nested block (`if { function foo() {} }`, `switch` case, etc.) are
880
+ // block-scoped. Only hoist them to the enclosing function frame when they
881
+ // sit directly in the function body, not inside a further block.
882
+ if (inNestedBlock) {
883
+ return;
884
+ }
885
+ if (
886
+ node.type === 'FunctionDeclaration' ||
887
+ node.type === 'ClassDeclaration' ||
888
+ node.type === 'TSEnumDeclaration' ||
889
+ node.type === 'TSModuleDeclaration'
890
+ ) {
891
+ addFunctionOrClassBindingName(node, into);
892
+ }
893
+ };
894
+
895
+ const NESTED_BLOCK_BOUNDARY_TYPES = new Set([
896
+ 'BlockStatement',
897
+ 'ForStatement',
898
+ 'ForInStatement',
899
+ 'ForOfStatement',
900
+ 'SwitchStatement',
901
+ 'CatchClause',
902
+ ]);
903
+
904
+ const visitForHoisted = (
905
+ node: AstNode,
906
+ isRoot: boolean,
907
+ into: Set<string>,
908
+ inNestedBlock: boolean
909
+ ): void => {
910
+ if (!isRoot && FUNCTION_BOUNDARY_TYPES.has(node.type)) {
911
+ return;
912
+ }
913
+ recordHoistedBinding(node, into, inNestedBlock);
914
+ const childInNestedBlock =
915
+ inNestedBlock || (!isRoot && NESTED_BLOCK_BOUNDARY_TYPES.has(node.type));
916
+ forEachAstChild(node, (child) => {
917
+ visitForHoisted(child, false, into, childInNestedBlock);
918
+ });
919
+ };
920
+
921
+ /**
922
+ * Collect `var` declarations and `function` declarations hoisted to the
923
+ * nearest function scope from anywhere inside `root`, without crossing a
924
+ * nested function or static-block boundary.
925
+ */
926
+ const collectHoistedVarAndFunctionBindings = (
927
+ root: AstNode,
928
+ into: Set<string>
929
+ ): void => {
930
+ visitForHoisted(root, true, into, false);
931
+ };
932
+
933
+ type FrameCollector = (node: AstNode, into: Set<string>) => void;
934
+
935
+ const collectProgramFrame: FrameCollector = (node, into) => {
936
+ const body = (node as unknown as { body?: readonly AstNode[] }).body ?? [];
937
+ for (const stmt of body) {
938
+ collectTopLevelStatementBindings(stmt, into);
939
+ }
940
+ };
941
+
942
+ const collectFunctionFrame: FrameCollector = (node, into) => {
943
+ const params =
944
+ (node as unknown as { params?: readonly AstNode[] }).params ?? [];
945
+ for (const param of params) {
946
+ addPatternBindingNames(param, into);
947
+ }
948
+ // Hoisted vars and function declarations inside the body live in the
949
+ // function's var-environment. A `var ns = ...;` inside an `if` still
950
+ // shadows a module-level `ns` for the whole function.
951
+ const { body } = node as unknown as { body?: AstNode };
952
+ if (body) {
953
+ collectHoistedVarAndFunctionBindings(body, into);
954
+ }
955
+ };
956
+
957
+ const collectBlockFrame: FrameCollector = (node, into) => {
958
+ const body = (node as unknown as { body?: readonly AstNode[] }).body ?? [];
959
+ for (const stmt of body) {
960
+ addBlockStatementBindings(stmt, into);
961
+ }
962
+ };
963
+
964
+ const collectForStatementFrame: FrameCollector = (node, into) => {
965
+ const { init } = node as unknown as { init?: AstNode };
966
+ if (init && init.type === 'VariableDeclaration') {
967
+ addVarDeclarationBindingNames(init, into);
968
+ }
969
+ };
970
+
971
+ const collectForInOfFrame: FrameCollector = (node, into) => {
972
+ const { left } = node as unknown as { left?: AstNode };
973
+ if (left && left.type === 'VariableDeclaration') {
974
+ addVarDeclarationBindingNames(left, into);
975
+ }
976
+ };
977
+
978
+ const collectSwitchStatementFrame: FrameCollector = (node, into) => {
979
+ // `switch` shares one scope across every case. A binding in one case
980
+ // shadows the namespace across sibling cases (fall-through or otherwise).
981
+ const cases = (node as unknown as { cases?: readonly AstNode[] }).cases ?? [];
982
+ for (const c of cases) {
983
+ const consequent =
984
+ (c as unknown as { consequent?: readonly AstNode[] }).consequent ?? [];
985
+ for (const stmt of consequent) {
986
+ addBlockStatementBindings(stmt, into);
987
+ }
988
+ }
989
+ };
990
+
991
+ const collectCatchClauseFrame: FrameCollector = (node, into) => {
992
+ const { param } = node as unknown as { param?: AstNode };
993
+ addPatternBindingNames(param, into);
994
+ };
995
+
996
+ const collectClassExpressionFrame: FrameCollector = (node, into) => {
997
+ // A named `class expr` (`const C = class foo { ... }`) binds its own name
998
+ // inside its body only. ClassDeclaration names are hoisted into the
999
+ // enclosing block/program frame instead, so only class *expression* names
1000
+ // need their own frame here.
1001
+ addFunctionOrClassBindingName(node, into);
1002
+ };
1003
+
1004
+ const SCOPE_FRAME_COLLECTORS: Record<string, FrameCollector> = {
1005
+ ArrowFunctionExpression: collectFunctionFrame,
1006
+ BlockStatement: collectBlockFrame,
1007
+ CatchClause: collectCatchClauseFrame,
1008
+ ClassExpression: collectClassExpressionFrame,
1009
+ ForInStatement: collectForInOfFrame,
1010
+ ForOfStatement: collectForInOfFrame,
1011
+ ForStatement: collectForStatementFrame,
1012
+ // oxc-parser emits `FunctionBody` for `function` expression bodies; without
1013
+ // this entry, a `const ns = ...` at the top of a function-expression body
1014
+ // would not push a scope frame, and a module-level namespace import with
1015
+ // the same name would be incorrectly recognized inside.
1016
+ FunctionBody: collectBlockFrame,
1017
+ FunctionDeclaration: collectFunctionFrame,
1018
+ FunctionExpression: collectFunctionFrame,
1019
+ Program: collectProgramFrame,
1020
+ StaticBlock: collectBlockFrame,
1021
+ SwitchStatement: collectSwitchStatementFrame,
1022
+ };
1023
+
1024
+ /**
1025
+ * Collect the identifier bindings introduced *directly* by a scope frame
1026
+ * node. Scope frames correspond to JS lexical scopes (function bodies, blocks,
1027
+ * catch clauses, for-statements, switch statements, module/script roots).
1028
+ */
1029
+ export const collectScopeFrameBindings = (
1030
+ node: AstNode
1031
+ ): ReadonlySet<string> => {
1032
+ const names = new Set<string>();
1033
+ const collector = SCOPE_FRAME_COLLECTORS[node.type];
1034
+ if (collector) {
1035
+ collector(node, names);
1036
+ }
1037
+ return names;
1038
+ };
1039
+
1040
+ export type ScopeAwareVisitor = (
1041
+ node: AstNode,
1042
+ scopes: readonly ReadonlySet<string>[]
1043
+ ) => void;
1044
+
1045
+ export interface ScopeWalkOptions {
1046
+ readonly initialScopes?: readonly ReadonlySet<string>[];
1047
+ readonly stopAtNestedFunctions?: boolean;
1048
+ }
1049
+
1050
+ const asAstNode = (node: unknown): AstNode | null => {
1051
+ if (!node || typeof node !== 'object') {
1052
+ return null;
1053
+ }
1054
+ const astNode = node as AstNode;
1055
+ return astNode.type ? astNode : null;
1056
+ };
1057
+
1058
+ /**
1059
+ * Walk an AST subtree while threading lexical scope bindings through each
1060
+ * visit. Callers can seed outer scopes and optionally stop at nested function
1061
+ * boundaries when only the current implementation body should be analyzed.
1062
+ */
1063
+ export const walkWithScopes = (
1064
+ node: unknown,
1065
+ visit: ScopeAwareVisitor,
1066
+ options: ScopeWalkOptions = {}
1067
+ ): void => {
1068
+ const root = asAstNode(node);
1069
+ if (!root) {
1070
+ return;
1071
+ }
1072
+
1073
+ const stack = [...(options.initialScopes ?? [])];
1074
+
1075
+ const walkNode = (current: AstNode, isRoot: boolean): void => {
1076
+ if (
1077
+ !isRoot &&
1078
+ options.stopAtNestedFunctions &&
1079
+ FUNCTION_BOUNDARY_TYPES.has(current.type)
1080
+ ) {
1081
+ return;
1082
+ }
1083
+
1084
+ const isScope = current.type in SCOPE_FRAME_COLLECTORS;
1085
+ if (isScope) {
1086
+ stack.unshift(collectScopeFrameBindings(current));
1087
+ }
1088
+
1089
+ try {
1090
+ visit(current, stack);
1091
+ forEachAstChild(current, (child) => {
1092
+ walkNode(child, false);
1093
+ });
1094
+ } finally {
1095
+ if (isScope) {
1096
+ stack.shift();
1097
+ }
1098
+ }
1099
+ };
1100
+
1101
+ walkNode(root, true);
1102
+ };
1103
+
1104
+ const isShadowed = (
1105
+ receiverName: string,
1106
+ scopeStack: readonly ReadonlySet<string>[]
1107
+ ): boolean => {
1108
+ // The module-level Program frame is the last entry and contains the
1109
+ // namespace imports themselves. A "shadow" must come from a frame *inside*
1110
+ // that one — i.e. any frame except the outermost.
1111
+ for (let i = 0; i < scopeStack.length - 1; i += 1) {
1112
+ const frame = scopeStack[i];
1113
+ if (frame?.has(receiverName)) {
1114
+ return true;
1115
+ }
1116
+ }
1117
+ return false;
1118
+ };
1119
+
1120
+ /**
1121
+ * Return `true` when `node` is a non-computed member access (`a.b` /
1122
+ * `a?.b`) and `false` for anything else, including computed access
1123
+ * (`a[b]`) or non-member nodes. Exported as the canonical predicate so
1124
+ * rule modules do not re-implement the check.
1125
+ *
1126
+ * @remarks
1127
+ * Declared near the top of the file so the scope walker can use it
1128
+ * without hitting `no-use-before-define`. A few sibling helpers in this
1129
+ * module still inline the same shape under different local names for
1130
+ * historical reasons; prefer this export for new call sites.
1131
+ */
1132
+ export const isMemberAccessNonComputed = (node: AstNode): boolean => {
1133
+ if (
1134
+ node.type !== 'MemberExpression' &&
1135
+ node.type !== 'StaticMemberExpression'
1136
+ ) {
1137
+ return false;
1138
+ }
1139
+ return (node as unknown as { computed?: boolean }).computed !== true;
1140
+ };
1141
+
1142
+ const resolveNamespacedMemberNames = (
1143
+ callee: AstNode
1144
+ ): { readonly receiver: string; readonly property: string } | null => {
1145
+ if (!isMemberAccessNonComputed(callee)) {
1146
+ return null;
1147
+ }
1148
+ const { object } = callee as unknown as { object?: AstNode };
1149
+ const receiver = identifierName(object);
1150
+ if (!receiver) {
1151
+ return null;
1152
+ }
1153
+ const prop = (callee as unknown as { property?: AstNode }).property;
1154
+ const property =
1155
+ prop?.type === 'Identifier'
1156
+ ? ((prop as unknown as { name?: string }).name ?? null)
1157
+ : null;
1158
+ return property ? { property, receiver } : null;
1159
+ };
1160
+
1161
+ const getFrameworkCallReceiver = (
1162
+ node: AstNode,
1163
+ frameworkNamespaces: ReadonlySet<string>
1164
+ ): string | null => {
1165
+ if (node.type !== 'CallExpression') {
1166
+ return null;
1167
+ }
1168
+ const callee = node['callee'] as AstNode | undefined;
1169
+ if (!callee) {
1170
+ return null;
1171
+ }
1172
+ const names = resolveNamespacedMemberNames(callee);
1173
+ if (!names || !frameworkNamespaces.has(names.receiver)) {
1174
+ return null;
1175
+ }
1176
+ return names.receiver;
1177
+ };
1178
+
1179
+ /**
1180
+ * Walk the AST with a scope stack and collect `CallExpression` start offsets
1181
+ * whose callee is `<receiver>.<property>` where `<receiver>` is proven to
1182
+ * resolve to a framework namespace import (i.e. not shadowed by any
1183
+ * enclosing scope). Used to gate namespaced `core.trail(...)` /
1184
+ * `core.signal(...)` / `core.contour(...)` resolution against local shadows.
1185
+ */
1186
+ const collectFrameworkNamespacedCallStarts = (
1187
+ ast: AstNode,
1188
+ frameworkNamespaces: ReadonlySet<string>
1189
+ ): ReadonlySet<number> => {
1190
+ const starts = new Set<number>();
1191
+ if (frameworkNamespaces.size === 0) {
1192
+ return starts;
1193
+ }
1194
+
1195
+ walkWithScopes(ast, (node, scopes) => {
1196
+ const receiver = getFrameworkCallReceiver(node, frameworkNamespaces);
1197
+ if (!receiver || isShadowed(receiver, scopes)) {
1198
+ return;
1199
+ }
1200
+ starts.add(node.start);
1201
+ });
1202
+
1203
+ return starts;
1204
+ };
1205
+
1206
+ const matchTrailPrimitiveName = (
1207
+ name: string | undefined | null
1208
+ ): string | null => (name && TRAIL_CALLEE_NAMES.has(name) ? name : null);
1209
+
1210
+ const getBareTrailCalleeName = (callee: AstNode): string | null => {
1211
+ if (callee.type !== 'Identifier') {
1212
+ return null;
1213
+ }
1214
+ return matchTrailPrimitiveName((callee as unknown as { name?: string }).name);
1215
+ };
1216
+
1217
+ /**
1218
+ * Extract the `{ receiverName, propertyName }` of a non-computed member-call
1219
+ * callee, or null for anything else. Computed access (`ns[trail]()`) is
1220
+ * intentionally rejected: the bracketed expression may resolve to any runtime
1221
+ * value, so we cannot prove the call targets a specific member.
1222
+ */
1223
+ const isNonComputedMemberAccess = (callee: AstNode): boolean => {
1224
+ if (
1225
+ callee.type !== 'MemberExpression' &&
1226
+ callee.type !== 'StaticMemberExpression'
1227
+ ) {
1228
+ return false;
1229
+ }
1230
+ return (callee as unknown as { computed?: boolean }).computed !== true;
1231
+ };
1232
+
1233
+ const getNamespacedMemberNames = (
1234
+ callee: AstNode
1235
+ ): { readonly receiver: string; readonly property: string } | null => {
1236
+ if (!isNonComputedMemberAccess(callee)) {
1237
+ return null;
1238
+ }
1239
+ const { object } = callee as unknown as { object?: AstNode };
1240
+ const receiver = identifierName(object);
1241
+ if (!receiver) {
1242
+ return null;
1243
+ }
1244
+ const prop = (callee as unknown as { property?: AstNode }).property;
1245
+ const property =
1246
+ prop?.type === 'Identifier'
1247
+ ? ((prop as unknown as { name?: string }).name ?? null)
1248
+ : null;
1249
+ return property ? { property, receiver } : null;
1250
+ };
1251
+
1252
+ /**
1253
+ * Resolution context for namespaced framework-primitive calls. Bundles the
1254
+ * bare namespace-binding set with an optional set of proven-safe
1255
+ * `CallExpression` start offsets from a scope-aware pre-pass. When the set of
1256
+ * safe starts is present, a namespaced call only resolves if its start is in
1257
+ * that set — so a function-local shadow of the namespace import does not
1258
+ * leak through. When absent (e.g. from test helpers), the name-only gate is
1259
+ * used as a backward-compatible fallback.
1260
+ */
1261
+ export interface FrameworkNamespaceContext {
1262
+ readonly namespaces: ReadonlySet<string>;
1263
+ readonly safeCallStarts?: ReadonlySet<number>;
1264
+ }
1265
+
1266
+ const asNamespaceContext = (
1267
+ input: ReadonlySet<string> | FrameworkNamespaceContext | undefined
1268
+ ): FrameworkNamespaceContext | undefined => {
1269
+ if (!input) {
1270
+ return undefined;
1271
+ }
1272
+ return input instanceof Set
1273
+ ? { namespaces: input }
1274
+ : (input as FrameworkNamespaceContext);
1275
+ };
1276
+
1277
+ const isNamespacedCallAllowed = (
1278
+ callStart: number,
1279
+ receiver: string,
1280
+ ctx: FrameworkNamespaceContext
1281
+ ): boolean => {
1282
+ if (!ctx.namespaces.has(receiver)) {
1283
+ return false;
1284
+ }
1285
+ // When `safeCallStarts` is present, it is the authoritative gate — it was
1286
+ // built by a scope-aware pre-pass and already excludes shadowed receivers.
1287
+ // Without it, fall back to the bare name check (used by unit-test hooks).
1288
+ return ctx.safeCallStarts ? ctx.safeCallStarts.has(callStart) : true;
1289
+ };
1290
+
1291
+ /**
1292
+ * Resolve a namespaced `ns.trail(...)` / `ns.signal(...)` callee to its
1293
+ * primitive name. When a {@link FrameworkNamespaceContext} is provided, the
1294
+ * receiver must be a framework namespace binding AND — when a
1295
+ * `safeCallStarts` set is present — the call site must appear in that set,
1296
+ * meaning the receiver is not shadowed by any enclosing scope.
1297
+ *
1298
+ * When `context` is `undefined`, this falls back to permissive matching
1299
+ * (any `ns.trail(...)` shape resolves). Inline resolution paths that do
1300
+ * not have the surrounding AST available (e.g. `crosses: [core.trail(...)]`
1301
+ * or `on: [core.signal(...)]`) rely on this fallback. Scope-aware call
1302
+ * sites always pass a context, so this only affects inline contexts where
1303
+ * a best-effort name match is the intended behavior.
1304
+ */
1305
+ const getNamespacedTrailCalleeName = (
1306
+ callExpr: AstNode,
1307
+ callee: AstNode,
1308
+ context?: ReadonlySet<string> | FrameworkNamespaceContext
1309
+ ): string | null => {
1310
+ const names = getNamespacedMemberNames(callee);
1311
+ if (!names) {
1312
+ return null;
1313
+ }
1314
+ const ctx = asNamespaceContext(context);
1315
+ if (ctx && !isNamespacedCallAllowed(callExpr.start, names.receiver, ctx)) {
1316
+ return null;
1317
+ }
1318
+ return matchTrailPrimitiveName(names.property);
1319
+ };
1320
+
1321
+ /**
1322
+ * Resolve the callee name of a trail/signal call expression.
1323
+ *
1324
+ * Matches both bare `trail(...)` / `signal(...)` identifiers and namespaced
1325
+ * member-expression callees like `core.trail(...)` or `ns.signal(...)`, where
1326
+ * the namespace must come from an `@ontrails/*` import and, when the scope
1327
+ * pre-pass is wired in, be unshadowed at the call site.
1328
+ */
1329
+ const getTrailCalleeName = (
1330
+ node: AstNode,
1331
+ context?: ReadonlySet<string> | FrameworkNamespaceContext
1332
+ ): string | null => {
1333
+ if (node.type !== 'CallExpression') {
1334
+ return null;
1335
+ }
1336
+ const callee = node['callee'] as AstNode | undefined;
1337
+ if (!callee) {
1338
+ return null;
1339
+ }
1340
+ return (
1341
+ getBareTrailCalleeName(callee) ??
1342
+ getNamespacedTrailCalleeName(node, callee, context)
1343
+ );
1344
+ };
1345
+
1346
+ /**
1347
+ * Test hook: exposes {@link getTrailCalleeName} for unit tests.
1348
+ *
1349
+ * Kept unexported from the module's public surface (no re-export from
1350
+ * `index.ts`) so internal refactors stay free.
1351
+ */
1352
+ export const __getTrailCalleeNameForTest = getTrailCalleeName;
1353
+
1354
+ /**
1355
+ * Test hook: exposes {@link collectFrameworkNamespaceBindings} for unit tests.
1356
+ *
1357
+ * Not re-exported from `index.ts`; the double-underscore prefix marks it as an
1358
+ * internal-only handle so consumer code cannot rely on it.
1359
+ */
1360
+ export const __collectFrameworkNamespaceBindingsForTest =
1361
+ collectFrameworkNamespaceBindings;
1362
+
1363
+ /** Extract args from a trail() call, handling both two-arg and single-object forms. */
1364
+ const extractTrailArgs = (
1365
+ node: AstNode
1366
+ ): { idArg: AstNode | null; configArg: AstNode } | null => {
1367
+ const args = node['arguments'] as readonly AstNode[] | undefined;
1368
+ if (!args || args.length === 0) {
1369
+ return null;
1370
+ }
1371
+
1372
+ const [firstArg, secondArg] = args;
1373
+ if (!firstArg) {
1374
+ return null;
1375
+ }
1376
+
1377
+ // Two-arg form: trail('id', { ... })
1378
+ if (secondArg && firstArg.type !== 'ObjectExpression') {
1379
+ return { configArg: secondArg, idArg: firstArg };
1380
+ }
1381
+
1382
+ // Single-object form: trail({ id: 'x', ... })
1383
+ return firstArg.type === 'ObjectExpression'
1384
+ ? { configArg: firstArg, idArg: null }
1385
+ : null;
1386
+ };
1387
+
1388
+ /** Extract the string value from an `id` property inside a config ObjectExpression. */
1389
+ const extractIdFromConfig = (config: AstNode): string | null => {
1390
+ const idProp = findConfigProperty(config, 'id');
1391
+ if (!idProp || !idProp.value) {
1392
+ return null;
1393
+ }
1394
+ return extractStringOrTemplateLiteral(idProp.value as AstNode);
1395
+ };
1396
+
1397
+ const extractTrailId = (trailArgs: {
1398
+ idArg: AstNode | null;
1399
+ configArg: AstNode;
1400
+ }): string | null => {
1401
+ if (trailArgs.idArg) {
1402
+ return extractStringOrTemplateLiteral(trailArgs.idArg);
1403
+ }
1404
+ return extractIdFromConfig(trailArgs.configArg);
1405
+ };
1406
+
1407
+ const extractTrailDefinition = (
1408
+ node: AstNode,
1409
+ context?: ReadonlySet<string> | FrameworkNamespaceContext
1410
+ ): TrailDefinition | null => {
1411
+ const calleeName = getTrailCalleeName(node, context);
1412
+ if (!calleeName) {
1413
+ return null;
1414
+ }
1415
+
1416
+ const trailArgs = extractTrailArgs(node);
1417
+ if (!trailArgs) {
1418
+ return null;
1419
+ }
1420
+
1421
+ const trailId = extractTrailId(trailArgs);
1422
+ if (!trailId) {
1423
+ return null;
1424
+ }
1425
+
1426
+ return {
1427
+ config: trailArgs.configArg,
1428
+ id: trailId,
1429
+ kind: calleeName,
1430
+ start: node.start,
1431
+ };
1432
+ };
1433
+
1434
+ const buildFrameworkNamespaceContext = (
1435
+ ast: AstNode
1436
+ ): FrameworkNamespaceContext => {
1437
+ const namespaces = collectFrameworkNamespaceBindings(ast);
1438
+ return {
1439
+ namespaces,
1440
+ safeCallStarts: collectFrameworkNamespacedCallStarts(ast, namespaces),
1441
+ };
1442
+ };
1443
+
1444
+ export const findTrailDefinitions = (ast: AstNode): TrailDefinition[] => {
1445
+ const definitions: TrailDefinition[] = [];
1446
+ const context = buildFrameworkNamespaceContext(ast);
1447
+
1448
+ walk(ast, (node) => {
1449
+ const def = extractTrailDefinition(node, context);
1450
+ if (def) {
1451
+ definitions.push(def);
1452
+ }
1453
+ });
1454
+
1455
+ return definitions;
1456
+ };
1457
+
1458
+ // ---------------------------------------------------------------------------
1459
+ // Contour definition extraction
1460
+ // ---------------------------------------------------------------------------
1461
+
1462
+ export interface ContourDefinition {
1463
+ /** Local binding name when the contour is assigned to a variable. */
1464
+ readonly bindingName?: string;
1465
+ /** Contour name string, e.g. "user". */
1466
+ readonly name: string;
1467
+ /** Original call expression for the contour declaration. */
1468
+ readonly call: AstNode;
1469
+ /** Options object argument passed to contour(), when present. */
1470
+ readonly options: AstNode | null;
1471
+ /** Shape object argument passed to contour(). */
1472
+ readonly shape: AstNode;
1473
+ /** Start offset of the call expression. */
1474
+ readonly start: number;
1475
+ }
1476
+
1477
+ const CONTOUR_PRIMITIVE_NAME = 'contour';
1478
+
1479
+ const matchContourPrimitiveName = (
1480
+ name: string | undefined | null
1481
+ ): string | null => (name === CONTOUR_PRIMITIVE_NAME ? name : null);
1482
+
1483
+ const getBareContourCalleeName = (callee: AstNode): string | null => {
1484
+ if (callee.type !== 'Identifier') {
1485
+ return null;
1486
+ }
1487
+ return matchContourPrimitiveName(
1488
+ (callee as unknown as { name?: string }).name
1489
+ );
1490
+ };
1491
+
1492
+ /**
1493
+ * Resolve a namespaced `ns.contour(...)` callee to its primitive name. Mirrors
1494
+ * {@link getNamespacedTrailCalleeName}: the receiver identifier must resolve
1495
+ * to an `@ontrails/*` namespace import, and — when a scope-aware
1496
+ * `safeCallStarts` set is provided — the call site must not be shadowed by a
1497
+ * local binding of the same name.
1498
+ */
1499
+ const getNamespacedContourCalleeName = (
1500
+ callExpr: AstNode,
1501
+ callee: AstNode,
1502
+ context?: ReadonlySet<string> | FrameworkNamespaceContext
1503
+ ): string | null => {
1504
+ const names = getNamespacedMemberNames(callee);
1505
+ if (!names) {
1506
+ return null;
1507
+ }
1508
+ // Unlike the trail/signal variant, contour has no inline-resolution callers
1509
+ // that legitimately invoke this without a FrameworkNamespaceContext, so the
1510
+ // strict namespace gate stays on. If a future caller needs the permissive
1511
+ // fallback, mirror the trail shape and add a regression test first.
1512
+ const ctx = asNamespaceContext(context);
1513
+ if (!ctx || !isNamespacedCallAllowed(callExpr.start, names.receiver, ctx)) {
1514
+ return null;
1515
+ }
1516
+ return matchContourPrimitiveName(names.property);
1517
+ };
1518
+
1519
+ /**
1520
+ * Resolve the callee name of a contour call expression. Matches both bare
1521
+ * `contour(...)` identifiers and namespaced `core.contour(...)` callees where
1522
+ * the namespace comes from an `@ontrails/*` import and is unshadowed.
1523
+ */
1524
+ const getContourCalleeName = (
1525
+ node: AstNode,
1526
+ context?: ReadonlySet<string> | FrameworkNamespaceContext
1527
+ ): string | null => {
1528
+ if (node.type !== 'CallExpression') {
1529
+ return null;
1530
+ }
1531
+ const callee = node['callee'] as AstNode | undefined;
1532
+ if (!callee) {
1533
+ return null;
1534
+ }
1535
+ return (
1536
+ getBareContourCalleeName(callee) ??
1537
+ getNamespacedContourCalleeName(node, callee, context)
1538
+ );
1539
+ };
1540
+
1541
+ const extractContourDefinition = (
1542
+ node: AstNode,
1543
+ context?: ReadonlySet<string> | FrameworkNamespaceContext
1544
+ ): Omit<ContourDefinition, 'bindingName'> | null => {
1545
+ if (!getContourCalleeName(node, context)) {
1546
+ return null;
1547
+ }
1548
+
1549
+ const args = node['arguments'] as readonly AstNode[] | undefined;
1550
+ const [nameArg, shapeArg, optionsArg] = args ?? [];
1551
+ const name = extractStringLiteral(nameArg);
1552
+ if (!name || shapeArg?.type !== 'ObjectExpression') {
1553
+ return null;
1554
+ }
1555
+
1556
+ return {
1557
+ call: node,
1558
+ name,
1559
+ options: optionsArg?.type === 'ObjectExpression' ? optionsArg : null,
1560
+ shape: shapeArg,
1561
+ start: node.start,
1562
+ };
1563
+ };
1564
+
1565
+ const getCallStartFromCandidate = (
1566
+ node: AstNode | undefined
1567
+ ): number | null => {
1568
+ if (!node) {
1569
+ return null;
1570
+ }
1571
+ if (node.type === 'CallExpression') {
1572
+ return node.start;
1573
+ }
1574
+ if (node.type !== 'ExpressionStatement') {
1575
+ return null;
1576
+ }
1577
+ const { expression } = node as unknown as { expression?: AstNode };
1578
+ return expression?.type === 'CallExpression' ? expression.start : null;
1579
+ };
1580
+
1581
+ // Statement forms that can directly contain a top-level contour call:
1582
+ // `core.contour(...)` as a bare statement,
1583
+ // `export const ... = core.contour(...)` (handled via VariableDeclarator),
1584
+ // `export default core.contour(...);`.
1585
+ const getCandidateCallHosts = (
1586
+ statement: AstNode
1587
+ ): readonly (AstNode | undefined)[] => {
1588
+ if (
1589
+ statement.type !== 'ExportNamedDeclaration' &&
1590
+ statement.type !== 'ExportDefaultDeclaration'
1591
+ ) {
1592
+ return [statement];
1593
+ }
1594
+ const { declaration } = statement as unknown as {
1595
+ declaration?: AstNode;
1596
+ };
1597
+ return [statement, declaration];
1598
+ };
1599
+
1600
+ const getTopLevelCallStartsFrom = (statement: AstNode): readonly number[] => {
1601
+ const hosts = getCandidateCallHosts(statement);
1602
+ const starts: number[] = [];
1603
+ for (const host of hosts) {
1604
+ const start = getCallStartFromCandidate(host);
1605
+ if (start !== null) {
1606
+ starts.push(start);
1607
+ }
1608
+ }
1609
+ return starts;
1610
+ };
1611
+
1612
+ /**
1613
+ * Collect the `start` offsets of `CallExpression` nodes that appear as
1614
+ * top-level `ExpressionStatement`s in a program body — including inside a
1615
+ * top-level `ExportNamedDeclaration` / `ExportDefaultDeclaration` wrapper.
1616
+ * Used to discriminate top-level statement-form calls from inline nested
1617
+ * calls when `topLevelOnly` is enabled.
1618
+ */
1619
+ const collectTopLevelStatementCallStarts = (
1620
+ ast: AstNode
1621
+ ): ReadonlySet<number> => {
1622
+ const body = (ast as unknown as { body?: readonly AstNode[] }).body ?? [];
1623
+ return new Set(body.flatMap(getTopLevelCallStartsFrom));
1624
+ };
1625
+
1626
+ export interface FindContourDefinitionsOptions {
1627
+ /**
1628
+ * When true, skip contour calls nested inside other expressions (e.g.
1629
+ * `core.contour('inner', {...}).id()` used as a field of an outer contour).
1630
+ * Top-level forms are still surfaced: both `const foo = contour(...)`
1631
+ * declarations and bare `contour('name', {...});` statement-form calls that
1632
+ * appear directly in the program body (optionally wrapped in `export`) are
1633
+ * returned.
1634
+ *
1635
+ * Defaults to `false`: both top-level and inline contours are returned so
1636
+ * that reference-site resolution can reach anonymous inline contours.
1637
+ */
1638
+ readonly topLevelOnly?: boolean;
1639
+ }
1640
+
1641
+ /**
1642
+ * Return every `contour('name', ...)` definition reachable from the AST, in
1643
+ * source order, deduplicated by call-expression start offset.
1644
+ *
1645
+ * Includes both top-level bindings (`const user = contour('user', ...)`) and
1646
+ * inline contour calls nested inside other expressions (e.g.
1647
+ * `contour('outer', { inner: contour('inner', ...).id() })`). Inline contours
1648
+ * carry no `bindingName` because they have no local binding — this asymmetry
1649
+ * is why {@link collectNamedContourIds} returns only the top-level subset
1650
+ * while {@link collectContourDefinitionIds} returns the full set.
1651
+ *
1652
+ * Pass `{ topLevelOnly: true }` via `options` to opt out of inline discovery
1653
+ * without disturbing callers that rely on the default behavior.
1654
+ *
1655
+ * @remarks
1656
+ * Supplying a pre-built `context` skips the second full-AST traversal inside
1657
+ * `buildFrameworkNamespaceContext` — useful for callers (such as
1658
+ * {@link collectContourReferenceSites}) that already built one.
1659
+ */
1660
+ export const findContourDefinitions = (
1661
+ ast: AstNode,
1662
+ context?: FrameworkNamespaceContext,
1663
+ options?: FindContourDefinitionsOptions
1664
+ ): ContourDefinition[] => {
1665
+ const definitions: ContourDefinition[] = [];
1666
+ const seenStarts = new Set<number>();
1667
+ const resolvedContext = context ?? buildFrameworkNamespaceContext(ast);
1668
+ const topLevelOnly = options?.topLevelOnly === true;
1669
+
1670
+ const addContourDefinition = (definition: ContourDefinition): void => {
1671
+ if (seenStarts.has(definition.start)) {
1672
+ return;
1673
+ }
1674
+
1675
+ definitions.push(definition);
1676
+ seenStarts.add(definition.start);
1677
+ };
1678
+
1679
+ const addNamedContourDefinition = (
1680
+ id: AstNode | undefined,
1681
+ init: AstNode | undefined
1682
+ ): void => {
1683
+ if (!init) {
1684
+ return;
1685
+ }
1686
+
1687
+ const definition = extractContourDefinition(init, resolvedContext);
1688
+ if (!definition) {
1689
+ return;
1690
+ }
1691
+
1692
+ const bindingName = extractBindingName(id);
1693
+ if (bindingName) {
1694
+ addContourDefinition({ ...definition, bindingName });
1695
+ return;
1696
+ }
1697
+
1698
+ addContourDefinition(definition);
1699
+ };
1700
+
1701
+ // When `topLevelOnly` is set, collect the start offsets of call expressions
1702
+ // that sit directly in the program body as `ExpressionStatement`s (optionally
1703
+ // wrapped in `export`). These are top-level statement-form contour calls and
1704
+ // should still surface alongside `VariableDeclarator` bindings; only calls
1705
+ // nested inside other expressions are excluded.
1706
+ const topLevelStatementCallStarts = topLevelOnly
1707
+ ? collectTopLevelStatementCallStarts(ast)
1708
+ : null;
1709
+
1710
+ walk(ast, (node) => {
1711
+ if (node.type === 'VariableDeclarator') {
1712
+ const { id, init } = node as unknown as {
1713
+ readonly id?: AstNode;
1714
+ readonly init?: AstNode;
1715
+ };
1716
+ addNamedContourDefinition(id, init);
1717
+ return;
1718
+ }
1719
+
1720
+ if (
1721
+ topLevelStatementCallStarts &&
1722
+ !topLevelStatementCallStarts.has(node.start)
1723
+ ) {
1724
+ return;
1725
+ }
1726
+
1727
+ const definition = extractContourDefinition(node, resolvedContext);
1728
+ if (definition) {
1729
+ addContourDefinition(definition);
1730
+ }
1731
+ });
1732
+
1733
+ return definitions.toSorted((left, right) => left.start - right.start);
1734
+ };
1735
+
1736
+ /**
1737
+ * Collect the `name` of every contour definition in a parsed file, including
1738
+ * inline contours nested inside other expressions. Returns the same set of
1739
+ * names that {@link findContourDefinitions} discovers under default options.
1740
+ */
1741
+ export const collectContourDefinitionIds = (
1742
+ ast: AstNode
1743
+ ): ReadonlySet<string> =>
1744
+ new Set(findContourDefinitions(ast).map((def) => def.name));
1745
+
1746
+ /**
1747
+ * Collect the `localBinding → contourName` map for `const foo = contour(...)`
1748
+ * declarations. Inline contour calls are intentionally excluded because they
1749
+ * have no local binding — use {@link collectContourDefinitionIds} when the
1750
+ * full set of declared names is required.
1751
+ */
1752
+ export const collectNamedContourIds = (
1753
+ ast: AstNode
1754
+ ): ReadonlyMap<string, string> => {
1755
+ const ids = new Map<string, string>();
1756
+
1757
+ for (const def of findContourDefinitions(ast)) {
1758
+ if (def.bindingName) {
1759
+ ids.set(def.bindingName, def.name);
1760
+ }
1761
+ }
1762
+
1763
+ return ids;
1764
+ };
1765
+
1766
+ const resolveNamedImportedName = (
1767
+ specifier: AstNode,
1768
+ localName: string
1769
+ ): string => {
1770
+ const { imported } = specifier as unknown as { imported?: AstNode };
1771
+ const importedName = imported
1772
+ ? (identifierName(imported) ?? extractStringLiteral(imported))
1773
+ : null;
1774
+ return importedName ?? localName;
1775
+ };
1776
+
1777
+ const extractImportSpecifierAlias = (
1778
+ specifier: AstNode
1779
+ ): { readonly localName: string; readonly importedName: string } | null => {
1780
+ if (
1781
+ specifier.type !== 'ImportSpecifier' &&
1782
+ specifier.type !== 'ImportDefaultSpecifier'
1783
+ ) {
1784
+ return null;
1785
+ }
1786
+
1787
+ const { local } = specifier as unknown as { local?: AstNode };
1788
+ const localName = identifierName(local);
1789
+ if (!localName) {
1790
+ return null;
1791
+ }
1792
+
1793
+ // Default imports bind the default export of the source module to the local
1794
+ // name. We cannot statically recover the exported name without cross-file
1795
+ // analysis, so the local name is the best identifier we have for resolving
1796
+ // against `knownContourIds`. Treat the alias as an identity mapping; the
1797
+ // downstream resolver will fall through to `knownContourIds` on the binding
1798
+ // name and report it as missing when not found.
1799
+ if (specifier.type === 'ImportDefaultSpecifier') {
1800
+ return { importedName: localName, localName };
1801
+ }
1802
+
1803
+ return {
1804
+ importedName: resolveNamedImportedName(specifier, localName),
1805
+ localName,
1806
+ };
1807
+ };
1808
+
1809
+ /**
1810
+ * Collect `import { foo as bar } from '...'` and `import bar from '...'`
1811
+ * specifier mappings keyed by local binding name. The value is the original
1812
+ * exported name for named imports. Default imports map to themselves because
1813
+ * the exported name cannot be recovered statically — callers should fall
1814
+ * through to `knownContourIds` membership on the local binding name.
1815
+ */
1816
+ export const collectImportAliasMap = (
1817
+ ast: AstNode
1818
+ ): ReadonlyMap<string, string> => {
1819
+ const aliases = new Map<string, string>();
1820
+
1821
+ walk(ast, (node) => {
1822
+ if (node.type !== 'ImportDeclaration') {
1823
+ return;
1824
+ }
1825
+
1826
+ const specifiers =
1827
+ (node['specifiers'] as readonly AstNode[] | undefined) ?? [];
1828
+ for (const specifier of specifiers) {
1829
+ const alias = extractImportSpecifierAlias(specifier);
1830
+ if (alias) {
1831
+ aliases.set(alias.localName, alias.importedName);
1832
+ }
1833
+ }
1834
+ });
1835
+
1836
+ return aliases;
1837
+ };
1838
+
1839
+ const addUserNamespaceBindingsFromDeclaration = (
1840
+ node: AstNode,
1841
+ into: Set<string>
1842
+ ): void => {
1843
+ if (isFrameworkNamespaceSource(getImportSourceValue(node))) {
1844
+ return;
1845
+ }
1846
+ const specifiers =
1847
+ (node['specifiers'] as readonly AstNode[] | undefined) ?? [];
1848
+ for (const specifier of specifiers) {
1849
+ if (specifier.type !== 'ImportNamespaceSpecifier') {
1850
+ continue;
1851
+ }
1852
+ const { local } = specifier as unknown as { local?: AstNode };
1853
+ const localName = identifierName(local);
1854
+ if (localName) {
1855
+ into.add(localName);
1856
+ }
1857
+ }
1858
+ };
1859
+
1860
+ /**
1861
+ * Collect local binding names introduced by `import * as <name> from '<src>'`
1862
+ * declarations whose source is NOT an `@ontrails/*` framework package. These
1863
+ * are user-defined namespace imports of contour modules (e.g. `import * as
1864
+ * contours from './contours'`), used to resolve `contours.user` member-access
1865
+ * references to contour ids.
1866
+ *
1867
+ * Framework namespace imports (`import * as core from '@ontrails/core'`) are
1868
+ * intentionally excluded — they carry framework primitives like
1869
+ * `core.contour(...)` and are resolved by {@link buildFrameworkNamespaceContext}.
1870
+ * Mixing them here would treat `core.contour` as a reference to a contour
1871
+ * named "contour", producing false positives.
1872
+ */
1873
+ export const collectUserNamespaceImportBindings = (
1874
+ ast: AstNode
1875
+ ): ReadonlySet<string> => {
1876
+ const bindings = new Set<string>();
1877
+
1878
+ walk(ast, (node) => {
1879
+ if (node.type !== 'ImportDeclaration') {
1880
+ return;
1881
+ }
1882
+ addUserNamespaceBindingsFromDeclaration(node, bindings);
1883
+ });
1884
+
1885
+ return bindings;
1886
+ };
1887
+
1888
+ /**
1889
+ * Resolution context for user-namespace member access like `contours.user`.
1890
+ * Bundles the set of local namespace-binding names (from `import * as x from
1891
+ * './contours'`) with an optional set of proven-safe `MemberExpression` start
1892
+ * offsets from a scope-aware pre-pass. When `safeMemberStarts` is present, a
1893
+ * member access only resolves to a user-namespace target if its start is in
1894
+ * the set — so a function-local shadow of the namespace import does not leak
1895
+ * through. When absent, the name-only gate is used as a
1896
+ * backward-compatible fallback for ad-hoc callers.
1897
+ */
1898
+ export interface UserNamespaceContext {
1899
+ readonly bindings: ReadonlySet<string>;
1900
+ readonly safeMemberStarts?: ReadonlySet<number>;
1901
+ }
1902
+
1903
+ /**
1904
+ * Walk the AST with a scope stack and collect `MemberExpression` start offsets
1905
+ * whose receiver is a user-namespace binding that is NOT shadowed by any
1906
+ * enclosing scope. Mirrors `collectFrameworkNamespacedCallStarts` for the
1907
+ * framework-namespace path so `contours.user` inside
1908
+ * `function f(contours) { ... }` is rejected as shadowed.
1909
+ */
1910
+ /**
1911
+ * Return the receiver-identifier name of a non-computed member access, or
1912
+ * `null` for any other node shape (computed access, non-member, etc.).
1913
+ */
1914
+ const getNonComputedMemberReceiver = (node: AstNode): string | null => {
1915
+ if (!isMemberAccessNonComputed(node)) {
1916
+ return null;
1917
+ }
1918
+ const { object } = node as unknown as { object?: AstNode };
1919
+ return object ? identifierName(object) : null;
1920
+ };
1921
+
1922
+ const collectUserNamespacedMemberStarts = (
1923
+ ast: AstNode,
1924
+ bindings: ReadonlySet<string>
1925
+ ): ReadonlySet<number> => {
1926
+ const starts = new Set<number>();
1927
+ if (bindings.size === 0) {
1928
+ return starts;
1929
+ }
1930
+
1931
+ walkWithScopes(ast, (node, scopes) => {
1932
+ const receiver = getNonComputedMemberReceiver(node);
1933
+ if (!receiver || !bindings.has(receiver) || isShadowed(receiver, scopes)) {
1934
+ return;
1935
+ }
1936
+ starts.add(node.start);
1937
+ });
1938
+
1939
+ return starts;
1940
+ };
1941
+
1942
+ /**
1943
+ * Build a {@link UserNamespaceContext} for `ast`, including the scope-aware
1944
+ * `safeMemberStarts` gate. Prefer this over bare
1945
+ * {@link collectUserNamespaceImportBindings} so member access like
1946
+ * `contours.user` is rejected when `contours` is shadowed by a local binding.
1947
+ */
1948
+ export const buildUserNamespaceContext = (
1949
+ ast: AstNode
1950
+ ): UserNamespaceContext => {
1951
+ const bindings = collectUserNamespaceImportBindings(ast);
1952
+ return {
1953
+ bindings,
1954
+ safeMemberStarts: collectUserNamespacedMemberStarts(ast, bindings),
1955
+ };
1956
+ };
1957
+
1958
+ export interface ContourReferenceSite {
1959
+ /** Field on the source contour that declares the reference. */
1960
+ readonly field: string;
1961
+ /** Source contour name. */
1962
+ readonly source: string;
1963
+ /** Start offset of the field declaration. */
1964
+ readonly start: number;
1965
+ /** Target contour name. */
1966
+ readonly target: string;
1967
+ }
1968
+
1969
+ /**
1970
+ * Read a property key or member access identifier.
1971
+ *
1972
+ * Returns the identifier name for `Identifier` keys, or the underlying
1973
+ * string literal value for computed access via `['name']` / `"name"`.
1974
+ */
1975
+ export const getPropertyName = (node: unknown): string | null => {
1976
+ if (typeof node !== 'object' || node === null) {
1977
+ return null;
1978
+ }
1979
+
1980
+ const { name } = node as { readonly name?: unknown };
1981
+ if (typeof name === 'string') {
1982
+ return name;
1983
+ }
1984
+
1985
+ return isAstNode(node) ? extractStringLiteral(node) : null;
1986
+ };
1987
+
1988
+ const stripContourSuffix = (name: string): string => {
1989
+ const suffix = 'Contour';
1990
+ return name.endsWith(suffix) ? name.slice(0, -suffix.length) : name;
1991
+ };
1992
+
1993
+ const resolveKnownContourName = (
1994
+ name: string,
1995
+ knownContourIds?: ReadonlySet<string>
1996
+ ): string | null => {
1997
+ if (knownContourIds?.has(name)) {
1998
+ return name;
1999
+ }
2000
+
2001
+ // Support the common `const userContour = contour('user', ...)` naming
2002
+ // pattern when callers refer to the binding name instead of the contour ID.
2003
+ // Exact matches always win; suffix stripping is a fallback only.
2004
+ const stripped = stripContourSuffix(name);
2005
+ if (stripped !== name && knownContourIds?.has(stripped)) {
2006
+ return stripped;
2007
+ }
2008
+
2009
+ return null;
2010
+ };
2011
+
2012
+ /**
2013
+ * Resolve a local binding name to a contour ID, honoring import aliases.
2014
+ *
2015
+ * Strategies, in order:
2016
+ * 1. Local `const foo = contour('name', ...)` binding → the contour name.
2017
+ * 2. `knownContourIds` membership on the binding name itself (or the
2018
+ * conventional `Contour` suffix strip).
2019
+ * 3. `import { foo as bar }` → use the original exported name `foo`
2020
+ * (and apply strategy 2 / suffix-stripping against it so aliased imports
2021
+ * resolve correctly). If the imported name still isn't recognized, the
2022
+ * imported name is returned so the caller can report it missing.
2023
+ *
2024
+ * Returns `null` only when the name belongs to no known resolution path —
2025
+ * no local binding, no known contour ID, no import, and no suffix match.
2026
+ * Returning `null` means "this identifier is not a contour reference we can
2027
+ * reason about" (e.g. a bare undeclared variable), as opposed to
2028
+ * "a contour reference whose target is missing".
2029
+ */
2030
+ export const deriveContourIdentifierName = (
2031
+ bindingName: string,
2032
+ namedContourIds: ReadonlyMap<string, string>,
2033
+ knownContourIds?: ReadonlySet<string>,
2034
+ importAliases?: ReadonlyMap<string, string>
2035
+ ): string | null => {
2036
+ const localName = namedContourIds.get(bindingName);
2037
+ if (localName) {
2038
+ return localName;
2039
+ }
2040
+
2041
+ const known = resolveKnownContourName(bindingName, knownContourIds);
2042
+ if (known) {
2043
+ return known;
2044
+ }
2045
+
2046
+ // If the binding came from an import, use the original exported name as
2047
+ // the resolution target. This lets `import { foo as bar }` resolve to
2048
+ // the exported `foo` rather than the local alias `bar`. If the imported
2049
+ // name still isn't recognized, return it so callers can report it as
2050
+ // missing under its original name.
2051
+ const importedName = importAliases?.get(bindingName);
2052
+ if (importedName) {
2053
+ return (
2054
+ resolveKnownContourName(importedName, knownContourIds) ?? importedName
2055
+ );
2056
+ }
2057
+
2058
+ return null;
2059
+ };
2060
+
2061
+ const getContourReferenceMember = (
2062
+ node: AstNode
2063
+ ): {
2064
+ readonly object?: AstNode;
2065
+ readonly property?: AstNode;
2066
+ readonly start: number;
2067
+ } | null => {
2068
+ if (
2069
+ node.type !== 'MemberExpression' &&
2070
+ node.type !== 'StaticMemberExpression'
2071
+ ) {
2072
+ return null;
2073
+ }
2074
+
2075
+ return node as unknown as {
2076
+ readonly object?: AstNode;
2077
+ readonly property?: AstNode;
2078
+ readonly start: number;
2079
+ };
2080
+ };
2081
+
2082
+ const asUserNamespaceContext = (
2083
+ input: ReadonlySet<string> | UserNamespaceContext | undefined
2084
+ ): UserNamespaceContext | undefined => {
2085
+ if (!input) {
2086
+ return undefined;
2087
+ }
2088
+ return input instanceof Set
2089
+ ? { bindings: input }
2090
+ : (input as UserNamespaceContext);
2091
+ };
2092
+
2093
+ /**
2094
+ * Resolve a user-namespace member access like `contours.user` to its contour
2095
+ * id. Returns the property name (e.g. `'user'`) when the receiver identifier
2096
+ * is a known user-defined namespace binding AND — when the caller provides a
2097
+ * {@link UserNamespaceContext} with `safeMemberStarts` — the member access
2098
+ * site is in that set (i.e. the receiver is not shadowed by any enclosing
2099
+ * scope). Otherwise returns `null`.
2100
+ *
2101
+ * The property name is taken as the contour id verbatim — we cannot statically
2102
+ * resolve what `contours.user` binds to without reading the other file, so we
2103
+ * treat the member name as the candidate target and let
2104
+ * {@link deriveContourIdentifierName}'s downstream `knownContourIds` check
2105
+ * report a missing target.
2106
+ */
2107
+ export const isUserNamespaceReceiverAllowed = (
2108
+ receiver: string,
2109
+ memberStart: number,
2110
+ ctx: UserNamespaceContext
2111
+ ): boolean => {
2112
+ if (!ctx.bindings.has(receiver)) {
2113
+ return false;
2114
+ }
2115
+ // Scope-aware gate: when the pre-pass produced a set, the member access
2116
+ // must appear in it. Without the set, fall back to the bare name check.
2117
+ return ctx.safeMemberStarts ? ctx.safeMemberStarts.has(memberStart) : true;
2118
+ };
2119
+
2120
+ const getContourReferenceTargetFromNamespaceMember = (
2121
+ member: {
2122
+ readonly object?: AstNode;
2123
+ readonly property?: AstNode;
2124
+ readonly start: number;
2125
+ },
2126
+ userNamespace?: ReadonlySet<string> | UserNamespaceContext
2127
+ ): string | null => {
2128
+ const ctx = asUserNamespaceContext(userNamespace);
2129
+ if (!ctx || ctx.bindings.size === 0) {
2130
+ return null;
2131
+ }
2132
+ const receiver = member.object ? identifierName(member.object) : null;
2133
+ if (
2134
+ !receiver ||
2135
+ !isUserNamespaceReceiverAllowed(receiver, member.start, ctx)
2136
+ ) {
2137
+ return null;
2138
+ }
2139
+ const { property } = member;
2140
+ if (!property || property.type !== 'Identifier') {
2141
+ return null;
2142
+ }
2143
+ return identifierName(property);
2144
+ };
2145
+
2146
+ const getContourReferenceTargetFromObject = (
2147
+ object: AstNode,
2148
+ namedContourIds: ReadonlyMap<string, string>,
2149
+ knownContourIds?: ReadonlySet<string>,
2150
+ importAliases?: ReadonlyMap<string, string>,
2151
+ context?: ReadonlySet<string> | FrameworkNamespaceContext,
2152
+ userNamespace?: ReadonlySet<string> | UserNamespaceContext
2153
+ ): string | null => {
2154
+ if (object.type === 'Identifier') {
2155
+ const bindingName = identifierName(object);
2156
+ return bindingName
2157
+ ? deriveContourIdentifierName(
2158
+ bindingName,
2159
+ namedContourIds,
2160
+ knownContourIds,
2161
+ importAliases
2162
+ )
2163
+ : null;
2164
+ }
2165
+
2166
+ const member = getContourReferenceMember(object);
2167
+ if (member) {
2168
+ const namespaceTarget = getContourReferenceTargetFromNamespaceMember(
2169
+ member,
2170
+ userNamespace
2171
+ );
2172
+ if (namespaceTarget) {
2173
+ return namespaceTarget;
2174
+ }
2175
+ }
2176
+
2177
+ return extractContourDefinition(object, context)?.name ?? null;
2178
+ };
2179
+
2180
+ const CONTOUR_ID_WRAPPER_METHODS = new Set([
2181
+ 'brand',
2182
+ 'catch',
2183
+ 'default',
2184
+ 'describe',
2185
+ 'meta',
2186
+ 'nullable',
2187
+ 'nullish',
2188
+ 'optional',
2189
+ 'readonly',
2190
+ ]);
2191
+
2192
+ const getContourIdCallMember = (
2193
+ node: AstNode
2194
+ ): {
2195
+ readonly member: NonNullable<ReturnType<typeof getContourReferenceMember>>;
2196
+ readonly propertyName: string;
2197
+ } | null => {
2198
+ const callee = node['callee'] as AstNode | undefined;
2199
+ const member = callee ? getContourReferenceMember(callee) : null;
2200
+ const propertyName = member ? identifierName(member.property) : null;
2201
+ return member && propertyName ? { member, propertyName } : null;
2202
+ };
2203
+
2204
+ const getContourIdCallObject = function getContourIdCallObject(
2205
+ node: AstNode | undefined
2206
+ ): AstNode | null {
2207
+ const current = node;
2208
+ if (!current || current.type !== 'CallExpression') {
2209
+ return null;
2210
+ }
2211
+
2212
+ const member = getContourIdCallMember(current);
2213
+ if (!member) {
2214
+ return null;
2215
+ }
2216
+ if (member.propertyName === 'id') {
2217
+ return member.member.object ?? null;
2218
+ }
2219
+
2220
+ return CONTOUR_ID_WRAPPER_METHODS.has(member.propertyName)
2221
+ ? getContourIdCallObject(member.member.object)
2222
+ : null;
2223
+ };
2224
+
2225
+ const extractContourReferenceTarget = (
2226
+ node: AstNode | undefined,
2227
+ namedContourIds: ReadonlyMap<string, string>,
2228
+ knownContourIds?: ReadonlySet<string>,
2229
+ importAliases?: ReadonlyMap<string, string>,
2230
+ context?: ReadonlySet<string> | FrameworkNamespaceContext,
2231
+ userNamespace?: ReadonlySet<string> | UserNamespaceContext
2232
+ ): string | null => {
2233
+ const object = getContourIdCallObject(node);
2234
+ return object
2235
+ ? getContourReferenceTargetFromObject(
2236
+ object,
2237
+ namedContourIds,
2238
+ knownContourIds,
2239
+ importAliases,
2240
+ context,
2241
+ userNamespace
2242
+ )
2243
+ : null;
2244
+ };
2245
+
2246
+ const getContourShapeProperties = (
2247
+ definition: ContourDefinition
2248
+ ): readonly AstNode[] =>
2249
+ (definition.shape['properties'] as readonly AstNode[] | undefined) ?? [];
2250
+
2251
+ const buildContourReferenceSite = (
2252
+ definition: ContourDefinition,
2253
+ property: AstNode,
2254
+ namedContourIds: ReadonlyMap<string, string>,
2255
+ knownContourIds?: ReadonlySet<string>,
2256
+ importAliases?: ReadonlyMap<string, string>,
2257
+ context?: ReadonlySet<string> | FrameworkNamespaceContext,
2258
+ userNamespace?: ReadonlySet<string> | UserNamespaceContext
2259
+ ): ContourReferenceSite | null => {
2260
+ if (property.type !== 'Property') {
2261
+ return null;
2262
+ }
2263
+
2264
+ const field = getPropertyName(property.key);
2265
+ const target = extractContourReferenceTarget(
2266
+ property.value as AstNode | undefined,
2267
+ namedContourIds,
2268
+ knownContourIds,
2269
+ importAliases,
2270
+ context,
2271
+ userNamespace
2272
+ );
2273
+ if (!field || !target) {
2274
+ return null;
2275
+ }
2276
+
2277
+ return {
2278
+ field,
2279
+ source: definition.name,
2280
+ start: property.start,
2281
+ target,
2282
+ };
2283
+ };
2284
+
2285
+ const findContourReferenceSitesForDefinition = (
2286
+ definition: ContourDefinition,
2287
+ namedContourIds: ReadonlyMap<string, string>,
2288
+ knownContourIds?: ReadonlySet<string>,
2289
+ importAliases?: ReadonlyMap<string, string>,
2290
+ context?: ReadonlySet<string> | FrameworkNamespaceContext,
2291
+ userNamespace?: ReadonlySet<string> | UserNamespaceContext
2292
+ ): readonly ContourReferenceSite[] =>
2293
+ getContourShapeProperties(definition).flatMap((property) => {
2294
+ const reference = buildContourReferenceSite(
2295
+ definition,
2296
+ property,
2297
+ namedContourIds,
2298
+ knownContourIds,
2299
+ importAliases,
2300
+ context,
2301
+ userNamespace
2302
+ );
2303
+ return reference ? [reference] : [];
2304
+ });
2305
+
2306
+ /** Collect all contour field references declared via `.id()` in a parsed file. */
2307
+ export const collectContourReferenceSites = (
2308
+ ast: AstNode,
2309
+ knownContourIds?: ReadonlySet<string>
2310
+ ): readonly ContourReferenceSite[] => {
2311
+ const namedContourIds = collectNamedContourIds(ast);
2312
+ const importAliases = collectImportAliasMap(ast);
2313
+ const userNamespace = buildUserNamespaceContext(ast);
2314
+ const context = buildFrameworkNamespaceContext(ast);
2315
+ return findContourDefinitions(ast, context).flatMap((definition) =>
2316
+ findContourReferenceSitesForDefinition(
2317
+ definition,
2318
+ namedContourIds,
2319
+ knownContourIds,
2320
+ importAliases,
2321
+ context,
2322
+ userNamespace
2323
+ )
2324
+ );
2325
+ };
2326
+
2327
+ /** Collect contour reference targets keyed by source contour name. */
2328
+ export const collectContourReferenceTargetsByName = (
2329
+ ast: AstNode,
2330
+ knownContourIds?: ReadonlySet<string>
2331
+ ): ReadonlyMap<string, readonly string[]> => {
2332
+ const targetsByName = new Map<string, Set<string>>();
2333
+
2334
+ for (const reference of collectContourReferenceSites(ast, knownContourIds)) {
2335
+ const existing = targetsByName.get(reference.source);
2336
+ if (existing) {
2337
+ existing.add(reference.target);
2338
+ continue;
2339
+ }
2340
+
2341
+ targetsByName.set(reference.source, new Set([reference.target]));
2342
+ }
2343
+
2344
+ return new Map(
2345
+ [...targetsByName.entries()].map(([name, targets]) => [name, [...targets]])
2346
+ );
2347
+ };
2348
+
2349
+ // ---------------------------------------------------------------------------
2350
+ // Blaze body extraction
2351
+ // ---------------------------------------------------------------------------
2352
+
2353
+ /**
2354
+ * Extract top-level `blaze:` property values from an ObjectExpression's direct properties.
2355
+ *
2356
+ * Does not recurse into nested objects, so `meta: { blaze: ... }` is ignored.
2357
+ */
2358
+ const extractBlazeFromConfig = (config: AstNode): AstNode[] => {
2359
+ const bodies: AstNode[] = [];
2360
+ const properties = config['properties'] as readonly AstNode[] | undefined;
2361
+ if (!properties) {
2362
+ return bodies;
2363
+ }
2364
+ for (const prop of properties) {
2365
+ if (
2366
+ prop.type === 'Property' &&
2367
+ prop.key?.name === 'blaze' &&
2368
+ isAstNode(prop.value)
2369
+ ) {
2370
+ bodies.push(prop.value);
2371
+ }
2372
+ }
2373
+ return bodies;
2374
+ };
2375
+
2376
+ /**
2377
+ * Find `blaze:` property values.
2378
+ *
2379
+ * When given an ObjectExpression (trail config), returns only its direct `blaze:`
2380
+ * properties. When given a full AST, finds trail definitions first and extracts
2381
+ * `blaze:` from each config — in both cases ignoring nested `blaze:` properties
2382
+ * (e.g. `meta: { blaze: ... }`).
2383
+ */
2384
+ export const findBlazeBodies = (node: AstNode): AstNode[] => {
2385
+ if (node.type === 'ObjectExpression') {
2386
+ return extractBlazeFromConfig(node);
2387
+ }
2388
+
2389
+ // Full AST — find trail definitions and extract blaze from their configs
420
2390
  const bodies: AstNode[] = [];
421
2391
  for (const def of findTrailDefinitions(node)) {
422
2392
  bodies.push(...extractBlazeFromConfig(def.config));
423
2393
  }
424
- return bodies;
2394
+ return bodies;
2395
+ };
2396
+
2397
+ /**
2398
+ * Collect all `signal('id', { ... })` / `signal({ id: 'x', ... })` definition IDs.
2399
+ *
2400
+ * Uses `findTrailDefinitions` under the hood — it already recognizes both
2401
+ * `trail` and `signal` call sites, distinguished by the `kind` field.
2402
+ */
2403
+ export const collectSignalDefinitionIds = (
2404
+ ast: AstNode
2405
+ ): ReadonlySet<string> => {
2406
+ const ids = new Set<string>();
2407
+ for (const def of findTrailDefinitions(ast)) {
2408
+ if (def.kind === 'signal') {
2409
+ ids.add(def.id);
2410
+ }
2411
+ }
2412
+ return ids;
2413
+ };
2414
+
2415
+ /** Collect `const foo = trail('id', ...)` bindings from a parsed file. */
2416
+ export const collectNamedTrailIds = (
2417
+ ast: AstNode
2418
+ ): ReadonlyMap<string, string> => {
2419
+ const ids = new Map<string, string>();
2420
+ const context = buildFrameworkNamespaceContext(ast);
2421
+
2422
+ walk(ast, (node) => {
2423
+ if (node.type !== 'VariableDeclarator') {
2424
+ return;
2425
+ }
2426
+
2427
+ const { id, init } = node as unknown as {
2428
+ readonly id?: AstNode;
2429
+ readonly init?: AstNode;
2430
+ };
2431
+ if (!init) {
2432
+ return;
2433
+ }
2434
+
2435
+ const def = extractTrailDefinition(init, context);
2436
+ const name = extractBindingName(id);
2437
+ if (def?.kind === 'trail' && name) {
2438
+ ids.set(name, def.id);
2439
+ }
2440
+ });
2441
+
2442
+ return ids;
2443
+ };
2444
+
2445
+ /** Extract the raw `crosses: [...]` array elements from a trail config. */
2446
+ export const getCrossElements = (config: AstNode): readonly AstNode[] => {
2447
+ const crossesProp = findConfigProperty(config, 'crosses');
2448
+ if (!crossesProp) {
2449
+ return [];
2450
+ }
2451
+
2452
+ const arrayNode = crossesProp.value;
2453
+ if (!arrayNode || (arrayNode as AstNode).type !== 'ArrayExpression') {
2454
+ return [];
2455
+ }
2456
+
2457
+ const elements = (arrayNode as AstNode)['elements'] as
2458
+ | readonly AstNode[]
2459
+ | undefined;
2460
+ return elements ?? [];
2461
+ };
2462
+
2463
+ /**
2464
+ * Resolve a single `crosses: [...]` element to its target trail ID.
2465
+ *
2466
+ * Handles string literals, identifier references (via `namedTrailIds` map or
2467
+ * `const NAME = '...'` resolution), and inline `trail(...)` call expressions.
2468
+ */
2469
+ export const deriveCrossElementId = (
2470
+ element: AstNode,
2471
+ sourceCode: string,
2472
+ namedTrailIds: ReadonlyMap<string, string>
2473
+ ): string | null => {
2474
+ if (isStringLiteral(element)) {
2475
+ return getStringValue(element);
2476
+ }
2477
+
2478
+ if (element.type === 'Identifier') {
2479
+ const name = identifierName(element);
2480
+ return name
2481
+ ? (namedTrailIds.get(name) ?? deriveConstString(name, sourceCode))
2482
+ : null;
2483
+ }
2484
+
2485
+ const inlineDef = extractTrailDefinition(element);
2486
+ return inlineDef?.kind === 'trail' ? inlineDef.id : null;
2487
+ };
2488
+
2489
+ /**
2490
+ * Collect all trail IDs referenced by a single trail definition's
2491
+ * `crosses: [...]` array, deduplicated.
2492
+ */
2493
+ export const extractDefinitionCrossTargetIds = (
2494
+ config: AstNode,
2495
+ sourceCode: string,
2496
+ namedTrailIds: ReadonlyMap<string, string>
2497
+ ): readonly string[] => [
2498
+ ...new Set(
2499
+ getCrossElements(config).flatMap((element) => {
2500
+ const id = deriveCrossElementId(element, sourceCode, namedTrailIds);
2501
+ return id ? [id] : [];
2502
+ })
2503
+ ),
2504
+ ];
2505
+
2506
+ /** Collect all trail IDs referenced by declared `crosses: [...]` arrays. */
2507
+ export const collectCrossTargetTrailIds = (
2508
+ ast: AstNode,
2509
+ sourceCode: string
2510
+ ): ReadonlySet<string> => {
2511
+ const ids = new Set<string>();
2512
+ const namedTrailIds = collectNamedTrailIds(ast);
2513
+
2514
+ for (const def of findTrailDefinitions(ast)) {
2515
+ if (def.kind !== 'trail') {
2516
+ continue;
2517
+ }
2518
+
2519
+ for (const id of extractDefinitionCrossTargetIds(
2520
+ def.config,
2521
+ sourceCode,
2522
+ namedTrailIds
2523
+ )) {
2524
+ ids.add(id);
2525
+ }
2526
+ }
2527
+
2528
+ return ids;
2529
+ };
2530
+
2531
+ const extractTrailIntent = (config: AstNode): 'destroy' | 'read' | 'write' => {
2532
+ const intentProp = findConfigProperty(config, 'intent');
2533
+ if (!intentProp || !isStringLiteral(intentProp.value as AstNode)) {
2534
+ return 'write';
2535
+ }
2536
+
2537
+ const value = getStringValue(intentProp.value as AstNode);
2538
+ return value === 'destroy' || value === 'read' ? value : 'write';
2539
+ };
2540
+
2541
+ /** Collect the normalized intent for every trail definition in a parsed file. */
2542
+ export const collectTrailIntentsById = (
2543
+ ast: AstNode
2544
+ ): ReadonlyMap<string, 'destroy' | 'read' | 'write'> => {
2545
+ const intents = new Map<string, 'destroy' | 'read' | 'write'>();
2546
+
2547
+ for (const def of findTrailDefinitions(ast)) {
2548
+ if (def.kind === 'trail') {
2549
+ intents.set(def.id, extractTrailIntent(def.config));
2550
+ }
2551
+ }
2552
+
2553
+ return intents;
2554
+ };
2555
+
2556
+ // ---------------------------------------------------------------------------
2557
+ // Store / factory pattern extraction
2558
+ // ---------------------------------------------------------------------------
2559
+
2560
+ export interface StoreTableDefinition {
2561
+ /** Table name declared inside store({ ... }). */
2562
+ readonly name: string;
2563
+ /**
2564
+ * Local binding name of the enclosing `store(...)` declaration, if the
2565
+ * `store(...)` call is bound to a `const`/`let`/`var` (e.g. `db` in
2566
+ * `const db = store({ ... })`). Null for anonymous stores.
2567
+ */
2568
+ readonly storeBinding: string | null;
2569
+ /**
2570
+ * Stable composite key for this table in the form `${storeBinding}:${name}`,
2571
+ * falling back to the bare `name` when the store is anonymous. Use this for
2572
+ * cross-rule / cross-file keying so two stores with the same table name
2573
+ * never collide.
2574
+ */
2575
+ readonly key: string;
2576
+ /** Start offset of the table property declaration. */
2577
+ readonly start: number;
2578
+ /** Whether the authored table opts into version tracking. */
2579
+ readonly versioned: boolean;
2580
+ }
2581
+
2582
+ /**
2583
+ * Build a composite key for a store table: `${storeBinding}:${tableName}`,
2584
+ * falling back to the bare `tableName` when the enclosing store has no local
2585
+ * binding. Centralized so rule keying stays stable.
2586
+ *
2587
+ * @remarks
2588
+ * The key is intentionally file-local (no module path prefix). Cross-file
2589
+ * aggregation in `ProjectContext` merges keys from all files, so two files
2590
+ * with `const db = store({ notes: ... })` both produce `db:notes` — this is
2591
+ * the desired behavior because the warden checks for *pattern completeness*
2592
+ * across the project and matching keys signals that the same logical table
2593
+ * is covered. If two genuinely different tables share a binding and name,
2594
+ * that is a code-level naming collision the developer should resolve.
2595
+ */
2596
+ export const makeStoreTableKey = (
2597
+ storeBinding: string | null,
2598
+ tableName: string
2599
+ ): string => (storeBinding ? `${storeBinding}:${tableName}` : tableName);
2600
+
2601
+ const isBooleanLiteral = (node: AstNode | undefined): boolean =>
2602
+ Boolean(
2603
+ node &&
2604
+ ((node.type === 'BooleanLiteral' &&
2605
+ (node as unknown as { value?: unknown }).value === true) ||
2606
+ (node.type === 'Literal' &&
2607
+ (node as unknown as { value?: unknown }).value === true))
2608
+ );
2609
+
2610
+ /**
2611
+ * Check if a node is a `CallExpression` to the identifier `name`.
2612
+ *
2613
+ * e.g. `isNamedCall(node, 'store')` matches `store({...})` but not
2614
+ * `someObj.store()` or `storeAlt()`.
2615
+ */
2616
+ export const isNamedCall = (node: AstNode | undefined, name: string): boolean =>
2617
+ !!node &&
2618
+ node.type === 'CallExpression' &&
2619
+ identifierName((node as unknown as { callee?: AstNode }).callee) === name;
2620
+
2621
+ /**
2622
+ * Narrow a member-expression node (`a.b` or `a['b']`) to its `object` /
2623
+ * `property` pair, returning `null` for anything else.
2624
+ */
2625
+ export const getMemberExpression = (
2626
+ node: AstNode | undefined
2627
+ ): { readonly object?: AstNode; readonly property?: AstNode } | null => {
2628
+ if (
2629
+ !node ||
2630
+ (node.type !== 'MemberExpression' && node.type !== 'StaticMemberExpression')
2631
+ ) {
2632
+ return null;
2633
+ }
2634
+
2635
+ return node as unknown as {
2636
+ readonly object?: AstNode;
2637
+ readonly property?: AstNode;
2638
+ };
2639
+ };
2640
+
2641
+ /**
2642
+ * Resolve a `<store>.tables.<name>` member expression to its store binding
2643
+ * and table name.
2644
+ *
2645
+ * Returns `null` for anything that isn't a two-level member access ending in
2646
+ * `.tables.<name>`. The store binding is the identifier of the object owning
2647
+ * `.tables` — typically the local binding from `const db = store(...)`.
2648
+ */
2649
+ export const extractStoreTableFromMember = (
2650
+ node: AstNode | undefined
2651
+ ): {
2652
+ readonly storeBinding: string | null;
2653
+ readonly tableName: string;
2654
+ } | null => {
2655
+ const member = getMemberExpression(node);
2656
+ const tableName = member ? getPropertyName(member.property) : null;
2657
+ const tablesMember = member ? getMemberExpression(member.object) : null;
2658
+ if (!tableName || !tablesMember) {
2659
+ return null;
2660
+ }
2661
+
2662
+ if (getPropertyName(tablesMember.property) !== 'tables') {
2663
+ return null;
2664
+ }
2665
+
2666
+ const storeBinding = identifierName(tablesMember.object) ?? null;
2667
+ return { storeBinding, tableName };
2668
+ };
2669
+
2670
+ /**
2671
+ * Back-compat shim for rule code that only needs the table name. Prefer
2672
+ * `extractStoreTableFromMember` so the caller can build a composite key.
2673
+ */
2674
+ export const extractStoreTableIdFromMember = (
2675
+ node: AstNode | undefined
2676
+ ): string | null => extractStoreTableFromMember(node)?.tableName ?? null;
2677
+
2678
+ /**
2679
+ * Collect `const foo = <store>.tables.<name>` bindings from a parsed file,
2680
+ * keyed by the local binding name. Values are the composite table key
2681
+ * (`${storeBinding}:${tableName}`) so callers can dedupe across stores that
2682
+ * share a table name.
2683
+ */
2684
+ export const collectNamedStoreTableIds = (
2685
+ ast: AstNode
2686
+ ): ReadonlyMap<string, string> => {
2687
+ const ids = new Map<string, string>();
2688
+
2689
+ walk(ast, (node) => {
2690
+ if (node.type !== 'VariableDeclarator') {
2691
+ return;
2692
+ }
2693
+
2694
+ const { id, init } = node as unknown as {
2695
+ readonly id?: AstNode;
2696
+ readonly init?: AstNode;
2697
+ };
2698
+ const name = extractBindingName(id);
2699
+ const table = extractStoreTableFromMember(init);
2700
+ if (name && table) {
2701
+ ids.set(name, makeStoreTableKey(table.storeBinding, table.tableName));
2702
+ }
2703
+ });
2704
+
2705
+ return ids;
2706
+ };
2707
+
2708
+ /**
2709
+ * Resolve an argument node to a composite store-table key
2710
+ * (`${storeBinding}:${tableName}` or bare `tableName` when anonymous).
2711
+ *
2712
+ * Handles the two authoring patterns:
2713
+ * - direct member access: `db.tables.notes`
2714
+ * - identifier reference: `const notesTable = db.tables.notes; crud(notesTable, …)`
2715
+ */
2716
+ export const deriveStoreTableId = (
2717
+ node: AstNode | undefined,
2718
+ namedStoreTableIds: ReadonlyMap<string, string>
2719
+ ): string | null => {
2720
+ if (!node) {
2721
+ return null;
2722
+ }
2723
+
2724
+ if (node.type === 'Identifier') {
2725
+ const name = identifierName(node);
2726
+ return name ? (namedStoreTableIds.get(name) ?? null) : null;
2727
+ }
2728
+
2729
+ const member = extractStoreTableFromMember(node);
2730
+ return member
2731
+ ? makeStoreTableKey(member.storeBinding, member.tableName)
2732
+ : null;
2733
+ };
2734
+
2735
+ const extractStoreTableDefinitions = (
2736
+ node: AstNode,
2737
+ storeBinding: string | null
2738
+ ): readonly StoreTableDefinition[] => {
2739
+ if (!isNamedCall(node, 'store')) {
2740
+ return [];
2741
+ }
2742
+
2743
+ const [tablesArg] = ((node as unknown as { arguments?: readonly AstNode[] })
2744
+ .arguments ?? []) as readonly AstNode[];
2745
+ if (!tablesArg || tablesArg.type !== 'ObjectExpression') {
2746
+ return [];
2747
+ }
2748
+
2749
+ const properties = tablesArg['properties'] as readonly AstNode[] | undefined;
2750
+ if (!properties) {
2751
+ return [];
2752
+ }
2753
+
2754
+ return properties.flatMap((property) => {
2755
+ if (property.type !== 'Property') {
2756
+ return [];
2757
+ }
2758
+
2759
+ const name = getPropertyName(property.key);
2760
+ const value = property.value as AstNode | undefined;
2761
+ if (!name || value?.type !== 'ObjectExpression') {
2762
+ return [];
2763
+ }
2764
+
2765
+ const versionedProp = findConfigProperty(value, 'versioned');
2766
+ return [
2767
+ {
2768
+ key: makeStoreTableKey(storeBinding, name),
2769
+ name,
2770
+ start: property.start,
2771
+ storeBinding,
2772
+ versioned: isBooleanLiteral(
2773
+ versionedProp?.value as AstNode | undefined
2774
+ ),
2775
+ },
2776
+ ];
2777
+ });
2778
+ };
2779
+
2780
+ export const findStoreTableDefinitions = (
2781
+ ast: AstNode
2782
+ ): readonly StoreTableDefinition[] => {
2783
+ const definitions: StoreTableDefinition[] = [];
2784
+ const seenStoreCalls = new WeakSet<AstNode>();
2785
+
2786
+ // First pass: bound stores (walk VariableDeclarators so we know the binding).
2787
+ walk(ast, (node) => {
2788
+ if (node.type !== 'VariableDeclarator') {
2789
+ return;
2790
+ }
2791
+
2792
+ const { id, init } = node as unknown as {
2793
+ readonly id?: AstNode;
2794
+ readonly init?: AstNode;
2795
+ };
2796
+ if (!init || !isNamedCall(init, 'store')) {
2797
+ return;
2798
+ }
2799
+
2800
+ seenStoreCalls.add(init);
2801
+ const storeBinding = extractBindingName(id);
2802
+ definitions.push(...extractStoreTableDefinitions(init, storeBinding));
2803
+ });
2804
+
2805
+ // Second pass: anonymous `store({...})` calls not bound to a variable
2806
+ // (e.g. an inline default export). Use the bare table name as the key.
2807
+ walk(ast, (node) => {
2808
+ if (!isNamedCall(node, 'store') || seenStoreCalls.has(node)) {
2809
+ return;
2810
+ }
2811
+ definitions.push(...extractStoreTableDefinitions(node, null));
2812
+ });
2813
+
2814
+ return definitions;
2815
+ };
2816
+
2817
+ export const collectVersionedStoreTableIds = (
2818
+ ast: AstNode
2819
+ ): ReadonlySet<string> =>
2820
+ new Set(
2821
+ findStoreTableDefinitions(ast).flatMap((definition) =>
2822
+ definition.versioned ? [definition.key] : []
2823
+ )
2824
+ );
2825
+
2826
+ export const collectCrudTableIds = (ast: AstNode): ReadonlySet<string> => {
2827
+ const ids = new Set<string>();
2828
+ const namedStoreTableIds = collectNamedStoreTableIds(ast);
2829
+
2830
+ walk(ast, (node) => {
2831
+ if (!isNamedCall(node, 'crud')) {
2832
+ return;
2833
+ }
2834
+
2835
+ const [tableArg] = ((node as unknown as { arguments?: readonly AstNode[] })
2836
+ .arguments ?? []) as readonly AstNode[];
2837
+ const tableId = deriveStoreTableId(tableArg, namedStoreTableIds);
2838
+ if (tableId) {
2839
+ ids.add(tableId);
2840
+ }
2841
+ });
2842
+
2843
+ return ids;
2844
+ };
2845
+
2846
+ export const collectReconcileTableIds = (ast: AstNode): ReadonlySet<string> => {
2847
+ const ids = new Set<string>();
2848
+ const namedStoreTableIds = collectNamedStoreTableIds(ast);
2849
+
2850
+ walk(ast, (node) => {
2851
+ if (!isNamedCall(node, 'reconcile')) {
2852
+ return;
2853
+ }
2854
+
2855
+ const [configArg] = ((
2856
+ node as unknown as {
2857
+ arguments?: readonly AstNode[];
2858
+ }
2859
+ ).arguments ?? []) as readonly AstNode[];
2860
+ if (!configArg || configArg.type !== 'ObjectExpression') {
2861
+ return;
2862
+ }
2863
+
2864
+ const tableProp = findConfigProperty(configArg, 'table');
2865
+ const tableId = deriveStoreTableId(
2866
+ tableProp?.value as AstNode | undefined,
2867
+ namedStoreTableIds
2868
+ );
2869
+ if (tableId) {
2870
+ ids.add(tableId);
2871
+ }
2872
+ });
2873
+
2874
+ return ids;
2875
+ };
2876
+
2877
+ const STORE_SIGNAL_OPERATIONS = new Set(['created', 'removed', 'updated']);
2878
+
2879
+ const extractStoreSignalIdFromMember = (
2880
+ node: AstNode | undefined,
2881
+ namedStoreTableIds: ReadonlyMap<string, string>
2882
+ ): string | null => {
2883
+ const member = getMemberExpression(node);
2884
+ const operation = member ? getPropertyName(member.property) : null;
2885
+ if (!operation || !STORE_SIGNAL_OPERATIONS.has(operation)) {
2886
+ return null;
2887
+ }
2888
+
2889
+ const signalsMember = member ? getMemberExpression(member.object) : null;
2890
+ if (!signalsMember || getPropertyName(signalsMember.property) !== 'signals') {
2891
+ return null;
2892
+ }
2893
+
2894
+ const tableId = deriveStoreTableId(signalsMember.object, namedStoreTableIds);
2895
+ return tableId ? `${tableId}.${operation}` : null;
2896
+ };
2897
+
2898
+ const collectNamedStoreSignalIds = (
2899
+ ast: AstNode,
2900
+ namedStoreTableIds: ReadonlyMap<string, string>
2901
+ ): ReadonlyMap<string, string> => {
2902
+ const ids = new Map<string, string>();
2903
+
2904
+ walk(ast, (node) => {
2905
+ if (node.type !== 'VariableDeclarator') {
2906
+ return;
2907
+ }
2908
+
2909
+ const { id, init } = node as unknown as {
2910
+ readonly id?: AstNode;
2911
+ readonly init?: AstNode;
2912
+ };
2913
+ const name = extractBindingName(id);
2914
+ const signalId = extractStoreSignalIdFromMember(init, namedStoreTableIds);
2915
+ if (name && signalId) {
2916
+ ids.set(name, signalId);
2917
+ }
2918
+ });
2919
+
2920
+ return ids;
2921
+ };
2922
+
2923
+ const getOnElements = (config: AstNode): readonly AstNode[] => {
2924
+ const onProp = findConfigProperty(config, 'on');
2925
+ if (!onProp) {
2926
+ return [];
2927
+ }
2928
+
2929
+ const arrayNode = onProp.value;
2930
+ if (!arrayNode || (arrayNode as AstNode).type !== 'ArrayExpression') {
2931
+ return [];
2932
+ }
2933
+
2934
+ const elements = (arrayNode as AstNode)['elements'] as
2935
+ | readonly AstNode[]
2936
+ | undefined;
2937
+ return elements ?? [];
2938
+ };
2939
+
2940
+ const resolveNamedOnSignalId = (
2941
+ element: AstNode,
2942
+ sourceCode: string,
2943
+ namedStoreSignalIds: ReadonlyMap<string, string>
2944
+ ): string | null => {
2945
+ if (element.type !== 'Identifier') {
2946
+ return null;
2947
+ }
2948
+
2949
+ const name = identifierName(element);
2950
+ return name
2951
+ ? (namedStoreSignalIds.get(name) ?? deriveConstString(name, sourceCode))
2952
+ : null;
2953
+ };
2954
+
2955
+ const resolveInlineOnSignalId = (element: AstNode): string | null => {
2956
+ const definition = extractTrailDefinition(element);
2957
+ return definition?.kind === 'signal' ? definition.id : null;
2958
+ };
2959
+
2960
+ const resolveOnElementSignalId = (
2961
+ element: AstNode,
2962
+ sourceCode: string,
2963
+ namedStoreSignalIds: ReadonlyMap<string, string>,
2964
+ namedStoreTableIds: ReadonlyMap<string, string>
2965
+ ): string | null => {
2966
+ if (isStringLiteral(element)) {
2967
+ return getStringValue(element);
2968
+ }
2969
+
2970
+ return (
2971
+ extractStoreSignalIdFromMember(element, namedStoreTableIds) ??
2972
+ resolveNamedOnSignalId(element, sourceCode, namedStoreSignalIds) ??
2973
+ resolveInlineOnSignalId(element)
2974
+ );
2975
+ };
2976
+
2977
+ const addOnTargetSignalIds = (
2978
+ config: AstNode,
2979
+ ids: Set<string>,
2980
+ sourceCode: string,
2981
+ namedStoreSignalIds: ReadonlyMap<string, string>,
2982
+ namedStoreTableIds: ReadonlyMap<string, string>
2983
+ ): void => {
2984
+ for (const element of getOnElements(config)) {
2985
+ const signalId = resolveOnElementSignalId(
2986
+ element,
2987
+ sourceCode,
2988
+ namedStoreSignalIds,
2989
+ namedStoreTableIds
2990
+ );
2991
+ if (signalId) {
2992
+ ids.add(signalId);
2993
+ }
2994
+ }
2995
+ };
2996
+
2997
+ export const collectOnTargetSignalIds = (
2998
+ ast: AstNode,
2999
+ sourceCode: string
3000
+ ): ReadonlySet<string> => {
3001
+ const ids = new Set<string>();
3002
+ const namedStoreTableIds = collectNamedStoreTableIds(ast);
3003
+ const namedStoreSignalIds = collectNamedStoreSignalIds(
3004
+ ast,
3005
+ namedStoreTableIds
3006
+ );
3007
+
3008
+ for (const definition of findTrailDefinitions(ast)) {
3009
+ if (definition.kind === 'trail') {
3010
+ addOnTargetSignalIds(
3011
+ definition.config,
3012
+ ids,
3013
+ sourceCode,
3014
+ namedStoreSignalIds,
3015
+ namedStoreTableIds
3016
+ );
3017
+ }
3018
+ }
3019
+
3020
+ return ids;
425
3021
  };
426
3022
 
427
3023
  // ---------------------------------------------------------------------------