redscript-mc 2.6.2 → 3.0.0

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 (636) hide show
  1. package/.github/workflows/ci.yml +11 -0
  2. package/CHANGELOG.md +18 -9
  3. package/README-benchmarks.md +48 -0
  4. package/README-vscode-test.md +251 -0
  5. package/RELEASE_NOTES.md +74 -0
  6. package/ROADMAP.md +131 -167
  7. package/benchmarks/_shared.ts +468 -0
  8. package/benchmarks/baseline.json +2816 -0
  9. package/benchmarks/baseline.md +13 -0
  10. package/benchmarks/compiler-perf.report.json +207 -0
  11. package/benchmarks/compiler-perf.ts +76 -0
  12. package/benchmarks/results.md +13 -0
  13. package/benchmarks/stdlib-complexity.report.json +2606 -0
  14. package/benchmarks/stdlib-complexity.ts +54 -0
  15. package/benchmarks/stdlib-size.md +57 -0
  16. package/benchmarks/stdlib-size.ts +91 -0
  17. package/coverage-report.md +177 -0
  18. package/dist/src/__tests__/budget.test.js +4 -0
  19. package/dist/src/__tests__/cache/cache-behavior.test.d.ts +10 -0
  20. package/dist/src/__tests__/cache/cache-behavior.test.js +425 -0
  21. package/dist/src/__tests__/cache-extra.test.d.ts +5 -0
  22. package/dist/src/__tests__/cache-extra.test.js +211 -0
  23. package/dist/src/__tests__/cli-init.test.d.ts +1 -0
  24. package/dist/src/__tests__/cli-init.test.js +97 -0
  25. package/dist/src/__tests__/cli-publish.test.d.ts +9 -0
  26. package/dist/src/__tests__/cli-publish.test.js +189 -0
  27. package/dist/src/__tests__/cli.test.js +76 -0
  28. package/dist/src/__tests__/compile-preprocess.test.d.ts +11 -0
  29. package/dist/src/__tests__/compile-preprocess.test.js +328 -0
  30. package/dist/src/__tests__/compiler/break-stmt.test.d.ts +1 -0
  31. package/dist/src/__tests__/compiler/break-stmt.test.js +58 -0
  32. package/dist/src/__tests__/compiler/const-decl.test.d.ts +1 -0
  33. package/dist/src/__tests__/compiler/const-decl.test.js +123 -0
  34. package/dist/src/__tests__/compiler/continue-stmt.test.d.ts +1 -0
  35. package/dist/src/__tests__/compiler/continue-stmt.test.js +67 -0
  36. package/dist/src/__tests__/compiler/coroutine-extended.test.d.ts +17 -0
  37. package/dist/src/__tests__/compiler/coroutine-extended.test.js +565 -0
  38. package/dist/src/__tests__/compiler/deprecated.test.d.ts +4 -0
  39. package/dist/src/__tests__/compiler/deprecated.test.js +285 -0
  40. package/dist/src/__tests__/compiler/do-while.test.d.ts +1 -0
  41. package/dist/src/__tests__/compiler/do-while.test.js +120 -0
  42. package/dist/src/__tests__/compiler/enum-payload.test.d.ts +9 -0
  43. package/dist/src/__tests__/compiler/enum-payload.test.js +272 -0
  44. package/dist/src/__tests__/compiler/interface.test.d.ts +10 -0
  45. package/dist/src/__tests__/compiler/interface.test.js +258 -0
  46. package/dist/src/__tests__/compiler/labeled-loops.test.d.ts +1 -0
  47. package/dist/src/__tests__/compiler/labeled-loops.test.js +263 -0
  48. package/dist/src/__tests__/compiler/match-string.test.d.ts +1 -0
  49. package/dist/src/__tests__/compiler/match-string.test.js +43 -0
  50. package/dist/src/__tests__/compiler/memoize.test.d.ts +1 -0
  51. package/dist/src/__tests__/compiler/memoize.test.js +113 -0
  52. package/dist/src/__tests__/compiler/method-chain.test.d.ts +5 -0
  53. package/dist/src/__tests__/compiler/method-chain.test.js +115 -0
  54. package/dist/src/__tests__/compiler/module-import.test.d.ts +12 -0
  55. package/dist/src/__tests__/compiler/module-import.test.js +261 -0
  56. package/dist/src/__tests__/compiler/option-extensions.test.d.ts +6 -0
  57. package/dist/src/__tests__/compiler/option-extensions.test.js +191 -0
  58. package/dist/src/__tests__/compiler/profile-decorator.test.d.ts +1 -0
  59. package/dist/src/__tests__/compiler/profile-decorator.test.js +69 -0
  60. package/dist/src/__tests__/compiler/string-advanced.test.d.ts +7 -0
  61. package/dist/src/__tests__/compiler/string-advanced.test.js +281 -0
  62. package/dist/src/__tests__/compiler/struct-extends.test.d.ts +1 -0
  63. package/dist/src/__tests__/compiler/struct-extends.test.js +95 -0
  64. package/dist/src/__tests__/compiler/throttle-retry.test.d.ts +1 -0
  65. package/dist/src/__tests__/compiler/throttle-retry.test.js +166 -0
  66. package/dist/src/__tests__/compiler/tuple-type.test.d.ts +10 -0
  67. package/dist/src/__tests__/compiler/tuple-type.test.js +229 -0
  68. package/dist/src/__tests__/compiler/watch-decorator.test.d.ts +1 -0
  69. package/dist/src/__tests__/compiler/watch-decorator.test.js +65 -0
  70. package/dist/src/__tests__/config/project-config.test.d.ts +1 -0
  71. package/dist/src/__tests__/config/project-config.test.js +199 -0
  72. package/dist/src/__tests__/config-decorator.test.d.ts +8 -0
  73. package/dist/src/__tests__/config-decorator.test.js +142 -0
  74. package/dist/src/__tests__/diagnostics-extra.test.d.ts +6 -0
  75. package/dist/src/__tests__/diagnostics-extra.test.js +132 -0
  76. package/dist/src/__tests__/emit/compile-branches.test.d.ts +1 -0
  77. package/dist/src/__tests__/emit/compile-branches.test.js +123 -0
  78. package/dist/src/__tests__/emit/compile-coverage.test.d.ts +25 -0
  79. package/dist/src/__tests__/emit/compile-coverage.test.js +617 -0
  80. package/dist/src/__tests__/emit/compile-extra-branches.test.d.ts +12 -0
  81. package/dist/src/__tests__/emit/compile-extra-branches.test.js +225 -0
  82. package/dist/src/__tests__/emit/compile-mocked-branches.test.d.ts +0 -0
  83. package/dist/src/__tests__/emit/compile-mocked-branches.test.js +238 -0
  84. package/dist/src/__tests__/emit/execute-chain.test.d.ts +10 -0
  85. package/dist/src/__tests__/emit/execute-chain.test.js +94 -0
  86. package/dist/src/__tests__/emit/index.test.js +2 -1
  87. package/dist/src/__tests__/emit/modules-branches.test.d.ts +1 -0
  88. package/dist/src/__tests__/emit/modules-branches.test.js +88 -0
  89. package/dist/src/__tests__/emit/modules-coverage.test.d.ts +15 -0
  90. package/dist/src/__tests__/emit/modules-coverage.test.js +221 -0
  91. package/dist/src/__tests__/emit/modules-errors.test.d.ts +12 -0
  92. package/dist/src/__tests__/emit/modules-errors.test.js +169 -0
  93. package/dist/src/__tests__/emit/modules-rewrite.test.d.ts +17 -0
  94. package/dist/src/__tests__/emit/modules-rewrite.test.js +204 -0
  95. package/dist/src/__tests__/emit/source-map.test.d.ts +1 -0
  96. package/dist/src/__tests__/emit/source-map.test.js +167 -0
  97. package/dist/src/__tests__/enum.test.js +9 -4
  98. package/dist/src/__tests__/error-recovery.test.d.ts +7 -0
  99. package/dist/src/__tests__/error-recovery.test.js +217 -0
  100. package/dist/src/__tests__/events-types-extra.test.d.ts +10 -0
  101. package/dist/src/__tests__/events-types-extra.test.js +91 -0
  102. package/dist/src/__tests__/events-types.test.d.ts +4 -0
  103. package/dist/src/__tests__/events-types.test.js +56 -0
  104. package/dist/src/__tests__/formatter.test.js +13 -5
  105. package/dist/src/__tests__/hir/lower-extra.test.d.ts +9 -0
  106. package/dist/src/__tests__/hir/lower-extra.test.js +140 -0
  107. package/dist/src/__tests__/hir/monomorphize-extra.test.d.ts +15 -0
  108. package/dist/src/__tests__/hir/monomorphize-extra.test.js +200 -0
  109. package/dist/src/__tests__/hir/monomorphize-extra2.test.d.ts +16 -0
  110. package/dist/src/__tests__/hir/monomorphize-extra2.test.js +316 -0
  111. package/dist/src/__tests__/incremental.test.js +10 -2
  112. package/dist/src/__tests__/index-extra.test.d.ts +10 -0
  113. package/dist/src/__tests__/index-extra.test.js +71 -0
  114. package/dist/src/__tests__/lexer.test.js +2 -2
  115. package/dist/src/__tests__/lint/rules.test.d.ts +5 -0
  116. package/dist/src/__tests__/lint/rules.test.js +208 -0
  117. package/dist/src/__tests__/lir/lower.test.js +29 -0
  118. package/dist/src/__tests__/lir/verify.test.js +30 -0
  119. package/dist/src/__tests__/lsp/completion.test.d.ts +7 -0
  120. package/dist/src/__tests__/lsp/completion.test.js +583 -0
  121. package/dist/src/__tests__/lsp/definition.test.d.ts +7 -0
  122. package/dist/src/__tests__/lsp/definition.test.js +454 -0
  123. package/dist/src/__tests__/lsp/diagnostics.test.d.ts +10 -0
  124. package/dist/src/__tests__/lsp/diagnostics.test.js +98 -0
  125. package/dist/src/__tests__/lsp/hover-docs.test.d.ts +10 -0
  126. package/dist/src/__tests__/lsp/hover-docs.test.js +210 -0
  127. package/dist/src/__tests__/lsp.test.js +4 -1
  128. package/dist/src/__tests__/mc-integration/item-entity-events.test.js +4 -0
  129. package/dist/src/__tests__/mc-integration/stdlib-coverage-2.test.js +4 -0
  130. package/dist/src/__tests__/mc-integration/stdlib-coverage-3.test.d.ts +13 -0
  131. package/dist/src/__tests__/mc-integration/stdlib-coverage-3.test.js +1227 -0
  132. package/dist/src/__tests__/mc-integration/stdlib-coverage-4.test.d.ts +13 -0
  133. package/dist/src/__tests__/mc-integration/stdlib-coverage-4.test.js +1509 -0
  134. package/dist/src/__tests__/mc-integration/stdlib-coverage-5.test.d.ts +14 -0
  135. package/dist/src/__tests__/mc-integration/stdlib-coverage-5.test.js +1374 -0
  136. package/dist/src/__tests__/mc-integration/stdlib-coverage-6.test.d.ts +10 -0
  137. package/dist/src/__tests__/mc-integration/stdlib-coverage-6.test.js +759 -0
  138. package/dist/src/__tests__/mc-integration/stdlib-coverage-7.test.d.ts +13 -0
  139. package/dist/src/__tests__/mc-integration/stdlib-coverage-7.test.js +855 -0
  140. package/dist/src/__tests__/mc-integration/stdlib-coverage.test.js +4 -0
  141. package/dist/src/__tests__/mc-integration/syntax-coverage.test.js +4 -0
  142. package/dist/src/__tests__/mc-validator-coverage.test.d.ts +13 -0
  143. package/dist/src/__tests__/mc-validator-coverage.test.js +296 -0
  144. package/dist/src/__tests__/mc-validator-extra.test.d.ts +13 -0
  145. package/dist/src/__tests__/mc-validator-extra.test.js +245 -0
  146. package/dist/src/__tests__/mir/lower-extra.test.d.ts +20 -0
  147. package/dist/src/__tests__/mir/lower-extra.test.js +361 -0
  148. package/dist/src/__tests__/mir/lower-extra2.test.d.ts +17 -0
  149. package/dist/src/__tests__/mir/lower-extra2.test.js +317 -0
  150. package/dist/src/__tests__/mir/lower-extra3.test.d.ts +19 -0
  151. package/dist/src/__tests__/mir/lower-extra3.test.js +249 -0
  152. package/dist/src/__tests__/mir/lower-extra4.test.d.ts +23 -0
  153. package/dist/src/__tests__/mir/lower-extra4.test.js +606 -0
  154. package/dist/src/__tests__/mir/lower-extra5.test.d.ts +25 -0
  155. package/dist/src/__tests__/mir/lower-extra5.test.js +543 -0
  156. package/dist/src/__tests__/mir/lower-extra6.test.d.ts +16 -0
  157. package/dist/src/__tests__/mir/lower-extra6.test.js +471 -0
  158. package/dist/src/__tests__/mir/lower-extra7.test.d.ts +35 -0
  159. package/dist/src/__tests__/mir/lower-extra7.test.js +921 -0
  160. package/dist/src/__tests__/mir/lower-extra8.test.d.ts +19 -0
  161. package/dist/src/__tests__/mir/lower-extra8.test.js +626 -0
  162. package/dist/src/__tests__/mir/lower-extra9.test.d.ts +14 -0
  163. package/dist/src/__tests__/mir/lower-extra9.test.js +717 -0
  164. package/dist/src/__tests__/optimizer/auto-inline.test.d.ts +1 -0
  165. package/dist/src/__tests__/optimizer/auto-inline.test.js +176 -0
  166. package/dist/src/__tests__/optimizer/cse.test.d.ts +4 -0
  167. package/dist/src/__tests__/optimizer/cse.test.js +178 -0
  168. package/dist/src/__tests__/optimizer/inline_fn.test.d.ts +1 -0
  169. package/dist/src/__tests__/optimizer/inline_fn.test.js +221 -0
  170. package/dist/src/__tests__/optimizer/licm.test.d.ts +1 -0
  171. package/dist/src/__tests__/optimizer/licm.test.js +244 -0
  172. package/dist/src/__tests__/optimizer/optimizer-extended.test.d.ts +12 -0
  173. package/dist/src/__tests__/optimizer/optimizer-extended.test.js +993 -0
  174. package/dist/src/__tests__/optimizer/strength-reduction.test.d.ts +1 -0
  175. package/dist/src/__tests__/optimizer/strength-reduction.test.js +86 -0
  176. package/dist/src/__tests__/optimizer/tco.test.d.ts +14 -0
  177. package/dist/src/__tests__/optimizer/tco.test.js +203 -0
  178. package/dist/src/__tests__/parser-coverage.test.d.ts +25 -0
  179. package/dist/src/__tests__/parser-coverage.test.js +491 -0
  180. package/dist/src/__tests__/parser-extra.test.d.ts +6 -0
  181. package/dist/src/__tests__/parser-extra.test.js +451 -0
  182. package/dist/src/__tests__/parser.test.js +12 -0
  183. package/dist/src/__tests__/repl-extra.test.d.ts +13 -0
  184. package/dist/src/__tests__/repl-extra.test.js +174 -0
  185. package/dist/src/__tests__/repl-server-extra.test.d.ts +10 -0
  186. package/dist/src/__tests__/repl-server-extra.test.js +161 -0
  187. package/dist/src/__tests__/repl-server.test.d.ts +6 -0
  188. package/dist/src/__tests__/repl-server.test.js +146 -0
  189. package/dist/src/__tests__/runtime-extra.test.d.ts +15 -0
  190. package/dist/src/__tests__/runtime-extra.test.js +732 -0
  191. package/dist/src/__tests__/singleton-decorator.test.d.ts +11 -0
  192. package/dist/src/__tests__/singleton-decorator.test.js +260 -0
  193. package/dist/src/__tests__/sourcemap.test.js +1 -1
  194. package/dist/src/__tests__/stdlib/advanced.test.d.ts +5 -0
  195. package/dist/src/__tests__/stdlib/advanced.test.js +301 -0
  196. package/dist/src/__tests__/stdlib/bigint.test.d.ts +4 -0
  197. package/dist/src/__tests__/stdlib/bigint.test.js +83 -0
  198. package/dist/src/__tests__/stdlib/bits.test.d.ts +4 -0
  199. package/dist/src/__tests__/stdlib/bits.test.js +96 -0
  200. package/dist/src/__tests__/stdlib/bossbar.test.d.ts +4 -0
  201. package/dist/src/__tests__/stdlib/bossbar.test.js +72 -0
  202. package/dist/src/__tests__/stdlib/color.test.d.ts +4 -0
  203. package/dist/src/__tests__/stdlib/color.test.js +84 -0
  204. package/dist/src/__tests__/stdlib/combat.test.d.ts +4 -0
  205. package/dist/src/__tests__/stdlib/combat.test.js +64 -0
  206. package/dist/src/__tests__/stdlib/cooldown.test.d.ts +4 -0
  207. package/dist/src/__tests__/stdlib/cooldown.test.js +64 -0
  208. package/dist/src/__tests__/stdlib/dialog.test.js +15 -7
  209. package/dist/src/__tests__/stdlib/ecs.test.d.ts +4 -0
  210. package/dist/src/__tests__/stdlib/ecs.test.js +81 -0
  211. package/dist/src/__tests__/stdlib/effects.test.d.ts +4 -0
  212. package/dist/src/__tests__/stdlib/effects.test.js +72 -0
  213. package/dist/src/__tests__/stdlib/events.test.d.ts +4 -0
  214. package/dist/src/__tests__/stdlib/events.test.js +55 -0
  215. package/dist/src/__tests__/stdlib/expr.test.d.ts +4 -0
  216. package/dist/src/__tests__/stdlib/expr.test.js +77 -0
  217. package/dist/src/__tests__/stdlib/fft.test.d.ts +4 -0
  218. package/dist/src/__tests__/stdlib/fft.test.js +82 -0
  219. package/dist/src/__tests__/stdlib/graph.test.d.ts +4 -0
  220. package/dist/src/__tests__/stdlib/graph.test.js +102 -0
  221. package/dist/src/__tests__/stdlib/interactions.test.d.ts +4 -0
  222. package/dist/src/__tests__/stdlib/interactions.test.js +60 -0
  223. package/dist/src/__tests__/stdlib/inventory.test.d.ts +4 -0
  224. package/dist/src/__tests__/stdlib/inventory.test.js +68 -0
  225. package/dist/src/__tests__/stdlib/linalg.test.d.ts +5 -0
  226. package/dist/src/__tests__/stdlib/linalg.test.js +78 -0
  227. package/dist/src/__tests__/stdlib/map.test.d.ts +1 -0
  228. package/dist/src/__tests__/stdlib/map.test.js +84 -0
  229. package/dist/src/__tests__/stdlib/math.test.js +19 -6
  230. package/dist/src/__tests__/stdlib/math_hp.test.d.ts +4 -0
  231. package/dist/src/__tests__/stdlib/math_hp.test.js +80 -0
  232. package/dist/src/__tests__/stdlib/mobs.test.d.ts +4 -0
  233. package/dist/src/__tests__/stdlib/mobs.test.js +61 -0
  234. package/dist/src/__tests__/stdlib/noise.test.d.ts +4 -0
  235. package/dist/src/__tests__/stdlib/noise.test.js +73 -0
  236. package/dist/src/__tests__/stdlib/ode.test.d.ts +4 -0
  237. package/dist/src/__tests__/stdlib/ode.test.js +68 -0
  238. package/dist/src/__tests__/stdlib/parabola.test.d.ts +4 -0
  239. package/dist/src/__tests__/stdlib/parabola.test.js +77 -0
  240. package/dist/src/__tests__/stdlib/particles.test.d.ts +4 -0
  241. package/dist/src/__tests__/stdlib/particles.test.js +68 -0
  242. package/dist/src/__tests__/stdlib/physics.test.d.ts +4 -0
  243. package/dist/src/__tests__/stdlib/physics.test.js +76 -0
  244. package/dist/src/__tests__/stdlib/player.test.d.ts +4 -0
  245. package/dist/src/__tests__/stdlib/player.test.js +64 -0
  246. package/dist/src/__tests__/stdlib/quaternion.test.d.ts +4 -0
  247. package/dist/src/__tests__/stdlib/quaternion.test.js +73 -0
  248. package/dist/src/__tests__/stdlib/queue.test.d.ts +1 -0
  249. package/dist/src/__tests__/stdlib/queue.test.js +97 -0
  250. package/dist/src/__tests__/stdlib/random.test.d.ts +4 -0
  251. package/dist/src/__tests__/stdlib/random.test.js +76 -0
  252. package/dist/src/__tests__/stdlib/result.test.d.ts +12 -0
  253. package/dist/src/__tests__/stdlib/result.test.js +329 -0
  254. package/dist/src/__tests__/stdlib/scheduler.test.js +19 -8
  255. package/dist/src/__tests__/stdlib/set_int.test.d.ts +1 -0
  256. package/dist/src/__tests__/stdlib/set_int.test.js +88 -0
  257. package/dist/src/__tests__/stdlib/sets.test.d.ts +6 -0
  258. package/dist/src/__tests__/stdlib/sets.test.js +60 -0
  259. package/dist/src/__tests__/stdlib/signal.test.d.ts +4 -0
  260. package/dist/src/__tests__/stdlib/signal.test.js +84 -0
  261. package/dist/src/__tests__/stdlib/spawn.test.d.ts +4 -0
  262. package/dist/src/__tests__/stdlib/spawn.test.js +68 -0
  263. package/dist/src/__tests__/stdlib/string.test.d.ts +12 -0
  264. package/dist/src/__tests__/stdlib/string.test.js +231 -0
  265. package/dist/src/__tests__/stdlib/strings.test.d.ts +4 -0
  266. package/dist/src/__tests__/stdlib/strings.test.js +83 -0
  267. package/dist/src/__tests__/stdlib/tags.test.d.ts +4 -0
  268. package/dist/src/__tests__/stdlib/tags.test.js +57 -0
  269. package/dist/src/__tests__/stdlib/teams.test.d.ts +4 -0
  270. package/dist/src/__tests__/stdlib/teams.test.js +72 -0
  271. package/dist/src/__tests__/stdlib/timer.test.d.ts +4 -0
  272. package/dist/src/__tests__/stdlib/timer.test.js +79 -0
  273. package/dist/src/__tests__/stdlib/vec.test.d.ts +5 -0
  274. package/dist/src/__tests__/stdlib/vec.test.js +94 -0
  275. package/dist/src/__tests__/stdlib/world.test.d.ts +4 -0
  276. package/dist/src/__tests__/stdlib/world.test.js +72 -0
  277. package/dist/src/__tests__/struct-display.test.d.ts +1 -0
  278. package/dist/src/__tests__/struct-display.test.js +64 -0
  279. package/dist/src/__tests__/test-framework/runner.test.d.ts +10 -0
  280. package/dist/src/__tests__/test-framework/runner.test.js +193 -0
  281. package/dist/src/__tests__/tuner/adapters.test.d.ts +14 -0
  282. package/dist/src/__tests__/tuner/adapters.test.js +194 -0
  283. package/dist/src/__tests__/tuner/simulator-extra.test.d.ts +4 -0
  284. package/dist/src/__tests__/tuner/simulator-extra.test.js +193 -0
  285. package/dist/src/__tests__/typechecker-coverage.test.d.ts +30 -0
  286. package/dist/src/__tests__/typechecker-coverage.test.js +627 -0
  287. package/dist/src/__tests__/typechecker.test.js +3 -3
  288. package/dist/src/__tests__/watch-decorator.test.d.ts +1 -0
  289. package/dist/src/__tests__/watch-decorator.test.js +54 -0
  290. package/dist/src/ast/types.d.ts +102 -3
  291. package/dist/src/cache/incremental.d.ts +13 -14
  292. package/dist/src/cache/incremental.js +106 -89
  293. package/dist/src/cache/index.d.ts +8 -2
  294. package/dist/src/cache/index.js +18 -6
  295. package/dist/src/cli.d.ts +1 -0
  296. package/dist/src/cli.js +466 -17
  297. package/dist/src/config/project-config.d.ts +29 -0
  298. package/dist/src/config/project-config.js +180 -0
  299. package/dist/src/diagnostics/index.d.ts +9 -0
  300. package/dist/src/diagnostics/index.js +18 -1
  301. package/dist/src/emit/compile.d.ts +10 -0
  302. package/dist/src/emit/compile.js +395 -50
  303. package/dist/src/emit/index.d.ts +40 -0
  304. package/dist/src/emit/index.js +307 -14
  305. package/dist/src/emit/modules.js +21 -3
  306. package/dist/src/emit/sourcemap.d.ts +23 -27
  307. package/dist/src/emit/sourcemap.js +52 -30
  308. package/dist/src/formatter/index.js +33 -8
  309. package/dist/src/hir/deprecated.d.ts +13 -0
  310. package/dist/src/hir/deprecated.js +218 -0
  311. package/dist/src/hir/lower.js +114 -8
  312. package/dist/src/hir/monomorphize.js +22 -2
  313. package/dist/src/hir/types.d.ts +65 -1
  314. package/dist/src/index.d.ts +6 -0
  315. package/dist/src/index.js +18 -3
  316. package/dist/src/lexer/index.d.ts +2 -1
  317. package/dist/src/lexer/index.js +39 -3
  318. package/dist/src/lint/index.d.ts +45 -0
  319. package/dist/src/lint/index.js +930 -0
  320. package/dist/src/lir/lower.js +29 -2
  321. package/dist/src/lir/types.d.ts +2 -0
  322. package/dist/src/lsp/server.js +92 -5
  323. package/dist/src/mir/lower.js +775 -34
  324. package/dist/src/mir/macro.js +36 -2
  325. package/dist/src/mir/types.d.ts +12 -0
  326. package/dist/src/mir/verify.js +9 -0
  327. package/dist/src/optimizer/auto-inline.d.ts +2 -0
  328. package/dist/src/optimizer/auto-inline.js +67 -0
  329. package/dist/src/optimizer/cse.d.ts +20 -0
  330. package/dist/src/optimizer/cse.js +234 -0
  331. package/dist/src/optimizer/inline.d.ts +26 -0
  332. package/dist/src/optimizer/inline.js +286 -0
  333. package/dist/src/optimizer/interprocedural.js +4 -0
  334. package/dist/src/optimizer/licm.d.ts +32 -0
  335. package/dist/src/optimizer/licm.js +371 -0
  336. package/dist/src/optimizer/pipeline.js +12 -2
  337. package/dist/src/optimizer/strength_reduction.d.ts +15 -0
  338. package/dist/src/optimizer/strength_reduction.js +90 -0
  339. package/dist/src/optimizer/tco.d.ts +53 -0
  340. package/dist/src/optimizer/tco.js +238 -0
  341. package/dist/src/parser/index.d.ts +32 -0
  342. package/dist/src/parser/index.js +421 -59
  343. package/dist/src/repl-server.d.ts +13 -0
  344. package/dist/src/repl-server.js +127 -0
  345. package/dist/src/structs/expand.d.ts +15 -0
  346. package/dist/src/structs/expand.js +46 -0
  347. package/dist/src/testing/runner.d.ts +40 -0
  348. package/dist/src/testing/runner.js +237 -0
  349. package/dist/src/typechecker/index.d.ts +3 -0
  350. package/dist/src/typechecker/index.js +254 -9
  351. package/dist/tsconfig.tsbuildinfo +1 -1
  352. package/doc-drafts/redscript-docs/docs/en/stdlib/graph.md +104 -0
  353. package/doc-drafts/redscript-docs/docs/en/stdlib/parabola.md +113 -0
  354. package/doc-drafts/redscript-docs/docs/en/stdlib/pathfind.md +104 -0
  355. package/doc-drafts/redscript-docs/docs/en/stdlib/physics.md +134 -0
  356. package/doc-drafts/redscript-docs/docs/en/stdlib/quaternion.md +135 -0
  357. package/doc-drafts/redscript-docs/docs/zh/stdlib/graph.md +104 -0
  358. package/doc-drafts/redscript-docs/docs/zh/stdlib/parabola.md +113 -0
  359. package/doc-drafts/redscript-docs/docs/zh/stdlib/pathfind.md +104 -0
  360. package/doc-drafts/redscript-docs/docs/zh/stdlib/physics.md +134 -0
  361. package/doc-drafts/redscript-docs/docs/zh/stdlib/quaternion.md +135 -0
  362. package/docs/stdlib/result.md +156 -0
  363. package/docs/stdlib/result.zh.md +156 -0
  364. package/editors/vscode/fixtures/test.mcrs +7 -0
  365. package/editors/vscode/out/extension.js +2095 -225
  366. package/editors/vscode/out/lsp-server.js +519 -51
  367. package/editors/vscode/package-lock.json +9 -4
  368. package/editors/vscode/package.json +1 -1
  369. package/examples/display-demo.mcrs +64 -0
  370. package/examples/game/racing.mcrs +301 -0
  371. package/examples/game/tower_defense.mcrs +311 -0
  372. package/examples/math/physics_sim.mcrs +322 -0
  373. package/examples/rpg/boss_fight.mcrs +313 -0
  374. package/examples/rpg/health_system.mcrs +237 -0
  375. package/examples/rpg/inventory.mcrs +265 -0
  376. package/examples/util/debug_hud.mcrs +279 -0
  377. package/jest.config.js +10 -0
  378. package/package.json +12 -3
  379. package/playground/index.html +823 -0
  380. package/scripts/gen-docs.ts +533 -0
  381. package/scripts/update-redscript-docs-stdlib.sh +770 -0
  382. package/src/__tests__/budget.test.ts +5 -0
  383. package/src/__tests__/cache/cache-behavior.test.ts +480 -0
  384. package/src/__tests__/cache-extra.test.ts +199 -0
  385. package/src/__tests__/cli-docs.test.ts +77 -0
  386. package/src/__tests__/cli-init.test.ts +91 -0
  387. package/src/__tests__/cli-publish.test.ts +190 -0
  388. package/src/__tests__/cli.test.ts +117 -1
  389. package/src/__tests__/compile-preprocess.test.ts +366 -0
  390. package/src/__tests__/compiler/break-stmt.test.ts +66 -0
  391. package/src/__tests__/compiler/const-decl.test.ts +141 -0
  392. package/src/__tests__/compiler/continue-stmt.test.ts +81 -0
  393. package/src/__tests__/compiler/coroutine-extended.test.ts +723 -0
  394. package/src/__tests__/compiler/deprecated.test.ts +305 -0
  395. package/src/__tests__/compiler/do-while.test.ts +130 -0
  396. package/src/__tests__/compiler/enum-payload.test.ts +299 -0
  397. package/src/__tests__/compiler/interface.test.ts +287 -0
  398. package/src/__tests__/compiler/labeled-loops.test.ts +279 -0
  399. package/src/__tests__/compiler/match-string.test.ts +45 -0
  400. package/src/__tests__/compiler/memoize.test.ts +126 -0
  401. package/src/__tests__/compiler/method-chain.test.ts +121 -0
  402. package/src/__tests__/compiler/module-import.test.ts +240 -0
  403. package/src/__tests__/compiler/option-extensions.test.ts +207 -0
  404. package/src/__tests__/compiler/profile-decorator.test.ts +79 -0
  405. package/src/__tests__/compiler/string-advanced.test.ts +310 -0
  406. package/src/__tests__/compiler/struct-extends.test.ts +109 -0
  407. package/src/__tests__/compiler/throttle-retry.test.ts +191 -0
  408. package/src/__tests__/compiler/tuple-type.test.ts +263 -0
  409. package/src/__tests__/compiler/watch-decorator.test.ts +72 -0
  410. package/src/__tests__/config/project-config.test.ts +181 -0
  411. package/src/__tests__/config-decorator.test.ts +157 -0
  412. package/src/__tests__/diagnostics-extra.test.ts +155 -0
  413. package/src/__tests__/emit/compile-branches.test.ts +135 -0
  414. package/src/__tests__/emit/compile-coverage.test.ts +696 -0
  415. package/src/__tests__/emit/compile-extra-branches.test.ts +228 -0
  416. package/src/__tests__/emit/compile-mocked-branches.test.ts +249 -0
  417. package/src/__tests__/emit/compile.test.ts +6 -1
  418. package/src/__tests__/emit/execute-chain.test.ts +114 -0
  419. package/src/__tests__/emit/index.test.ts +2 -1
  420. package/src/__tests__/emit/modules-branches.test.ts +90 -0
  421. package/src/__tests__/emit/modules-coverage.test.ts +241 -0
  422. package/src/__tests__/emit/modules-errors.test.ts +192 -0
  423. package/src/__tests__/emit/modules-rewrite.test.ts +232 -0
  424. package/src/__tests__/emit/source-map.test.ts +152 -0
  425. package/src/__tests__/enum.test.ts +9 -4
  426. package/src/__tests__/error-recovery.test.ts +226 -0
  427. package/src/__tests__/events-types-extra.test.ts +110 -0
  428. package/src/__tests__/events-types.test.ts +66 -0
  429. package/src/__tests__/formatter.test.ts +15 -5
  430. package/src/__tests__/generics.test.ts +16 -9
  431. package/src/__tests__/hir/lower-extra.test.ts +151 -0
  432. package/src/__tests__/hir/monomorphize-coverage.test.ts +432 -0
  433. package/src/__tests__/hir/monomorphize-extra.test.ts +220 -0
  434. package/src/__tests__/hir/monomorphize-extra2.test.ts +350 -0
  435. package/src/__tests__/impl.test.ts +12 -8
  436. package/src/__tests__/incremental.test.ts +10 -2
  437. package/src/__tests__/index-extra.test.ts +79 -0
  438. package/src/__tests__/lexer.test.ts +2 -2
  439. package/src/__tests__/lint/hir-coverage.test.ts +1716 -0
  440. package/src/__tests__/lint/rules-coverage.test.ts +598 -0
  441. package/src/__tests__/lint/rules.test.ts +230 -0
  442. package/src/__tests__/lir/lower.test.ts +33 -0
  443. package/src/__tests__/lir/verify.test.ts +33 -0
  444. package/src/__tests__/lsp/completion.test.ts +687 -0
  445. package/src/__tests__/lsp/definition.test.ts +499 -0
  446. package/src/__tests__/lsp/diagnostics.test.ts +108 -0
  447. package/src/__tests__/lsp/hover-docs.test.ts +222 -0
  448. package/src/__tests__/lsp.test.ts +4 -1
  449. package/src/__tests__/mc-integration/item-entity-events.test.ts +5 -0
  450. package/src/__tests__/mc-integration/stdlib-coverage-2.test.ts +5 -0
  451. package/src/__tests__/mc-integration/stdlib-coverage-3.test.ts +1105 -0
  452. package/src/__tests__/mc-integration/stdlib-coverage-4.test.ts +1366 -0
  453. package/src/__tests__/mc-integration/stdlib-coverage-5.test.ts +1245 -0
  454. package/src/__tests__/mc-integration/stdlib-coverage-6.test.ts +755 -0
  455. package/src/__tests__/mc-integration/stdlib-coverage-7.test.ts +771 -0
  456. package/src/__tests__/mc-integration/stdlib-coverage.test.ts +5 -0
  457. package/src/__tests__/mc-integration/syntax-coverage.test.ts +5 -0
  458. package/src/__tests__/mc-validator-coverage.test.ts +325 -0
  459. package/src/__tests__/mc-validator-extra.test.ts +252 -0
  460. package/src/__tests__/mir/lower-extra.test.ts +402 -0
  461. package/src/__tests__/mir/lower-extra2.test.ts +348 -0
  462. package/src/__tests__/mir/lower-extra3.test.ts +277 -0
  463. package/src/__tests__/mir/lower-extra4.test.ts +636 -0
  464. package/src/__tests__/mir/lower-extra5.test.ts +612 -0
  465. package/src/__tests__/mir/lower-extra6.test.ts +520 -0
  466. package/src/__tests__/mir/lower-extra7.test.ts +1045 -0
  467. package/src/__tests__/mir/lower-extra8.test.ts +704 -0
  468. package/src/__tests__/mir/lower-extra9.test.ts +821 -0
  469. package/src/__tests__/optimizer/auto-inline.test.ts +206 -0
  470. package/src/__tests__/optimizer/cse.test.ts +195 -0
  471. package/src/__tests__/optimizer/inline_fn.test.ts +263 -0
  472. package/src/__tests__/optimizer/licm.test.ts +358 -0
  473. package/src/__tests__/optimizer/nbt-coalesce.test.ts +147 -0
  474. package/src/__tests__/optimizer/optimizer-extended.test.ts +1081 -0
  475. package/src/__tests__/optimizer/scoreboard-batch.test.ts +141 -0
  476. package/src/__tests__/optimizer/strength-reduction.test.ts +111 -0
  477. package/src/__tests__/optimizer/tco-coverage.test.ts +309 -0
  478. package/src/__tests__/optimizer/tco.test.ts +238 -0
  479. package/src/__tests__/option.test.ts +14 -7
  480. package/src/__tests__/parser-coverage.test.ts +576 -0
  481. package/src/__tests__/parser-extra.test.ts +531 -0
  482. package/src/__tests__/parser.test.ts +14 -0
  483. package/src/__tests__/repl-extra.test.ts +195 -0
  484. package/src/__tests__/repl-server-extra.test.ts +150 -0
  485. package/src/__tests__/repl-server.test.ts +122 -0
  486. package/src/__tests__/runtime-extra.test.ts +862 -0
  487. package/src/__tests__/singleton-decorator.test.ts +285 -0
  488. package/src/__tests__/sourcemap.test.ts +1 -1
  489. package/src/__tests__/stdlib/advanced.test.ts +312 -0
  490. package/src/__tests__/stdlib/bigint.test.ts +57 -0
  491. package/src/__tests__/stdlib/bits.test.ts +75 -0
  492. package/src/__tests__/stdlib/bossbar.test.ts +45 -0
  493. package/src/__tests__/stdlib/color.test.ts +60 -0
  494. package/src/__tests__/stdlib/combat.test.ts +35 -0
  495. package/src/__tests__/stdlib/cooldown.test.ts +35 -0
  496. package/src/__tests__/stdlib/dialog.test.ts +14 -6
  497. package/src/__tests__/stdlib/ecs.test.ts +54 -0
  498. package/src/__tests__/stdlib/effects.test.ts +45 -0
  499. package/src/__tests__/stdlib/events.test.ts +23 -0
  500. package/src/__tests__/stdlib/expr.test.ts +48 -0
  501. package/src/__tests__/stdlib/fft.test.ts +54 -0
  502. package/src/__tests__/stdlib/graph.test.ts +77 -0
  503. package/src/__tests__/stdlib/interactions.test.ts +30 -0
  504. package/src/__tests__/stdlib/inventory.test.ts +40 -0
  505. package/src/__tests__/stdlib/linalg.test.ts +52 -0
  506. package/src/__tests__/stdlib/map.test.ts +55 -0
  507. package/src/__tests__/stdlib/math.test.ts +19 -5
  508. package/src/__tests__/stdlib/math_hp.test.ts +55 -0
  509. package/src/__tests__/stdlib/mobs.test.ts +40 -0
  510. package/src/__tests__/stdlib/noise.test.ts +46 -0
  511. package/src/__tests__/stdlib/ode.test.ts +40 -0
  512. package/src/__tests__/stdlib/parabola.test.ts +51 -0
  513. package/src/__tests__/stdlib/particles.test.ts +40 -0
  514. package/src/__tests__/stdlib/physics.test.ts +50 -0
  515. package/src/__tests__/stdlib/player.test.ts +35 -0
  516. package/src/__tests__/stdlib/quaternion.test.ts +46 -0
  517. package/src/__tests__/stdlib/queue.test.ts +73 -0
  518. package/src/__tests__/stdlib/random.test.ts +50 -0
  519. package/src/__tests__/stdlib/result.test.ts +326 -0
  520. package/src/__tests__/stdlib/scheduler.test.ts +18 -7
  521. package/src/__tests__/stdlib/set_int.test.ts +62 -0
  522. package/src/__tests__/stdlib/sets.test.ts +28 -0
  523. package/src/__tests__/stdlib/signal.test.ts +60 -0
  524. package/src/__tests__/stdlib/spawn.test.ts +40 -0
  525. package/src/__tests__/stdlib/string.test.ts +224 -0
  526. package/src/__tests__/stdlib/strings.test.ts +55 -0
  527. package/src/__tests__/stdlib/tags.test.ts +32 -0
  528. package/src/__tests__/stdlib/teams.test.ts +45 -0
  529. package/src/__tests__/stdlib/timer.test.ts +53 -0
  530. package/src/__tests__/stdlib/vec.test.ts +72 -0
  531. package/src/__tests__/stdlib/world.test.ts +45 -0
  532. package/src/__tests__/struct-display.test.ts +69 -0
  533. package/src/__tests__/test-framework/runner.test.ts +208 -0
  534. package/src/__tests__/tuner/adapters.test.ts +232 -0
  535. package/src/__tests__/tuner/simulator-extra.test.ts +222 -0
  536. package/src/__tests__/tuple.test.ts +11 -4
  537. package/src/__tests__/typechecker-coverage.test.ts +671 -0
  538. package/src/__tests__/typechecker.test.ts +4 -3
  539. package/src/__tests__/watch-decorator.test.ts +59 -0
  540. package/src/ast/types.ts +65 -3
  541. package/src/cache/incremental.ts +128 -99
  542. package/src/cache/index.ts +35 -8
  543. package/src/cli.ts +538 -29
  544. package/src/config/project-config.ts +176 -0
  545. package/src/diagnostics/index.ts +22 -0
  546. package/src/docs.ts +98 -0
  547. package/src/emit/compile.ts +408 -51
  548. package/src/emit/index.ts +366 -18
  549. package/src/emit/modules.ts +19 -3
  550. package/src/emit/sourcemap.ts +64 -43
  551. package/src/formatter/index.ts +35 -8
  552. package/src/hir/deprecated.ts +212 -0
  553. package/src/hir/lower.ts +128 -8
  554. package/src/hir/monomorphize.ts +24 -2
  555. package/src/hir/types.ts +26 -1
  556. package/src/index.ts +23 -3
  557. package/src/lexer/index.ts +45 -6
  558. package/src/lint/index.ts +922 -0
  559. package/src/lir/lower.ts +30 -2
  560. package/src/lir/types.ts +4 -0
  561. package/src/lsp/server.ts +100 -1
  562. package/src/mir/lower.ts +785 -40
  563. package/src/mir/macro.ts +30 -2
  564. package/src/mir/types.ts +13 -0
  565. package/src/mir/verify.ts +10 -2
  566. package/src/optimizer/auto-inline.ts +86 -0
  567. package/src/optimizer/copy_prop.ts +2 -2
  568. package/src/optimizer/coroutine.ts +3 -3
  569. package/src/optimizer/cse.ts +205 -0
  570. package/src/optimizer/dce.ts +2 -2
  571. package/src/optimizer/inline.ts +335 -0
  572. package/src/optimizer/interprocedural.ts +5 -1
  573. package/src/optimizer/licm.ts +454 -0
  574. package/src/optimizer/nbt-coalesce.ts +109 -0
  575. package/src/optimizer/pipeline.ts +16 -2
  576. package/src/optimizer/scoreboard-batch.ts +52 -0
  577. package/src/optimizer/strength_reduction.ts +95 -0
  578. package/src/optimizer/tco.ts +267 -0
  579. package/src/optimizer/unroll.ts +2 -2
  580. package/src/parser/index.ts +426 -53
  581. package/src/repl-server.ts +102 -0
  582. package/src/stdlib/advanced.mcrs +271 -101
  583. package/src/stdlib/bigint.mcrs +97 -11
  584. package/src/stdlib/bits.mcrs +75 -12
  585. package/src/stdlib/bossbar.mcrs +37 -8
  586. package/src/stdlib/calculus.mcrs +82 -26
  587. package/src/stdlib/color.mcrs +98 -16
  588. package/src/stdlib/combat.mcrs +23 -5
  589. package/src/stdlib/cooldown.mcrs +19 -0
  590. package/src/stdlib/dialog.mcrs +45 -7
  591. package/src/stdlib/easing.mcrs +132 -12
  592. package/src/stdlib/ecs.mcrs +142 -25
  593. package/src/stdlib/effects.mcrs +88 -12
  594. package/src/stdlib/events.mcrs +21 -2
  595. package/src/stdlib/expr.mcrs +18 -3
  596. package/src/stdlib/fft.mcrs +66 -56
  597. package/src/stdlib/geometry.mcrs +137 -39
  598. package/src/stdlib/graph.mcrs +73 -0
  599. package/src/stdlib/heap.mcrs +49 -8
  600. package/src/stdlib/i18n/zh.yaml +2891 -0
  601. package/src/stdlib/interactions.mcrs +43 -20
  602. package/src/stdlib/inventory.mcrs +14 -3
  603. package/src/stdlib/linalg.mcrs +185 -30
  604. package/src/stdlib/list.mcrs +168 -18
  605. package/src/stdlib/map.mcrs +112 -0
  606. package/src/stdlib/math.mcrs +68 -18
  607. package/src/stdlib/math_hp.mcrs +124 -33
  608. package/src/stdlib/matrix.mcrs +133 -20
  609. package/src/stdlib/mobs.mcrs +87 -0
  610. package/src/stdlib/noise.mcrs +65 -21
  611. package/src/stdlib/ode.mcrs +96 -0
  612. package/src/stdlib/parabola.mcrs +104 -29
  613. package/src/stdlib/particles.mcrs +78 -21
  614. package/src/stdlib/pathfind.mcrs +89 -35
  615. package/src/stdlib/physics.mcrs +134 -26
  616. package/src/stdlib/player.mcrs +18 -0
  617. package/src/stdlib/quaternion.mcrs +213 -9
  618. package/src/stdlib/queue.mcrs +123 -0
  619. package/src/stdlib/random.mcrs +63 -18
  620. package/src/stdlib/result.mcrs +111 -0
  621. package/src/stdlib/scheduler.mcrs +59 -10
  622. package/src/stdlib/set_int.mcrs +240 -0
  623. package/src/stdlib/sets.mcrs +49 -19
  624. package/src/stdlib/signal.mcrs +151 -79
  625. package/src/stdlib/sort.mcrs +44 -24
  626. package/src/stdlib/spawn.mcrs +30 -7
  627. package/src/stdlib/state.mcrs +40 -5
  628. package/src/stdlib/strings.mcrs +131 -3
  629. package/src/stdlib/tags.mcrs +2 -2
  630. package/src/stdlib/teams.mcrs +22 -10
  631. package/src/stdlib/timer.mcrs +36 -6
  632. package/src/stdlib/vec.mcrs +44 -9
  633. package/src/stdlib/world.mcrs +57 -25
  634. package/src/structs/expand.ts +64 -0
  635. package/src/testing/runner.ts +271 -0
  636. package/src/typechecker/index.ts +273 -9
@@ -0,0 +1,1716 @@
1
+ /**
2
+ * Direct HIR injection tests for src/lint/index.ts
3
+ * Covers branches that require specific HIR statement/expression kinds
4
+ * which cannot easily be produced by parsing RedScript source code.
5
+ */
6
+
7
+ import { lintSource, lintFile, formatLintWarning } from '../../lint/index'
8
+ import type { ImportDecl } from '../../ast/types'
9
+ import type {
10
+ HIRModule,
11
+ HIRFunction,
12
+ HIRStmt,
13
+ HIRExpr,
14
+ HIRBlock,
15
+ } from '../../hir/types'
16
+ import * as fs from 'fs'
17
+ import * as path from 'path'
18
+ import * as os from 'os'
19
+
20
+ // ---------------------------------------------------------------------------
21
+ // Helpers
22
+ // ---------------------------------------------------------------------------
23
+
24
+ function makeModule(fns: HIRFunction[]): HIRModule {
25
+ return {
26
+ namespace: 'test',
27
+ globals: [],
28
+ functions: fns,
29
+ structs: [],
30
+ implBlocks: [],
31
+ enums: [],
32
+ consts: [],
33
+ }
34
+ }
35
+
36
+ function makeSimpleFn(name: string, body: HIRBlock): HIRFunction {
37
+ return {
38
+ name,
39
+ params: [],
40
+ returnType: { kind: 'named', name: 'void' } as any,
41
+ decorators: [],
42
+ body,
43
+ }
44
+ }
45
+
46
+ function makeIntLit(value: number): HIRExpr {
47
+ return { kind: 'int_lit', value }
48
+ }
49
+
50
+ function makeIdent(name: string): HIRExpr {
51
+ return { kind: 'ident', name }
52
+ }
53
+
54
+ function makeBoolLit(value: boolean): HIRExpr {
55
+ return { kind: 'bool_lit', value }
56
+ }
57
+
58
+ // ---------------------------------------------------------------------------
59
+ // lintSource: collectCalledNamesStmt — extra branches
60
+ // ---------------------------------------------------------------------------
61
+
62
+ describe('collectCalledNamesStmt — all HIR statement kinds', () => {
63
+ // Used to test that import "foo" is considered used when "foo" is called
64
+ const importFoo: ImportDecl = { moduleName: 'mylib', symbol: 'foo' }
65
+
66
+ it('handles const_decl with a call in value', () => {
67
+ const body: HIRBlock = [
68
+ {
69
+ kind: 'const_decl',
70
+ name: 'X',
71
+ type: { kind: 'named', name: 'int' } as any,
72
+ value: { kind: 'call', fn: 'foo', args: [] },
73
+ },
74
+ ]
75
+ const hir = makeModule([makeSimpleFn('bar', body)])
76
+ const w = lintSource('', [importFoo], hir)
77
+ // foo is called in const_decl → no unused-import warning
78
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
79
+ })
80
+
81
+ it('handles let_destruct with a call in init', () => {
82
+ const body: HIRBlock = [
83
+ {
84
+ kind: 'let_destruct',
85
+ names: ['a', 'b'],
86
+ init: { kind: 'call', fn: 'foo', args: [] },
87
+ },
88
+ ]
89
+ const hir = makeModule([makeSimpleFn('bar', body)])
90
+ const w = lintSource('', [importFoo], hir)
91
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
92
+ })
93
+
94
+ it('handles return with a call in value', () => {
95
+ const body: HIRBlock = [
96
+ {
97
+ kind: 'return',
98
+ value: { kind: 'call', fn: 'foo', args: [] },
99
+ },
100
+ ]
101
+ const hir = makeModule([makeSimpleFn('bar', body)])
102
+ const w = lintSource('', [importFoo], hir)
103
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
104
+ })
105
+
106
+ it('handles expr statement with a call', () => {
107
+ const body: HIRBlock = [
108
+ {
109
+ kind: 'expr',
110
+ expr: { kind: 'call', fn: 'foo', args: [] },
111
+ },
112
+ ]
113
+ const hir = makeModule([makeSimpleFn('bar', body)])
114
+ const w = lintSource('', [importFoo], hir)
115
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
116
+ })
117
+
118
+ it('handles if statement: call in cond, then, else', () => {
119
+ const body: HIRBlock = [
120
+ {
121
+ kind: 'if',
122
+ cond: { kind: 'call', fn: 'foo', args: [] },
123
+ then: [],
124
+ else_: [],
125
+ },
126
+ ]
127
+ const hir = makeModule([makeSimpleFn('bar', body)])
128
+ const w = lintSource('', [importFoo], hir)
129
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
130
+ })
131
+
132
+ it('handles while statement: call in cond and body', () => {
133
+ const body: HIRBlock = [
134
+ {
135
+ kind: 'while',
136
+ cond: { kind: 'bool_lit', value: false },
137
+ body: [{ kind: 'expr', expr: { kind: 'call', fn: 'foo', args: [] } }],
138
+ step: [{ kind: 'expr', expr: { kind: 'call', fn: 'foo', args: [] } }],
139
+ },
140
+ ]
141
+ const hir = makeModule([makeSimpleFn('bar', body)])
142
+ const w = lintSource('', [importFoo], hir)
143
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
144
+ })
145
+
146
+ it('handles foreach statement: call in iterable and body', () => {
147
+ const body: HIRBlock = [
148
+ {
149
+ kind: 'foreach',
150
+ binding: 'p',
151
+ iterable: { kind: 'ident', name: 'list' },
152
+ body: [{ kind: 'expr', expr: { kind: 'call', fn: 'foo', args: [] } }],
153
+ },
154
+ ]
155
+ const hir = makeModule([makeSimpleFn('bar', body)])
156
+ const w = lintSource('', [importFoo], hir)
157
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
158
+ })
159
+
160
+ it('handles match statement: call in arms', () => {
161
+ const body: HIRBlock = [
162
+ {
163
+ kind: 'match',
164
+ expr: { kind: 'ident', name: 'x' },
165
+ arms: [
166
+ {
167
+ pattern: { kind: 'PatWild' },
168
+ body: [{ kind: 'expr', expr: { kind: 'call', fn: 'foo', args: [] } }],
169
+ },
170
+ ],
171
+ },
172
+ ]
173
+ const hir = makeModule([makeSimpleFn('bar', body)])
174
+ const w = lintSource('', [importFoo], hir)
175
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
176
+ })
177
+
178
+ it('handles execute statement: call in body', () => {
179
+ const body: HIRBlock = [
180
+ {
181
+ kind: 'execute',
182
+ subcommands: [],
183
+ body: [{ kind: 'expr', expr: { kind: 'call', fn: 'foo', args: [] } }],
184
+ },
185
+ ]
186
+ const hir = makeModule([makeSimpleFn('bar', body)])
187
+ const w = lintSource('', [importFoo], hir)
188
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
189
+ })
190
+
191
+ it('handles if_let_some statement: call in then and else', () => {
192
+ const body: HIRBlock = [
193
+ {
194
+ kind: 'if_let_some',
195
+ binding: 'val',
196
+ init: { kind: 'ident', name: 'opt' },
197
+ then: [{ kind: 'expr', expr: { kind: 'call', fn: 'foo', args: [] } }],
198
+ else_: [],
199
+ },
200
+ ]
201
+ const hir = makeModule([makeSimpleFn('bar', body)])
202
+ const w = lintSource('', [importFoo], hir)
203
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
204
+ })
205
+
206
+ it('handles while_let_some statement: call in body', () => {
207
+ const body: HIRBlock = [
208
+ {
209
+ kind: 'while_let_some',
210
+ binding: 'val',
211
+ init: { kind: 'ident', name: 'opt' },
212
+ body: [{ kind: 'expr', expr: { kind: 'call', fn: 'foo', args: [] } }],
213
+ },
214
+ ]
215
+ const hir = makeModule([makeSimpleFn('bar', body)])
216
+ const w = lintSource('', [importFoo], hir)
217
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
218
+ })
219
+
220
+ it('handles labeled_loop statement: call in body', () => {
221
+ const body: HIRBlock = [
222
+ {
223
+ kind: 'labeled_loop',
224
+ label: 'outer',
225
+ body: {
226
+ kind: 'expr',
227
+ expr: { kind: 'call', fn: 'foo', args: [] },
228
+ } as HIRStmt,
229
+ },
230
+ ]
231
+ const hir = makeModule([makeSimpleFn('bar', body)])
232
+ const w = lintSource('', [importFoo], hir)
233
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
234
+ })
235
+ })
236
+
237
+ // ---------------------------------------------------------------------------
238
+ // collectCalledNamesExpr — all expression kinds
239
+ // ---------------------------------------------------------------------------
240
+
241
+ describe('collectCalledNamesExpr — all expression kinds', () => {
242
+ const importFoo: ImportDecl = { moduleName: 'mylib', symbol: 'foo' }
243
+
244
+ it('handles invoke expression', () => {
245
+ const body: HIRBlock = [
246
+ {
247
+ kind: 'expr',
248
+ expr: {
249
+ kind: 'invoke',
250
+ callee: { kind: 'ident', name: 'obj' },
251
+ args: [{ kind: 'call', fn: 'foo', args: [] }],
252
+ },
253
+ },
254
+ ]
255
+ const hir = makeModule([makeSimpleFn('bar', body)])
256
+ const w = lintSource('', [importFoo], hir)
257
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
258
+ })
259
+
260
+ it('handles static_call expression', () => {
261
+ const body: HIRBlock = [
262
+ {
263
+ kind: 'expr',
264
+ expr: {
265
+ kind: 'static_call',
266
+ type: 'Math',
267
+ method: 'foo',
268
+ args: [],
269
+ },
270
+ },
271
+ ]
272
+ const hir = makeModule([makeSimpleFn('bar', body)])
273
+ const w = lintSource('', [importFoo], hir)
274
+ // static_call adds "foo" to calledNames
275
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
276
+ })
277
+
278
+ it('handles binary expression with call in operands', () => {
279
+ const body: HIRBlock = [
280
+ {
281
+ kind: 'expr',
282
+ expr: {
283
+ kind: 'binary',
284
+ op: '+',
285
+ left: { kind: 'call', fn: 'foo', args: [] },
286
+ right: makeIntLit(1),
287
+ },
288
+ },
289
+ ]
290
+ const hir = makeModule([makeSimpleFn('bar', body)])
291
+ const w = lintSource('', [importFoo], hir)
292
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
293
+ })
294
+
295
+ it('handles unary expression with call in operand', () => {
296
+ const body: HIRBlock = [
297
+ {
298
+ kind: 'expr',
299
+ expr: {
300
+ kind: 'unary',
301
+ op: '-',
302
+ operand: { kind: 'call', fn: 'foo', args: [] },
303
+ },
304
+ },
305
+ ]
306
+ const hir = makeModule([makeSimpleFn('bar', body)])
307
+ const w = lintSource('', [importFoo], hir)
308
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
309
+ })
310
+
311
+ it('handles member expression', () => {
312
+ const body: HIRBlock = [
313
+ {
314
+ kind: 'expr',
315
+ expr: {
316
+ kind: 'member',
317
+ obj: { kind: 'call', fn: 'foo', args: [] },
318
+ field: 'x',
319
+ },
320
+ },
321
+ ]
322
+ const hir = makeModule([makeSimpleFn('bar', body)])
323
+ const w = lintSource('', [importFoo], hir)
324
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
325
+ })
326
+
327
+ it('handles member_assign expression', () => {
328
+ const body: HIRBlock = [
329
+ {
330
+ kind: 'expr',
331
+ expr: {
332
+ kind: 'member_assign',
333
+ obj: { kind: 'call', fn: 'foo', args: [] },
334
+ field: 'x',
335
+ value: makeIntLit(1),
336
+ },
337
+ },
338
+ ]
339
+ const hir = makeModule([makeSimpleFn('bar', body)])
340
+ const w = lintSource('', [importFoo], hir)
341
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
342
+ })
343
+
344
+ it('handles index expression', () => {
345
+ const body: HIRBlock = [
346
+ {
347
+ kind: 'expr',
348
+ expr: {
349
+ kind: 'index',
350
+ obj: { kind: 'call', fn: 'foo', args: [] },
351
+ index: makeIntLit(0),
352
+ },
353
+ },
354
+ ]
355
+ const hir = makeModule([makeSimpleFn('bar', body)])
356
+ const w = lintSource('', [importFoo], hir)
357
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
358
+ })
359
+
360
+ it('handles index_assign expression', () => {
361
+ const body: HIRBlock = [
362
+ {
363
+ kind: 'expr',
364
+ expr: {
365
+ kind: 'index_assign',
366
+ obj: { kind: 'call', fn: 'foo', args: [] },
367
+ index: makeIntLit(0),
368
+ op: '=',
369
+ value: makeIntLit(1),
370
+ },
371
+ },
372
+ ]
373
+ const hir = makeModule([makeSimpleFn('bar', body)])
374
+ const w = lintSource('', [importFoo], hir)
375
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
376
+ })
377
+
378
+ it('handles assign expression', () => {
379
+ const body: HIRBlock = [
380
+ {
381
+ kind: 'expr',
382
+ expr: {
383
+ kind: 'assign',
384
+ target: 'x',
385
+ value: { kind: 'call', fn: 'foo', args: [] },
386
+ },
387
+ },
388
+ ]
389
+ const hir = makeModule([makeSimpleFn('bar', body)])
390
+ const w = lintSource('', [importFoo], hir)
391
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
392
+ })
393
+
394
+ it('handles some_lit expression', () => {
395
+ const body: HIRBlock = [
396
+ {
397
+ kind: 'expr',
398
+ expr: {
399
+ kind: 'some_lit',
400
+ value: { kind: 'call', fn: 'foo', args: [] },
401
+ },
402
+ },
403
+ ]
404
+ const hir = makeModule([makeSimpleFn('bar', body)])
405
+ const w = lintSource('', [importFoo], hir)
406
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
407
+ })
408
+
409
+ it('handles unwrap_or expression', () => {
410
+ const body: HIRBlock = [
411
+ {
412
+ kind: 'expr',
413
+ expr: {
414
+ kind: 'unwrap_or',
415
+ opt: { kind: 'call', fn: 'foo', args: [] },
416
+ default_: makeIntLit(0),
417
+ },
418
+ },
419
+ ]
420
+ const hir = makeModule([makeSimpleFn('bar', body)])
421
+ const w = lintSource('', [importFoo], hir)
422
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
423
+ })
424
+
425
+ it('handles type_cast expression', () => {
426
+ const body: HIRBlock = [
427
+ {
428
+ kind: 'expr',
429
+ expr: {
430
+ kind: 'type_cast',
431
+ expr: { kind: 'call', fn: 'foo', args: [] },
432
+ targetType: { kind: 'named', name: 'int' } as any,
433
+ },
434
+ },
435
+ ]
436
+ const hir = makeModule([makeSimpleFn('bar', body)])
437
+ const w = lintSource('', [importFoo], hir)
438
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
439
+ })
440
+
441
+ it('handles array_lit expression', () => {
442
+ const body: HIRBlock = [
443
+ {
444
+ kind: 'expr',
445
+ expr: {
446
+ kind: 'array_lit',
447
+ elements: [{ kind: 'call', fn: 'foo', args: [] }],
448
+ },
449
+ },
450
+ ]
451
+ const hir = makeModule([makeSimpleFn('bar', body)])
452
+ const w = lintSource('', [importFoo], hir)
453
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
454
+ })
455
+
456
+ it('handles tuple_lit expression', () => {
457
+ const body: HIRBlock = [
458
+ {
459
+ kind: 'expr',
460
+ expr: {
461
+ kind: 'tuple_lit',
462
+ elements: [{ kind: 'call', fn: 'foo', args: [] }],
463
+ },
464
+ },
465
+ ]
466
+ const hir = makeModule([makeSimpleFn('bar', body)])
467
+ const w = lintSource('', [importFoo], hir)
468
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
469
+ })
470
+
471
+ it('handles struct_lit expression', () => {
472
+ const body: HIRBlock = [
473
+ {
474
+ kind: 'expr',
475
+ expr: {
476
+ kind: 'struct_lit',
477
+ fields: [{ name: 'x', value: { kind: 'call', fn: 'foo', args: [] } }],
478
+ },
479
+ },
480
+ ]
481
+ const hir = makeModule([makeSimpleFn('bar', body)])
482
+ const w = lintSource('', [importFoo], hir)
483
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
484
+ })
485
+
486
+ it('handles str_interp expression with non-string parts', () => {
487
+ const body: HIRBlock = [
488
+ {
489
+ kind: 'expr',
490
+ expr: {
491
+ kind: 'str_interp',
492
+ parts: ['hello ', { kind: 'call', fn: 'foo', args: [] }],
493
+ },
494
+ },
495
+ ]
496
+ const hir = makeModule([makeSimpleFn('bar', body)])
497
+ const w = lintSource('', [importFoo], hir)
498
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
499
+ })
500
+
501
+ it('handles f_string expression', () => {
502
+ const body: HIRBlock = [
503
+ {
504
+ kind: 'expr',
505
+ expr: {
506
+ kind: 'f_string',
507
+ parts: [
508
+ { kind: 'text', value: 'hello' },
509
+ { kind: 'expr', expr: { kind: 'call', fn: 'foo', args: [] } },
510
+ ],
511
+ },
512
+ },
513
+ ]
514
+ const hir = makeModule([makeSimpleFn('bar', body)])
515
+ const w = lintSource('', [importFoo], hir)
516
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
517
+ })
518
+
519
+ it('handles lambda expression (block body)', () => {
520
+ const body: HIRBlock = [
521
+ {
522
+ kind: 'expr',
523
+ expr: {
524
+ kind: 'lambda',
525
+ params: [],
526
+ body: [{ kind: 'expr', expr: { kind: 'call', fn: 'foo', args: [] } }] as HIRBlock,
527
+ },
528
+ },
529
+ ]
530
+ const hir = makeModule([makeSimpleFn('bar', body)])
531
+ const w = lintSource('', [importFoo], hir)
532
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
533
+ })
534
+
535
+ it('handles lambda expression (expr body)', () => {
536
+ const body: HIRBlock = [
537
+ {
538
+ kind: 'expr',
539
+ expr: {
540
+ kind: 'lambda',
541
+ params: [],
542
+ body: { kind: 'call', fn: 'foo', args: [] } as HIRExpr,
543
+ },
544
+ },
545
+ ]
546
+ const hir = makeModule([makeSimpleFn('bar', body)])
547
+ const w = lintSource('', [importFoo], hir)
548
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
549
+ })
550
+
551
+ it('handles enum_construct expression', () => {
552
+ const body: HIRBlock = [
553
+ {
554
+ kind: 'expr',
555
+ expr: {
556
+ kind: 'enum_construct',
557
+ enumName: 'Color',
558
+ variant: 'RGB',
559
+ args: [{ name: 'r', value: { kind: 'call', fn: 'foo', args: [] } }],
560
+ },
561
+ },
562
+ ]
563
+ const hir = makeModule([makeSimpleFn('bar', body)])
564
+ const w = lintSource('', [importFoo], hir)
565
+ expect(w.filter(x => x.rule === 'unused-import')).toHaveLength(0)
566
+ })
567
+ })
568
+
569
+ // ---------------------------------------------------------------------------
570
+ // unused-variable: collectLetDeclsStmt — extra HIR branches
571
+ // ---------------------------------------------------------------------------
572
+
573
+ describe('unused-variable via collectLetDeclsStmt', () => {
574
+ it('tracks variables declared in if body', () => {
575
+ const body: HIRBlock = [
576
+ {
577
+ kind: 'if',
578
+ cond: makeBoolLit(true),
579
+ then: [{ kind: 'let', name: 'x', init: makeIntLit(5) }],
580
+ },
581
+ ]
582
+ const hir = makeModule([makeSimpleFn('bar', body)])
583
+ const w = lintSource('', [], hir)
584
+ expect(w.some(x => x.rule === 'unused-variable' && x.message.includes('"x"'))).toBe(true)
585
+ })
586
+
587
+ it('tracks variables declared in if else body', () => {
588
+ const body: HIRBlock = [
589
+ {
590
+ kind: 'if',
591
+ cond: makeBoolLit(true),
592
+ then: [],
593
+ else_: [{ kind: 'let', name: 'y', init: makeIntLit(5) }],
594
+ },
595
+ ]
596
+ const hir = makeModule([makeSimpleFn('bar', body)])
597
+ const w = lintSource('', [], hir)
598
+ expect(w.some(x => x.rule === 'unused-variable' && x.message.includes('"y"'))).toBe(true)
599
+ })
600
+
601
+ it('tracks variables declared in while body', () => {
602
+ const body: HIRBlock = [
603
+ {
604
+ kind: 'while',
605
+ cond: makeBoolLit(false),
606
+ body: [{ kind: 'let', name: 'z', init: makeIntLit(5) }],
607
+ step: [{ kind: 'let', name: 'w', init: makeIntLit(5) }],
608
+ },
609
+ ]
610
+ const hir = makeModule([makeSimpleFn('bar', body)])
611
+ const w = lintSource('', [], hir)
612
+ expect(w.some(x => x.rule === 'unused-variable' && x.message.includes('"z"'))).toBe(true)
613
+ expect(w.some(x => x.rule === 'unused-variable' && x.message.includes('"w"'))).toBe(true)
614
+ })
615
+
616
+ it('tracks variables declared in foreach body', () => {
617
+ const body: HIRBlock = [
618
+ {
619
+ kind: 'foreach',
620
+ binding: 'item',
621
+ iterable: { kind: 'ident', name: 'list' },
622
+ body: [{ kind: 'let', name: 'tmp', init: makeIntLit(5) }],
623
+ },
624
+ ]
625
+ const hir = makeModule([makeSimpleFn('bar', body)])
626
+ const w = lintSource('', [], hir)
627
+ // Both 'item' (binding) and 'tmp' should be unused
628
+ const names = w.filter(x => x.rule === 'unused-variable').map(x => x.message)
629
+ expect(names.some(m => m.includes('"item"') || m.includes('"tmp"'))).toBe(true)
630
+ })
631
+
632
+ it('tracks variables declared in match arm bodies', () => {
633
+ const body: HIRBlock = [
634
+ {
635
+ kind: 'match',
636
+ expr: { kind: 'ident', name: 'x' },
637
+ arms: [
638
+ {
639
+ pattern: { kind: 'PatWild' },
640
+ body: [{ kind: 'let', name: 'armVar', init: makeIntLit(5) }],
641
+ },
642
+ ],
643
+ },
644
+ ]
645
+ const hir = makeModule([makeSimpleFn('bar', body)])
646
+ const w = lintSource('', [], hir)
647
+ expect(w.some(x => x.rule === 'unused-variable' && x.message.includes('"armVar"'))).toBe(true)
648
+ })
649
+
650
+ it('tracks variables declared in execute body', () => {
651
+ const body: HIRBlock = [
652
+ {
653
+ kind: 'execute',
654
+ subcommands: [],
655
+ body: [{ kind: 'let', name: 'execVar', init: makeIntLit(5) }],
656
+ },
657
+ ]
658
+ const hir = makeModule([makeSimpleFn('bar', body)])
659
+ const w = lintSource('', [], hir)
660
+ expect(w.some(x => x.rule === 'unused-variable' && x.message.includes('"execVar"'))).toBe(true)
661
+ })
662
+
663
+ it('tracks variables declared in if_let_some bodies', () => {
664
+ const body: HIRBlock = [
665
+ {
666
+ kind: 'if_let_some',
667
+ binding: 'bound',
668
+ init: { kind: 'ident', name: 'opt' },
669
+ then: [{ kind: 'let', name: 'thenVar', init: makeIntLit(5) }],
670
+ else_: [{ kind: 'let', name: 'elseVar', init: makeIntLit(5) }],
671
+ },
672
+ ]
673
+ const hir = makeModule([makeSimpleFn('bar', body)])
674
+ const w = lintSource('', [], hir)
675
+ const names = w.filter(x => x.rule === 'unused-variable').map(x => x.message)
676
+ // 'bound', 'thenVar', 'elseVar' should all be unused
677
+ expect(names.some(m => m.includes('"bound"') || m.includes('"thenVar"') || m.includes('"elseVar"'))).toBe(true)
678
+ })
679
+
680
+ it('tracks variables declared in while_let_some body', () => {
681
+ const body: HIRBlock = [
682
+ {
683
+ kind: 'while_let_some',
684
+ binding: 'loopBound',
685
+ init: { kind: 'ident', name: 'opt' },
686
+ body: [{ kind: 'let', name: 'loopVar', init: makeIntLit(5) }],
687
+ },
688
+ ]
689
+ const hir = makeModule([makeSimpleFn('bar', body)])
690
+ const w = lintSource('', [], hir)
691
+ const names = w.filter(x => x.rule === 'unused-variable').map(x => x.message)
692
+ expect(names.some(m => m.includes('"loopBound"') || m.includes('"loopVar"'))).toBe(true)
693
+ })
694
+
695
+ it('tracks variables declared via labeled_loop body', () => {
696
+ const innerLetStmt: HIRStmt = { kind: 'let', name: 'innerVar', init: makeIntLit(5) }
697
+ const body: HIRBlock = [
698
+ {
699
+ kind: 'labeled_loop',
700
+ label: 'outer',
701
+ body: innerLetStmt,
702
+ },
703
+ ]
704
+ // Note: labeled_loop body is a single stmt (not HIRBlock) — we need to wrap in if
705
+ // Actually labeled_loop.body is HIRStmt directly
706
+ const hir = makeModule([makeSimpleFn('bar', body)])
707
+ const w = lintSource('', [], hir)
708
+ // Should not throw
709
+ expect(Array.isArray(w)).toBe(true)
710
+ })
711
+ })
712
+
713
+ // ---------------------------------------------------------------------------
714
+ // countIdentReadsStmt — all HIR statement kinds
715
+ // ---------------------------------------------------------------------------
716
+
717
+ describe('countIdentReadsStmt — all HIR statement kinds', () => {
718
+ it('reads idents in let_destruct init', () => {
719
+ const body: HIRBlock = [
720
+ { kind: 'let', name: 'myVar', init: makeIntLit(1) },
721
+ {
722
+ kind: 'let_destruct',
723
+ names: ['a', 'b'],
724
+ init: makeIdent('myVar'), // reads myVar
725
+ },
726
+ ]
727
+ const hir = makeModule([makeSimpleFn('bar', body)])
728
+ const w = lintSource('', [], hir)
729
+ // myVar is read in let_destruct init
730
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
731
+ })
732
+
733
+ it('reads idents in const_decl value', () => {
734
+ const body: HIRBlock = [
735
+ { kind: 'let', name: 'myVar', init: makeIntLit(1) },
736
+ {
737
+ kind: 'const_decl',
738
+ name: 'C',
739
+ type: { kind: 'named', name: 'int' } as any,
740
+ value: makeIdent('myVar'), // reads myVar
741
+ },
742
+ ]
743
+ const hir = makeModule([makeSimpleFn('bar', body)])
744
+ const w = lintSource('', [], hir)
745
+ // myVar is read in const_decl value → not unused
746
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
747
+ })
748
+
749
+ it('reads idents in foreach iterable', () => {
750
+ const body: HIRBlock = [
751
+ { kind: 'let', name: 'myList', init: { kind: 'array_lit', elements: [] } },
752
+ {
753
+ kind: 'foreach',
754
+ binding: 'item',
755
+ iterable: makeIdent('myList'), // reads myList
756
+ body: [],
757
+ },
758
+ ]
759
+ const hir = makeModule([makeSimpleFn('bar', body)])
760
+ const w = lintSource('', [], hir)
761
+ // myList is read in foreach iterable → not unused
762
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myList"'))).toHaveLength(0)
763
+ })
764
+
765
+ it('reads idents in match expr', () => {
766
+ const body: HIRBlock = [
767
+ { kind: 'let', name: 'myVal', init: makeIntLit(1) },
768
+ {
769
+ kind: 'match',
770
+ expr: makeIdent('myVal'), // reads myVal
771
+ arms: [],
772
+ },
773
+ ]
774
+ const hir = makeModule([makeSimpleFn('bar', body)])
775
+ const w = lintSource('', [], hir)
776
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVal"'))).toHaveLength(0)
777
+ })
778
+
779
+ it('reads idents in execute body', () => {
780
+ const body: HIRBlock = [
781
+ { kind: 'let', name: 'myVar2', init: makeIntLit(1) },
782
+ {
783
+ kind: 'execute',
784
+ subcommands: [],
785
+ body: [{ kind: 'return', value: makeIdent('myVar2') }],
786
+ },
787
+ ]
788
+ const hir = makeModule([makeSimpleFn('bar', body)])
789
+ const w = lintSource('', [], hir)
790
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar2"'))).toHaveLength(0)
791
+ })
792
+
793
+ it('reads idents in if_let_some then and else', () => {
794
+ const body: HIRBlock = [
795
+ { kind: 'let', name: 'optVal', init: makeIntLit(1) },
796
+ {
797
+ kind: 'if_let_some',
798
+ binding: 'bound',
799
+ init: { kind: 'ident', name: 'opt' },
800
+ then: [{ kind: 'return', value: makeIdent('optVal') }],
801
+ else_: [],
802
+ },
803
+ ]
804
+ const hir = makeModule([makeSimpleFn('bar', body)])
805
+ const w = lintSource('', [], hir)
806
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"optVal"'))).toHaveLength(0)
807
+ })
808
+
809
+ it('reads idents in while_let_some body', () => {
810
+ const body: HIRBlock = [
811
+ { kind: 'let', name: 'wlVar', init: makeIntLit(1) },
812
+ {
813
+ kind: 'while_let_some',
814
+ binding: 'bound2',
815
+ init: { kind: 'ident', name: 'opt' },
816
+ body: [{ kind: 'return', value: makeIdent('wlVar') }],
817
+ },
818
+ ]
819
+ const hir = makeModule([makeSimpleFn('bar', body)])
820
+ const w = lintSource('', [], hir)
821
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"wlVar"'))).toHaveLength(0)
822
+ })
823
+
824
+ it('reads idents in labeled_loop body stmt', () => {
825
+ const body: HIRBlock = [
826
+ { kind: 'let', name: 'llVar', init: makeIntLit(1) },
827
+ {
828
+ kind: 'labeled_loop',
829
+ label: 'loop1',
830
+ body: { kind: 'return', value: makeIdent('llVar') } as HIRStmt,
831
+ },
832
+ ]
833
+ const hir = makeModule([makeSimpleFn('bar', body)])
834
+ const w = lintSource('', [], hir)
835
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"llVar"'))).toHaveLength(0)
836
+ })
837
+ })
838
+
839
+ // ---------------------------------------------------------------------------
840
+ // countIdentReadsExpr — all expression kinds
841
+ // ---------------------------------------------------------------------------
842
+
843
+ describe('countIdentReadsExpr — all expression kinds', () => {
844
+ function makeUsedVar(name: string, initExpr: HIRExpr): HIRBlock {
845
+ return [
846
+ { kind: 'let', name, init: makeIntLit(0) },
847
+ { kind: 'expr', expr: initExpr },
848
+ ]
849
+ }
850
+
851
+ it('reads ident inside invoke callee', () => {
852
+ const body = makeUsedVar('myVar', { kind: 'invoke', callee: makeIdent('myVar'), args: [] })
853
+ const hir = makeModule([makeSimpleFn('bar', body)])
854
+ const w = lintSource('', [], hir)
855
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
856
+ })
857
+
858
+ it('reads ident inside static_call args', () => {
859
+ const body = makeUsedVar('myVar', {
860
+ kind: 'static_call',
861
+ type: 'Math',
862
+ method: 'abs',
863
+ args: [makeIdent('myVar')],
864
+ })
865
+ const hir = makeModule([makeSimpleFn('bar', body)])
866
+ const w = lintSource('', [], hir)
867
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
868
+ })
869
+
870
+ it('reads ident inside member object', () => {
871
+ const body = makeUsedVar('myVar', { kind: 'member', obj: makeIdent('myVar'), field: 'x' })
872
+ const hir = makeModule([makeSimpleFn('bar', body)])
873
+ const w = lintSource('', [], hir)
874
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
875
+ })
876
+
877
+ it('reads ident inside member_assign object and value', () => {
878
+ const body = makeUsedVar('myVar', {
879
+ kind: 'member_assign',
880
+ obj: makeIdent('myVar'),
881
+ field: 'x',
882
+ value: makeIntLit(1),
883
+ })
884
+ const hir = makeModule([makeSimpleFn('bar', body)])
885
+ const w = lintSource('', [], hir)
886
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
887
+ })
888
+
889
+ it('reads ident inside index object and index', () => {
890
+ const body = makeUsedVar('myVar', {
891
+ kind: 'index',
892
+ obj: makeIdent('myVar'),
893
+ index: makeIntLit(0),
894
+ })
895
+ const hir = makeModule([makeSimpleFn('bar', body)])
896
+ const w = lintSource('', [], hir)
897
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
898
+ })
899
+
900
+ it('reads ident inside index_assign', () => {
901
+ const body = makeUsedVar('myVar', {
902
+ kind: 'index_assign',
903
+ obj: makeIdent('myVar'),
904
+ index: makeIntLit(0),
905
+ op: '=',
906
+ value: makeIntLit(1),
907
+ })
908
+ const hir = makeModule([makeSimpleFn('bar', body)])
909
+ const w = lintSource('', [], hir)
910
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
911
+ })
912
+
913
+ it('reads ident inside some_lit', () => {
914
+ const body = makeUsedVar('myVar', { kind: 'some_lit', value: makeIdent('myVar') })
915
+ const hir = makeModule([makeSimpleFn('bar', body)])
916
+ const w = lintSource('', [], hir)
917
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
918
+ })
919
+
920
+ it('reads ident inside unwrap_or opt and default', () => {
921
+ const body = makeUsedVar('myVar', {
922
+ kind: 'unwrap_or',
923
+ opt: makeIdent('myVar'),
924
+ default_: makeIntLit(0),
925
+ })
926
+ const hir = makeModule([makeSimpleFn('bar', body)])
927
+ const w = lintSource('', [], hir)
928
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
929
+ })
930
+
931
+ it('reads ident inside type_cast', () => {
932
+ const body = makeUsedVar('myVar', {
933
+ kind: 'type_cast',
934
+ expr: makeIdent('myVar'),
935
+ targetType: { kind: 'named', name: 'float' } as any,
936
+ })
937
+ const hir = makeModule([makeSimpleFn('bar', body)])
938
+ const w = lintSource('', [], hir)
939
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
940
+ })
941
+
942
+ it('reads ident inside array_lit elements', () => {
943
+ const body = makeUsedVar('myVar', { kind: 'array_lit', elements: [makeIdent('myVar')] })
944
+ const hir = makeModule([makeSimpleFn('bar', body)])
945
+ const w = lintSource('', [], hir)
946
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
947
+ })
948
+
949
+ it('reads ident inside tuple_lit elements', () => {
950
+ const body = makeUsedVar('myVar', { kind: 'tuple_lit', elements: [makeIdent('myVar')] })
951
+ const hir = makeModule([makeSimpleFn('bar', body)])
952
+ const w = lintSource('', [], hir)
953
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
954
+ })
955
+
956
+ it('reads ident inside struct_lit fields', () => {
957
+ const body = makeUsedVar('myVar', {
958
+ kind: 'struct_lit',
959
+ fields: [{ name: 'x', value: makeIdent('myVar') }],
960
+ })
961
+ const hir = makeModule([makeSimpleFn('bar', body)])
962
+ const w = lintSource('', [], hir)
963
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
964
+ })
965
+
966
+ it('reads ident inside str_interp parts', () => {
967
+ const body = makeUsedVar('myVar', {
968
+ kind: 'str_interp',
969
+ parts: ['hello ', makeIdent('myVar')],
970
+ })
971
+ const hir = makeModule([makeSimpleFn('bar', body)])
972
+ const w = lintSource('', [], hir)
973
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
974
+ })
975
+
976
+ it('reads ident inside f_string expr parts', () => {
977
+ const body = makeUsedVar('myVar', {
978
+ kind: 'f_string',
979
+ parts: [
980
+ { kind: 'text', value: 'hello' },
981
+ { kind: 'expr', expr: makeIdent('myVar') },
982
+ ],
983
+ })
984
+ const hir = makeModule([makeSimpleFn('bar', body)])
985
+ const w = lintSource('', [], hir)
986
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
987
+ })
988
+
989
+ it('reads ident inside lambda block body', () => {
990
+ const body = makeUsedVar('myVar', {
991
+ kind: 'lambda',
992
+ params: [],
993
+ body: [{ kind: 'return', value: makeIdent('myVar') }] as HIRBlock,
994
+ })
995
+ const hir = makeModule([makeSimpleFn('bar', body)])
996
+ const w = lintSource('', [], hir)
997
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
998
+ })
999
+
1000
+ it('reads ident inside lambda expr body', () => {
1001
+ const body = makeUsedVar('myVar', {
1002
+ kind: 'lambda',
1003
+ params: [],
1004
+ body: makeIdent('myVar') as HIRExpr,
1005
+ })
1006
+ const hir = makeModule([makeSimpleFn('bar', body)])
1007
+ const w = lintSource('', [], hir)
1008
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
1009
+ })
1010
+
1011
+ it('reads ident inside enum_construct args', () => {
1012
+ const body = makeUsedVar('myVar', {
1013
+ kind: 'enum_construct',
1014
+ enumName: 'Color',
1015
+ variant: 'RGB',
1016
+ args: [{ name: 'r', value: makeIdent('myVar') }],
1017
+ })
1018
+ const hir = makeModule([makeSimpleFn('bar', body)])
1019
+ const w = lintSource('', [], hir)
1020
+ expect(w.filter(x => x.rule === 'unused-variable' && x.message.includes('"myVar"'))).toHaveLength(0)
1021
+ })
1022
+ })
1023
+
1024
+ // ---------------------------------------------------------------------------
1025
+ // magic-number: extra statement kinds
1026
+ // ---------------------------------------------------------------------------
1027
+
1028
+ describe('checkMagicNumbersStmt — extra HIR statement kinds', () => {
1029
+ it('flags magic number in let_destruct init', () => {
1030
+ const body: HIRBlock = [
1031
+ {
1032
+ kind: 'let_destruct',
1033
+ names: ['a'],
1034
+ init: makeIntLit(999),
1035
+ },
1036
+ ]
1037
+ const hir = makeModule([makeSimpleFn('bar', body)])
1038
+ const w = lintSource('', [], hir)
1039
+ expect(w.some(x => x.rule === 'magic-number' && x.message.includes('999'))).toBe(true)
1040
+ })
1041
+
1042
+ it('flags magic number in foreach iterable (though iterable is usually a selector)', () => {
1043
+ const body: HIRBlock = [
1044
+ {
1045
+ kind: 'foreach',
1046
+ binding: 'p',
1047
+ iterable: makeIntLit(999),
1048
+ body: [],
1049
+ },
1050
+ ]
1051
+ const hir = makeModule([makeSimpleFn('bar', body)])
1052
+ const w = lintSource('', [], hir)
1053
+ expect(w.some(x => x.rule === 'magic-number')).toBe(true)
1054
+ })
1055
+
1056
+ it('flags magic number in match expr', () => {
1057
+ const body: HIRBlock = [
1058
+ {
1059
+ kind: 'match',
1060
+ expr: makeIntLit(999),
1061
+ arms: [],
1062
+ },
1063
+ ]
1064
+ const hir = makeModule([makeSimpleFn('bar', body)])
1065
+ const w = lintSource('', [], hir)
1066
+ expect(w.some(x => x.rule === 'magic-number' && x.message.includes('999'))).toBe(true)
1067
+ })
1068
+
1069
+ it('flags magic number in execute body', () => {
1070
+ const body: HIRBlock = [
1071
+ {
1072
+ kind: 'execute',
1073
+ subcommands: [],
1074
+ body: [{ kind: 'return', value: makeIntLit(999) }],
1075
+ },
1076
+ ]
1077
+ const hir = makeModule([makeSimpleFn('bar', body)])
1078
+ const w = lintSource('', [], hir)
1079
+ expect(w.some(x => x.rule === 'magic-number' && x.message.includes('999'))).toBe(true)
1080
+ })
1081
+
1082
+ it('flags magic number in if_let_some init and then/else', () => {
1083
+ const body: HIRBlock = [
1084
+ {
1085
+ kind: 'if_let_some',
1086
+ binding: 'val',
1087
+ init: makeIntLit(999),
1088
+ then: [{ kind: 'return', value: makeIntLit(888) }],
1089
+ else_: [{ kind: 'return', value: makeIntLit(777) }],
1090
+ },
1091
+ ]
1092
+ const hir = makeModule([makeSimpleFn('bar', body)])
1093
+ const w = lintSource('', [], hir)
1094
+ const magicW = w.filter(x => x.rule === 'magic-number')
1095
+ expect(magicW.length).toBeGreaterThanOrEqual(3)
1096
+ })
1097
+
1098
+ it('flags magic number in while_let_some init and body', () => {
1099
+ const body: HIRBlock = [
1100
+ {
1101
+ kind: 'while_let_some',
1102
+ binding: 'val',
1103
+ init: makeIntLit(999),
1104
+ body: [{ kind: 'return', value: makeIntLit(888) }],
1105
+ },
1106
+ ]
1107
+ const hir = makeModule([makeSimpleFn('bar', body)])
1108
+ const w = lintSource('', [], hir)
1109
+ const magicW = w.filter(x => x.rule === 'magic-number')
1110
+ expect(magicW.length).toBeGreaterThanOrEqual(2)
1111
+ })
1112
+
1113
+ it('flags magic number via labeled_loop', () => {
1114
+ const body: HIRBlock = [
1115
+ {
1116
+ kind: 'labeled_loop',
1117
+ label: 'loop1',
1118
+ body: { kind: 'return', value: makeIntLit(999) } as HIRStmt,
1119
+ },
1120
+ ]
1121
+ const hir = makeModule([makeSimpleFn('bar', body)])
1122
+ const w = lintSource('', [], hir)
1123
+ expect(w.some(x => x.rule === 'magic-number' && x.message.includes('999'))).toBe(true)
1124
+ })
1125
+ })
1126
+
1127
+ // ---------------------------------------------------------------------------
1128
+ // checkMagicNumbersExpr — extra expression kinds
1129
+ // ---------------------------------------------------------------------------
1130
+
1131
+ describe('checkMagicNumbersExpr — extra expression kinds', () => {
1132
+ function makeFn(expr: HIRExpr): HIRModule {
1133
+ return makeModule([makeSimpleFn('bar', [{ kind: 'expr', expr }])])
1134
+ }
1135
+
1136
+ it('flags magic number in invoke callee and args', () => {
1137
+ const w = lintSource('', [], makeFn({
1138
+ kind: 'invoke',
1139
+ callee: makeIntLit(999),
1140
+ args: [makeIntLit(888)],
1141
+ }))
1142
+ const magicW = w.filter(x => x.rule === 'magic-number')
1143
+ expect(magicW.length).toBeGreaterThanOrEqual(2)
1144
+ })
1145
+
1146
+ it('flags magic number in static_call args', () => {
1147
+ const w = lintSource('', [], makeFn({
1148
+ kind: 'static_call',
1149
+ type: 'Math',
1150
+ method: 'abs',
1151
+ args: [makeIntLit(999)],
1152
+ }))
1153
+ expect(w.some(x => x.rule === 'magic-number' && x.message.includes('999'))).toBe(true)
1154
+ })
1155
+
1156
+ it('flags magic number in member_assign value', () => {
1157
+ const w = lintSource('', [], makeFn({
1158
+ kind: 'member_assign',
1159
+ obj: makeIdent('obj'),
1160
+ field: 'x',
1161
+ value: makeIntLit(999),
1162
+ }))
1163
+ expect(w.some(x => x.rule === 'magic-number' && x.message.includes('999'))).toBe(true)
1164
+ })
1165
+
1166
+ it('flags magic number in index object and index', () => {
1167
+ const w = lintSource('', [], makeFn({
1168
+ kind: 'index',
1169
+ obj: makeIdent('arr'),
1170
+ index: makeIntLit(999),
1171
+ }))
1172
+ expect(w.some(x => x.rule === 'magic-number' && x.message.includes('999'))).toBe(true)
1173
+ })
1174
+
1175
+ it('flags magic number in index_assign', () => {
1176
+ const w = lintSource('', [], makeFn({
1177
+ kind: 'index_assign',
1178
+ obj: makeIdent('arr'),
1179
+ index: makeIntLit(5),
1180
+ op: '=',
1181
+ value: makeIntLit(999),
1182
+ }))
1183
+ expect(w.some(x => x.rule === 'magic-number')).toBe(true)
1184
+ })
1185
+
1186
+ it('flags magic number in assign value', () => {
1187
+ const w = lintSource('', [], makeFn({
1188
+ kind: 'assign',
1189
+ target: 'x',
1190
+ value: makeIntLit(999),
1191
+ }))
1192
+ expect(w.some(x => x.rule === 'magic-number' && x.message.includes('999'))).toBe(true)
1193
+ })
1194
+
1195
+ it('flags magic number in some_lit', () => {
1196
+ const w = lintSource('', [], makeFn({
1197
+ kind: 'some_lit',
1198
+ value: makeIntLit(999),
1199
+ }))
1200
+ expect(w.some(x => x.rule === 'magic-number' && x.message.includes('999'))).toBe(true)
1201
+ })
1202
+
1203
+ it('flags magic number in unwrap_or', () => {
1204
+ const w = lintSource('', [], makeFn({
1205
+ kind: 'unwrap_or',
1206
+ opt: makeIdent('x'),
1207
+ default_: makeIntLit(999),
1208
+ }))
1209
+ expect(w.some(x => x.rule === 'magic-number' && x.message.includes('999'))).toBe(true)
1210
+ })
1211
+
1212
+ it('flags magic number in type_cast', () => {
1213
+ const w = lintSource('', [], makeFn({
1214
+ kind: 'type_cast',
1215
+ expr: makeIntLit(999),
1216
+ targetType: { kind: 'named', name: 'float' } as any,
1217
+ }))
1218
+ expect(w.some(x => x.rule === 'magic-number' && x.message.includes('999'))).toBe(true)
1219
+ })
1220
+
1221
+ it('flags magic number in str_interp parts', () => {
1222
+ const w = lintSource('', [], makeFn({
1223
+ kind: 'str_interp',
1224
+ parts: ['text ', makeIntLit(999)],
1225
+ }))
1226
+ expect(w.some(x => x.rule === 'magic-number' && x.message.includes('999'))).toBe(true)
1227
+ })
1228
+
1229
+ it('flags magic number in f_string expr parts', () => {
1230
+ const w = lintSource('', [], makeFn({
1231
+ kind: 'f_string',
1232
+ parts: [
1233
+ { kind: 'text', value: 'hello' },
1234
+ { kind: 'expr', expr: makeIntLit(999) },
1235
+ ],
1236
+ }))
1237
+ expect(w.some(x => x.rule === 'magic-number' && x.message.includes('999'))).toBe(true)
1238
+ })
1239
+
1240
+ it('flags magic number in lambda block body', () => {
1241
+ const w = lintSource('', [], makeFn({
1242
+ kind: 'lambda',
1243
+ params: [],
1244
+ body: [{ kind: 'return', value: makeIntLit(999) }] as HIRBlock,
1245
+ }))
1246
+ expect(w.some(x => x.rule === 'magic-number' && x.message.includes('999'))).toBe(true)
1247
+ })
1248
+
1249
+ it('flags magic number in lambda expr body', () => {
1250
+ const w = lintSource('', [], makeFn({
1251
+ kind: 'lambda',
1252
+ params: [],
1253
+ body: makeIntLit(999) as HIRExpr,
1254
+ }))
1255
+ expect(w.some(x => x.rule === 'magic-number' && x.message.includes('999'))).toBe(true)
1256
+ })
1257
+
1258
+ it('flags magic number in enum_construct args', () => {
1259
+ const w = lintSource('', [], makeFn({
1260
+ kind: 'enum_construct',
1261
+ enumName: 'Color',
1262
+ variant: 'RGB',
1263
+ args: [{ name: 'r', value: makeIntLit(255) }],
1264
+ }))
1265
+ expect(w.some(x => x.rule === 'magic-number' && x.message.includes('255'))).toBe(true)
1266
+ })
1267
+
1268
+ it('skips const_decl in magic-number check', () => {
1269
+ const body: HIRBlock = [
1270
+ {
1271
+ kind: 'const_decl',
1272
+ name: 'X',
1273
+ type: { kind: 'named', name: 'int' } as any,
1274
+ value: makeIntLit(9999),
1275
+ },
1276
+ ]
1277
+ const hir = makeModule([makeSimpleFn('bar', body)])
1278
+ const w = lintSource('', [], hir)
1279
+ expect(w.filter(x => x.rule === 'magic-number')).toHaveLength(0)
1280
+ })
1281
+
1282
+ it('handles float_lit, byte_lit, short_lit, long_lit, double_lit magic numbers', () => {
1283
+ const kinds = ['float_lit', 'byte_lit', 'short_lit', 'long_lit', 'double_lit'] as const
1284
+ for (const k of kinds) {
1285
+ const w = lintSource('', [], makeFn({ kind: k, value: 999 }))
1286
+ expect(w.some(x => x.rule === 'magic-number')).toBe(true)
1287
+ }
1288
+ })
1289
+ })
1290
+
1291
+ // ---------------------------------------------------------------------------
1292
+ // dead-branch: checkDeadBranchesStmt — extra HIR statement kinds
1293
+ // ---------------------------------------------------------------------------
1294
+
1295
+ describe('checkDeadBranchesStmt — extra HIR statement kinds', () => {
1296
+ it('recurses into while step block', () => {
1297
+ const body: HIRBlock = [
1298
+ {
1299
+ kind: 'while',
1300
+ cond: makeBoolLit(false),
1301
+ body: [],
1302
+ step: [
1303
+ {
1304
+ kind: 'if',
1305
+ cond: makeBoolLit(true),
1306
+ then: [],
1307
+ },
1308
+ ],
1309
+ },
1310
+ ]
1311
+ const hir = makeModule([makeSimpleFn('bar', body)])
1312
+ const w = lintSource('', [], hir)
1313
+ // The if(true) inside the step should be detected
1314
+ expect(w.some(x => x.rule === 'dead-branch')).toBe(true)
1315
+ })
1316
+
1317
+ it('recurses into foreach body for dead-branch', () => {
1318
+ const body: HIRBlock = [
1319
+ {
1320
+ kind: 'foreach',
1321
+ binding: 'p',
1322
+ iterable: makeIdent('list'),
1323
+ body: [
1324
+ {
1325
+ kind: 'if',
1326
+ cond: makeBoolLit(false),
1327
+ then: [],
1328
+ },
1329
+ ],
1330
+ },
1331
+ ]
1332
+ const hir = makeModule([makeSimpleFn('bar', body)])
1333
+ const w = lintSource('', [], hir)
1334
+ expect(w.some(x => x.rule === 'dead-branch')).toBe(true)
1335
+ })
1336
+
1337
+ it('recurses into match arm bodies for dead-branch', () => {
1338
+ const body: HIRBlock = [
1339
+ {
1340
+ kind: 'match',
1341
+ expr: makeIdent('x'),
1342
+ arms: [
1343
+ {
1344
+ pattern: { kind: 'PatWild' },
1345
+ body: [
1346
+ {
1347
+ kind: 'if',
1348
+ cond: makeBoolLit(true),
1349
+ then: [],
1350
+ },
1351
+ ],
1352
+ },
1353
+ ],
1354
+ },
1355
+ ]
1356
+ const hir = makeModule([makeSimpleFn('bar', body)])
1357
+ const w = lintSource('', [], hir)
1358
+ expect(w.some(x => x.rule === 'dead-branch')).toBe(true)
1359
+ })
1360
+
1361
+ it('recurses into execute body for dead-branch', () => {
1362
+ const body: HIRBlock = [
1363
+ {
1364
+ kind: 'execute',
1365
+ subcommands: [],
1366
+ body: [
1367
+ {
1368
+ kind: 'if',
1369
+ cond: makeBoolLit(true),
1370
+ then: [],
1371
+ },
1372
+ ],
1373
+ },
1374
+ ]
1375
+ const hir = makeModule([makeSimpleFn('bar', body)])
1376
+ const w = lintSource('', [], hir)
1377
+ expect(w.some(x => x.rule === 'dead-branch')).toBe(true)
1378
+ })
1379
+
1380
+ it('recurses into if_let_some then and else for dead-branch', () => {
1381
+ const body: HIRBlock = [
1382
+ {
1383
+ kind: 'if_let_some',
1384
+ binding: 'val',
1385
+ init: makeIdent('opt'),
1386
+ then: [
1387
+ {
1388
+ kind: 'if',
1389
+ cond: makeBoolLit(true),
1390
+ then: [],
1391
+ },
1392
+ ],
1393
+ else_: [
1394
+ {
1395
+ kind: 'if',
1396
+ cond: makeBoolLit(false),
1397
+ then: [],
1398
+ },
1399
+ ],
1400
+ },
1401
+ ]
1402
+ const hir = makeModule([makeSimpleFn('bar', body)])
1403
+ const w = lintSource('', [], hir)
1404
+ expect(w.filter(x => x.rule === 'dead-branch').length).toBeGreaterThanOrEqual(2)
1405
+ })
1406
+
1407
+ it('recurses into while_let_some body for dead-branch', () => {
1408
+ const body: HIRBlock = [
1409
+ {
1410
+ kind: 'while_let_some',
1411
+ binding: 'val',
1412
+ init: makeIdent('opt'),
1413
+ body: [
1414
+ {
1415
+ kind: 'if',
1416
+ cond: makeBoolLit(true),
1417
+ then: [],
1418
+ },
1419
+ ],
1420
+ },
1421
+ ]
1422
+ const hir = makeModule([makeSimpleFn('bar', body)])
1423
+ const w = lintSource('', [], hir)
1424
+ expect(w.some(x => x.rule === 'dead-branch')).toBe(true)
1425
+ })
1426
+
1427
+ it('recurses into labeled_loop for dead-branch (via while wrapper)', () => {
1428
+ // labeled_loop.body is a single HIRStmt; checkDeadBranchesStmt is called with it.
1429
+ // An if stmt alone in labeled_loop.body is not detected (checkDeadBranchesStmt
1430
+ // doesn't handle 'if'), but a while whose inner body has an if will be detected.
1431
+ const whileWithDeadIf: HIRStmt = {
1432
+ kind: 'while',
1433
+ cond: makeBoolLit(false),
1434
+ body: [
1435
+ {
1436
+ kind: 'if',
1437
+ cond: makeBoolLit(true),
1438
+ then: [],
1439
+ },
1440
+ ],
1441
+ }
1442
+ const body: HIRBlock = [
1443
+ {
1444
+ kind: 'labeled_loop',
1445
+ label: 'loop1',
1446
+ body: whileWithDeadIf,
1447
+ },
1448
+ ]
1449
+ const hir = makeModule([makeSimpleFn('bar', body)])
1450
+ const w = lintSource('', [], hir)
1451
+ // The if(true) inside while inside labeled_loop should be detected
1452
+ expect(w.some(x => x.rule === 'dead-branch')).toBe(true)
1453
+ })
1454
+ })
1455
+
1456
+ // ---------------------------------------------------------------------------
1457
+ // countStmts — extra HIR statement kinds (for function-too-long)
1458
+ // ---------------------------------------------------------------------------
1459
+
1460
+ describe('countStmts — extra HIR statement kinds', () => {
1461
+ it('counts stmts in if/else, while/step, foreach, match, execute, if_let_some, while_let_some, labeled_loop', () => {
1462
+ // Build a function with nested stmts to trigger countStmts recursion
1463
+ const innerStmts: HIRStmt[] = Array.from({ length: 15 }, (_, i) => ({
1464
+ kind: 'expr',
1465
+ expr: makeIntLit(i),
1466
+ } as HIRStmt))
1467
+
1468
+ const body: HIRBlock = [
1469
+ {
1470
+ kind: 'if',
1471
+ cond: makeIdent('cond'),
1472
+ then: innerStmts.slice(0, 5),
1473
+ else_: innerStmts.slice(5, 10),
1474
+ },
1475
+ {
1476
+ kind: 'while',
1477
+ cond: makeIdent('cond'),
1478
+ body: innerStmts.slice(0, 5),
1479
+ step: innerStmts.slice(0, 5),
1480
+ },
1481
+ {
1482
+ kind: 'foreach',
1483
+ binding: 'p',
1484
+ iterable: makeIdent('list'),
1485
+ body: innerStmts.slice(0, 5),
1486
+ },
1487
+ {
1488
+ kind: 'match',
1489
+ expr: makeIdent('x'),
1490
+ arms: [
1491
+ { pattern: { kind: 'PatWild' }, body: innerStmts.slice(0, 5) },
1492
+ ],
1493
+ },
1494
+ {
1495
+ kind: 'execute',
1496
+ subcommands: [],
1497
+ body: innerStmts.slice(0, 5),
1498
+ },
1499
+ {
1500
+ kind: 'if_let_some',
1501
+ binding: 'val',
1502
+ init: makeIdent('opt'),
1503
+ then: innerStmts.slice(0, 5),
1504
+ else_: innerStmts.slice(0, 5),
1505
+ },
1506
+ {
1507
+ kind: 'while_let_some',
1508
+ binding: 'val',
1509
+ init: makeIdent('opt'),
1510
+ body: innerStmts.slice(0, 5),
1511
+ },
1512
+ {
1513
+ kind: 'labeled_loop',
1514
+ label: 'loop1',
1515
+ body: { kind: 'expr', expr: makeIdent('x') } as HIRStmt,
1516
+ },
1517
+ ]
1518
+ const hir = makeModule([makeSimpleFn('bar', body)])
1519
+ const w = lintSource('', [], hir, { maxFunctionLines: 1 })
1520
+ // With maxFunctionLines=1, any function with more than 1 stmt should warn
1521
+ expect(w.some(x => x.rule === 'function-too-long')).toBe(true)
1522
+ })
1523
+ })
1524
+
1525
+ // ---------------------------------------------------------------------------
1526
+ // lintFile — success path
1527
+ // ---------------------------------------------------------------------------
1528
+
1529
+ describe('lintFile — success path', () => {
1530
+ it('successfully lints a file that exists', () => {
1531
+ const tmpFile = path.join(os.tmpdir(), `test-lint-${Date.now()}.rs`)
1532
+ try {
1533
+ fs.writeFileSync(tmpFile, `fn foo(): void { say("hello"); }\n`, 'utf-8')
1534
+ const w = lintFile(tmpFile)
1535
+ expect(Array.isArray(w)).toBe(true)
1536
+ } finally {
1537
+ try { fs.unlinkSync(tmpFile) } catch {}
1538
+ }
1539
+ })
1540
+ })
1541
+
1542
+ // ---------------------------------------------------------------------------
1543
+ // Import with span (line/col reporting)
1544
+ // ---------------------------------------------------------------------------
1545
+
1546
+ describe('import with span in unused-import warning', () => {
1547
+ it('sets line and col from import span', () => {
1548
+ const imports: ImportDecl[] = [
1549
+ {
1550
+ moduleName: 'mylib',
1551
+ symbol: 'unused',
1552
+ span: { line: 5, col: 3 },
1553
+ },
1554
+ ]
1555
+ const hir = makeModule([])
1556
+ const w = lintSource('', imports, hir)
1557
+ const uw = w.filter(x => x.rule === 'unused-import')
1558
+ expect(uw.length).toBeGreaterThan(0)
1559
+ expect(uw[0].line).toBe(5)
1560
+ expect(uw[0].col).toBe(3)
1561
+ })
1562
+ })
1563
+
1564
+ // ---------------------------------------------------------------------------
1565
+ // dead-branch: cond.span fallback
1566
+ // ---------------------------------------------------------------------------
1567
+
1568
+ describe('dead-branch span fallback', () => {
1569
+ it('uses cond.span when stmt.span is not set', () => {
1570
+ const body: HIRBlock = [
1571
+ {
1572
+ kind: 'if',
1573
+ cond: { kind: 'bool_lit', value: true, span: { line: 10, col: 5 } },
1574
+ then: [],
1575
+ // no span on stmt itself
1576
+ },
1577
+ ]
1578
+ const hir = makeModule([makeSimpleFn('bar', body)])
1579
+ const w = lintSource('', [], hir)
1580
+ const dw = w.filter(x => x.rule === 'dead-branch')
1581
+ expect(dw.length).toBeGreaterThan(0)
1582
+ // Should use cond.span
1583
+ expect(dw[0].line).toBe(10)
1584
+ expect(dw[0].col).toBe(5)
1585
+ })
1586
+
1587
+ it('uses stmt.span when available (takes priority over cond.span)', () => {
1588
+ const body: HIRBlock = [
1589
+ {
1590
+ kind: 'if',
1591
+ cond: { kind: 'bool_lit', value: true, span: { line: 10, col: 5 } },
1592
+ then: [],
1593
+ span: { line: 8, col: 1 },
1594
+ },
1595
+ ]
1596
+ const hir = makeModule([makeSimpleFn('bar', body)])
1597
+ const w = lintSource('', [], hir)
1598
+ const dw = w.filter(x => x.rule === 'dead-branch')
1599
+ expect(dw.length).toBeGreaterThan(0)
1600
+ expect(dw[0].line).toBe(8)
1601
+ expect(dw[0].col).toBe(1)
1602
+ })
1603
+ })
1604
+
1605
+ // ---------------------------------------------------------------------------
1606
+ // unused-variable span
1607
+ // ---------------------------------------------------------------------------
1608
+
1609
+ describe('unused-variable span', () => {
1610
+ it('sets line and col from let span', () => {
1611
+ const body: HIRBlock = [
1612
+ {
1613
+ kind: 'let',
1614
+ name: 'unusedVar',
1615
+ init: makeIntLit(0),
1616
+ span: { line: 7, col: 2 },
1617
+ },
1618
+ ]
1619
+ const hir = makeModule([makeSimpleFn('bar', body)])
1620
+ const w = lintSource('', [], hir)
1621
+ const uw = w.filter(x => x.rule === 'unused-variable' && x.message.includes('"unusedVar"'))
1622
+ expect(uw.length).toBeGreaterThan(0)
1623
+ expect(uw[0].line).toBe(7)
1624
+ expect(uw[0].col).toBe(2)
1625
+ })
1626
+ })
1627
+
1628
+ // ---------------------------------------------------------------------------
1629
+ // magic-number span
1630
+ // ---------------------------------------------------------------------------
1631
+
1632
+ describe('magic-number span', () => {
1633
+ it('sets line and col from expr span', () => {
1634
+ const body: HIRBlock = [
1635
+ {
1636
+ kind: 'expr',
1637
+ expr: { kind: 'int_lit', value: 999, span: { line: 12, col: 4 } },
1638
+ },
1639
+ ]
1640
+ const hir = makeModule([makeSimpleFn('bar', body)])
1641
+ const w = lintSource('', [], hir)
1642
+ const mw = w.filter(x => x.rule === 'magic-number' && x.message.includes('999'))
1643
+ expect(mw.length).toBeGreaterThan(0)
1644
+ expect(mw[0].line).toBe(12)
1645
+ expect(mw[0].col).toBe(4)
1646
+ })
1647
+ })
1648
+
1649
+ // ---------------------------------------------------------------------------
1650
+ // function-too-long span
1651
+ // ---------------------------------------------------------------------------
1652
+
1653
+ describe('function-too-long span', () => {
1654
+ it('sets line and col from fn.span', () => {
1655
+ const stmts: HIRStmt[] = Array.from({ length: 60 }, () => ({
1656
+ kind: 'expr',
1657
+ expr: makeIntLit(1),
1658
+ } as HIRStmt))
1659
+ const fn_: HIRFunction = {
1660
+ name: 'longFn',
1661
+ params: [],
1662
+ returnType: { kind: 'named', name: 'void' } as any,
1663
+ decorators: [],
1664
+ body: stmts,
1665
+ span: { line: 3, col: 1 },
1666
+ }
1667
+ const hir = makeModule([fn_])
1668
+ const w = lintSource('', [], hir, { maxFunctionLines: 50 })
1669
+ const fw = w.filter(x => x.rule === 'function-too-long')
1670
+ expect(fw.length).toBeGreaterThan(0)
1671
+ expect(fw[0].line).toBe(3)
1672
+ expect(fw[0].col).toBe(1)
1673
+ })
1674
+ })
1675
+
1676
+ // ---------------------------------------------------------------------------
1677
+ // return without value
1678
+ // ---------------------------------------------------------------------------
1679
+
1680
+ describe('collectCalledNamesStmt — return without value', () => {
1681
+ it('handles return with no value (void return)', () => {
1682
+ const importFoo: ImportDecl = { moduleName: 'mylib', symbol: 'foo' }
1683
+ const body: HIRBlock = [
1684
+ { kind: 'return' }, // no value
1685
+ ]
1686
+ const hir = makeModule([makeSimpleFn('bar', body)])
1687
+ const w = lintSource('', [importFoo], hir)
1688
+ // 'foo' not used → should warn
1689
+ expect(w.filter(x => x.rule === 'unused-import').length).toBeGreaterThan(0)
1690
+ })
1691
+ })
1692
+
1693
+ // ---------------------------------------------------------------------------
1694
+ // Library functions are skipped
1695
+ // ---------------------------------------------------------------------------
1696
+
1697
+ describe('library function skip', () => {
1698
+ it('skips lint for functions with isLibraryFn=true', () => {
1699
+ const body: HIRBlock = [
1700
+ { kind: 'let', name: 'x', init: makeIntLit(9999) },
1701
+ ]
1702
+ const fn_: HIRFunction = {
1703
+ name: 'libFn',
1704
+ params: [],
1705
+ returnType: { kind: 'named', name: 'void' } as any,
1706
+ decorators: [],
1707
+ body,
1708
+ isLibraryFn: true,
1709
+ }
1710
+ const hir = makeModule([fn_])
1711
+ const w = lintSource('', [], hir)
1712
+ // Library fn should be entirely skipped
1713
+ expect(w.filter(x => x.rule === 'unused-variable')).toHaveLength(0)
1714
+ expect(w.filter(x => x.rule === 'magic-number')).toHaveLength(0)
1715
+ })
1716
+ })