funkscript 0.0.0-stage → 0.2.4

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 (631) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +204 -2
  3. package/dist/Widget.tsx +9 -0
  4. package/dist/ast/types.d.ts +513 -0
  5. package/dist/ast/types.d.ts.map +1 -0
  6. package/dist/ast/types.js +5 -0
  7. package/dist/ast/types.js.map +1 -0
  8. package/dist/ast/walk.d.ts +8 -0
  9. package/dist/ast/walk.d.ts.map +1 -0
  10. package/dist/ast/walk.js +23 -0
  11. package/dist/ast/walk.js.map +1 -0
  12. package/dist/cli/build-bun.d.ts +24 -0
  13. package/dist/cli/build-bun.d.ts.map +1 -0
  14. package/dist/cli/build-bun.js +190 -0
  15. package/dist/cli/build-bun.js.map +1 -0
  16. package/dist/cli/build-watch.d.ts +21 -0
  17. package/dist/cli/build-watch.d.ts.map +1 -0
  18. package/dist/cli/build-watch.js +244 -0
  19. package/dist/cli/build-watch.js.map +1 -0
  20. package/dist/cli/build.d.ts +4 -0
  21. package/dist/cli/build.d.ts.map +1 -0
  22. package/dist/cli/build.js +338 -0
  23. package/dist/cli/build.js.map +1 -0
  24. package/dist/cli/check.d.ts +4 -0
  25. package/dist/cli/check.d.ts.map +1 -0
  26. package/dist/cli/check.js +278 -0
  27. package/dist/cli/check.js.map +1 -0
  28. package/dist/cli/commands.d.ts +13 -0
  29. package/dist/cli/commands.d.ts.map +1 -0
  30. package/dist/cli/commands.js +262 -0
  31. package/dist/cli/commands.js.map +1 -0
  32. package/dist/cli/doc.d.ts +3 -0
  33. package/dist/cli/doc.d.ts.map +1 -0
  34. package/dist/cli/doc.js +71 -0
  35. package/dist/cli/doc.js.map +1 -0
  36. package/dist/cli/doctor.d.ts +3 -0
  37. package/dist/cli/doctor.d.ts.map +1 -0
  38. package/dist/cli/doctor.js +121 -0
  39. package/dist/cli/doctor.js.map +1 -0
  40. package/dist/cli/emit.d.ts +4 -0
  41. package/dist/cli/emit.d.ts.map +1 -0
  42. package/dist/cli/emit.js +87 -0
  43. package/dist/cli/emit.js.map +1 -0
  44. package/dist/cli/explain.d.ts +3 -0
  45. package/dist/cli/explain.d.ts.map +1 -0
  46. package/dist/cli/explain.js +103 -0
  47. package/dist/cli/explain.js.map +1 -0
  48. package/dist/cli/fmt.d.ts +3 -0
  49. package/dist/cli/fmt.d.ts.map +1 -0
  50. package/dist/cli/fmt.js +146 -0
  51. package/dist/cli/fmt.js.map +1 -0
  52. package/dist/cli/index.d.ts +3 -0
  53. package/dist/cli/index.d.ts.map +1 -0
  54. package/dist/cli/index.js +186 -0
  55. package/dist/cli/index.js.map +1 -0
  56. package/dist/cli/init.d.ts +10 -0
  57. package/dist/cli/init.d.ts.map +1 -0
  58. package/dist/cli/init.js +412 -0
  59. package/dist/cli/init.js.map +1 -0
  60. package/dist/cli/io.d.ts +9 -0
  61. package/dist/cli/io.d.ts.map +1 -0
  62. package/dist/cli/io.js +113 -0
  63. package/dist/cli/io.js.map +1 -0
  64. package/dist/cli/output-collisions.d.ts +2 -0
  65. package/dist/cli/output-collisions.d.ts.map +1 -0
  66. package/dist/cli/output-collisions.js +39 -0
  67. package/dist/cli/output-collisions.js.map +1 -0
  68. package/dist/cli/pipeline-style.d.ts +3 -0
  69. package/dist/cli/pipeline-style.d.ts.map +1 -0
  70. package/dist/cli/pipeline-style.js +45 -0
  71. package/dist/cli/pipeline-style.js.map +1 -0
  72. package/dist/cli/repl.d.ts +37 -0
  73. package/dist/cli/repl.d.ts.map +1 -0
  74. package/dist/cli/repl.js +334 -0
  75. package/dist/cli/repl.js.map +1 -0
  76. package/dist/cli/run.d.ts +4 -0
  77. package/dist/cli/run.d.ts.map +1 -0
  78. package/dist/cli/run.js +185 -0
  79. package/dist/cli/run.js.map +1 -0
  80. package/dist/cli/test.d.ts +3 -0
  81. package/dist/cli/test.d.ts.map +1 -0
  82. package/dist/cli/test.js +124 -0
  83. package/dist/cli/test.js.map +1 -0
  84. package/dist/cli/tsc.d.ts +3 -0
  85. package/dist/cli/tsc.d.ts.map +1 -0
  86. package/dist/cli/tsc.js +143 -0
  87. package/dist/cli/tsc.js.map +1 -0
  88. package/dist/cli/typecheck.d.ts +23 -0
  89. package/dist/cli/typecheck.d.ts.map +1 -0
  90. package/dist/cli/typecheck.js +65 -0
  91. package/dist/cli/typecheck.js.map +1 -0
  92. package/dist/compiler/ast-inspection.d.ts +18 -0
  93. package/dist/compiler/ast-inspection.d.ts.map +1 -0
  94. package/dist/compiler/ast-inspection.js +96 -0
  95. package/dist/compiler/ast-inspection.js.map +1 -0
  96. package/dist/compiler/boolean-operators.d.ts +8 -0
  97. package/dist/compiler/boolean-operators.d.ts.map +1 -0
  98. package/dist/compiler/boolean-operators.js +84 -0
  99. package/dist/compiler/boolean-operators.js.map +1 -0
  100. package/dist/compiler/check.d.ts +87 -0
  101. package/dist/compiler/check.d.ts.map +1 -0
  102. package/dist/compiler/check.js +1830 -0
  103. package/dist/compiler/check.js.map +1 -0
  104. package/dist/compiler/diagnostics.d.ts +6 -0
  105. package/dist/compiler/diagnostics.d.ts.map +1 -0
  106. package/dist/compiler/diagnostics.js +136 -0
  107. package/dist/compiler/diagnostics.js.map +1 -0
  108. package/dist/compiler/effect-mutation-rule.d.ts +4 -0
  109. package/dist/compiler/effect-mutation-rule.d.ts.map +1 -0
  110. package/dist/compiler/effect-mutation-rule.js +348 -0
  111. package/dist/compiler/effect-mutation-rule.js.map +1 -0
  112. package/dist/compiler/entry-rules.d.ts +5 -0
  113. package/dist/compiler/entry-rules.d.ts.map +1 -0
  114. package/dist/compiler/entry-rules.js +89 -0
  115. package/dist/compiler/entry-rules.js.map +1 -0
  116. package/dist/compiler/imported-record-functions.d.ts +4 -0
  117. package/dist/compiler/imported-record-functions.d.ts.map +1 -0
  118. package/dist/compiler/imported-record-functions.js +706 -0
  119. package/dist/compiler/imported-record-functions.js.map +1 -0
  120. package/dist/compiler/imported-schemas.d.ts +21 -0
  121. package/dist/compiler/imported-schemas.d.ts.map +1 -0
  122. package/dist/compiler/imported-schemas.js +461 -0
  123. package/dist/compiler/imported-schemas.js.map +1 -0
  124. package/dist/compiler/incremental-build.d.ts +23 -0
  125. package/dist/compiler/incremental-build.d.ts.map +1 -0
  126. package/dist/compiler/incremental-build.js +90 -0
  127. package/dist/compiler/incremental-build.js.map +1 -0
  128. package/dist/compiler/incremental-dependencies.d.ts +7 -0
  129. package/dist/compiler/incremental-dependencies.d.ts.map +1 -0
  130. package/dist/compiler/incremental-dependencies.js +190 -0
  131. package/dist/compiler/incremental-dependencies.js.map +1 -0
  132. package/dist/compiler/js-interop-nullability.d.ts +9 -0
  133. package/dist/compiler/js-interop-nullability.d.ts.map +1 -0
  134. package/dist/compiler/js-interop-nullability.js +57 -0
  135. package/dist/compiler/js-interop-nullability.js.map +1 -0
  136. package/dist/compiler/module-resolution.d.ts +6 -0
  137. package/dist/compiler/module-resolution.d.ts.map +1 -0
  138. package/dist/compiler/module-resolution.js +44 -0
  139. package/dist/compiler/module-resolution.js.map +1 -0
  140. package/dist/compiler/modules/output-collisions.d.ts +2 -0
  141. package/dist/compiler/modules/output-collisions.d.ts.map +1 -0
  142. package/dist/compiler/modules/output-collisions.js +39 -0
  143. package/dist/compiler/modules/output-collisions.js.map +1 -0
  144. package/dist/compiler/modules/output-plan.d.ts +7 -0
  145. package/dist/compiler/modules/output-plan.d.ts.map +1 -0
  146. package/dist/compiler/modules/output-plan.js +37 -0
  147. package/dist/compiler/modules/output-plan.js.map +1 -0
  148. package/dist/compiler/modules/resolution.d.ts +8 -0
  149. package/dist/compiler/modules/resolution.d.ts.map +1 -0
  150. package/dist/compiler/modules/resolution.js +48 -0
  151. package/dist/compiler/modules/resolution.js.map +1 -0
  152. package/dist/compiler/numeric-comparisons.d.ts +9 -0
  153. package/dist/compiler/numeric-comparisons.d.ts.map +1 -0
  154. package/dist/compiler/numeric-comparisons.js +153 -0
  155. package/dist/compiler/numeric-comparisons.js.map +1 -0
  156. package/dist/compiler/optional-arguments.d.ts +11 -0
  157. package/dist/compiler/optional-arguments.d.ts.map +1 -0
  158. package/dist/compiler/optional-arguments.js +246 -0
  159. package/dist/compiler/optional-arguments.js.map +1 -0
  160. package/dist/compiler/package-fingerprint.d.ts +7 -0
  161. package/dist/compiler/package-fingerprint.d.ts.map +1 -0
  162. package/dist/compiler/package-fingerprint.js +178 -0
  163. package/dist/compiler/package-fingerprint.js.map +1 -0
  164. package/dist/compiler/package-record-contracts.d.ts +5 -0
  165. package/dist/compiler/package-record-contracts.d.ts.map +1 -0
  166. package/dist/compiler/package-record-contracts.js +126 -0
  167. package/dist/compiler/package-record-contracts.js.map +1 -0
  168. package/dist/compiler/pipeline-companions.d.ts +18 -0
  169. package/dist/compiler/pipeline-companions.d.ts.map +1 -0
  170. package/dist/compiler/pipeline-companions.js +1042 -0
  171. package/dist/compiler/pipeline-companions.js.map +1 -0
  172. package/dist/compiler/pipelines/catalog.d.ts +15 -0
  173. package/dist/compiler/pipelines/catalog.d.ts.map +1 -0
  174. package/dist/compiler/pipelines/catalog.js +55 -0
  175. package/dist/compiler/pipelines/catalog.js.map +1 -0
  176. package/dist/compiler/pipelines/companions.d.ts +18 -0
  177. package/dist/compiler/pipelines/companions.d.ts.map +1 -0
  178. package/dist/compiler/pipelines/companions.js +1042 -0
  179. package/dist/compiler/pipelines/companions.js.map +1 -0
  180. package/dist/compiler/pipelines/resolution.d.ts +38 -0
  181. package/dist/compiler/pipelines/resolution.d.ts.map +1 -0
  182. package/dist/compiler/pipelines/resolution.js +571 -0
  183. package/dist/compiler/pipelines/resolution.js.map +1 -0
  184. package/dist/compiler/project-config.d.ts +28 -0
  185. package/dist/compiler/project-config.d.ts.map +1 -0
  186. package/dist/compiler/project-config.js +155 -0
  187. package/dist/compiler/project-config.js.map +1 -0
  188. package/dist/compiler/providers.d.ts +4 -0
  189. package/dist/compiler/providers.d.ts.map +1 -0
  190. package/dist/compiler/providers.js +101 -0
  191. package/dist/compiler/providers.js.map +1 -0
  192. package/dist/compiler/record-contracts.d.ts +6 -0
  193. package/dist/compiler/record-contracts.d.ts.map +1 -0
  194. package/dist/compiler/record-contracts.js +41 -0
  195. package/dist/compiler/record-contracts.js.map +1 -0
  196. package/dist/compiler/rules/control-flow.d.ts +10 -0
  197. package/dist/compiler/rules/control-flow.d.ts.map +1 -0
  198. package/dist/compiler/rules/control-flow.js +19 -0
  199. package/dist/compiler/rules/control-flow.js.map +1 -0
  200. package/dist/compiler/rules/diagnostic.d.ts +4 -0
  201. package/dist/compiler/rules/diagnostic.d.ts.map +1 -0
  202. package/dist/compiler/rules/diagnostic.js +2 -0
  203. package/dist/compiler/rules/diagnostic.js.map +1 -0
  204. package/dist/compiler/rules/module-scope-refs.d.ts +5 -0
  205. package/dist/compiler/rules/module-scope-refs.d.ts.map +1 -0
  206. package/dist/compiler/rules/module-scope-refs.js +47 -0
  207. package/dist/compiler/rules/module-scope-refs.js.map +1 -0
  208. package/dist/compiler/schemas/analysis.d.ts +13 -0
  209. package/dist/compiler/schemas/analysis.d.ts.map +1 -0
  210. package/dist/compiler/schemas/analysis.js +24 -0
  211. package/dist/compiler/schemas/analysis.js.map +1 -0
  212. package/dist/compiler/schemas/imported-record-functions.d.ts +4 -0
  213. package/dist/compiler/schemas/imported-record-functions.d.ts.map +1 -0
  214. package/dist/compiler/schemas/imported-record-functions.js +706 -0
  215. package/dist/compiler/schemas/imported-record-functions.js.map +1 -0
  216. package/dist/compiler/schemas/imported-schemas.d.ts +21 -0
  217. package/dist/compiler/schemas/imported-schemas.d.ts.map +1 -0
  218. package/dist/compiler/schemas/imported-schemas.js +461 -0
  219. package/dist/compiler/schemas/imported-schemas.js.map +1 -0
  220. package/dist/compiler/source-map.d.ts +6 -0
  221. package/dist/compiler/source-map.d.ts.map +1 -0
  222. package/dist/compiler/source-map.js +127 -0
  223. package/dist/compiler/source-map.js.map +1 -0
  224. package/dist/compiler/typescript-context.d.ts +33 -0
  225. package/dist/compiler/typescript-context.d.ts.map +1 -0
  226. package/dist/compiler/typescript-context.js +200 -0
  227. package/dist/compiler/typescript-context.js.map +1 -0
  228. package/dist/compiler.d.ts +77 -0
  229. package/dist/compiler.d.ts.map +1 -0
  230. package/dist/compiler.js +497 -0
  231. package/dist/compiler.js.map +1 -0
  232. package/dist/emitter/assemblyscript.d.ts +5 -0
  233. package/dist/emitter/assemblyscript.d.ts.map +1 -0
  234. package/dist/emitter/assemblyscript.js +979 -0
  235. package/dist/emitter/assemblyscript.js.map +1 -0
  236. package/dist/emitter/declarations.d.ts +46 -0
  237. package/dist/emitter/declarations.d.ts.map +1 -0
  238. package/dist/emitter/declarations.js +739 -0
  239. package/dist/emitter/declarations.js.map +1 -0
  240. package/dist/emitter/emitter.d.ts +165 -0
  241. package/dist/emitter/emitter.d.ts.map +1 -0
  242. package/dist/emitter/emitter.js +2448 -0
  243. package/dist/emitter/emitter.js.map +1 -0
  244. package/dist/emitter/imports.d.ts +6 -0
  245. package/dist/emitter/imports.d.ts.map +1 -0
  246. package/dist/emitter/imports.js +73 -0
  247. package/dist/emitter/imports.js.map +1 -0
  248. package/dist/emitter/indent.d.ts +3 -0
  249. package/dist/emitter/indent.d.ts.map +1 -0
  250. package/dist/emitter/indent.js +31 -0
  251. package/dist/emitter/indent.js.map +1 -0
  252. package/dist/emitter/jsx.d.ts +20 -0
  253. package/dist/emitter/jsx.d.ts.map +1 -0
  254. package/dist/emitter/jsx.js +82 -0
  255. package/dist/emitter/jsx.js.map +1 -0
  256. package/dist/emitter/local-bindings.d.ts +6 -0
  257. package/dist/emitter/local-bindings.d.ts.map +1 -0
  258. package/dist/emitter/local-bindings.js +32 -0
  259. package/dist/emitter/local-bindings.js.map +1 -0
  260. package/dist/emitter/match.d.ts +56 -0
  261. package/dist/emitter/match.d.ts.map +1 -0
  262. package/dist/emitter/match.js +1287 -0
  263. package/dist/emitter/match.js.map +1 -0
  264. package/dist/emitter/pipeline-catalog.d.ts +15 -0
  265. package/dist/emitter/pipeline-catalog.d.ts.map +1 -0
  266. package/dist/emitter/pipeline-catalog.js +55 -0
  267. package/dist/emitter/pipeline-catalog.js.map +1 -0
  268. package/dist/emitter/pipeline-resolution.d.ts +38 -0
  269. package/dist/emitter/pipeline-resolution.d.ts.map +1 -0
  270. package/dist/emitter/pipeline-resolution.js +571 -0
  271. package/dist/emitter/pipeline-resolution.js.map +1 -0
  272. package/dist/emitter/runtime-helpers.d.ts +10 -0
  273. package/dist/emitter/runtime-helpers.d.ts.map +1 -0
  274. package/dist/emitter/runtime-helpers.js +22 -0
  275. package/dist/emitter/runtime-helpers.js.map +1 -0
  276. package/dist/emitter/schemas.d.ts +36 -0
  277. package/dist/emitter/schemas.d.ts.map +1 -0
  278. package/dist/emitter/schemas.js +766 -0
  279. package/dist/emitter/schemas.js.map +1 -0
  280. package/dist/emitter/statements.d.ts +23 -0
  281. package/dist/emitter/statements.d.ts.map +1 -0
  282. package/dist/emitter/statements.js +216 -0
  283. package/dist/emitter/statements.js.map +1 -0
  284. package/dist/emitter/tagged-match.d.ts +23 -0
  285. package/dist/emitter/tagged-match.d.ts.map +1 -0
  286. package/dist/emitter/tagged-match.js +109 -0
  287. package/dist/emitter/tagged-match.js.map +1 -0
  288. package/dist/emitter/types.d.ts +22 -0
  289. package/dist/emitter/types.d.ts.map +1 -0
  290. package/dist/emitter/types.js +231 -0
  291. package/dist/emitter/types.js.map +1 -0
  292. package/dist/errors.d.ts +39 -0
  293. package/dist/errors.d.ts.map +1 -0
  294. package/dist/errors.js +98 -0
  295. package/dist/errors.js.map +1 -0
  296. package/dist/formatter/comments.d.ts +25 -0
  297. package/dist/formatter/comments.d.ts.map +1 -0
  298. package/dist/formatter/comments.js +283 -0
  299. package/dist/formatter/comments.js.map +1 -0
  300. package/dist/formatter/index.d.ts +71 -0
  301. package/dist/formatter/index.d.ts.map +1 -0
  302. package/dist/formatter/index.js +1113 -0
  303. package/dist/formatter/index.js.map +1 -0
  304. package/dist/formatter/operator-precedence.d.ts +3 -0
  305. package/dist/formatter/operator-precedence.d.ts.map +1 -0
  306. package/dist/formatter/operator-precedence.js +27 -0
  307. package/dist/formatter/operator-precedence.js.map +1 -0
  308. package/dist/formatter/prettier.d.ts +7 -0
  309. package/dist/formatter/prettier.d.ts.map +1 -0
  310. package/dist/formatter/prettier.js +51 -0
  311. package/dist/formatter/prettier.js.map +1 -0
  312. package/dist/index.d.ts +17 -0
  313. package/dist/index.d.ts.map +1 -0
  314. package/dist/index.js +17 -0
  315. package/dist/index.js.map +1 -0
  316. package/dist/interop/callback.d.ts +38 -0
  317. package/dist/interop/callback.d.ts.map +1 -0
  318. package/dist/interop/callback.js +130 -0
  319. package/dist/interop/callback.js.map +1 -0
  320. package/dist/interop/index.d.ts +6 -0
  321. package/dist/interop/index.d.ts.map +1 -0
  322. package/dist/interop/index.js +6 -0
  323. package/dist/interop/index.js.map +1 -0
  324. package/dist/interop/js.d.ts +16 -0
  325. package/dist/interop/js.d.ts.map +1 -0
  326. package/dist/interop/js.js +35 -0
  327. package/dist/interop/js.js.map +1 -0
  328. package/dist/interop/record.d.ts +9 -0
  329. package/dist/interop/record.d.ts.map +1 -0
  330. package/dist/interop/record.js +18 -0
  331. package/dist/interop/record.js.map +1 -0
  332. package/dist/interop/valibot.d.ts +9 -0
  333. package/dist/interop/valibot.d.ts.map +1 -0
  334. package/dist/interop/valibot.js +94 -0
  335. package/dist/interop/valibot.js.map +1 -0
  336. package/dist/interop/zod.d.ts +45 -0
  337. package/dist/interop/zod.d.ts.map +1 -0
  338. package/dist/interop/zod.js +143 -0
  339. package/dist/interop/zod.js.map +1 -0
  340. package/dist/lexer/lexer.d.ts +65 -0
  341. package/dist/lexer/lexer.d.ts.map +1 -0
  342. package/dist/lexer/lexer.js +1335 -0
  343. package/dist/lexer/lexer.js.map +1 -0
  344. package/dist/lexer/tokens.d.ts +96 -0
  345. package/dist/lexer/tokens.d.ts.map +1 -0
  346. package/dist/lexer/tokens.js +150 -0
  347. package/dist/lexer/tokens.js.map +1 -0
  348. package/dist/lsp/code-action-worker.d.ts +2 -0
  349. package/dist/lsp/code-action-worker.d.ts.map +1 -0
  350. package/dist/lsp/code-action-worker.js +17 -0
  351. package/dist/lsp/code-action-worker.js.map +1 -0
  352. package/dist/lsp/completion.d.ts +11 -0
  353. package/dist/lsp/completion.d.ts.map +1 -0
  354. package/dist/lsp/completion.js +260 -0
  355. package/dist/lsp/completion.js.map +1 -0
  356. package/dist/lsp/definition.d.ts +42 -0
  357. package/dist/lsp/definition.d.ts.map +1 -0
  358. package/dist/lsp/definition.js +1074 -0
  359. package/dist/lsp/definition.js.map +1 -0
  360. package/dist/lsp/diagnostics-worker.d.ts +2 -0
  361. package/dist/lsp/diagnostics-worker.d.ts.map +1 -0
  362. package/dist/lsp/diagnostics-worker.js +169 -0
  363. package/dist/lsp/diagnostics-worker.js.map +1 -0
  364. package/dist/lsp/docs.d.ts +37 -0
  365. package/dist/lsp/docs.d.ts.map +1 -0
  366. package/dist/lsp/docs.js +830 -0
  367. package/dist/lsp/docs.js.map +1 -0
  368. package/dist/lsp/error-channel.d.ts +3 -0
  369. package/dist/lsp/error-channel.d.ts.map +1 -0
  370. package/dist/lsp/error-channel.js +48 -0
  371. package/dist/lsp/error-channel.js.map +1 -0
  372. package/dist/lsp/hover.d.ts +5 -0
  373. package/dist/lsp/hover.d.ts.map +1 -0
  374. package/dist/lsp/hover.js +651 -0
  375. package/dist/lsp/hover.js.map +1 -0
  376. package/dist/lsp/imports.d.ts +5 -0
  377. package/dist/lsp/imports.d.ts.map +1 -0
  378. package/dist/lsp/imports.js +317 -0
  379. package/dist/lsp/imports.js.map +1 -0
  380. package/dist/lsp/index.d.ts +16 -0
  381. package/dist/lsp/index.d.ts.map +1 -0
  382. package/dist/lsp/index.js +17 -0
  383. package/dist/lsp/index.js.map +1 -0
  384. package/dist/lsp/inlay-hints.d.ts +3 -0
  385. package/dist/lsp/inlay-hints.d.ts.map +1 -0
  386. package/dist/lsp/inlay-hints.js +236 -0
  387. package/dist/lsp/inlay-hints.js.map +1 -0
  388. package/dist/lsp/interop-rename.d.ts +26 -0
  389. package/dist/lsp/interop-rename.d.ts.map +1 -0
  390. package/dist/lsp/interop-rename.js +346 -0
  391. package/dist/lsp/interop-rename.js.map +1 -0
  392. package/dist/lsp/match-arms.d.ts +3 -0
  393. package/dist/lsp/match-arms.d.ts.map +1 -0
  394. package/dist/lsp/match-arms.js +341 -0
  395. package/dist/lsp/match-arms.js.map +1 -0
  396. package/dist/lsp/navigation.d.ts +23 -0
  397. package/dist/lsp/navigation.d.ts.map +1 -0
  398. package/dist/lsp/navigation.js +430 -0
  399. package/dist/lsp/navigation.js.map +1 -0
  400. package/dist/lsp/parsed-documents.d.ts +25 -0
  401. package/dist/lsp/parsed-documents.d.ts.map +1 -0
  402. package/dist/lsp/parsed-documents.js +47 -0
  403. package/dist/lsp/parsed-documents.js.map +1 -0
  404. package/dist/lsp/rename-worker.d.ts +2 -0
  405. package/dist/lsp/rename-worker.d.ts.map +1 -0
  406. package/dist/lsp/rename-worker.js +27 -0
  407. package/dist/lsp/rename-worker.js.map +1 -0
  408. package/dist/lsp/server.d.ts +78 -0
  409. package/dist/lsp/server.d.ts.map +1 -0
  410. package/dist/lsp/server.js +1098 -0
  411. package/dist/lsp/server.js.map +1 -0
  412. package/dist/lsp/thunk-pipeline.d.ts +4 -0
  413. package/dist/lsp/thunk-pipeline.d.ts.map +1 -0
  414. package/dist/lsp/thunk-pipeline.js +95 -0
  415. package/dist/lsp/thunk-pipeline.js.map +1 -0
  416. package/dist/lsp/validation.d.ts +8 -0
  417. package/dist/lsp/validation.d.ts.map +1 -0
  418. package/dist/lsp/validation.js +124 -0
  419. package/dist/lsp/validation.js.map +1 -0
  420. package/dist/lsp/workspace.d.ts +8 -0
  421. package/dist/lsp/workspace.d.ts.map +1 -0
  422. package/dist/lsp/workspace.js +70 -0
  423. package/dist/lsp/workspace.js.map +1 -0
  424. package/dist/math.ts +1 -0
  425. package/dist/parser/declarations.d.ts +21 -0
  426. package/dist/parser/declarations.d.ts.map +1 -0
  427. package/dist/parser/declarations.js +626 -0
  428. package/dist/parser/declarations.js.map +1 -0
  429. package/dist/parser/errors.d.ts +9 -0
  430. package/dist/parser/errors.d.ts.map +1 -0
  431. package/dist/parser/errors.js +11 -0
  432. package/dist/parser/errors.js.map +1 -0
  433. package/dist/parser/index.d.ts +9 -0
  434. package/dist/parser/index.d.ts.map +1 -0
  435. package/dist/parser/index.js +9 -0
  436. package/dist/parser/index.js.map +1 -0
  437. package/dist/parser/jsx.d.ts +5 -0
  438. package/dist/parser/jsx.d.ts.map +1 -0
  439. package/dist/parser/jsx.js +188 -0
  440. package/dist/parser/jsx.js.map +1 -0
  441. package/dist/parser/operator-expressions.d.ts +16 -0
  442. package/dist/parser/operator-expressions.d.ts.map +1 -0
  443. package/dist/parser/operator-expressions.js +146 -0
  444. package/dist/parser/operator-expressions.js.map +1 -0
  445. package/dist/parser/parser.d.ts +104 -0
  446. package/dist/parser/parser.d.ts.map +1 -0
  447. package/dist/parser/parser.js +1216 -0
  448. package/dist/parser/parser.js.map +1 -0
  449. package/dist/parser/patterns.d.ts +9 -0
  450. package/dist/parser/patterns.d.ts.map +1 -0
  451. package/dist/parser/patterns.js +292 -0
  452. package/dist/parser/patterns.js.map +1 -0
  453. package/dist/parser/pipeline-expressions.d.ts +10 -0
  454. package/dist/parser/pipeline-expressions.d.ts.map +1 -0
  455. package/dist/parser/pipeline-expressions.js +57 -0
  456. package/dist/parser/pipeline-expressions.js.map +1 -0
  457. package/dist/parser/scope.d.ts +17 -0
  458. package/dist/parser/scope.d.ts.map +1 -0
  459. package/dist/parser/scope.js +56 -0
  460. package/dist/parser/scope.js.map +1 -0
  461. package/dist/parser/statements.d.ts +13 -0
  462. package/dist/parser/statements.d.ts.map +1 -0
  463. package/dist/parser/statements.js +737 -0
  464. package/dist/parser/statements.js.map +1 -0
  465. package/dist/parser/types.d.ts +13 -0
  466. package/dist/parser/types.d.ts.map +1 -0
  467. package/dist/parser/types.js +371 -0
  468. package/dist/parser/types.js.map +1 -0
  469. package/dist/plugins/bun-preload.d.ts +4 -0
  470. package/dist/plugins/bun-preload.d.ts.map +1 -0
  471. package/dist/plugins/bun-preload.js +18 -0
  472. package/dist/plugins/bun-preload.js.map +1 -0
  473. package/dist/plugins/bun.d.ts +115 -0
  474. package/dist/plugins/bun.d.ts.map +1 -0
  475. package/dist/plugins/bun.js +240 -0
  476. package/dist/plugins/bun.js.map +1 -0
  477. package/dist/plugins/esbuild.d.ts +45 -0
  478. package/dist/plugins/esbuild.d.ts.map +1 -0
  479. package/dist/plugins/esbuild.js +184 -0
  480. package/dist/plugins/esbuild.js.map +1 -0
  481. package/dist/plugins/type-view.d.ts +30 -0
  482. package/dist/plugins/type-view.d.ts.map +1 -0
  483. package/dist/plugins/type-view.js +245 -0
  484. package/dist/plugins/type-view.js.map +1 -0
  485. package/dist/plugins/vite.d.ts +56 -0
  486. package/dist/plugins/vite.d.ts.map +1 -0
  487. package/dist/plugins/vite.js +182 -0
  488. package/dist/plugins/vite.js.map +1 -0
  489. package/dist/service.ts +3 -0
  490. package/dist/service.ts.map +1 -0
  491. package/dist/testing/index.d.ts +24 -0
  492. package/dist/testing/index.d.ts.map +1 -0
  493. package/dist/testing/index.js +56 -0
  494. package/dist/testing/index.js.map +1 -0
  495. package/dist/tooling/doc.d.ts +82 -0
  496. package/dist/tooling/doc.d.ts.map +1 -0
  497. package/dist/tooling/doc.js +624 -0
  498. package/dist/tooling/doc.js.map +1 -0
  499. package/dist/tooling/idiomatic.d.ts +20 -0
  500. package/dist/tooling/idiomatic.d.ts.map +1 -0
  501. package/dist/tooling/idiomatic.js +154 -0
  502. package/dist/tooling/idiomatic.js.map +1 -0
  503. package/dist/tooling/migrate.d.ts +62 -0
  504. package/dist/tooling/migrate.d.ts.map +1 -0
  505. package/dist/tooling/migrate.js +608 -0
  506. package/dist/tooling/migrate.js.map +1 -0
  507. package/dist/tooling/pipeline-style.d.ts +12 -0
  508. package/dist/tooling/pipeline-style.d.ts.map +1 -0
  509. package/dist/tooling/pipeline-style.js +208 -0
  510. package/dist/tooling/pipeline-style.js.map +1 -0
  511. package/dist/version.d.ts +2 -0
  512. package/dist/version.d.ts.map +1 -0
  513. package/dist/version.js +25 -0
  514. package/dist/version.js.map +1 -0
  515. package/package.json +128 -4
  516. package/src/ast/types.ts +753 -0
  517. package/src/ast/walk.ts +35 -0
  518. package/src/cli/build-bun.ts +249 -0
  519. package/src/cli/build-watch.ts +299 -0
  520. package/src/cli/build.ts +450 -0
  521. package/src/cli/check.ts +342 -0
  522. package/src/cli/commands.ts +301 -0
  523. package/src/cli/doc.ts +81 -0
  524. package/src/cli/doctor.ts +173 -0
  525. package/src/cli/emit.ts +138 -0
  526. package/src/cli/explain.ts +127 -0
  527. package/src/cli/fmt.ts +170 -0
  528. package/src/cli/index.ts +227 -0
  529. package/src/cli/init.ts +491 -0
  530. package/src/cli/io.ts +150 -0
  531. package/src/cli/pipeline-style.ts +55 -0
  532. package/src/cli/repl.ts +443 -0
  533. package/src/cli/run.ts +238 -0
  534. package/src/cli/test.ts +148 -0
  535. package/src/cli/tsc.ts +207 -0
  536. package/src/cli/typecheck.ts +100 -0
  537. package/src/compiler/ast-inspection.ts +121 -0
  538. package/src/compiler/boolean-operators.ts +108 -0
  539. package/src/compiler/check.ts +2763 -0
  540. package/src/compiler/diagnostics.ts +179 -0
  541. package/src/compiler/effect-mutation-rule.ts +407 -0
  542. package/src/compiler/entry-rules.ts +116 -0
  543. package/src/compiler/incremental-build.ts +131 -0
  544. package/src/compiler/incremental-dependencies.ts +200 -0
  545. package/src/compiler/js-interop-nullability.ts +67 -0
  546. package/src/compiler/modules/output-collisions.ts +48 -0
  547. package/src/compiler/modules/output-plan.ts +46 -0
  548. package/src/compiler/modules/resolution.ts +56 -0
  549. package/src/compiler/numeric-comparisons.ts +213 -0
  550. package/src/compiler/optional-arguments.ts +335 -0
  551. package/src/compiler/package-fingerprint.ts +216 -0
  552. package/src/compiler/package-record-contracts.ts +156 -0
  553. package/src/compiler/pipelines/catalog.ts +1128 -0
  554. package/src/compiler/pipelines/companions.ts +1279 -0
  555. package/src/compiler/pipelines/resolution.ts +684 -0
  556. package/src/compiler/project-config.ts +221 -0
  557. package/src/compiler/record-contracts.ts +46 -0
  558. package/src/compiler/rules/control-flow.ts +57 -0
  559. package/src/compiler/rules/diagnostic.ts +11 -0
  560. package/src/compiler/rules/module-scope-refs.ts +63 -0
  561. package/src/compiler/schemas/analysis.ts +45 -0
  562. package/src/compiler/schemas/imported-record-functions.ts +984 -0
  563. package/src/compiler/schemas/imported-schemas.ts +761 -0
  564. package/src/compiler/source-map.ts +165 -0
  565. package/src/compiler/typescript-context.ts +250 -0
  566. package/src/compiler.ts +865 -0
  567. package/src/emitter/assemblyscript.ts +1220 -0
  568. package/src/emitter/declarations.ts +916 -0
  569. package/src/emitter/emitter.ts +3128 -0
  570. package/src/emitter/imports.ts +89 -0
  571. package/src/emitter/indent.ts +39 -0
  572. package/src/emitter/jsx.ts +122 -0
  573. package/src/emitter/local-bindings.ts +40 -0
  574. package/src/emitter/match.ts +1631 -0
  575. package/src/emitter/runtime-helpers.ts +31 -0
  576. package/src/emitter/schemas.ts +969 -0
  577. package/src/emitter/statements.ts +310 -0
  578. package/src/emitter/tagged-match.ts +171 -0
  579. package/src/emitter/types.ts +289 -0
  580. package/src/errors.ts +115 -0
  581. package/src/formatter/comments.ts +350 -0
  582. package/src/formatter/index.ts +1457 -0
  583. package/src/formatter/operator-precedence.ts +36 -0
  584. package/src/formatter/prettier.ts +61 -0
  585. package/src/index.ts +16 -0
  586. package/src/interop/callback.ts +188 -0
  587. package/src/interop/js.ts +66 -0
  588. package/src/interop/record.ts +36 -0
  589. package/src/interop/valibot.ts +141 -0
  590. package/src/interop/zod.ts +219 -0
  591. package/src/lexer/lexer.ts +1521 -0
  592. package/src/lexer/tokens.ts +174 -0
  593. package/src/lsp/code-action-worker.ts +31 -0
  594. package/src/lsp/completion.ts +343 -0
  595. package/src/lsp/definition.ts +1165 -0
  596. package/src/lsp/diagnostics-worker.ts +198 -0
  597. package/src/lsp/docs.ts +864 -0
  598. package/src/lsp/error-channel.ts +63 -0
  599. package/src/lsp/hover.ts +803 -0
  600. package/src/lsp/imports.ts +380 -0
  601. package/src/lsp/index.ts +55 -0
  602. package/src/lsp/inlay-hints.ts +308 -0
  603. package/src/lsp/interop-rename.ts +454 -0
  604. package/src/lsp/match-arms.ts +400 -0
  605. package/src/lsp/navigation.ts +553 -0
  606. package/src/lsp/parsed-documents.ts +64 -0
  607. package/src/lsp/rename-worker.ts +40 -0
  608. package/src/lsp/server.ts +1437 -0
  609. package/src/lsp/thunk-pipeline.ts +117 -0
  610. package/src/lsp/validation.ts +147 -0
  611. package/src/lsp/workspace.ts +74 -0
  612. package/src/parser/declarations.ts +808 -0
  613. package/src/parser/errors.ts +18 -0
  614. package/src/parser/jsx.ts +253 -0
  615. package/src/parser/operator-expressions.ts +170 -0
  616. package/src/parser/parser.ts +1565 -0
  617. package/src/parser/patterns.ts +342 -0
  618. package/src/parser/pipeline-expressions.ts +77 -0
  619. package/src/parser/scope.ts +69 -0
  620. package/src/parser/statements.ts +831 -0
  621. package/src/parser/types.ts +453 -0
  622. package/src/plugins/bun-preload.ts +25 -0
  623. package/src/plugins/bun.ts +448 -0
  624. package/src/plugins/esbuild.ts +308 -0
  625. package/src/plugins/type-view.ts +326 -0
  626. package/src/plugins/vite.ts +294 -0
  627. package/src/testing/index.ts +72 -0
  628. package/src/tooling/doc.ts +786 -0
  629. package/src/tooling/idiomatic.ts +202 -0
  630. package/src/tooling/pipeline-style.ts +248 -0
  631. package/src/version.ts +27 -0
@@ -0,0 +1,916 @@
1
+ import type {
2
+ ArrayPattern,
3
+ BindingDeclaration,
4
+ BindingTarget,
5
+ ErrorDeclaration,
6
+ Expression,
7
+ FunctionExpression,
8
+ Identifier,
9
+ Pattern,
10
+ RecordPattern,
11
+ RecordTypeNode,
12
+ SchemaDeclaration,
13
+ ServiceDeclaration,
14
+ Statement,
15
+ TopLevelDeclaration,
16
+ TypeNode,
17
+ WhenStatement,
18
+ } from "../ast/types.js";
19
+ import { emitAssemblyScript } from "./assemblyscript.js";
20
+ import { renderExportFrom, renderImport } from "./imports.js";
21
+ import type { RecordFieldInfo } from "./match.js";
22
+
23
+ export interface DeclarationEmitterContext {
24
+ emitExpression(expr: Expression): string;
25
+ emitNamedEffectFunction(expr: FunctionExpression, name: string): string;
26
+ emitStatement(stmt: Statement): string;
27
+ emitTypeNode(typeNode: TypeNode): string;
28
+ emitSchemaTypeField(typeNode: TypeNode): string;
29
+ emitBindingTarget(target: BindingTarget | Pattern): string;
30
+ emitSchemaDeclaration(decl: SchemaDeclaration, isExport: boolean): string;
31
+ emitWhenStatement(stmt: WhenStatement): string;
32
+ addEffectImport(name: string): void;
33
+ recordContracts: Map<string, TypeNode>;
34
+ parameterScopes: Array<Map<string, boolean>>;
35
+ variableTypeScopes: Array<Map<string, TypeNode | undefined>>;
36
+ recordScopes: Array<Map<string, Map<string, RecordFieldInfo> | undefined>>;
37
+ primitiveIdentifiers: Set<string>;
38
+ isOptionalValue(expr: Expression): boolean;
39
+ recordFields(name: string): Map<string, RecordFieldInfo> | undefined;
40
+ recordField?(expr: Expression): RecordFieldInfo | undefined;
41
+ isPrimitive(expr: Expression): boolean;
42
+ modularEffectImports?: boolean;
43
+ preserveFunImports?: boolean;
44
+ getRecordTypeNode?(name: string): RecordTypeNode | undefined;
45
+ getTypeNodeForExpression?(expr: Expression): TypeNode | undefined;
46
+ }
47
+
48
+ export function isPrimitiveType(type?: TypeNode): boolean {
49
+ if (!type) return false;
50
+ if (type.kind === "TypeReferenceNode") {
51
+ const name = type.name.name.toLowerCase();
52
+ return (
53
+ name === "number" ||
54
+ name === "string" ||
55
+ name === "boolean" ||
56
+ name === "int" ||
57
+ name === "float"
58
+ );
59
+ }
60
+ return false;
61
+ }
62
+
63
+ export function collectBindingNames(target: BindingTarget | Pattern): string[] {
64
+ if (target.kind === "Identifier" || target.kind === "UpperIdentifier")
65
+ return [target.name];
66
+ if (target.kind === "VariablePattern") return [target.name.name];
67
+ if (target.kind === "RestPattern")
68
+ return collectBindingNames(target.argument);
69
+ if (target.kind === "RecordPattern") {
70
+ return target.fields.flatMap((f) =>
71
+ f.pattern ? collectBindingNames(f.pattern) : [f.key.name],
72
+ );
73
+ }
74
+ if (target.kind === "ArrayPattern") {
75
+ return target.elements.flatMap((el) => collectBindingNames(el));
76
+ }
77
+ return [];
78
+ }
79
+
80
+ export function emitErrorDeclaration(
81
+ ctx: DeclarationEmitterContext,
82
+ decl: ErrorDeclaration,
83
+ isExport: boolean,
84
+ ): string {
85
+ ctx.addEffectImport("Schema");
86
+ const name = decl.id.name;
87
+ const exp = isExport ? "export " : "";
88
+ const fields = decl.fields
89
+ .map(
90
+ (f) => ` ${f.name.name}: ${ctx.emitSchemaTypeField(f.typeAnnotation)}`,
91
+ )
92
+ .join(",\n");
93
+
94
+ const className = `${name}$Class`;
95
+ return `${exp}class ${className} extends Schema.TaggedError<${className}>()("${name}", {\n${fields}\n}) {}\n${exp}type ${name} = InstanceType<typeof ${className}>;\n${exp}const ${name} = (function(args: ConstructorParameters<typeof ${className}>[0]): ${name} { return new ${className}(args); }) as { (args: ConstructorParameters<typeof ${className}>[0]): ${name}; new (args: ConstructorParameters<typeof ${className}>[0]): ${name} };`;
96
+ }
97
+
98
+ export function emitServiceDeclaration(
99
+ ctx: DeclarationEmitterContext,
100
+ decl: ServiceDeclaration,
101
+ isExport: boolean,
102
+ ): string {
103
+ ctx.addEffectImport("Context");
104
+ ctx.addEffectImport("Effect");
105
+ const name = decl.id.name;
106
+ const exp = isExport ? "export " : "";
107
+ const tag = decl.customTag ? decl.customTag.raw : `"app/${name}"`;
108
+ const members = decl.members
109
+ .map((m) => ` readonly ${m.name.name}: ${ctx.emitTypeNode(m.signature)};`)
110
+ .join("\n");
111
+
112
+ return `${exp}class ${name} extends Context.Service<${name}, {\n${members}\n}>()(${tag}) {}`;
113
+ }
114
+
115
+ let syntheticDestructureId = 0;
116
+
117
+ export function extractRecordFields(
118
+ typeNode: RecordTypeNode,
119
+ getRecordType?: (name: string) => RecordTypeNode | undefined,
120
+ ): Map<string, RecordFieldInfo> {
121
+ const result = new Map<string, RecordFieldInfo>();
122
+ for (const field of typeNode.fields) {
123
+ let childNode: RecordTypeNode | undefined;
124
+ if (field.typeAnnotation?.kind === "RecordTypeNode") {
125
+ childNode = field.typeAnnotation;
126
+ } else if (
127
+ field.typeAnnotation?.kind === "TypeReferenceNode" &&
128
+ getRecordType
129
+ ) {
130
+ childNode = getRecordType(field.typeAnnotation.name.name);
131
+ }
132
+ const children = childNode
133
+ ? extractRecordFields(childNode, getRecordType)
134
+ : undefined;
135
+ result.set(field.name.name, {
136
+ optional: Boolean(field.optional),
137
+ fields: children,
138
+ });
139
+ }
140
+ return result;
141
+ }
142
+
143
+ export function patternHasOptionalField(
144
+ pattern: RecordPattern,
145
+ fieldsInfo?: Map<string, RecordFieldInfo>,
146
+ ): boolean {
147
+ if (!fieldsInfo) return false;
148
+ for (const f of pattern.fields) {
149
+ if (f.isRest) continue;
150
+ const info = fieldsInfo.get(f.key.name);
151
+ if (info?.optional) return true;
152
+ if (f.pattern?.kind === "RecordPattern" && info?.fields) {
153
+ if (patternHasOptionalField(f.pattern, info.fields)) return true;
154
+ }
155
+ }
156
+ return false;
157
+ }
158
+
159
+ export function emitUnpackedRecordPattern(options: {
160
+ pat: RecordPattern;
161
+ basePath: string;
162
+ fieldsMap?: Map<string, RecordFieldInfo>;
163
+ emitBindingTarget: (target: BindingTarget | Pattern) => string;
164
+ addEffectImport: (name: string) => void;
165
+ exp?: string;
166
+ keyword?: string;
167
+ indent?: string;
168
+ onRecordBinding?: (
169
+ varName: string,
170
+ fields: Map<string, RecordFieldInfo>,
171
+ ) => void;
172
+ }): string[] {
173
+ const {
174
+ pat,
175
+ basePath,
176
+ fieldsMap,
177
+ emitBindingTarget,
178
+ addEffectImport,
179
+ exp = "",
180
+ keyword = "const",
181
+ indent = "",
182
+ onRecordBinding,
183
+ } = options;
184
+ const lines: string[] = [];
185
+
186
+ const unpackOptionalArray = (arrPat: ArrayPattern, optRef: string) => {
187
+ addEffectImport("Option");
188
+ for (let i = 0; i < arrPat.elements.length; i++) {
189
+ const el = arrPat.elements[i];
190
+ if (el.kind === "WildcardPattern" || el.kind === "LiteralPattern") {
191
+ continue;
192
+ }
193
+ if (el.kind === "RestPattern") {
194
+ const arg = emitBindingTarget(el.argument);
195
+ if (arg) {
196
+ lines.push(
197
+ `${indent}${exp}${keyword} ${arg} = Option.map(${optRef}, ($v) => $v.slice(${i}));`,
198
+ );
199
+ }
200
+ continue;
201
+ }
202
+ const elemOpt = `Option.flatMap(${optRef}, ($v) => Option.fromNullishOr($v[${i}]))`;
203
+ if (el.kind === "RecordPattern") {
204
+ const subOpt = `$funscriptOpt${syntheticDestructureId++}`;
205
+ lines.push(`${indent}const ${subOpt} = ${elemOpt};`);
206
+ unpackOptionalRecord(el, subOpt, undefined);
207
+ } else if (el.kind === "ArrayPattern") {
208
+ const subOpt = `$funscriptOpt${syntheticDestructureId++}`;
209
+ lines.push(`${indent}const ${subOpt} = ${elemOpt};`);
210
+ unpackOptionalArray(el, subOpt);
211
+ } else {
212
+ const varName = emitBindingTarget(el);
213
+ if (varName) {
214
+ lines.push(`${indent}${exp}${keyword} ${varName} = ${elemOpt};`);
215
+ }
216
+ }
217
+ }
218
+ };
219
+
220
+ const unpackOptionalRecord = (
221
+ recPat: RecordPattern,
222
+ optRef: string,
223
+ subFieldsMap?: Map<string, RecordFieldInfo>,
224
+ ) => {
225
+ addEffectImport("Option");
226
+ const restField = recPat.fields.find((f) => f.isRest);
227
+ if (restField) {
228
+ const nonRest = recPat.fields.filter((f) => !f.isRest);
229
+ lines.push(
230
+ `${indent}${exp}${keyword} ${restField.key.name} = Option.map(${optRef}, ({ ${nonRest.map((f) => `${f.key.name}: _${f.key.name}`).join(", ")}, ...$funscriptRest }) => $funscriptRest);`,
231
+ );
232
+ }
233
+ for (const f of recPat.fields) {
234
+ if (f.isRest) continue;
235
+ if (
236
+ f.pattern?.kind === "WildcardPattern" ||
237
+ f.pattern?.kind === "LiteralPattern"
238
+ ) {
239
+ continue;
240
+ }
241
+ const info = subFieldsMap?.get(f.key.name);
242
+ const isOpt = info?.optional === true;
243
+ const mappedOpt = isOpt
244
+ ? `Option.flatMap(${optRef}, ($v) => Option.fromNullishOr($v.${f.key.name}))`
245
+ : `Option.map(${optRef}, ($v) => $v.${f.key.name})`;
246
+ if (f.pattern?.kind === "RecordPattern") {
247
+ const subOpt = `$funscriptOpt${syntheticDestructureId++}`;
248
+ lines.push(`${indent}const ${subOpt} = ${mappedOpt};`);
249
+ unpackOptionalRecord(f.pattern, subOpt, info?.fields);
250
+ } else if (f.pattern?.kind === "ArrayPattern") {
251
+ const subOpt = `$funscriptOpt${syntheticDestructureId++}`;
252
+ lines.push(`${indent}const ${subOpt} = ${mappedOpt};`);
253
+ unpackOptionalArray(f.pattern, subOpt);
254
+ } else {
255
+ const varName = f.pattern ? emitBindingTarget(f.pattern) : f.key.name;
256
+ if (varName) {
257
+ lines.push(`${indent}${exp}${keyword} ${varName} = ${mappedOpt};`);
258
+ }
259
+ }
260
+ }
261
+ };
262
+
263
+ const unpackRecord = (
264
+ recPat: RecordPattern,
265
+ currentPath: string,
266
+ subFieldsMap?: Map<string, RecordFieldInfo>,
267
+ ) => {
268
+ const restField = recPat.fields.find((f) => f.isRest);
269
+ if (restField) {
270
+ const nonRest = recPat.fields.filter((f) => !f.isRest);
271
+ const restTemp = `$funscriptRest${syntheticDestructureId++}`;
272
+ lines.push(
273
+ `${indent}const { ${nonRest.map((f) => `${f.key.name}: _${f.key.name}`).join(", ")}, ...${restTemp} } = ${currentPath};`,
274
+ );
275
+ lines.push(
276
+ `${indent}${exp}${keyword} ${restField.key.name} = ${restTemp};`,
277
+ );
278
+ }
279
+ for (const f of recPat.fields) {
280
+ if (f.isRest) continue;
281
+ if (
282
+ f.pattern?.kind === "WildcardPattern" ||
283
+ f.pattern?.kind === "LiteralPattern"
284
+ ) {
285
+ continue;
286
+ }
287
+ const info = subFieldsMap?.get(f.key.name);
288
+ const isOpt = info?.optional === true;
289
+ const fieldPath = `${currentPath}.${f.key.name}`;
290
+ if (f.pattern?.kind === "RecordPattern") {
291
+ if (isOpt) {
292
+ const optTemp = `$funscriptOpt${syntheticDestructureId++}`;
293
+ addEffectImport("Option");
294
+ lines.push(
295
+ `${indent}const ${optTemp} = Option.fromNullishOr(${fieldPath});`,
296
+ );
297
+ unpackOptionalRecord(f.pattern, optTemp, info?.fields);
298
+ } else {
299
+ unpackRecord(f.pattern, fieldPath, info?.fields);
300
+ }
301
+ } else if (f.pattern?.kind === "ArrayPattern") {
302
+ if (isOpt) {
303
+ const optTemp = `$funscriptOpt${syntheticDestructureId++}`;
304
+ addEffectImport("Option");
305
+ lines.push(
306
+ `${indent}const ${optTemp} = Option.fromNullishOr(${fieldPath});`,
307
+ );
308
+ unpackOptionalArray(f.pattern, optTemp);
309
+ } else {
310
+ const varName = emitBindingTarget(f.pattern);
311
+ if (varName) {
312
+ lines.push(`${indent}${exp}${keyword} ${varName} = ${fieldPath};`);
313
+ }
314
+ }
315
+ } else {
316
+ const varName = f.pattern ? emitBindingTarget(f.pattern) : f.key.name;
317
+ if (!varName) continue;
318
+ if (isOpt) {
319
+ addEffectImport("Option");
320
+ lines.push(
321
+ `${indent}${exp}${keyword} ${varName} = Option.fromNullishOr(${fieldPath});`,
322
+ );
323
+ } else {
324
+ lines.push(`${indent}${exp}${keyword} ${varName} = ${fieldPath};`);
325
+ if (info?.fields) {
326
+ onRecordBinding?.(varName, info.fields);
327
+ }
328
+ }
329
+ }
330
+ }
331
+ };
332
+
333
+ unpackRecord(pat, basePath, fieldsMap);
334
+ return lines;
335
+ }
336
+
337
+ function getRecordFieldInfoForBinding(
338
+ ctx: DeclarationEmitterContext,
339
+ decl: BindingDeclaration,
340
+ ): Map<string, RecordFieldInfo> | undefined {
341
+ const lookup = (name: string): RecordTypeNode | undefined => {
342
+ const res = ctx.getRecordTypeNode?.(name) ?? ctx.recordContracts.get(name);
343
+ return res?.kind === "RecordTypeNode" ? res : undefined;
344
+ };
345
+
346
+ if (decl.typeAnnotation) {
347
+ if (decl.typeAnnotation.kind === "RecordTypeNode") {
348
+ return extractRecordFields(decl.typeAnnotation, lookup);
349
+ }
350
+ if (decl.typeAnnotation.kind === "TypeReferenceNode") {
351
+ const contract = lookup(decl.typeAnnotation.name.name);
352
+ if (contract) {
353
+ return extractRecordFields(contract, lookup);
354
+ }
355
+ }
356
+ }
357
+ if (decl.init.kind === "Identifier") {
358
+ const fields = ctx.recordFields(decl.init.name);
359
+ if (fields) return fields;
360
+ const typeNode = ctx.getTypeNodeForExpression?.(decl.init);
361
+ if (typeNode?.kind === "RecordTypeNode") {
362
+ return extractRecordFields(typeNode, lookup);
363
+ }
364
+ if (typeNode?.kind === "TypeReferenceNode") {
365
+ const contract = lookup(typeNode.name.name);
366
+ if (contract) {
367
+ return extractRecordFields(contract, lookup);
368
+ }
369
+ }
370
+ }
371
+ if (decl.init.kind === "MemberExpression") {
372
+ const field = ctx.recordField?.(decl.init);
373
+ if (field?.fields) return field.fields;
374
+ const typeNode = ctx.getTypeNodeForExpression?.(decl.init);
375
+ if (typeNode?.kind === "RecordTypeNode") {
376
+ return extractRecordFields(typeNode, lookup);
377
+ }
378
+ if (typeNode?.kind === "TypeReferenceNode") {
379
+ const contract = lookup(typeNode.name.name);
380
+ if (contract) {
381
+ return extractRecordFields(contract, lookup);
382
+ }
383
+ }
384
+ }
385
+ if (decl.init.kind === "RecordExpression") {
386
+ return extractRecordFieldsForBinding(ctx, decl);
387
+ }
388
+ return undefined;
389
+ }
390
+
391
+ function extractRecordFieldsForBinding(
392
+ ctx: DeclarationEmitterContext,
393
+ decl: BindingDeclaration,
394
+ ): Map<string, RecordFieldInfo> | undefined {
395
+ if (decl.isMut) return undefined;
396
+ const lookup = (name: string): RecordTypeNode | undefined => {
397
+ const res = ctx.getRecordTypeNode?.(name) ?? ctx.recordContracts.get(name);
398
+ return res?.kind === "RecordTypeNode" ? res : undefined;
399
+ };
400
+ if (decl.typeAnnotation?.kind === "RecordTypeNode") {
401
+ return extractRecordFields(decl.typeAnnotation, lookup);
402
+ }
403
+ if (decl.typeAnnotation?.kind === "TypeReferenceNode") {
404
+ const contract = lookup(decl.typeAnnotation.name.name);
405
+ if (contract) {
406
+ return extractRecordFields(contract, lookup);
407
+ }
408
+ }
409
+ if (decl.init.kind === "Identifier") {
410
+ return ctx.recordFields(decl.init.name);
411
+ }
412
+ if (decl.init.kind === "RecordExpression") {
413
+ const fields = new Map<string, RecordFieldInfo>();
414
+ let hasTracked = false;
415
+ for (const prop of decl.init.entries) {
416
+ if (prop.kind === "SpreadEntry") {
417
+ if (prop.argument.kind === "Identifier") {
418
+ const spreadFields = ctx.recordFields(prop.argument.name);
419
+ if (spreadFields) {
420
+ for (const [key, info] of spreadFields) {
421
+ if (info.optional) hasTracked = true;
422
+ fields.set(key, {
423
+ optional: info.optional,
424
+ defaultBinding: undefined,
425
+ fields: info.fields,
426
+ });
427
+ }
428
+ }
429
+ }
430
+ } else if (prop.kind === "FieldEntry" || prop.kind === "ShorthandEntry") {
431
+ const keyName = prop.key.name;
432
+ if (keyName) {
433
+ const isOpt =
434
+ prop.kind === "FieldEntry"
435
+ ? ctx.isOptionalValue(prop.value)
436
+ : ctx.isOptionalValue(prop.key);
437
+ if (isOpt) hasTracked = true;
438
+ fields.set(keyName, {
439
+ optional: isOpt,
440
+ defaultBinding: undefined,
441
+ fields: undefined,
442
+ });
443
+ }
444
+ }
445
+ }
446
+ if (hasTracked) return fields;
447
+ }
448
+ return undefined;
449
+ }
450
+
451
+ export function emitBindingDeclaration(
452
+ ctx: DeclarationEmitterContext,
453
+ decl: BindingDeclaration,
454
+ isExport = false,
455
+ ): string {
456
+ const scope = ctx.parameterScopes.at(-1);
457
+ const idName =
458
+ decl.id.kind === "Identifier" || decl.id.kind === "UpperIdentifier"
459
+ ? decl.id.name
460
+ : undefined;
461
+ if (idName) {
462
+ if (scope) scope.set(idName, ctx.isOptionalValue(decl.init));
463
+ const aliasedFields = extractRecordFieldsForBinding(ctx, decl);
464
+ ctx.recordScopes.at(-1)?.set(idName, aliasedFields);
465
+ const inferred = !decl.typeAnnotation
466
+ ? ctx.getTypeNodeForExpression?.(decl.init)
467
+ : undefined;
468
+ const inferredNullish =
469
+ inferred?.kind === "TypeReferenceNode" &&
470
+ inferred.name.name === "JSNullish";
471
+ const inferredNullishFunction =
472
+ inferred?.kind === "FunctionTypeNode" &&
473
+ inferred.returnType.kind === "TypeReferenceNode" &&
474
+ inferred.returnType.name.name === "JSNullish";
475
+ ctx.variableTypeScopes
476
+ .at(-1)
477
+ ?.set(
478
+ idName,
479
+ decl.typeAnnotation ??
480
+ (inferredNullish || inferredNullishFunction ? inferred : undefined),
481
+ );
482
+ if (isPrimitiveType(decl.typeAnnotation) || ctx.isPrimitive(decl.init)) {
483
+ ctx.primitiveIdentifiers.add(idName);
484
+ } else {
485
+ ctx.primitiveIdentifiers.delete(idName);
486
+ }
487
+ }
488
+
489
+ const fieldsInfo =
490
+ decl.id.kind === "RecordPattern"
491
+ ? getRecordFieldInfoForBinding(ctx, decl)
492
+ : undefined;
493
+
494
+ if (decl.id.kind === "RecordPattern" && fieldsInfo) {
495
+ for (const f of decl.id.fields) {
496
+ if (f.isRest) continue;
497
+ const varName =
498
+ f.pattern?.kind === "VariablePattern"
499
+ ? f.pattern.name.name
500
+ : f.key.name;
501
+ const info = fieldsInfo.get(f.key.name);
502
+ const isOpt = Boolean(info?.optional);
503
+ if (scope) scope.set(varName, isOpt);
504
+ if (info?.fields) {
505
+ ctx.recordScopes.at(-1)?.set(varName, info.fields);
506
+ }
507
+ }
508
+ }
509
+
510
+ const bindingNames = collectBindingNames(decl.id);
511
+ const exp = isExport && bindingNames.length > 0 ? "export " : "";
512
+ const keyword = decl.isMut ? "let" : "const";
513
+ const typeAnn = decl.typeAnnotation
514
+ ? `: ${ctx.emitTypeNode(decl.typeAnnotation)}`
515
+ : "";
516
+
517
+ if (
518
+ decl.id.kind === "RecordPattern" &&
519
+ fieldsInfo &&
520
+ patternHasOptionalField(decl.id, fieldsInfo)
521
+ ) {
522
+ ctx.addEffectImport("Option");
523
+ const boundNames = collectBindingNames(decl.id);
524
+ const canUseDirectInit =
525
+ decl.init.kind === "Identifier" &&
526
+ !boundNames.includes(decl.init.name) &&
527
+ !decl.typeAnnotation &&
528
+ !decl.isJs;
529
+ let sourceRef = "";
530
+ let initStatement = "";
531
+ const initExpr = decl.isJs
532
+ ? `Effect.runPromise(${ctx.emitExpression(decl.init)})`
533
+ : ctx.emitExpression(decl.init);
534
+
535
+ if (canUseDirectInit) {
536
+ sourceRef = (decl.init as Identifier).name;
537
+ } else {
538
+ sourceRef = `$funscriptDestructure${syntheticDestructureId++}`;
539
+ initStatement = `const ${sourceRef}${typeAnn} = ${initExpr};\n`;
540
+ }
541
+
542
+ const lines = emitUnpackedRecordPattern({
543
+ pat: decl.id,
544
+ basePath: sourceRef,
545
+ fieldsMap: fieldsInfo,
546
+ emitBindingTarget: (target) => ctx.emitBindingTarget(target),
547
+ addEffectImport: (name) => ctx.addEffectImport(name),
548
+ exp,
549
+ keyword,
550
+ });
551
+
552
+ return `${initStatement}${lines.join("\n")}`;
553
+ }
554
+
555
+ const target = ctx.emitBindingTarget(decl.id);
556
+
557
+ if (decl.isJs) {
558
+ ctx.addEffectImport("Effect");
559
+ if (decl.init.kind === "FunctionExpression") {
560
+ const fn = decl.init;
561
+ const validations = fn.params
562
+ .flatMap((p, index) => {
563
+ if (p.name.kind === "Identifier") {
564
+ if (!p.typeAnnotation) return [];
565
+ const paramName = p.name.name;
566
+ const typeAnn = p.typeAnnotation;
567
+ if (
568
+ typeAnn.kind === "TypeReferenceNode" &&
569
+ ctx.recordContracts.has(typeAnn.name.name)
570
+ ) {
571
+ return [
572
+ ` ${paramName} = ${typeAnn.name.name}DecodeChecked(${paramName});`,
573
+ ];
574
+ }
575
+ return [
576
+ ` ${paramName} = Schema.decodeUnknownSync(${ctx.emitSchemaTypeField(typeAnn)})(${paramName});`,
577
+ ];
578
+ }
579
+ if (p.name.kind === "RecordPattern") {
580
+ const rawParam = `$funscriptParam${index}`;
581
+ if (!p.typeAnnotation) {
582
+ return [
583
+ ` const ${ctx.emitBindingTarget(p.name)} = ${rawParam};`,
584
+ ];
585
+ }
586
+ const typeAnn = p.typeAnnotation;
587
+ const decodedVar = `$funscriptDecoded${index}`;
588
+ const lookup = (name: string): RecordTypeNode | undefined => {
589
+ const res =
590
+ ctx.getRecordTypeNode?.(name) ?? ctx.recordContracts.get(name);
591
+ return res?.kind === "RecordTypeNode" ? res : undefined;
592
+ };
593
+ let recType: TypeNode | undefined = typeAnn;
594
+ if (recType?.kind === "TypeReferenceNode") {
595
+ recType = lookup(recType.name.name);
596
+ }
597
+ const isContract =
598
+ typeAnn.kind === "TypeReferenceNode" &&
599
+ ctx.recordContracts.has(typeAnn.name.name);
600
+ const decodeStmt = isContract
601
+ ? ` const ${decodedVar} = ${typeAnn.name.name}DecodeChecked(${rawParam});`
602
+ : ` const ${decodedVar} = Schema.decodeUnknownSync(${ctx.emitSchemaTypeField(typeAnn)})(${rawParam});`;
603
+ const recFields =
604
+ recType?.kind === "RecordTypeNode"
605
+ ? extractRecordFields(recType, lookup)
606
+ : undefined;
607
+ if (patternHasOptionalField(p.name, recFields)) {
608
+ return [decodeStmt];
609
+ }
610
+ return [
611
+ decodeStmt,
612
+ ` const ${ctx.emitBindingTarget(p.name)} = ${decodedVar};`,
613
+ ];
614
+ }
615
+ return [];
616
+ })
617
+ .join("\n");
618
+ const optionalBindings = fn.params.flatMap((p, index) => {
619
+ if (p.name.kind === "RecordPattern") {
620
+ const lookup = (name: string): RecordTypeNode | undefined => {
621
+ const res =
622
+ ctx.getRecordTypeNode?.(name) ?? ctx.recordContracts.get(name);
623
+ return res?.kind === "RecordTypeNode" ? res : undefined;
624
+ };
625
+ let recType: TypeNode | undefined = p.typeAnnotation;
626
+ if (recType?.kind === "TypeReferenceNode") {
627
+ recType = lookup(recType.name.name);
628
+ }
629
+ const recFields =
630
+ recType?.kind === "RecordTypeNode"
631
+ ? extractRecordFields(recType, lookup)
632
+ : undefined;
633
+ if (patternHasOptionalField(p.name, recFields)) {
634
+ ctx.addEffectImport("Option");
635
+ const sourceRef = p.typeAnnotation
636
+ ? `$funscriptDecoded${index}`
637
+ : `$funscriptParam${index}`;
638
+ return emitUnpackedRecordPattern({
639
+ pat: p.name,
640
+ basePath: sourceRef,
641
+ fieldsMap: recFields,
642
+ emitBindingTarget: (target) => ctx.emitBindingTarget(target),
643
+ addEffectImport: (name) => ctx.addEffectImport(name),
644
+ indent: " ",
645
+ });
646
+ }
647
+ }
648
+ return [];
649
+ });
650
+ if (validations || optionalBindings.length > 0)
651
+ ctx.addEffectImport("Schema");
652
+ const prevPrimitiveIdentifiers = new Set(ctx.primitiveIdentifiers);
653
+ for (const p of fn.params) {
654
+ const isPrim = isPrimitiveType(p.typeAnnotation);
655
+ for (const n of collectBindingNames(p.name)) {
656
+ if (isPrim) {
657
+ ctx.primitiveIdentifiers.add(n);
658
+ } else {
659
+ ctx.primitiveIdentifiers.delete(n);
660
+ }
661
+ }
662
+ }
663
+ const params = fn.params
664
+ .map((p, index) => {
665
+ const rest = p.isRest ? "..." : "";
666
+ let name = ctx.emitBindingTarget(p.name);
667
+ if (p.name.kind === "RecordPattern") {
668
+ name = `$funscriptParam${index}`;
669
+ }
670
+ const t = p.typeAnnotation
671
+ ? `: ${ctx.emitTypeNode(p.typeAnnotation)}`
672
+ : "";
673
+ const def = p.defaultValue
674
+ ? ` = ${ctx.emitExpression(p.defaultValue)}`
675
+ : "";
676
+ return `${rest}${name}${t}${def}`;
677
+ })
678
+ .join(", ");
679
+ const paramScopeEntries: [string, boolean][] = [];
680
+ const varTypeEntries: [string, TypeNode | undefined][] = [];
681
+ const recordDefaults: string[] = [];
682
+ const recordScope = new Map<
683
+ string,
684
+ Map<string, RecordFieldInfo> | undefined
685
+ >();
686
+ for (let paramIndex = 0; paramIndex < fn.params.length; paramIndex++) {
687
+ const p = fn.params[paramIndex];
688
+ if (p.name.kind === "Identifier") {
689
+ const paramName = p.name.name;
690
+ paramScopeEntries.push([paramName, false]);
691
+ if (p.typeAnnotation?.kind === "RecordTypeNode") {
692
+ const mapped = new Map<string, RecordFieldInfo>();
693
+ p.typeAnnotation.fields.forEach((field, fieldIndex) => {
694
+ const binding = field.defaultValue
695
+ ? `$funscriptField${paramIndex}_${fieldIndex}`
696
+ : undefined;
697
+ if (field.defaultValue) {
698
+ const raw = `$funscriptFieldRaw${paramIndex}_${fieldIndex}`;
699
+ recordDefaults.push(
700
+ ` const ${raw} = ${paramName}.${field.name.name};`,
701
+ );
702
+ recordDefaults.push(
703
+ ` const ${binding} = ${raw} === undefined ? ${ctx.emitExpression(field.defaultValue)} : ${raw};`,
704
+ );
705
+ }
706
+ const children =
707
+ field.typeAnnotation?.kind === "RecordTypeNode"
708
+ ? extractRecordFields(field.typeAnnotation)
709
+ : undefined;
710
+ mapped.set(field.name.name, {
711
+ optional: Boolean(field.optional),
712
+ defaultBinding: binding,
713
+ fields: children,
714
+ });
715
+ });
716
+ recordScope.set(p.name.name, mapped);
717
+ } else if (
718
+ p.typeAnnotation?.kind === "TypeReferenceNode" &&
719
+ ctx.recordContracts.has(p.typeAnnotation.name.name)
720
+ ) {
721
+ const contract = ctx.recordContracts.get(
722
+ p.typeAnnotation.name.name,
723
+ )!;
724
+ if (contract?.kind === "RecordTypeNode") {
725
+ recordScope.set(p.name.name, extractRecordFields(contract));
726
+ }
727
+ }
728
+ } else if (p.name.kind === "RecordPattern") {
729
+ const lookup = (name: string): RecordTypeNode | undefined => {
730
+ const res =
731
+ ctx.getRecordTypeNode?.(name) ?? ctx.recordContracts.get(name);
732
+ return res?.kind === "RecordTypeNode" ? res : undefined;
733
+ };
734
+ let recType: TypeNode | undefined = p.typeAnnotation;
735
+ if (recType?.kind === "TypeReferenceNode") {
736
+ recType = lookup(recType.name.name);
737
+ }
738
+ const recFields =
739
+ recType?.kind === "RecordTypeNode"
740
+ ? extractRecordFields(recType, lookup)
741
+ : undefined;
742
+ if (patternHasOptionalField(p.name, recFields)) {
743
+ const collectParamsFromPattern = (
744
+ pat: RecordPattern,
745
+ fieldsMap?: Map<string, RecordFieldInfo>,
746
+ typeNode?: TypeNode,
747
+ parentIsOpt = false,
748
+ ) => {
749
+ for (const f of pat.fields) {
750
+ if (f.isRest) {
751
+ paramScopeEntries.push([f.key.name, parentIsOpt]);
752
+ varTypeEntries.push([f.key.name, undefined]);
753
+ continue;
754
+ }
755
+ const info = fieldsMap?.get(f.key.name);
756
+ const isOpt = parentIsOpt || info?.optional === true;
757
+ const matchingFieldType =
758
+ typeNode?.kind === "RecordTypeNode"
759
+ ? typeNode.fields.find((tf) => tf.name.name === f.key.name)
760
+ ?.typeAnnotation
761
+ : undefined;
762
+ if (f.pattern?.kind === "RecordPattern") {
763
+ let subType: TypeNode | undefined = matchingFieldType;
764
+ if (subType?.kind === "TypeReferenceNode") {
765
+ subType = lookup(subType.name.name);
766
+ }
767
+ collectParamsFromPattern(
768
+ f.pattern,
769
+ info?.fields,
770
+ subType,
771
+ isOpt,
772
+ );
773
+ } else if (f.pattern?.kind === "ArrayPattern") {
774
+ const elemType =
775
+ matchingFieldType?.kind === "ArrayTypeNode"
776
+ ? matchingFieldType.elementType
777
+ : undefined;
778
+ for (const el of f.pattern.elements) {
779
+ for (const n of collectBindingNames(el)) {
780
+ paramScopeEntries.push([n, isOpt]);
781
+ varTypeEntries.push([n, elemType]);
782
+ }
783
+ }
784
+ } else {
785
+ const varName = f.pattern
786
+ ? f.pattern.kind === "VariablePattern"
787
+ ? f.pattern.name.name
788
+ : ctx.emitBindingTarget(f.pattern)
789
+ : f.key.name;
790
+ paramScopeEntries.push([varName, isOpt]);
791
+ if (!isOpt && info?.fields) {
792
+ recordScope.set(varName, info.fields);
793
+ }
794
+ if (matchingFieldType) {
795
+ varTypeEntries.push([varName, matchingFieldType]);
796
+ }
797
+ }
798
+ }
799
+ };
800
+ collectParamsFromPattern(p.name, recFields, recType);
801
+ continue;
802
+ }
803
+ }
804
+ for (const n of collectBindingNames(p.name)) {
805
+ varTypeEntries.push([n, p.typeAnnotation]);
806
+ }
807
+ }
808
+ ctx.parameterScopes.push(new Map(paramScopeEntries));
809
+ ctx.variableTypeScopes.push(new Map(varTypeEntries));
810
+ ctx.recordScopes.push(recordScope);
811
+ const headerStatements = [
812
+ validations,
813
+ ...recordDefaults,
814
+ ...optionalBindings,
815
+ ]
816
+ .filter(Boolean)
817
+ .join("\n");
818
+ const prefix = headerStatements ? `${headerStatements}\n` : "";
819
+ try {
820
+ if (fn.body.kind === "BlockStatement") {
821
+ const retExpr = fn.body.finalExpression
822
+ ? ctx.emitExpression(fn.body.finalExpression)
823
+ : "undefined";
824
+ if (fn.body.statements.length === 0) {
825
+ return `${exp}${keyword} ${target} = async (${params}) => {\n${prefix} return Effect.runPromise(${retExpr});\n};`;
826
+ }
827
+ const stmts = fn.body.statements
828
+ .map((s) => " " + ctx.emitStatement(s))
829
+ .join("\n");
830
+ return `${exp}${keyword} ${target} = async (${params}) => {\n${prefix} return Effect.runPromise(Effect.gen(function* () {\n${stmts}\n return ${retExpr};\n }));\n};`;
831
+ }
832
+ return `${exp}${keyword} ${target} = async (${params}) => {\n${prefix} return Effect.runPromise(${ctx.emitExpression(fn.body)});\n};`;
833
+ } finally {
834
+ ctx.primitiveIdentifiers = prevPrimitiveIdentifiers;
835
+ ctx.recordScopes.pop();
836
+ ctx.parameterScopes.pop();
837
+ ctx.variableTypeScopes.pop();
838
+ }
839
+ }
840
+ return `${exp}${keyword} ${target}${typeAnn} = Effect.runPromise(${ctx.emitExpression(decl.init)});`;
841
+ }
842
+
843
+ const bindingName =
844
+ decl.id.kind === "Identifier" || decl.id.kind === "UpperIdentifier"
845
+ ? decl.id.name
846
+ : undefined;
847
+ const fnBody =
848
+ decl.init.kind === "FunctionExpression" ? decl.init.body : undefined;
849
+ const finalExpr =
850
+ fnBody?.kind === "BlockStatement" ? fnBody.finalExpression : fnBody;
851
+ const returnsEffectCall =
852
+ finalExpr?.kind === "CallExpression" &&
853
+ finalExpr.callee.kind === "MemberExpression" &&
854
+ finalExpr.callee.object.kind === "UpperIdentifier" &&
855
+ finalExpr.callee.object.name === "Effect";
856
+ const isEffectFunction =
857
+ bindingName !== undefined &&
858
+ decl.init.kind === "FunctionExpression" &&
859
+ (decl.init.returnType?.kind === "EffectTypeNode" ||
860
+ decl.init.body.kind === "EffectBlockExpression" ||
861
+ (decl.init.body.kind === "BlockStatement" &&
862
+ decl.init.body.finalExpression?.kind === "EffectBlockExpression") ||
863
+ returnsEffectCall);
864
+ let initExpr: string;
865
+ if (
866
+ isEffectFunction &&
867
+ decl.init.kind === "FunctionExpression" &&
868
+ bindingName
869
+ ) {
870
+ ctx.addEffectImport("Effect");
871
+ initExpr = ctx.emitNamedEffectFunction(decl.init, bindingName);
872
+ } else {
873
+ initExpr = ctx.emitExpression(decl.init);
874
+ }
875
+ return `${exp}${keyword} ${target}${typeAnn} = ${initExpr};`;
876
+ }
877
+
878
+ export function emitTopLevelDeclaration(
879
+ ctx: DeclarationEmitterContext,
880
+ decl: TopLevelDeclaration,
881
+ isExport = false,
882
+ ): string {
883
+ switch (decl.kind) {
884
+ case "ImportDeclaration":
885
+ return renderImport(
886
+ decl,
887
+ ctx.preserveFunImports ? decl.source.raw : undefined,
888
+ ctx.modularEffectImports,
889
+ );
890
+ case "ExportFromDeclaration":
891
+ return renderExportFrom(
892
+ decl,
893
+ ctx.preserveFunImports ? decl.source.raw : undefined,
894
+ );
895
+ case "ExportDeclaration":
896
+ return emitTopLevelDeclaration(ctx, decl.declaration, true);
897
+ case "ExportDefaultDeclaration":
898
+ return `export default ${ctx.emitExpression(decl.expression)};`;
899
+ case "SchemaDeclaration":
900
+ return ctx.emitSchemaDeclaration(decl, isExport);
901
+ case "ErrorDeclaration":
902
+ return emitErrorDeclaration(ctx, decl, isExport);
903
+ case "ServiceDeclaration":
904
+ return emitServiceDeclaration(ctx, decl, isExport);
905
+ case "BindingDeclaration":
906
+ return emitBindingDeclaration(ctx, decl, isExport);
907
+ case "WhenStatement":
908
+ return ctx.emitWhenStatement(decl);
909
+ case "TsEscapeDeclaration":
910
+ return decl.code + "\n";
911
+ case "AsDeclaration":
912
+ ctx.addEffectImport("Effect");
913
+ ctx.addEffectImport("Scope");
914
+ return emitAssemblyScript(decl);
915
+ }
916
+ }