@whaaaley/crush 0.0.0-stage → 0.2.6

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 (358) hide show
  1. package/LICENSE +32 -0
  2. package/README.md +185 -2
  3. package/SKILL.md +187 -0
  4. package/THIRD_PARTY_NOTICES.md +277 -0
  5. package/dist/capture/capture.operations.d.ts +23 -0
  6. package/dist/capture/capture.operations.js +231 -0
  7. package/dist/capture/capture.schema.d.ts +73 -0
  8. package/dist/capture/capture.schema.js +28 -0
  9. package/dist/collapse/collapse.operations.d.ts +2 -0
  10. package/dist/collapse/collapse.operations.js +82 -0
  11. package/dist/collapse/collapse.schema.d.ts +40 -0
  12. package/dist/collapse/collapse.schema.js +35 -0
  13. package/dist/crush.tables.d.ts +3583 -0
  14. package/dist/crush.tables.js +456 -0
  15. package/dist/crushes/callback.crush.d.ts +2 -0
  16. package/dist/crushes/callback.crush.js +19 -0
  17. package/dist/crushes/crush.d.ts +36 -0
  18. package/dist/crushes/crush.js +12 -0
  19. package/dist/crushes/crush.names.d.ts +1 -0
  20. package/dist/crushes/crush.names.js +2 -0
  21. package/dist/crushes/crushes.d.ts +2 -0
  22. package/dist/crushes/crushes.js +9 -0
  23. package/dist/crushes/default-value.crush.d.ts +2 -0
  24. package/dist/crushes/default-value.crush.js +29 -0
  25. package/dist/crushes/drop-operand.crush.d.ts +2 -0
  26. package/dist/crushes/drop-operand.crush.js +37 -0
  27. package/dist/crushes/fold.d.ts +64 -0
  28. package/dist/crushes/fold.js +366 -0
  29. package/dist/crushes/force-fork.crush.d.ts +2 -0
  30. package/dist/crushes/force-fork.crush.js +79 -0
  31. package/dist/crushes/loop-once.crush.d.ts +2 -0
  32. package/dist/crushes/loop-once.crush.js +71 -0
  33. package/dist/crushes/optional-chain.crush.d.ts +2 -0
  34. package/dist/crushes/optional-chain.crush.js +30 -0
  35. package/dist/crushes/switch-case.crush.d.ts +2 -0
  36. package/dist/crushes/switch-case.crush.js +19 -0
  37. package/dist/decisions/decide.d.ts +61 -0
  38. package/dist/decisions/decide.js +161 -0
  39. package/dist/decisions/decisions.d.ts +24 -0
  40. package/dist/decisions/decisions.js +62 -0
  41. package/dist/decisions/forks.decision.d.ts +4 -0
  42. package/dist/decisions/forks.decision.js +32 -0
  43. package/dist/decisions/kinds.d.ts +101 -0
  44. package/dist/decisions/kinds.js +38 -0
  45. package/dist/decisions/loops.decision.d.ts +4 -0
  46. package/dist/decisions/loops.decision.js +39 -0
  47. package/dist/decisions/shorts.decision.d.ts +3 -0
  48. package/dist/decisions/shorts.decision.js +66 -0
  49. package/dist/decisions/switches.decision.d.ts +3 -0
  50. package/dist/decisions/switches.decision.js +20 -0
  51. package/dist/decisions/throws.decision.d.ts +3 -0
  52. package/dist/decisions/throws.decision.js +35 -0
  53. package/dist/decisions/values.decision.d.ts +3 -0
  54. package/dist/decisions/values.decision.js +67 -0
  55. package/dist/explore/edges/explore.d.ts +60 -0
  56. package/dist/explore/edges/explore.db.d.ts +22 -0
  57. package/dist/explore/edges/explore.db.js +55 -0
  58. package/dist/explore/edges/explore.js +305 -0
  59. package/dist/explore/edges/random.d.ts +2 -0
  60. package/dist/explore/edges/random.js +32 -0
  61. package/dist/explore/explore.operations.d.ts +30 -0
  62. package/dist/explore/explore.operations.js +117 -0
  63. package/dist/explore/limits.operations.d.ts +14 -0
  64. package/dist/explore/limits.operations.js +20 -0
  65. package/dist/explore/targets.operations.d.ts +36 -0
  66. package/dist/explore/targets.operations.js +32 -0
  67. package/dist/explore/utils/draws.d.ts +6 -0
  68. package/dist/explore/utils/draws.js +2 -0
  69. package/dist/explore/utils/edits.d.ts +15 -0
  70. package/dist/explore/utils/edits.js +151 -0
  71. package/dist/explore/utils/guide.d.ts +23 -0
  72. package/dist/explore/utils/guide.js +35 -0
  73. package/dist/explore/utils/keep.d.ts +10 -0
  74. package/dist/explore/utils/keep.js +61 -0
  75. package/dist/explore/utils/pools.d.ts +17 -0
  76. package/dist/explore/utils/pools.js +121 -0
  77. package/dist/explore/utils/reads.d.ts +19 -0
  78. package/dist/explore/utils/reads.js +118 -0
  79. package/dist/explore/utils/twin.d.ts +21 -0
  80. package/dist/explore/utils/twin.js +55 -0
  81. package/dist/forks/forks.operations.d.ts +32 -0
  82. package/dist/forks/forks.operations.js +134 -0
  83. package/dist/forks/forks.schema.d.ts +20 -0
  84. package/dist/forks/forks.schema.js +14 -0
  85. package/dist/graph/cfg/cfg.d.ts +28 -0
  86. package/dist/graph/cfg/cfg.js +31 -0
  87. package/dist/graph/cfg/code-paths/analyzer.d.ts +27 -0
  88. package/dist/graph/cfg/code-paths/analyzer.js +358 -0
  89. package/dist/graph/cfg/code-paths/segments.d.ts +44 -0
  90. package/dist/graph/cfg/code-paths/segments.js +105 -0
  91. package/dist/graph/cfg/code-paths/state.d.ts +121 -0
  92. package/dist/graph/cfg/code-paths/state.js +611 -0
  93. package/dist/graph/cfg/entries.d.ts +13 -0
  94. package/dist/graph/cfg/entries.js +120 -0
  95. package/dist/graph/cfg/functions.d.ts +6 -0
  96. package/dist/graph/cfg/functions.js +47 -0
  97. package/dist/graph/cfg/paths.d.ts +27 -0
  98. package/dist/graph/cfg/paths.js +91 -0
  99. package/dist/graph/cfg/scopes/scopes.d.ts +40 -0
  100. package/dist/graph/cfg/scopes/scopes.js +375 -0
  101. package/dist/graph/symbols/file.d.ts +90 -0
  102. package/dist/graph/symbols/file.js +427 -0
  103. package/dist/graph/symbols/keys.d.ts +13 -0
  104. package/dist/graph/symbols/keys.js +22 -0
  105. package/dist/graph/symbols/resolve.operations.d.ts +4 -0
  106. package/dist/graph/symbols/resolve.operations.js +33 -0
  107. package/dist/graph/symbols/symbols.d.ts +12 -0
  108. package/dist/graph/symbols/symbols.js +155 -0
  109. package/dist/graph/symbols/symbols.schema.d.ts +32 -0
  110. package/dist/graph/symbols/symbols.schema.js +19 -0
  111. package/dist/graph/unused/tests.d.ts +7 -0
  112. package/dist/graph/unused/tests.js +95 -0
  113. package/dist/graph/unused/unused.d.ts +53 -0
  114. package/dist/graph/unused/unused.js +319 -0
  115. package/dist/graph/walk/walk.d.ts +7 -0
  116. package/dist/graph/walk/walk.js +39 -0
  117. package/dist/instrument/instrument.operations.d.ts +72 -0
  118. package/dist/instrument/instrument.operations.js +786 -0
  119. package/dist/instrument/instrument.schema.d.ts +257 -0
  120. package/dist/instrument/instrument.schema.js +100 -0
  121. package/dist/judge/counters.d.ts +86 -0
  122. package/dist/judge/counters.js +402 -0
  123. package/dist/judge/edges/api.d.ts +14 -0
  124. package/dist/judge/edges/api.js +80 -0
  125. package/dist/judge/edges/completion.reporter.d.ts +9 -0
  126. package/dist/judge/edges/completion.reporter.js +51 -0
  127. package/dist/judge/edges/hints.reporter.d.ts +13 -0
  128. package/dist/judge/edges/hints.reporter.js +29 -0
  129. package/dist/judge/edges/judge.edges.d.ts +52 -0
  130. package/dist/judge/edges/judge.edges.js +59 -0
  131. package/dist/judge/edges/judge.fs.d.ts +8 -0
  132. package/dist/judge/edges/judge.fs.js +45 -0
  133. package/dist/judge/edges/merge.d.ts +11 -0
  134. package/dist/judge/edges/merge.js +60 -0
  135. package/dist/judge/edges/plugin.d.ts +132 -0
  136. package/dist/judge/edges/plugin.js +441 -0
  137. package/dist/judge/edges/reach.d.ts +68 -0
  138. package/dist/judge/edges/reach.js +132 -0
  139. package/dist/judge/edges/speed.d.ts +32 -0
  140. package/dist/judge/edges/speed.js +34 -0
  141. package/dist/judge/edges/strays.d.ts +1 -0
  142. package/dist/judge/edges/strays.js +14 -0
  143. package/dist/judge/edges/vitest.d.ts +20 -0
  144. package/dist/judge/edges/vitest.js +36 -0
  145. package/dist/judge/edges/vitest.schema.d.ts +14 -0
  146. package/dist/judge/edges/vitest.schema.js +16 -0
  147. package/dist/judge/explore.test.d.ts +1 -0
  148. package/dist/judge/explore.test.js +101 -0
  149. package/dist/judge/hints.d.ts +11 -0
  150. package/dist/judge/hints.js +44 -0
  151. package/dist/judge/judge.operations.d.ts +218 -0
  152. package/dist/judge/judge.operations.js +464 -0
  153. package/dist/judge/judge.schema.d.ts +135 -0
  154. package/dist/judge/judge.schema.js +129 -0
  155. package/dist/judge/judge.shard-1.test.d.ts +1 -0
  156. package/dist/judge/judge.shard-1.test.js +3 -0
  157. package/dist/judge/judge.shard-2.test.d.ts +1 -0
  158. package/dist/judge/judge.shard-2.test.js +3 -0
  159. package/dist/judge/judge.test.d.ts +1 -0
  160. package/dist/judge/judge.test.js +3 -0
  161. package/dist/judge/refusals.d.ts +14 -0
  162. package/dist/judge/refusals.js +10 -0
  163. package/dist/judge/report.test.d.ts +1 -0
  164. package/dist/judge/report.test.js +432 -0
  165. package/dist/judge/runner.d.ts +3 -0
  166. package/dist/judge/runner.js +1602 -0
  167. package/dist/judge/stresses.test.d.ts +1 -0
  168. package/dist/judge/stresses.test.js +12 -0
  169. package/dist/judge/task-meta.d.ts +51 -0
  170. package/dist/judge/task-meta.js +3 -0
  171. package/dist/justify/edges/load.d.ts +7 -0
  172. package/dist/justify/edges/load.js +40 -0
  173. package/dist/justify/edges/refresh.fs.d.ts +8 -0
  174. package/dist/justify/edges/refresh.fs.js +15 -0
  175. package/dist/justify/justify.operations.d.ts +46 -0
  176. package/dist/justify/justify.operations.js +80 -0
  177. package/dist/justify/justify.schema.d.ts +21 -0
  178. package/dist/justify/justify.schema.js +11 -0
  179. package/dist/mutators/arithmetic-operator.mutator.d.ts +2 -0
  180. package/dist/mutators/arithmetic-operator.mutator.js +22 -0
  181. package/dist/mutators/array-declaration.mutator.d.ts +2 -0
  182. package/dist/mutators/array-declaration.mutator.js +12 -0
  183. package/dist/mutators/arrow-function.mutator.d.ts +2 -0
  184. package/dist/mutators/arrow-function.mutator.js +10 -0
  185. package/dist/mutators/assignment-operator.mutator.d.ts +2 -0
  186. package/dist/mutators/assignment-operator.mutator.js +29 -0
  187. package/dist/mutators/await-expression.mutator.d.ts +2 -0
  188. package/dist/mutators/await-expression.mutator.js +7 -0
  189. package/dist/mutators/bitwise-operator.mutator.d.ts +2 -0
  190. package/dist/mutators/bitwise-operator.mutator.js +19 -0
  191. package/dist/mutators/block-statement.mutator.d.ts +2 -0
  192. package/dist/mutators/block-statement.mutator.js +30 -0
  193. package/dist/mutators/boolean-literal.mutator.d.ts +2 -0
  194. package/dist/mutators/boolean-literal.mutator.js +5 -0
  195. package/dist/mutators/coercion.mutator.d.ts +2 -0
  196. package/dist/mutators/coercion.mutator.js +28 -0
  197. package/dist/mutators/conditional-expression.mutator.d.ts +2 -0
  198. package/dist/mutators/conditional-expression.mutator.js +36 -0
  199. package/dist/mutators/disposal.mutator.d.ts +2 -0
  200. package/dist/mutators/disposal.mutator.js +13 -0
  201. package/dist/mutators/empty-destructuring.mutator.d.ts +2 -0
  202. package/dist/mutators/empty-destructuring.mutator.js +35 -0
  203. package/dist/mutators/empty-return.mutator.d.ts +2 -0
  204. package/dist/mutators/empty-return.mutator.js +35 -0
  205. package/dist/mutators/equality-operator.mutator.d.ts +2 -0
  206. package/dist/mutators/equality-operator.mutator.js +16 -0
  207. package/dist/mutators/loop-exit.mutator.d.ts +2 -0
  208. package/dist/mutators/loop-exit.mutator.js +5 -0
  209. package/dist/mutators/method-expression.mutator.d.ts +2 -0
  210. package/dist/mutators/method-expression.mutator.js +59 -0
  211. package/dist/mutators/missing-lookup.mutator.d.ts +2 -0
  212. package/dist/mutators/missing-lookup.mutator.js +25 -0
  213. package/dist/mutators/mutator.d.ts +40 -0
  214. package/dist/mutators/mutator.js +49 -0
  215. package/dist/mutators/mutator.names.d.ts +3 -0
  216. package/dist/mutators/mutator.names.js +43 -0
  217. package/dist/mutators/mutators.d.ts +8 -0
  218. package/dist/mutators/mutators.js +84 -0
  219. package/dist/mutators/nested-condition.mutator.d.ts +2 -0
  220. package/dist/mutators/nested-condition.mutator.js +16 -0
  221. package/dist/mutators/number-literal.mutator.d.ts +3 -0
  222. package/dist/mutators/number-literal.mutator.js +13 -0
  223. package/dist/mutators/object-literal.mutator.d.ts +2 -0
  224. package/dist/mutators/object-literal.mutator.js +6 -0
  225. package/dist/mutators/odd-number.mutator.d.ts +2 -0
  226. package/dist/mutators/odd-number.mutator.js +15 -0
  227. package/dist/mutators/operand-deletion.mutator.d.ts +2 -0
  228. package/dist/mutators/operand-deletion.mutator.js +9 -0
  229. package/dist/mutators/outcome.mutator.d.ts +3 -0
  230. package/dist/mutators/outcome.mutator.js +45 -0
  231. package/dist/mutators/plain-assignment.mutator.d.ts +2 -0
  232. package/dist/mutators/plain-assignment.mutator.js +7 -0
  233. package/dist/mutators/power-operator.mutator.d.ts +2 -0
  234. package/dist/mutators/power-operator.mutator.js +13 -0
  235. package/dist/mutators/private-brand.mutator.d.ts +2 -0
  236. package/dist/mutators/private-brand.mutator.js +7 -0
  237. package/dist/mutators/regex.mutator.d.ts +2 -0
  238. package/dist/mutators/regex.mutator.js +29 -0
  239. package/dist/mutators/short-iteration.mutator.d.ts +2 -0
  240. package/dist/mutators/short-iteration.mutator.js +8 -0
  241. package/dist/mutators/string-literal.mutator.d.ts +3 -0
  242. package/dist/mutators/string-literal.mutator.js +149 -0
  243. package/dist/mutators/switch-default.mutator.d.ts +2 -0
  244. package/dist/mutators/switch-default.mutator.js +13 -0
  245. package/dist/mutators/sync-iteration.mutator.d.ts +3 -0
  246. package/dist/mutators/sync-iteration.mutator.js +9 -0
  247. package/dist/mutators/thrown-call.mutator.d.ts +2 -0
  248. package/dist/mutators/thrown-call.mutator.js +7 -0
  249. package/dist/mutators/unary-operator.mutator.d.ts +2 -0
  250. package/dist/mutators/unary-operator.mutator.js +18 -0
  251. package/dist/mutators/unbound-call.mutator.d.ts +2 -0
  252. package/dist/mutators/unbound-call.mutator.js +14 -0
  253. package/dist/mutators/update-operator.mutator.d.ts +2 -0
  254. package/dist/mutators/update-operator.mutator.js +15 -0
  255. package/dist/mutators/yield-value.mutator.d.ts +2 -0
  256. package/dist/mutators/yield-value.mutator.js +7 -0
  257. package/dist/parity/compare.d.ts +27 -0
  258. package/dist/parity/compare.js +26 -0
  259. package/dist/parity/corpus.parity.d.ts +1 -0
  260. package/dist/parity/corpus.parity.js +97 -0
  261. package/dist/parity/edges/corpus.fs.d.ts +3 -0
  262. package/dist/parity/edges/corpus.fs.js +30 -0
  263. package/dist/parity/edges/ports.d.ts +2 -0
  264. package/dist/parity/edges/ports.js +21 -0
  265. package/dist/parity/edges/vitest.d.ts +2 -0
  266. package/dist/parity/edges/vitest.js +3 -0
  267. package/dist/parity/parity.operations.d.ts +33 -0
  268. package/dist/parity/parity.operations.js +60 -0
  269. package/dist/parity/parity.schema.d.ts +5 -0
  270. package/dist/parity/parity.schema.js +6 -0
  271. package/dist/parity/stryker/compare.d.ts +14 -0
  272. package/dist/parity/stryker/compare.js +52 -0
  273. package/dist/parity/stryker/edges/ports.d.ts +12 -0
  274. package/dist/parity/stryker/edges/ports.js +28 -0
  275. package/dist/parity/stryker/equivalence.d.ts +1 -0
  276. package/dist/parity/stryker/equivalence.js +42 -0
  277. package/dist/parity/stryker/intended.d.ts +29 -0
  278. package/dist/parity/stryker/intended.js +152 -0
  279. package/dist/parity/stryker/normalize.d.ts +27 -0
  280. package/dist/parity/stryker/normalize.js +181 -0
  281. package/dist/parity/stryker/rules.d.ts +6 -0
  282. package/dist/parity/stryker/rules.js +116 -0
  283. package/dist/parity/stryker/sides.d.ts +46 -0
  284. package/dist/parity/stryker/sides.js +199 -0
  285. package/dist/parity/stryker/stryker.parity.d.ts +1 -0
  286. package/dist/parity/stryker/stryker.parity.js +147 -0
  287. package/dist/parity/stryker/stryker.schema.d.ts +6 -0
  288. package/dist/parity/stryker/stryker.schema.js +7 -0
  289. package/dist/parity/views.d.ts +21 -0
  290. package/dist/parity/views.js +119 -0
  291. package/dist/questions/questions.operations.d.ts +110 -0
  292. package/dist/questions/questions.operations.js +446 -0
  293. package/dist/questions/questions.schema.d.ts +57 -0
  294. package/dist/questions/questions.schema.js +28 -0
  295. package/dist/record/edges/calls.db.d.ts +10 -0
  296. package/dist/record/edges/calls.db.js +91 -0
  297. package/dist/record/edges/hits.db.d.ts +3 -0
  298. package/dist/record/edges/hits.db.js +5 -0
  299. package/dist/record/edges/holds.db.d.ts +4 -0
  300. package/dist/record/edges/holds.db.js +31 -0
  301. package/dist/record/edges/judged-state.db.d.ts +5 -0
  302. package/dist/record/edges/judged-state.db.js +25 -0
  303. package/dist/record/edges/markers.db.d.ts +11 -0
  304. package/dist/record/edges/markers.db.js +55 -0
  305. package/dist/record/edges/merge.db.d.ts +7 -0
  306. package/dist/record/edges/merge.db.js +46 -0
  307. package/dist/record/edges/parts.db.d.ts +42 -0
  308. package/dist/record/edges/parts.db.js +389 -0
  309. package/dist/record/edges/parts.edges.d.ts +2 -0
  310. package/dist/record/edges/parts.edges.js +7 -0
  311. package/dist/record/edges/parts.fs.d.ts +1 -0
  312. package/dist/record/edges/parts.fs.js +11 -0
  313. package/dist/record/edges/parts.writer.d.ts +1 -0
  314. package/dist/record/edges/parts.writer.js +85 -0
  315. package/dist/record/edges/passes.db.d.ts +19 -0
  316. package/dist/record/edges/passes.db.js +31 -0
  317. package/dist/record/edges/plan.db.d.ts +21 -0
  318. package/dist/record/edges/plan.db.js +174 -0
  319. package/dist/record/edges/reach.db.d.ts +6 -0
  320. package/dist/record/edges/reach.db.js +62 -0
  321. package/dist/record/edges/record.db.d.ts +22 -0
  322. package/dist/record/edges/record.db.js +176 -0
  323. package/dist/record/edges/record.fs.d.ts +3 -0
  324. package/dist/record/edges/record.fs.js +7 -0
  325. package/dist/record/edges/site-verdicts.db.d.ts +11 -0
  326. package/dist/record/edges/site-verdicts.db.js +66 -0
  327. package/dist/record/edges/verified.db.d.ts +7 -0
  328. package/dist/record/edges/verified.db.js +71 -0
  329. package/dist/record/parts.operations.d.ts +26 -0
  330. package/dist/record/parts.operations.js +31 -0
  331. package/dist/record/path-checks.operations.d.ts +15 -0
  332. package/dist/record/path-checks.operations.js +84 -0
  333. package/dist/record/record.operations.d.ts +36 -0
  334. package/dist/record/record.operations.js +11 -0
  335. package/dist/stresses/edges/stresses.record.d.ts +4 -0
  336. package/dist/stresses/edges/stresses.record.js +37 -0
  337. package/dist/stresses/stresses.operations.d.ts +28 -0
  338. package/dist/stresses/stresses.operations.js +22 -0
  339. package/dist/utils/ast.d.ts +55 -0
  340. package/dist/utils/ast.js +181 -0
  341. package/dist/utils/errors.d.ts +8 -0
  342. package/dist/utils/errors.js +12 -0
  343. package/dist/utils/faults.d.ts +6 -0
  344. package/dist/utils/faults.js +10 -0
  345. package/dist/utils/lookup.d.ts +4 -0
  346. package/dist/utils/lookup.js +27 -0
  347. package/dist/utils/order.d.ts +2 -0
  348. package/dist/utils/order.js +17 -0
  349. package/dist/utils/retry.d.ts +14 -0
  350. package/dist/utils/retry.js +15 -0
  351. package/dist/utils/safe.d.ts +11 -0
  352. package/dist/utils/safe.js +31 -0
  353. package/migrations/main/20261129123324_initial/migration.sql +418 -0
  354. package/migrations/main/20261129123324_initial/snapshot.json +3641 -0
  355. package/package.json +89 -4
  356. package/references/consumer-setup.md +92 -0
  357. package/src/graph/cfg/code-paths/LICENSE +21 -0
  358. package/src/graph/cfg/scopes/LICENSE +24 -0
@@ -0,0 +1,61 @@
1
+ import { type Node, type Parsed } from '../utils/ast.ts';
2
+ import type { Paths } from '../graph/cfg/paths.ts';
3
+ import type { DecisionKind } from './decisions.ts';
4
+ export type Sides = {
5
+ true: string[];
6
+ false: string[];
7
+ };
8
+ type ShortOperator = '&&' | '||' | '??' | '&&=' | '||=' | '??=';
9
+ type HookOperator = 'case' | 'default' | 'of' | 'in' | 'value' | 'optional' | 'method' | 'callback';
10
+ export type Probe = {
11
+ start: number;
12
+ end: number;
13
+ operator: '' | ShortOperator | HookOperator;
14
+ leftStart: number;
15
+ leftEnd: number;
16
+ rightStart: number;
17
+ rightEnd: number;
18
+ discriminant: string;
19
+ reference: {
20
+ objectStart: number;
21
+ objectEnd: number;
22
+ propertyStart: number;
23
+ propertyEnd: number;
24
+ } | null;
25
+ };
26
+ export type Decision = {
27
+ kind: DecisionKind;
28
+ key: string;
29
+ id: string;
30
+ owner: string;
31
+ node: Node;
32
+ segments: string[];
33
+ sides: Sides;
34
+ probe: Probe | undefined;
35
+ };
36
+ type Found = Omit<Decision, 'id' | 'owner'>;
37
+ export declare const probeOf: (operator: Probe['operator'], span: {
38
+ start: number;
39
+ end: number;
40
+ }, discriminant: string) => Probe;
41
+ type Marker = {
42
+ node: Node;
43
+ side: boolean;
44
+ };
45
+ export type Decide = {
46
+ source: string;
47
+ comments: Parsed['comments'];
48
+ add: (kind: DecisionKind, offset: number, node: Node, anchor: Node, marker?: Marker, probe?: Probe) => void;
49
+ decideTest: (kind: DecisionKind, owner: Node, test: Node, marker?: Marker, probe?: Probe) => void;
50
+ isChained: (node: Node) => boolean;
51
+ isParameter: (name: string, ancestors: readonly Node[]) => boolean;
52
+ };
53
+ export declare const readsPlainly: (node: Node) => boolean;
54
+ export declare const propertyNameOf: (callee: Node) => string;
55
+ export declare const objectNameOf: (callee: Node) => string;
56
+ export declare const parameterNamesOf: (fn: Node) => Set<string>;
57
+ export declare const toolsOf: (source: string, comments: Parsed['comments']) => {
58
+ tools: Decide;
59
+ finish: (paths: Paths) => Found[];
60
+ };
61
+ export {};
@@ -0,0 +1,161 @@
1
+ import { entryAt, locate, namesProperty, walk } from '../utils/ast.js';
2
+ // A probe over a span whose left part is the whole span, the shape every hook and plain test takes.
3
+ export const probeOf = (operator, span, discriminant) => ({
4
+ start: span.start,
5
+ end: span.end,
6
+ operator,
7
+ leftStart: span.start,
8
+ leftEnd: span.end,
9
+ rightStart: span.end,
10
+ rightEnd: span.end,
11
+ discriminant,
12
+ reference: null,
13
+ });
14
+ const isChainLink = (node) => node.type === 'LogicalExpression' && node.operator !== '??';
15
+ // A test's `&&` and `||` chain, its links and its operands left to right across nested groups, each operand with its marker.
16
+ // An operand left of `&&` goes on to the right side when true, one left of `||` when false, and the test's last operand takes the test's own marker.
17
+ // The walk keeps its own stack, so a chain longer than the call stack is read whole.
18
+ const chainOf = (test, marker) => {
19
+ const links = [];
20
+ const markers = new Map();
21
+ const pending = [{ node: test, inherited: marker }];
22
+ for (let next = pending.pop(); next; next = pending.pop()) {
23
+ const { node, inherited } = next;
24
+ if (!isChainLink(node)) {
25
+ markers.set(node, inherited);
26
+ continue;
27
+ }
28
+ links.push(node);
29
+ pending.push({ node: node.right, inherited }, { node: node.left, inherited: { node: node.right, side: node.operator === '&&' } });
30
+ }
31
+ return { links, markers };
32
+ };
33
+ // A read the instrumenter can capture once: a name, `this`, or a property chain with no call or computed key.
34
+ export const readsPlainly = (node) => {
35
+ if (node.type === 'ChainExpression')
36
+ return readsPlainly(node.expression);
37
+ if (node.type === 'Identifier' || node.type === 'ThisExpression')
38
+ return true;
39
+ return node.type === 'MemberExpression' && !node.computed && readsPlainly(node.object);
40
+ };
41
+ export const propertyNameOf = (callee) => {
42
+ if (callee.type !== 'MemberExpression' || callee.computed || callee.property.type !== 'Identifier')
43
+ return '';
44
+ return callee.property.name;
45
+ };
46
+ export const objectNameOf = (callee) => {
47
+ if (callee.type !== 'MemberExpression' || callee.object.type !== 'Identifier')
48
+ return '';
49
+ return callee.object.name;
50
+ };
51
+ // The names a function's parameters bind, leaving out property keys and default values.
52
+ export const parameterNamesOf = (fn) => {
53
+ const names = new Set();
54
+ if (fn.type !== 'FunctionDeclaration' && fn.type !== 'FunctionExpression' && fn.type !== 'ArrowFunctionExpression') {
55
+ return names;
56
+ }
57
+ for (const param of fn.params) {
58
+ if (param.type === 'Identifier')
59
+ names.add(param.name);
60
+ walk(param, ({ node, parent, key }) => {
61
+ if (node.type === 'Identifier' && key !== 'right' && !namesProperty(parent, key))
62
+ names.add(node.name);
63
+ });
64
+ }
65
+ return names;
66
+ };
67
+ // The tools every decision kind of one file calls during the walk, and, once the walk has built the file's code paths, the decisions they added.
68
+ // A decision's segments and sides need the whole path, so each call is kept and resolved at the end, while the links a test claims are marked at once, since a later visit reads them.
69
+ export const toolsOf = (source, comments) => {
70
+ const at = locate(source);
71
+ const keyAt = (offset) => {
72
+ const { line, column } = at(offset);
73
+ return `L${line}C${column}`;
74
+ };
75
+ const calls = [];
76
+ const chained = new Set();
77
+ const parameters = new Map();
78
+ const isParameter = (name, ancestors) => {
79
+ return ancestors.some((ancestor) => {
80
+ const kept = parameters.get(ancestor);
81
+ if (kept)
82
+ return kept.has(name);
83
+ const names = parameterNamesOf(ancestor);
84
+ parameters.set(ancestor, names);
85
+ return names.has(name);
86
+ });
87
+ };
88
+ const add = (...args) => calls.push((resolve) => resolve.add(...args));
89
+ const decideTest = (...args) => {
90
+ for (const link of chainOf(args[2]).links)
91
+ chained.add(link);
92
+ calls.push((resolve) => resolve.decideTest(...args));
93
+ };
94
+ const finish = (paths) => {
95
+ const resolve = resolverOf(paths, keyAt);
96
+ for (const call of calls)
97
+ call(resolve);
98
+ return resolve.decisions;
99
+ };
100
+ return { tools: { source, comments, add, decideTest, isChained: (node) => chained.has(node), isParameter }, finish };
101
+ };
102
+ // Adds each decision with the segments it forks from and where each side goes, read from the file's finished code paths.
103
+ const resolverOf = (paths, keyAt) => {
104
+ const segmentsOf = (node) => entryAt(paths.exits, node, `${node.type} at ${keyAt(node.start)}`);
105
+ const segments = paths.graphs.flatMap((graph) => graph.segments);
106
+ const nextOf = new Map(segments.map((segment) => [segment.id, segment.next]));
107
+ // Every segment that lists a node, where its side of a decision can start.
108
+ const listedIn = new Map();
109
+ for (const segment of segments) {
110
+ for (const node of segment.nodes)
111
+ listedIn.set(node, (listedIn.get(node) ?? new Set()).add(segment.id));
112
+ }
113
+ // Each fork segment's next segment listing the marker is the marker's side, and its other next segments the other side.
114
+ // A next segment that starts another operand of a chain stays inside it, as `b` after `a` false in `a || b`, so it is no side.
115
+ const sidesOf = (forks, marker, inside = new Set()) => {
116
+ const sides = { true: [], false: [] };
117
+ if (!marker)
118
+ return sides;
119
+ const marked = entryAt(listedIn, marker.node, `${marker.node.type} at ${keyAt(marker.node.start)}`);
120
+ const deciding = forks.map((fork) => entryAt(nextOf, fork, `segment ${fork}`)).filter((next) => next.length > 1 && next.some((id) => marked.has(id)));
121
+ for (const next of deciding) {
122
+ for (const id of next.filter((each) => !inside.has(each))) {
123
+ const side = marked.has(id) === marker.side ? 'true' : 'false';
124
+ if (!sides[side].includes(id))
125
+ sides[side].push(id);
126
+ }
127
+ }
128
+ return sides;
129
+ };
130
+ const decisions = [];
131
+ const add = (kind, offset, node, anchor, marker, probe) => {
132
+ const forks = segmentsOf(anchor);
133
+ decisions.push({ kind, key: keyAt(offset), node, segments: forks, sides: sidesOf(forks, marker), probe });
134
+ };
135
+ // A test splits into its operands, whose own `&&` and `||` links are then no short-circuits of their own.
136
+ // A chain leaves for either side from any operand that decides it, as `a` false skips `b` in `a && b`, so the whole test forks from every operand's exits too.
137
+ // A next segment that starts another operand stays inside the chain, and every other one is the whole test's true side when it holds the marker.
138
+ // A test is read as its own value unless the caller names another probe, as a braced callback's result.
139
+ const decideTest = (kind, owner, test, marker, probe) => {
140
+ const forkKey = keyAt(owner.start);
141
+ const { links, markers } = chainOf(test, marker);
142
+ const parts = links.length ? [...markers.keys()] : [];
143
+ const operands = parts.map((part, index) => ({
144
+ kind: 'operand',
145
+ key: `${forkKey}.${index}`,
146
+ node: part,
147
+ segments: segmentsOf(part),
148
+ sides: sidesOf(segmentsOf(part), markers.get(part)),
149
+ probe: probeOf('', part, ''),
150
+ }));
151
+ const forks = [...new Set([test, ...parts].flatMap(segmentsOf))];
152
+ const inside = new Set();
153
+ for (const part of parts) {
154
+ for (const id of entryAt(listedIn, part, `${part.type} at ${keyAt(part.start)}`))
155
+ inside.add(id);
156
+ }
157
+ const sides = sidesOf(forks, marker, inside);
158
+ decisions.push({ kind, key: forkKey, node: owner, segments: forks, sides, probe: probe ?? probeOf('', test, '') }, ...operands);
159
+ };
160
+ return { add, decideTest, decisions };
161
+ };
@@ -0,0 +1,24 @@
1
+ import { type Parsed, type Visit } from '../utils/ast.ts';
2
+ import type { Paths } from '../graph/cfg/paths.ts';
3
+ import { type Decision, type Probe } from './decide.ts';
4
+ import type { kinds } from './kinds.ts';
5
+ export { type Decision, type Sides } from './decide.ts';
6
+ export type Fork = Decision & {
7
+ probe: Probe;
8
+ };
9
+ export declare const forksOf: (decisions: Decision[]) => Fork[];
10
+ export type DecisionKind = (typeof kinds)[number]['kind'];
11
+ type Owner = {
12
+ start: number;
13
+ end: number;
14
+ name: string | undefined;
15
+ isFunction: boolean;
16
+ };
17
+ export declare const ownerAt: (owners: Owner[], start: number) => string;
18
+ export declare const decisionCollector: (source: string, comments: Parsed['comments']) => {
19
+ visit: (visit: Visit) => void;
20
+ finish: (paths: Paths) => {
21
+ decisions: Decision[];
22
+ owners: Owner[];
23
+ };
24
+ };
@@ -0,0 +1,62 @@
1
+ import {} from '../utils/ast.js';
2
+ import { toolsOf } from './decide.js';
3
+ import { forks } from './forks.decision.js';
4
+ import { loops } from './loops.decision.js';
5
+ import { shorts } from './shorts.decision.js';
6
+ import { switches } from './switches.decision.js';
7
+ import { throws } from './throws.decision.js';
8
+ import { values } from './values.decision.js';
9
+ export {} from './decide.js';
10
+ // The forks among a file's decisions, narrowed once here so no reader guards a probe it always has.
11
+ export const forksOf = (decisions) => decisions.filter((decision) => decision.probe !== undefined);
12
+ // The topic files the walk hands every node to, forks first so a test's chain is claimed before its links are visited.
13
+ const topics = [forks, shorts, switches, loops, throws, values];
14
+ const functionTypes = new Set(['FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression']);
15
+ // The name a function or class goes by: a declaration's own, else the binding or target it is written to, or the key it sits under, absent for an anonymous callback or a computed key.
16
+ const nameOf = (node, parent, source) => {
17
+ if (node.type === 'FunctionDeclaration' || node.type === 'ClassDeclaration')
18
+ return node.id?.name;
19
+ if (parent.type === 'VariableDeclarator')
20
+ return parent.id.type === 'Identifier' ? parent.id.name : undefined;
21
+ if (parent.type === 'AssignmentExpression')
22
+ return source.slice(parent.left.start, parent.left.end);
23
+ const isKeyed = parent.type === 'Property' || parent.type === 'MethodDefinition' || parent.type === 'PropertyDefinition';
24
+ return isKeyed && !parent.computed && (parent.key.type === 'Identifier' || parent.key.type === 'PrivateIdentifier') ? parent.key.name : undefined;
25
+ };
26
+ const ownerTypes = new Set([...functionTypes, 'ClassDeclaration', 'ClassExpression']);
27
+ // The path of named functions and classes holding an offset, outermost first, as `A.read`, so two of one name stay apart.
28
+ export const ownerAt = (owners, start) => {
29
+ return owners
30
+ .filter((owner) => owner.start < start && start < owner.end)
31
+ .flatMap(({ name }) => (name ? [name] : []))
32
+ .join('.');
33
+ };
34
+ // Hands each visit of a file's one walk to every topic, and once the walk has built the code paths, gives the file's decisions and the owners it saw.
35
+ export const decisionCollector = (source, comments) => {
36
+ const { tools, finish: resolve } = toolsOf(source, comments);
37
+ const owners = [];
38
+ const visit = (each) => {
39
+ const { node, parent } = each;
40
+ if (ownerTypes.has(node.type))
41
+ owners.push({ start: node.start, end: node.end, name: nameOf(node, parent, source), isFunction: functionTypes.has(node.type) });
42
+ for (const topic of topics)
43
+ topic(each, tools);
44
+ };
45
+ const finish = (paths) => {
46
+ const seen = new Map();
47
+ const decisions = resolve(paths).map((decision) => {
48
+ const span = decision.probe ?? decision.node;
49
+ const owner = ownerAt(owners, decision.node.start);
50
+ const fields = [decision.kind, owner, source.slice(span.start, span.end)];
51
+ const base = JSON.stringify(fields);
52
+ const occurrence = seen.get(base) ?? 0;
53
+ seen.set(base, occurrence + 1);
54
+ // Only code inside a function runs under a case, and a `while (true)` loops on purpose, so neither is observed; a callback is observed where it is handed.
55
+ const isInside = decision.kind === 'callback' || owners.some((owner) => owner.isFunction && owner.start < decision.node.start && decision.node.start < owner.end);
56
+ const isEndless = decision.kind === 'while' && source.slice(span.start, span.end) === 'true';
57
+ return Object.assign(decision, { id: JSON.stringify([...fields, occurrence]), owner, probe: isInside && !isEndless ? decision.probe : undefined });
58
+ });
59
+ return { decisions, owners };
60
+ };
61
+ return { visit, finish };
62
+ };
@@ -0,0 +1,4 @@
1
+ import { type Visit } from '../utils/ast.ts';
2
+ import { type Decide } from './decide.ts';
3
+ export declare const decidesPerItem: (parent: Visit['parent']) => boolean;
4
+ export declare const forks: ({ node, parent }: Visit, { decideTest }: Decide) => void;
@@ -0,0 +1,32 @@
1
+ import { present } from '../utils/ast.js';
2
+ import { probeOf, propertyNameOf } from './decide.js';
3
+ const decidingMethods = new Set(['filter', 'some', 'every', 'find', 'findIndex', 'findLast', 'findLastIndex']);
4
+ // A callback handed to a per-item method decides, per item, whether the item counts.
5
+ // A function under a call is its argument or its callee, and a callee that is a function names no method.
6
+ export const decidesPerItem = (parent) => parent.type === 'CallExpression' && decidingMethods.has(propertyNameOf(parent.callee));
7
+ // The tests that pick a side: if, ternary and loop tests, and a per-item callback's answer.
8
+ export const forks = ({ node, parent }, { decideTest }) => {
9
+ switch (node.type) {
10
+ case 'IfStatement':
11
+ return decideTest('if', node, node.test, { node: node.consequent, side: true });
12
+ case 'ConditionalExpression':
13
+ return decideTest('ternary', node, node.test, { node: node.consequent, side: true });
14
+ case 'WhileStatement':
15
+ return decideTest('while', node, node.test, { node: node.body, side: true });
16
+ case 'DoWhileStatement':
17
+ return decideTest('doWhile', node, node.test, { node: node.body, side: true });
18
+ case 'ArrowFunctionExpression':
19
+ case 'FunctionExpression': {
20
+ if (!decidesPerItem(parent))
21
+ return;
22
+ const body = present(node.body);
23
+ // A braced callback has no one expression to read, so its result is read as it returns.
24
+ return decideTest('callback', node, body, undefined, body.type === 'BlockStatement' ? probeOf('callback', node, '') : undefined);
25
+ }
26
+ // Last, so a node of any other type reaching it is refused by its own test, which a switch case also carries.
27
+ case 'ForStatement':
28
+ if (node.test)
29
+ decideTest('for', node, node.test, { node: node.body, side: true });
30
+ return;
31
+ }
32
+ };
@@ -0,0 +1,101 @@
1
+ export declare const kinds: readonly [{
2
+ readonly kind: 'if';
3
+ readonly source: 'eslint';
4
+ }, {
5
+ readonly kind: 'ternary';
6
+ readonly source: 'eslint';
7
+ }, {
8
+ readonly kind: 'while';
9
+ readonly source: 'eslint';
10
+ }, {
11
+ readonly kind: 'doWhile';
12
+ readonly source: 'eslint';
13
+ }, {
14
+ readonly kind: 'for';
15
+ readonly source: 'eslint';
16
+ }, {
17
+ readonly kind: 'operand';
18
+ readonly source: 'eslint';
19
+ }, {
20
+ readonly kind: 'callback';
21
+ readonly source: 'crush';
22
+ }, {
23
+ readonly kind: 'short';
24
+ readonly source: 'eslint';
25
+ }, {
26
+ readonly kind: 'logicalAssign';
27
+ readonly source: 'eslint';
28
+ }, {
29
+ readonly kind: 'optional';
30
+ readonly source: 'eslint';
31
+ }, {
32
+ readonly kind: 'valueDefault';
33
+ readonly source: 'eslint';
34
+ }, {
35
+ readonly kind: 'case';
36
+ readonly source: 'eslint';
37
+ }, {
38
+ readonly kind: 'switchDefault';
39
+ readonly source: 'eslint';
40
+ }, {
41
+ readonly kind: 'forOf';
42
+ readonly source: 'eslint';
43
+ }, {
44
+ readonly kind: 'forIn';
45
+ readonly source: 'eslint';
46
+ }, {
47
+ readonly kind: 'forAwait';
48
+ readonly source: 'eslint';
49
+ }, {
50
+ readonly kind: 'iterate';
51
+ readonly source: 'crush';
52
+ }, {
53
+ readonly kind: 'iteratorClose';
54
+ readonly source: 'crush';
55
+ }, {
56
+ readonly kind: 'mayThrow';
57
+ readonly source: 'crush';
58
+ }, {
59
+ readonly kind: 'catch';
60
+ readonly source: 'eslint';
61
+ }, {
62
+ readonly kind: 'finallyOverride';
63
+ readonly source: 'crush';
64
+ }, {
65
+ readonly kind: 'coerce';
66
+ readonly source: 'crush';
67
+ }, {
68
+ readonly kind: 'notFound';
69
+ readonly source: 'crush';
70
+ }, {
71
+ readonly kind: 'oddResult';
72
+ readonly source: 'crush';
73
+ }, {
74
+ readonly kind: 'await';
75
+ readonly source: 'crush';
76
+ }, {
77
+ readonly kind: 'yield';
78
+ readonly source: 'crush';
79
+ }, {
80
+ readonly kind: 'dispatch';
81
+ readonly source: 'crush';
82
+ }, {
83
+ readonly kind: 'nonNull';
84
+ readonly source: 'crush';
85
+ }, {
86
+ readonly kind: 'privateBrand';
87
+ readonly source: 'crush';
88
+ }, {
89
+ readonly kind: 'dispose';
90
+ readonly source: 'crush';
91
+ }, {
92
+ readonly kind: 'destructure';
93
+ readonly source: 'crush';
94
+ }, {
95
+ readonly kind: 'classHeritage';
96
+ readonly source: 'crush';
97
+ }, {
98
+ readonly kind: 'superCall';
99
+ readonly source: 'crush';
100
+ }];
101
+ export declare const loopKinds: ReadonlySet<string>;
@@ -0,0 +1,38 @@
1
+ // Every decision kind, with whether ESLint's code paths fork there or crush reads a fork ESLint never draws.
2
+ export const kinds = [
3
+ { kind: 'if', source: 'eslint' },
4
+ { kind: 'ternary', source: 'eslint' },
5
+ { kind: 'while', source: 'eslint' },
6
+ { kind: 'doWhile', source: 'eslint' },
7
+ { kind: 'for', source: 'eslint' },
8
+ { kind: 'operand', source: 'eslint' },
9
+ { kind: 'callback', source: 'crush' },
10
+ { kind: 'short', source: 'eslint' },
11
+ { kind: 'logicalAssign', source: 'eslint' },
12
+ { kind: 'optional', source: 'eslint' },
13
+ { kind: 'valueDefault', source: 'eslint' },
14
+ { kind: 'case', source: 'eslint' },
15
+ { kind: 'switchDefault', source: 'eslint' },
16
+ { kind: 'forOf', source: 'eslint' },
17
+ { kind: 'forIn', source: 'eslint' },
18
+ { kind: 'forAwait', source: 'eslint' },
19
+ { kind: 'iterate', source: 'crush' },
20
+ { kind: 'iteratorClose', source: 'crush' },
21
+ { kind: 'mayThrow', source: 'crush' },
22
+ { kind: 'catch', source: 'eslint' },
23
+ { kind: 'finallyOverride', source: 'crush' },
24
+ { kind: 'coerce', source: 'crush' },
25
+ { kind: 'notFound', source: 'crush' },
26
+ { kind: 'oddResult', source: 'crush' },
27
+ { kind: 'await', source: 'crush' },
28
+ { kind: 'yield', source: 'crush' },
29
+ { kind: 'dispatch', source: 'crush' },
30
+ { kind: 'nonNull', source: 'crush' },
31
+ { kind: 'privateBrand', source: 'crush' },
32
+ { kind: 'dispose', source: 'crush' },
33
+ { kind: 'destructure', source: 'crush' },
34
+ { kind: 'classHeritage', source: 'crush' },
35
+ { kind: 'superCall', source: 'crush' },
36
+ ];
37
+ // The decision kinds whose test a loop takes both ways on any call that iterates, true while it runs and false to stop, so no-split never asks of them.
38
+ export const loopKinds = new Set(['while', 'doWhile', 'for', 'forOf', 'forIn', 'forAwait']);
@@ -0,0 +1,4 @@
1
+ import { type Node, type Visit } from '../utils/ast.ts';
2
+ import { type Decide } from './decide.ts';
3
+ export declare const leavesEarly: (body: Node) => boolean;
4
+ export declare const loops: ({ node, parent }: Visit, { add }: Decide) => void;
@@ -0,0 +1,39 @@
1
+ import { walk } from '../utils/ast.js';
2
+ import { probeOf } from './decide.js';
3
+ const loopTypes = new Set(['ForStatement', 'ForInStatement', 'ForOfStatement', 'WhileStatement', 'DoWhileStatement', 'SwitchStatement']);
4
+ // A `for...of` left early by `break`, `return` or `throw` closes its iterator, which runs the iterator's `return`.
5
+ // An unlabeled `break` leaves the loop only when no loop or switch inside the body holds it.
6
+ const leaves = (node, ancestors) => {
7
+ if (node.type === 'ReturnStatement' || node.type === 'ThrowStatement')
8
+ return true;
9
+ return node.type === 'BreakStatement' && (node.label !== null || !ancestors.some((ancestor) => loopTypes.has(ancestor.type)));
10
+ };
11
+ // A walk visits only what is under its root, so a body that is itself the `break` is checked first.
12
+ export const leavesEarly = (body) => {
13
+ let found = leaves(body, []);
14
+ walk(body, ({ node, ancestors, inFunction }) => {
15
+ if (!inFunction && leaves(node, ancestors))
16
+ found = true;
17
+ });
18
+ return found;
19
+ };
20
+ // The walks over an iterable or an object's keys: for...of, for...in, a spread and an array pattern.
21
+ export const loops = ({ node, parent }, { add }) => {
22
+ switch (node.type) {
23
+ case 'ForOfStatement':
24
+ // ESLint forks a `for...of` after its iterable, to binding its left side for a pass or to leaving the loop.
25
+ add(node.await ? 'forAwait' : 'forOf', node.start, node, node.right, { node: node.left, side: true }, node.await ? undefined : probeOf('of', node.right, ''));
26
+ add('iterate', node.right.start, node.right, node.right);
27
+ if (leavesEarly(node.body))
28
+ add('iteratorClose', node.start, node, node.body);
29
+ return;
30
+ case 'ForInStatement':
31
+ return add('forIn', node.start, node, node.right, { node: node.left, side: true }, probeOf('in', node.right, ''));
32
+ case 'SpreadElement':
33
+ if (parent.type !== 'ObjectExpression')
34
+ add('iterate', node.start, node, node);
35
+ return;
36
+ case 'ArrayPattern':
37
+ return add('iterate', node.start, node, node);
38
+ }
39
+ };
@@ -0,0 +1,3 @@
1
+ import { type Visit } from '../utils/ast.ts';
2
+ import { type Decide } from './decide.ts';
3
+ export declare const shorts: ({ node }: Visit, { add, isChained, source, comments }: Decide) => void;
@@ -0,0 +1,66 @@
1
+ import { ofType, tokenAt } from '../utils/ast.js';
2
+ import { probeOf, readsPlainly } from './decide.js';
3
+ const logicalAssignments = new Set(['&&=', '||=', '??=']);
4
+ // A short-circuit or logical assignment splits at its operator, its left side the value it decides by.
5
+ const splitOf = ({ start, end, left, right }, operator, reference) => ({
6
+ start,
7
+ end,
8
+ operator,
9
+ leftStart: left.start,
10
+ leftEnd: left.end,
11
+ rightStart: right.start,
12
+ rightEnd: right.end,
13
+ discriminant: '',
14
+ reference,
15
+ });
16
+ // A logical assignment the instrumenter can write back: a plain read, or a computed member whose object and key it captures once.
17
+ const assignProbeOf = (node, operator) => {
18
+ const { left } = node;
19
+ // Only a member carries `computed`, so reading it alone tells a computed member from every other target.
20
+ if ('computed' in left && left.computed === true) {
21
+ const member = ofType(left, 'MemberExpression');
22
+ return splitOf(node, operator, { objectStart: member.object.start, objectEnd: member.object.end, propertyStart: member.property.start, propertyEnd: member.property.end });
23
+ }
24
+ return readsPlainly(left) ? splitOf(node, operator, null) : undefined;
25
+ };
26
+ // An optional call through a member keeps its receiver, so it is read as a method; any other callee is read as a value.
27
+ const callProbeOf = (callee, source, comments) => {
28
+ const member = callee.type === 'ChainExpression' ? callee.expression : callee;
29
+ if (member.type !== 'MemberExpression')
30
+ return probeOf('optional', callee, '');
31
+ // ESTree omits grouping parentheses from the receiver's span. Split at the member operator so the group closes on the receiver.
32
+ const token = member.optional ? '?.' : member.computed ? '[' : '.';
33
+ const leftEnd = tokenAt({ source, comments, token, from: member.object.end, to: member.property.start });
34
+ return { ...probeOf('method', callee, ''), leftStart: member.object.start, leftEnd, rightStart: leftEnd, rightEnd: leftEnd };
35
+ };
36
+ // The forks that skip part of an expression: short-circuits, logical assignments, optional chains and default values.
37
+ export const shorts = ({ node }, { add, isChained, source, comments }) => {
38
+ const operatorAt = (token, left, right) => tokenAt({ source, comments, token, from: left.end, to: right.start });
39
+ switch (node.type) {
40
+ // `&&` runs its right side on its true side, where `||` and `??` run it on their false side.
41
+ case 'LogicalExpression':
42
+ if (!isChained(node))
43
+ add('short', operatorAt(node.operator, node.left, node.right), node, node.left, { node: node.right, side: node.operator === '&&' }, splitOf(node, node.operator, null));
44
+ return;
45
+ case 'AssignmentExpression':
46
+ if (logicalAssignments.has(node.operator)) {
47
+ add('logicalAssign', operatorAt(node.operator, node.left, node.right), node, node.left, { node: node.right, side: node.operator === '&&=' }, assignProbeOf(node, node.operator === '&&=' ? '&&=' : node.operator === '||=' ? '||=' : '??='));
48
+ }
49
+ return;
50
+ // An optional chain's true side is its object present, as `??` reads presence, which runs the rest of the chain.
51
+ case 'MemberExpression':
52
+ if (node.optional)
53
+ add('optional', operatorAt('?.', node.object, node.property), node, node.object, { node: node.property, side: true }, probeOf('optional', node.object, ''));
54
+ return;
55
+ // A call with no argument still forks on its callee, its true side the call itself, so a zero-argument `cleanup?.()` is judged too.
56
+ case 'CallExpression': {
57
+ const [firstArgument] = node.arguments;
58
+ if (node.optional)
59
+ add('optional', tokenAt({ source, comments, token: '?.', from: node.callee.end, to: node.end }), node, node.callee, firstArgument && { node: firstArgument, side: true }, callProbeOf(node.callee, source, comments));
60
+ return;
61
+ }
62
+ // A default's true side is the value arriving undefined, which runs the default.
63
+ case 'AssignmentPattern':
64
+ return add('valueDefault', node.start, node, node.left, { node: node.right, side: true }, probeOf('value', node.right, ''));
65
+ }
66
+ };
@@ -0,0 +1,3 @@
1
+ import type { Visit } from '../utils/ast.ts';
2
+ import { type Decide } from './decide.ts';
3
+ export declare const switches: ({ node }: Visit, { add, source }: Decide) => void;
@@ -0,0 +1,20 @@
1
+ import { probeOf, readsPlainly } from './decide.js';
2
+ // Each case of a switch, and its default, which runs when no case matches.
3
+ export const switches = ({ node }, { add, source }) => {
4
+ if (node.type !== 'SwitchStatement')
5
+ return;
6
+ // Only a switch on a plain read is observed, since the instrumenter reads its discriminant once for every case.
7
+ const discriminant = readsPlainly(node.discriminant) ? source.slice(node.discriminant.start, node.discriminant.end) : null;
8
+ // An empty case falls through, so its true side starts at the first statement of a case after it.
9
+ const caseProbe = discriminant === null ? null : (test) => probeOf('case', test, discriminant);
10
+ for (const [index, item] of node.cases.entries()) {
11
+ const [first] = node.cases.slice(index).flatMap((each) => each.consequent);
12
+ if (item.test)
13
+ add('case', item.start, item, item.test, first && { node: first, side: true }, caseProbe?.(item.test));
14
+ }
15
+ const fallback = node.cases.find((item) => !item.test) ?? node;
16
+ // A default written last runs once every case before it missed, so its keyword is observed as one more case.
17
+ const last = node.cases.at(-1);
18
+ const isLastDefault = discriminant !== null && last !== undefined && !last.test;
19
+ add('switchDefault', fallback.start, fallback, node.discriminant, undefined, isLastDefault ? probeOf('default', { start: last.start, end: last.start + 'default'.length }, discriminant) : undefined);
20
+ };
@@ -0,0 +1,3 @@
1
+ import { type Visit } from '../utils/ast.ts';
2
+ import type { Decide } from './decide.ts';
3
+ export declare const throws: ({ node }: Visit, { add, source, comments }: Decide) => void;