funkscript 0.0.0-stage → 0.2.3

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,1457 @@
1
+ import {
2
+ type ArrayExpression,
3
+ type BinaryExpression,
4
+ type BindingDeclaration,
5
+ type BindingTarget,
6
+ type BlockStatement,
7
+ type CallExpression,
8
+ type EffectBlockExpression,
9
+ type EffectStatement,
10
+ type ErrorDeclaration,
11
+ type Expression,
12
+ type FunctionBody,
13
+ type FunctionExpression,
14
+ type Identifier,
15
+ type ImportDeclaration,
16
+ type Literal,
17
+ type MatchArm,
18
+ type MatchExpression,
19
+ type ParBlockExpression,
20
+ type Pattern,
21
+ type PipelineExpression,
22
+ type Program,
23
+ type RecordEntry,
24
+ type RecordExpression,
25
+ type RecordSchema,
26
+ type SchemaBody,
27
+ type SchemaDeclaration,
28
+ type ServiceDeclaration,
29
+ type Statement,
30
+ type StringLiteral,
31
+ type TaggedTemplateExpression,
32
+ type TaggedUnionSchema,
33
+ type TemplateLiteral,
34
+ type TopLevelDeclaration,
35
+ type TypeNode,
36
+ type TypeParameter,
37
+ type UnaryExpression,
38
+ type WhenStatement,
39
+ } from "../ast/types.js";
40
+ import { renderExportFrom, renderImport } from "../emitter/imports.js";
41
+ import { Lexer } from "../lexer/lexer.js";
42
+ import { KEYWORDS, BANNED_KEYWORDS } from "../lexer/tokens.js";
43
+ import { Parser } from "../parser/parser.js";
44
+ import { convertPipelineStyle } from "../tooling/pipeline-style.js";
45
+ import { ASSOCIATIVITY, PRECEDENCE } from "./operator-precedence.js";
46
+ import {
47
+ CommentAttachments,
48
+ type Comment,
49
+ type AstInspectNode,
50
+ } from "./comments.js";
51
+ export { extractComments, type Comment } from "./comments.js";
52
+
53
+ const RESERVED_IDENTIFIERS = new Set<string>([
54
+ ...Object.keys(KEYWORDS),
55
+ ...Object.keys(BANNED_KEYWORDS),
56
+ ]);
57
+
58
+ export interface FormatOptions {
59
+ indentSize?: number;
60
+ lineWidth?: number;
61
+ filename?: string;
62
+ }
63
+
64
+ export class Formatter {
65
+ private indentSize: number;
66
+ private lineWidth: number;
67
+ private leadingComments = new Map<AstInspectNode, Comment[]>();
68
+ private trailingComments = new Map<number, Comment>();
69
+ private eofComments: Comment[] = [];
70
+
71
+ constructor(
72
+ private source: string,
73
+ private ast: Program,
74
+ options?: FormatOptions,
75
+ ) {
76
+ this.indentSize = options?.indentSize ?? 2;
77
+ this.lineWidth = options?.lineWidth ?? 100;
78
+ const comments = new CommentAttachments(ast, source);
79
+ this.leadingComments = comments.leadingComments;
80
+ this.trailingComments = comments.trailingComments;
81
+ this.eofComments = comments.eofComments;
82
+ }
83
+
84
+ private spaces(count: number): string {
85
+ return " ".repeat(Math.max(0, count));
86
+ }
87
+
88
+ private escapeStringPart(val: string): string {
89
+ return val
90
+ .replace(/\\/g, "\\\\")
91
+ .replace(/"/g, '\\"')
92
+ .replace(/\n/g, "\\n")
93
+ .replace(/\r/g, "\\r")
94
+ .replace(/\t/g, "\\t")
95
+ .replace(/\{/g, "\\{");
96
+ }
97
+
98
+ private formatLeadingComments(
99
+ node: AstInspectNode,
100
+ indentStr: string,
101
+ ): string {
102
+ const comments = this.leadingComments.get(node);
103
+ if (!comments || comments.length === 0) return "";
104
+
105
+ const lines: string[] = [];
106
+ for (let i = 0; i < comments.length; i++) {
107
+ const c = comments[i];
108
+ if (i > 0 && c.line - comments[i - 1].line > 1) {
109
+ lines.push("");
110
+ }
111
+ lines.push(`${indentStr}${c.text}`);
112
+ }
113
+
114
+ const lastComment = comments[comments.length - 1];
115
+ const blankAfter = node.span && node.span.start.line - lastComment.line > 1;
116
+
117
+ return lines.join("\n") + "\n" + (blankAfter ? "\n" : "");
118
+ }
119
+
120
+ private getTrailingComment(line: number): string {
121
+ const c = this.trailingComments.get(line);
122
+ if (!c) return "";
123
+ this.trailingComments.delete(line);
124
+ return ` ${c.text}`;
125
+ }
126
+
127
+ public format(): string {
128
+ const parts: string[] = [];
129
+
130
+ for (let i = 0; i < this.ast.body.length; i++) {
131
+ const decl = this.ast.body[i];
132
+ const leading = this.formatLeadingComments(decl, "");
133
+ const formattedDecl = this.formatTopLevelDeclaration(decl);
134
+ const trailing = this.getTrailingComment(decl.span.end.line);
135
+ const piece = `${leading}${formattedDecl}${trailing}`;
136
+ parts.push(piece);
137
+ }
138
+
139
+ let result = "";
140
+ for (let i = 0; i < parts.length; i++) {
141
+ result += parts[i];
142
+ if (i < parts.length - 1) {
143
+ const curr = this.ast.body[i];
144
+ const next = this.ast.body[i + 1];
145
+ const currInner =
146
+ curr.kind === "ExportDeclaration" ? curr.declaration : curr;
147
+ const nextInner =
148
+ next.kind === "ExportDeclaration" ? next.declaration : next;
149
+ const currIsMulti =
150
+ currInner.kind === "ServiceDeclaration" ||
151
+ (currInner.kind === "SchemaDeclaration" &&
152
+ currInner.body.kind !== "TypeSignatureSchema" &&
153
+ currInner.body.kind !== "TsDerivedSchema" &&
154
+ currInner.body.kind !== "ImportedTsSchema") ||
155
+ (currInner.kind === "BindingDeclaration" &&
156
+ currInner.init.span &&
157
+ currInner.init.span.end.line > currInner.init.span.start.line) ||
158
+ (currInner.kind === "ExportDefaultDeclaration" &&
159
+ currInner.expression.span &&
160
+ currInner.expression.span.end.line >
161
+ currInner.expression.span.start.line);
162
+ const nextIsMulti =
163
+ nextInner.kind === "ServiceDeclaration" ||
164
+ (nextInner.kind === "SchemaDeclaration" &&
165
+ nextInner.body.kind !== "TypeSignatureSchema" &&
166
+ nextInner.body.kind !== "TsDerivedSchema" &&
167
+ nextInner.body.kind !== "ImportedTsSchema") ||
168
+ (nextInner.kind === "BindingDeclaration" &&
169
+ nextInner.init.span &&
170
+ nextInner.init.span.end.line > nextInner.init.span.start.line) ||
171
+ (nextInner.kind === "ExportDefaultDeclaration" &&
172
+ nextInner.expression.span &&
173
+ nextInner.expression.span.end.line >
174
+ nextInner.expression.span.start.line);
175
+
176
+ if (currIsMulti || nextIsMulti) {
177
+ result += "\n\n";
178
+ } else {
179
+ const hasBlank = next.span.start.line - curr.span.end.line > 1;
180
+ if (hasBlank) {
181
+ result += "\n\n";
182
+ } else {
183
+ result += "\n";
184
+ }
185
+ }
186
+ }
187
+ }
188
+
189
+ if (this.eofComments.length > 0) {
190
+ if (result.length > 0) result += "\n\n";
191
+ result += this.eofComments.map((c) => c.text).join("\n");
192
+ }
193
+
194
+ return result.trimEnd() + "\n";
195
+ }
196
+
197
+ private formatTopLevelDeclaration(decl: TopLevelDeclaration): string {
198
+ switch (decl.kind) {
199
+ case "ExportDeclaration":
200
+ return `export ${this.formatDeclaration(decl.declaration)}`;
201
+ case "ExportDefaultDeclaration":
202
+ return `export default ${this.formatExpression(decl.expression, 0)}`;
203
+ case "ImportDeclaration":
204
+ return this.formatImportDeclaration(decl);
205
+ case "ExportFromDeclaration": {
206
+ const part = decl.source.parts[0];
207
+ const value = part && part.kind === "StringRawPart" ? part.value : "";
208
+ return renderExportFrom(
209
+ decl,
210
+ `"${this.escapeStringPart(value)}"`,
211
+ ).slice(0, -1);
212
+ }
213
+ case "SchemaDeclaration":
214
+ return this.formatSchemaDeclaration(decl, 0);
215
+ case "ErrorDeclaration":
216
+ return this.formatErrorDeclaration(decl);
217
+ case "ServiceDeclaration":
218
+ return this.formatServiceDeclaration(decl, 0);
219
+ case "BindingDeclaration":
220
+ return this.formatBindingDeclaration(decl, 0);
221
+ case "WhenStatement":
222
+ return this.formatStatement(decl, 0);
223
+ case "TsEscapeDeclaration":
224
+ return this.formatTsEscape(decl.code);
225
+ case "AsDeclaration": {
226
+ const prefix = decl.serviceName ? `as ${decl.serviceName.name}` : "as";
227
+ return decl.code.includes("\n")
228
+ ? `${prefix} {\n${decl.code}\n}`
229
+ : `${prefix} { ${decl.code} }`;
230
+ }
231
+ }
232
+ }
233
+
234
+ private formatDeclaration(
235
+ decl:
236
+ | SchemaDeclaration
237
+ | ErrorDeclaration
238
+ | ServiceDeclaration
239
+ | BindingDeclaration
240
+ | WhenStatement,
241
+ ): string {
242
+ switch (decl.kind) {
243
+ case "SchemaDeclaration":
244
+ return this.formatSchemaDeclaration(decl, 0);
245
+ case "ErrorDeclaration":
246
+ return this.formatErrorDeclaration(decl);
247
+ case "ServiceDeclaration":
248
+ return this.formatServiceDeclaration(decl, 0);
249
+ case "BindingDeclaration":
250
+ return this.formatBindingDeclaration(decl, 0);
251
+ case "WhenStatement":
252
+ return this.formatStatement(decl, 0);
253
+ }
254
+ }
255
+
256
+ private formatImportDeclaration(decl: ImportDeclaration): string {
257
+ const firstPart = decl.source.parts[0];
258
+ const rawVal =
259
+ firstPart && firstPart.kind === "StringRawPart" ? firstPart.value : "";
260
+ const source = `"${this.escapeStringPart(rawVal)}"`;
261
+ return renderImport(decl, source).slice(0, -1);
262
+ }
263
+
264
+ private formatTypeParameters(
265
+ typeParams: TypeParameter[] | undefined,
266
+ ): string {
267
+ if (!typeParams || typeParams.length === 0) return "";
268
+ return `<${typeParams
269
+ .map((param) =>
270
+ param.constraint
271
+ ? `${param.name.name} extends ${this.formatType(param.constraint)}`
272
+ : param.name.name,
273
+ )
274
+ .join(", ")}>`;
275
+ }
276
+
277
+ private formatSchemaDeclaration(decl: SchemaDeclaration, indent = 0): string {
278
+ const prefix = decl.isTs ? "ts schema " : "schema ";
279
+ const typeParams = this.formatTypeParameters(decl.typeParams);
280
+ const body = this.formatSchemaBody(decl.body, indent);
281
+ const sep = body.startsWith("\n") ? " =" : " = ";
282
+ return `${prefix}${decl.id.name}${typeParams}${sep}${body}`;
283
+ }
284
+
285
+ private formatSchemaBody(body: SchemaBody, indent = 0): string {
286
+ switch (body.kind) {
287
+ case "RecordSchema":
288
+ return this.formatRecordSchema(body, indent);
289
+ case "TaggedUnionSchema":
290
+ return this.formatTaggedUnionSchema(body, indent);
291
+ case "StructuralUnionSchema":
292
+ return (
293
+ "\n" +
294
+ body.members
295
+ .map(
296
+ (member) =>
297
+ `${this.spaces(indent + this.indentSize)}| ${this.formatType(member, indent + this.indentSize)}`,
298
+ )
299
+ .join("\n")
300
+ );
301
+ case "RefinementSchema":
302
+ return `${this.formatType(body.baseType)} |> ${this.formatExpression(body.refinement, indent)}`;
303
+ case "TypeSignatureSchema":
304
+ return this.formatType(body.signature);
305
+ case "TsDerivedSchema":
306
+ return this.formatType(body.targetType, indent);
307
+ case "ImportedTsSchema":
308
+ return `from ts<${body.typeArguments.map((t) => this.formatType(t, indent)).join(", ")}>`;
309
+ }
310
+ }
311
+
312
+ private formatRecordSchema(schema: RecordSchema, indent = 0): string {
313
+ if (schema.fields.length === 0) return "{}";
314
+ const fieldIndent = this.spaces(indent + this.indentSize);
315
+ const closingIndent = this.spaces(indent);
316
+ const fields = schema.fields
317
+ .map((f) => {
318
+ const leading = this.formatLeadingComments(f, fieldIndent);
319
+ const trailing = this.getTrailingComment(f.span.end.line);
320
+ const refinement = f.refinement
321
+ ? ` |> ${this.formatExpression(f.refinement, indent + this.indentSize)}`
322
+ : "";
323
+ const def = f.defaultValue
324
+ ? ` = ${this.formatExpression(f.defaultValue, indent + this.indentSize)}`
325
+ : "";
326
+ return `${leading}${fieldIndent}${f.name.name}: ${this.formatType(f.typeAnnotation, indent + this.indentSize)}${refinement}${def}${trailing}`;
327
+ })
328
+ .join("\n");
329
+ return `{\n${fields}\n${closingIndent}}`;
330
+ }
331
+
332
+ private formatTaggedUnionSchema(
333
+ schema: TaggedUnionSchema,
334
+ indent = 0,
335
+ ): string {
336
+ const variantIndent = this.spaces(indent + this.indentSize);
337
+ const variants = schema.variants
338
+ .map((v) => {
339
+ const leading = this.formatLeadingComments(v, variantIndent);
340
+ const trailing = this.getTrailingComment(v.span.end.line);
341
+ const fields =
342
+ v.fields.length > 0
343
+ ? `(${v.fields.map((f) => `${f.name.name}: ${this.formatType(f.typeAnnotation)}`).join(", ")})`
344
+ : "";
345
+ return `${leading}${variantIndent}| ${v.name.name}${fields}${trailing}`;
346
+ })
347
+ .join("\n");
348
+ return `\n${variants}`;
349
+ }
350
+
351
+ private formatErrorDeclaration(decl: ErrorDeclaration): string {
352
+ const fields =
353
+ decl.fields.length > 0
354
+ ? `(${decl.fields.map((f) => `${f.name.name}: ${this.formatType(f.typeAnnotation)}`).join(", ")})`
355
+ : "";
356
+ return `error ${decl.id.name}${fields}`;
357
+ }
358
+
359
+ private formatServiceDeclaration(
360
+ decl: ServiceDeclaration,
361
+ indent = 0,
362
+ ): string {
363
+ let tag = "";
364
+ if (decl.customTag) {
365
+ const firstPart = decl.customTag.parts[0];
366
+ const rawVal =
367
+ firstPart && firstPart.kind === "StringRawPart" ? firstPart.value : "";
368
+ tag = ` "${this.escapeStringPart(rawVal)}"`;
369
+ }
370
+ const memberIndent = this.spaces(indent + this.indentSize);
371
+ const members = decl.members
372
+ .map((m) => {
373
+ const leading = this.formatLeadingComments(m, memberIndent);
374
+ const trailing = this.getTrailingComment(m.span.end.line);
375
+ return `${leading}${memberIndent}${m.name.name}: ${this.formatType(m.signature)}${trailing}`;
376
+ })
377
+ .join("\n");
378
+ return `service ${decl.id.name}${tag}\n${members}`;
379
+ }
380
+
381
+ private formatBindingDeclaration(
382
+ decl: BindingDeclaration,
383
+ indent = 0,
384
+ ): string {
385
+ const js = decl.isJs ? "js " : "";
386
+ const mut = decl.isMut ? "mut " : "";
387
+ const typeAnn = decl.typeAnnotation
388
+ ? `: ${this.formatType(decl.typeAnnotation)}`
389
+ : "";
390
+ const idStr =
391
+ decl.id.kind === "Identifier" || decl.id.kind === "UpperIdentifier"
392
+ ? decl.id.name
393
+ : this.formatPattern(decl.id);
394
+ const prefix = `${js}${mut}${idStr}${typeAnn} =`;
395
+ const init = this.formatBindingInit(decl, indent);
396
+ return `${prefix}${init}`;
397
+ }
398
+
399
+ private formatBindingTarget(target: BindingTarget): string {
400
+ return target.kind === "Identifier" || target.kind === "UpperIdentifier"
401
+ ? target.name
402
+ : this.formatPattern(target);
403
+ }
404
+
405
+ private formatBindingInit(decl: BindingDeclaration, indent = 0): string {
406
+ const init = decl.init;
407
+ if (init.kind === "FunctionExpression") {
408
+ const typeParams = this.formatTypeParameters(init.typeParams);
409
+ const params = init.params
410
+ .map((p) => {
411
+ const rest = p.isRest ? "..." : "";
412
+ const type = p.typeAnnotation
413
+ ? `: ${this.formatType(p.typeAnnotation)}`
414
+ : "";
415
+ const defaultValue = p.defaultValue
416
+ ? ` = ${this.formatExpression(p.defaultValue, indent)}`
417
+ : "";
418
+ return `${rest}${this.formatBindingTarget(p.name)}${type}${defaultValue}`;
419
+ })
420
+ .join(", ");
421
+ const ret = init.returnType
422
+ ? `: ${this.formatType(init.returnType)}`
423
+ : "";
424
+ const isArrowOnly =
425
+ init.params.length === 0 &&
426
+ !init.returnType &&
427
+ init.span &&
428
+ this.source.slice(
429
+ init.span.start.offset,
430
+ init.span.start.offset + 2,
431
+ ) === "=>";
432
+ const sig = isArrowOnly ? ` =>` : ` ${typeParams}(${params})${ret} =>`;
433
+
434
+ if (init.body.kind === "BlockStatement") {
435
+ const body = this.formatBlockStatement(
436
+ init.body,
437
+ indent + this.indentSize,
438
+ );
439
+ return `${sig}\n${body}`;
440
+ } else if (
441
+ init.body.kind === "PipelineExpression" &&
442
+ this.shouldExpandPipeline(
443
+ this.unrollPipeline(init.body),
444
+ indent + this.indentSize,
445
+ )
446
+ ) {
447
+ const body = this.formatPipelineExpression(
448
+ init.body,
449
+ indent + this.indentSize,
450
+ );
451
+ return `${sig}\n${this.spaces(indent + this.indentSize)}${body}`;
452
+ } else {
453
+ const body = this.formatExpression(init.body, indent);
454
+ return `${sig} ${body}`;
455
+ }
456
+ }
457
+
458
+ if (init.kind === "RecordExpression") {
459
+ if (this.shouldExpandRecord(init, indent)) {
460
+ return ` ${this.formatRecordExpression(init, indent)}`;
461
+ }
462
+ }
463
+
464
+ if (init.kind === "PipelineExpression") {
465
+ const chain = this.unrollPipeline(init);
466
+ if (this.shouldExpandPipeline(chain, indent + this.indentSize)) {
467
+ return `\n${this.spaces(indent + this.indentSize)}${this.formatPipelineExpression(init, indent + this.indentSize)}`;
468
+ }
469
+ }
470
+
471
+ if (init.kind === "EffectBlockExpression") {
472
+ return `\n${this.spaces(indent + this.indentSize)}${this.formatEffectBlock(init, indent + this.indentSize)}`;
473
+ }
474
+
475
+ if (init.kind === "MatchExpression") {
476
+ const isSingleLine =
477
+ init.arms.length > 0 &&
478
+ init.arms[0].span.start.line === init.span.start.line &&
479
+ init.arms.every((a) => a.body.kind !== "BlockStatement");
480
+ if (!isSingleLine) {
481
+ return `\n${this.spaces(indent + this.indentSize)}${this.formatMatchExpression(init, indent + this.indentSize)}`;
482
+ }
483
+ }
484
+
485
+ if (init.span.start.line > decl.span.start.line) {
486
+ return `\n${this.spaces(indent + this.indentSize)}${this.formatExpression(init, indent + this.indentSize)}`;
487
+ }
488
+
489
+ return ` ${this.formatExpression(init, indent)}`;
490
+ }
491
+
492
+ private unrollPipeline(expr: PipelineExpression): Expression[] {
493
+ const chain: Expression[] = [];
494
+ let curr: Expression = expr;
495
+ while (curr.kind === "PipelineExpression") {
496
+ chain.unshift(curr.right);
497
+ curr = curr.left;
498
+ }
499
+ chain.unshift(curr);
500
+ return chain;
501
+ }
502
+
503
+ private shouldExpandPipeline(chain: Expression[], indent: number): boolean {
504
+ if (chain.length > 2) return true;
505
+ if (
506
+ chain[0].span.end.line < chain[1].span.start.line ||
507
+ chain[0].kind === "EffectBlockExpression"
508
+ )
509
+ return true;
510
+ const text = `${this.formatExpression(chain[0], indent)} |> ${this.formatExpression(chain[1], indent)}`;
511
+ const maxInline = Math.min(80, this.lineWidth);
512
+ return text.includes("\n") || indent + text.length > maxInline;
513
+ }
514
+
515
+ private formatPipelineExpression(
516
+ expr: PipelineExpression,
517
+ indent = 0,
518
+ ): string {
519
+ const chain = this.unrollPipeline(expr);
520
+ const operators: string[] = [];
521
+ let current: Expression = expr;
522
+ while (current.kind === "PipelineExpression") {
523
+ operators.unshift(current.deferred ? "|>>" : "|>");
524
+ current = current.left;
525
+ }
526
+ const isMulti = this.shouldExpandPipeline(chain, indent);
527
+
528
+ if (!isMulti) {
529
+ return `${this.formatExpression(chain[0], indent)} ${operators[0]} ${this.formatExpression(chain[1], indent)}`;
530
+ }
531
+
532
+ const head = this.formatExpression(chain[0], indent);
533
+ const stepIndent = this.spaces(indent);
534
+ const steps = chain
535
+ .slice(1)
536
+ .map((step, index) => {
537
+ const trailing = this.getTrailingComment(step.span.end.line);
538
+ return `${stepIndent}${operators[index]} ${this.formatExpression(step, indent)}${trailing}`;
539
+ })
540
+ .join("\n");
541
+
542
+ return `${head}\n${steps}`;
543
+ }
544
+
545
+ private formatTarget(
546
+ target:
547
+ | Identifier
548
+ | import("../ast/types.js").DotPathExpression
549
+ | import("../ast/types.js").IndexExpression,
550
+ ): string {
551
+ if (target.kind === "Identifier") {
552
+ return target.name;
553
+ }
554
+ if (target.kind === "IndexExpression")
555
+ return this.formatExpression(target, 0);
556
+ return target.path.map((p) => p.name).join(".");
557
+ }
558
+
559
+ private formatStatement(stmt: Statement, indent: number): string {
560
+ switch (stmt.kind) {
561
+ case "BindingDeclaration":
562
+ return this.formatBindingDeclaration(stmt, indent);
563
+ case "AssignmentStatement":
564
+ if (
565
+ stmt.value.kind === "PipelineExpression" &&
566
+ this.shouldExpandPipeline(
567
+ this.unrollPipeline(stmt.value),
568
+ indent + this.indentSize,
569
+ )
570
+ ) {
571
+ return `${this.formatTarget(stmt.target)} =\n${this.spaces(indent + this.indentSize)}${this.formatPipelineExpression(stmt.value, indent + this.indentSize)}`;
572
+ }
573
+ return `${this.formatTarget(stmt.target)} = ${this.formatExpression(stmt.value, indent)}`;
574
+ case "ExpressionStatement":
575
+ if (
576
+ stmt.expression.kind === "PipelineExpression" &&
577
+ this.shouldExpandPipeline(
578
+ this.unrollPipeline(stmt.expression),
579
+ indent + this.indentSize,
580
+ )
581
+ ) {
582
+ return this.formatPipelineExpression(
583
+ stmt.expression,
584
+ indent + this.indentSize,
585
+ );
586
+ }
587
+ return this.formatExpression(stmt.expression, indent);
588
+ case "ReturnStatement":
589
+ if (
590
+ stmt.argument.kind === "PipelineExpression" &&
591
+ this.shouldExpandPipeline(
592
+ this.unrollPipeline(stmt.argument),
593
+ indent + this.indentSize,
594
+ )
595
+ ) {
596
+ return `=>\n${this.spaces(indent + this.indentSize)}${this.formatPipelineExpression(stmt.argument, indent + this.indentSize)}`;
597
+ }
598
+ return `=> ${this.formatExpression(stmt.argument, indent)}`;
599
+ case "WhenStatement":
600
+ if (stmt.isEarlyReturn) {
601
+ const retExpr = stmt.consequent as Expression;
602
+ if (
603
+ retExpr.kind === "PipelineExpression" &&
604
+ this.shouldExpandPipeline(
605
+ this.unrollPipeline(retExpr),
606
+ indent + this.indentSize,
607
+ )
608
+ ) {
609
+ return `when (${this.formatExpression(stmt.condition, indent)}) =>\n${this.spaces(indent + this.indentSize)}${this.formatPipelineExpression(retExpr, indent + this.indentSize)}`;
610
+ }
611
+ return `when (${this.formatExpression(stmt.condition, indent)}) => ${this.formatExpression(retExpr, indent)}`;
612
+ }
613
+ return `when (${this.formatExpression(stmt.condition, indent)}) {\n${this.formatBlockStatement(stmt.consequent as BlockStatement, indent + this.indentSize, true)}\n${this.spaces(indent)}}`;
614
+ }
615
+ }
616
+
617
+ private formatBlockStatement(
618
+ stmt: BlockStatement,
619
+ indent: number,
620
+ preserveSingleReturn = false,
621
+ ): string {
622
+ const lines: string[] = [];
623
+ const lineIndent = this.spaces(indent);
624
+ const blockLeading = this.formatLeadingComments(stmt, lineIndent);
625
+ if (blockLeading) {
626
+ lines.push(blockLeading.trimEnd());
627
+ }
628
+
629
+ const isSingleReturn =
630
+ !preserveSingleReturn &&
631
+ stmt.statements.length === 1 &&
632
+ stmt.statements[0].kind === "ReturnStatement" &&
633
+ !stmt.finalExpression;
634
+
635
+ for (let i = 0; i < stmt.statements.length; i++) {
636
+ const s = stmt.statements[i];
637
+ const leading = this.formatLeadingComments(s, lineIndent);
638
+ const trailing = this.getTrailingComment(s.span.end.line);
639
+ const formattedStmt =
640
+ isSingleReturn && s.kind === "ReturnStatement"
641
+ ? this.formatExpression(s.argument, indent)
642
+ : this.formatStatement(s, indent);
643
+ lines.push(`${leading}${lineIndent}${formattedStmt}${trailing}`);
644
+ if (i < stmt.statements.length - 1) {
645
+ const next = stmt.statements[i + 1];
646
+ if (next.span && s.span && next.span.start.line - s.span.end.line > 1) {
647
+ lines.push("");
648
+ }
649
+ }
650
+ }
651
+
652
+ if (stmt.finalExpression) {
653
+ if (stmt.statements.length > 0) {
654
+ const last = stmt.statements[stmt.statements.length - 1];
655
+ if (
656
+ stmt.finalExpression.span &&
657
+ last.span &&
658
+ stmt.finalExpression.span.start.line - last.span.end.line > 1
659
+ ) {
660
+ lines.push("");
661
+ }
662
+ }
663
+
664
+ const leadingFinal = this.formatLeadingComments(
665
+ stmt.finalExpression,
666
+ lineIndent,
667
+ );
668
+ const trailingFinal = this.getTrailingComment(
669
+ stmt.finalExpression.span.end.line,
670
+ );
671
+ lines.push(
672
+ `${leadingFinal}${lineIndent}${this.formatExpression(stmt.finalExpression, indent)}${trailingFinal}`,
673
+ );
674
+ }
675
+
676
+ return lines.join("\n");
677
+ }
678
+
679
+ private formatEffectBlock(expr: EffectBlockExpression, indent = 0): string {
680
+ const stmtIndent = this.spaces(indent + this.indentSize);
681
+ const lines: string[] = [];
682
+
683
+ for (let i = 0; i < expr.body.length; i++) {
684
+ const stmt = expr.body[i];
685
+ const leading = this.formatLeadingComments(stmt, stmtIndent);
686
+ const trailing = this.getTrailingComment(stmt.span.end.line);
687
+ const formatted = this.formatEffectStatement(
688
+ stmt,
689
+ indent + this.indentSize,
690
+ );
691
+ lines.push(`${leading}${stmtIndent}${formatted}${trailing}`);
692
+ if (i < expr.body.length - 1) {
693
+ const next = expr.body[i + 1];
694
+ if (
695
+ next.span &&
696
+ stmt.span &&
697
+ next.span.start.line - stmt.span.end.line > 1
698
+ ) {
699
+ lines.push("");
700
+ }
701
+ }
702
+ }
703
+
704
+ if (expr.body.length > 0) {
705
+ const last = expr.body[expr.body.length - 1];
706
+ if (
707
+ expr.finalExpression.span &&
708
+ last.span &&
709
+ expr.finalExpression.span.start.line - last.span.end.line > 1
710
+ ) {
711
+ lines.push("");
712
+ }
713
+ }
714
+
715
+ const leadingFinal = this.formatLeadingComments(
716
+ expr.finalExpression,
717
+ stmtIndent,
718
+ );
719
+ const trailingFinal = this.getTrailingComment(
720
+ expr.finalExpression.span.end.line,
721
+ );
722
+ lines.push(
723
+ `${leadingFinal}${stmtIndent}=> ${this.formatExpression(expr.finalExpression, indent + this.indentSize)}${trailingFinal}`,
724
+ );
725
+
726
+ const prefix = expr.unscoped ? "effect (unscoped)" : "effect";
727
+ return `${prefix} {\n${lines.join("\n")}\n${this.spaces(indent)}}`;
728
+ }
729
+
730
+ private formatParBlock(expr: ParBlockExpression, indent = 0): string {
731
+ const stmtIndent = this.spaces(indent + this.indentSize);
732
+ const lines: string[] = [];
733
+
734
+ for (let i = 0; i < expr.body.length; i++) {
735
+ const stmt = expr.body[i];
736
+ const leading = this.formatLeadingComments(stmt, stmtIndent);
737
+ const trailing = this.getTrailingComment(stmt.span.end.line);
738
+ const formatted = this.formatEffectStatement(
739
+ stmt,
740
+ indent + this.indentSize,
741
+ );
742
+ lines.push(`${leading}${stmtIndent}${formatted}${trailing}`);
743
+ if (i < expr.body.length - 1) {
744
+ const next = expr.body[i + 1];
745
+ if (
746
+ next.span &&
747
+ stmt.span &&
748
+ next.span.start.line - stmt.span.end.line > 1
749
+ ) {
750
+ lines.push("");
751
+ }
752
+ }
753
+ }
754
+
755
+ if (expr.hasExplicitReturn) {
756
+ if (expr.body.length > 0) {
757
+ const last = expr.body[expr.body.length - 1];
758
+ if (
759
+ expr.finalExpression.span &&
760
+ last.span &&
761
+ expr.finalExpression.span.start.line - last.span.end.line > 1
762
+ ) {
763
+ lines.push("");
764
+ }
765
+ }
766
+
767
+ const leadingFinal = this.formatLeadingComments(
768
+ expr.finalExpression,
769
+ stmtIndent,
770
+ );
771
+ const trailingFinal = this.getTrailingComment(
772
+ expr.finalExpression.span.end.line,
773
+ );
774
+ lines.push(
775
+ `${leadingFinal}${stmtIndent}=> ${this.formatExpression(expr.finalExpression, indent + this.indentSize)}${trailingFinal}`,
776
+ );
777
+ }
778
+
779
+ const opt = expr.concurrency
780
+ ? ` (${this.formatExpression(expr.concurrency)})`
781
+ : "";
782
+ return `par${opt} {\n${lines.join("\n")}\n${this.spaces(indent)}}`;
783
+ }
784
+
785
+ private formatEffectStatement(stmt: EffectStatement, indent: number): string {
786
+ switch (stmt.kind) {
787
+ case "EffectBindStatement": {
788
+ const idStr =
789
+ stmt.id.kind === "Identifier"
790
+ ? stmt.id.name
791
+ : this.formatPattern(stmt.id);
792
+ return `${idStr} <- ${this.formatExpression(stmt.effect, indent)}`;
793
+ }
794
+ case "EffectDiscardStatement":
795
+ return `_ <- ${this.formatExpression(stmt.effect, indent)}`;
796
+ case "BindingDeclaration":
797
+ return this.formatBindingDeclaration(stmt, indent);
798
+ case "AssignmentStatement":
799
+ return `${this.formatTarget(stmt.target)} = ${this.formatExpression(stmt.value, indent)}`;
800
+ case "EffectControlFlowStatement":
801
+ return this.formatExpression(stmt.statement, indent);
802
+ case "WhenStatement":
803
+ return this.formatStatement(stmt, indent);
804
+ }
805
+ }
806
+
807
+ private formatMatchExpression(expr: MatchExpression, indent = 0): string {
808
+ const keyword = expr.isCatch ? "catch" : "match";
809
+ const isSingleLine =
810
+ expr.arms.length > 0 &&
811
+ expr.arms[0].span.start.line === expr.span.start.line &&
812
+ expr.arms.every((a) => a.body.kind !== "BlockStatement");
813
+
814
+ if (isSingleLine) {
815
+ const scrutinee = expr.scrutinee
816
+ ? ` ${this.formatExpression(expr.scrutinee, indent)}`
817
+ : "";
818
+ const arms = expr.arms.map((a) => this.formatMatchArmInline(a)).join(" ");
819
+ return `${keyword}${scrutinee} ${arms}`;
820
+ }
821
+
822
+ const scrutinee = expr.scrutinee
823
+ ? ` ${this.formatExpression(expr.scrutinee, indent)}`
824
+ : "";
825
+ const arms = expr.arms
826
+ .map((a) => this.formatMatchArm(a, indent + this.indentSize))
827
+ .join("\n");
828
+ return `${keyword}${scrutinee}\n${arms}`;
829
+ }
830
+
831
+ private formatMatchArm(arm: MatchArm, indent: number): string {
832
+ const armIndent = this.spaces(indent);
833
+ const leading = this.formatLeadingComments(arm, armIndent);
834
+ const trailing = this.getTrailingComment(arm.span.end.line);
835
+ const pat = this.formatPattern(arm.pattern);
836
+ const guard = arm.guard
837
+ ? ` when ${this.formatExpression(arm.guard, indent)}`
838
+ : "";
839
+
840
+ if (arm.body.kind === "BlockStatement") {
841
+ const bodyIndent = indent + this.indentSize * 2;
842
+ const body = this.formatBlockStatement(arm.body, bodyIndent);
843
+ return `${leading}${armIndent}| ${pat}${guard} =>\n${body}${trailing}`;
844
+ } else {
845
+ if (arm.body.kind === "PipelineExpression") {
846
+ const bodyIndent = indent + this.indentSize;
847
+ const body = this.formatExpression(arm.body, bodyIndent);
848
+ if (body.includes("\n")) {
849
+ return `${leading}${armIndent}| ${pat}${guard} =>\n${this.spaces(bodyIndent)}${body}${trailing}`;
850
+ }
851
+ }
852
+ const body = this.formatExpression(arm.body, indent);
853
+ return `${leading}${armIndent}| ${pat}${guard} => ${body}${trailing}`;
854
+ }
855
+ }
856
+
857
+ private formatMatchArmInline(arm: MatchArm): string {
858
+ const pat = this.formatPattern(arm.pattern);
859
+ const guard = arm.guard
860
+ ? ` when ${this.formatExpression(arm.guard, 0)}`
861
+ : "";
862
+ const body =
863
+ arm.body.kind === "BlockStatement"
864
+ ? this.formatBlockStatement(arm.body, 0)
865
+ : this.formatExpression(arm.body, 0);
866
+ return `| ${pat}${guard} => ${body}`;
867
+ }
868
+
869
+ private formatPattern(pat: Pattern): string {
870
+ switch (pat.kind) {
871
+ case "WildcardPattern":
872
+ return "_";
873
+ case "LiteralPattern": {
874
+ if (pat.literal.kind === "BooleanLiteral" && pat.literal.rawExpr) {
875
+ return this.formatExpression(pat.literal.rawExpr, 0);
876
+ }
877
+ return this.formatLiteral(pat.literal);
878
+ }
879
+ case "VariablePattern":
880
+ return pat.name.name;
881
+ case "ConstructorPattern": {
882
+ const args =
883
+ pat.args.length > 0
884
+ ? `(${pat.args.map((a) => this.formatPattern(a)).join(", ")})`
885
+ : "";
886
+ return `${pat.constructorName.name}${args}`;
887
+ }
888
+ case "RecordPattern":
889
+ return `{ ${pat.fields.map((f) => (f.isRest ? `...${f.key.name}` : f.pattern ? `${f.key.name}: ${this.formatPattern(f.pattern)}` : f.key.name)).join(", ")} }`;
890
+ case "ArrayPattern":
891
+ return `[${pat.elements.map((e) => this.formatPattern(e)).join(", ")}]`;
892
+ case "RestPattern":
893
+ return `...${this.formatPattern(pat.argument)}`;
894
+ case "OrPattern":
895
+ return pat.patterns.map((p) => this.formatPattern(p)).join(" | ");
896
+ }
897
+ }
898
+
899
+ private shouldExpandRecord(expr: RecordExpression, indent = 0): boolean {
900
+ if (expr.entries.length === 0) return false;
901
+ if (expr.entries.length >= 3) return true;
902
+ if (
903
+ expr.entries[0].span.start.line > expr.span.start.line ||
904
+ expr.entries.some((e) => e.span.end.line > e.span.start.line)
905
+ ) {
906
+ return true;
907
+ }
908
+ const hasSpread = expr.entries.some((e) => e.kind === "SpreadEntry");
909
+ const entriesStr = expr.entries
910
+ .map((e) => this.formatRecordEntry(e, indent))
911
+ .join(", ");
912
+ if (entriesStr.includes("\n")) return true;
913
+ if (
914
+ hasSpread &&
915
+ expr.entries.length >= 2 &&
916
+ indent + entriesStr.length + 4 > 50
917
+ ) {
918
+ return true;
919
+ }
920
+ const maxInline = Math.min(60, this.lineWidth);
921
+ return indent + entriesStr.length + 4 > maxInline;
922
+ }
923
+
924
+ private formatRecordExpression(expr: RecordExpression, indent = 0): string {
925
+ if (expr.entries.length === 0) return "{}";
926
+
927
+ if (!this.shouldExpandRecord(expr, indent)) {
928
+ const entriesStr = expr.entries
929
+ .map((e) => this.formatRecordEntry(e, indent))
930
+ .join(", ");
931
+ return `{ ${entriesStr} }`;
932
+ }
933
+
934
+ const entryIndent = this.spaces(indent + this.indentSize);
935
+ const closingIndent = this.spaces(indent);
936
+ const lines: string[] = [];
937
+ for (let i = 0; i < expr.entries.length; i++) {
938
+ const e = expr.entries[i];
939
+ const leading = this.formatLeadingComments(e, entryIndent);
940
+ const trailing = this.getTrailingComment(e.span.end.line);
941
+ lines.push(
942
+ `${leading}${entryIndent}${this.formatRecordEntry(e, indent + this.indentSize)}${trailing}`,
943
+ );
944
+ if (i < expr.entries.length - 1) {
945
+ const next = expr.entries[i + 1];
946
+ if (next.span && e.span && next.span.start.line - e.span.end.line > 1) {
947
+ lines.push("");
948
+ }
949
+ }
950
+ }
951
+ return `{\n${lines.join("\n")}\n${closingIndent}}`;
952
+ }
953
+
954
+ private formatRecordEntry(entry: RecordEntry, indent = 0): string {
955
+ switch (entry.kind) {
956
+ case "SpreadEntry":
957
+ return `...${this.formatExpression(entry.argument, indent)}`;
958
+ case "FieldEntry": {
959
+ const key = RESERVED_IDENTIFIERS.has(entry.key.name)
960
+ ? `"${entry.key.name}"`
961
+ : entry.key.name;
962
+ return `${key}: ${this.formatExpression(entry.value, indent)}`;
963
+ }
964
+ case "DotPathEntry":
965
+ return `${entry.path.map((p) => p.name).join(".")}: ${this.formatExpression(entry.value, indent)}`;
966
+ case "ShorthandEntry":
967
+ return entry.key.name;
968
+ }
969
+ }
970
+
971
+ private formatArrayExpression(expr: ArrayExpression, indent = 0): string {
972
+ if (expr.elements.length === 0) return "[]";
973
+
974
+ const isMultiline =
975
+ expr.elements.some((e) => e.span.end.line > e.span.start.line) ||
976
+ (expr.elements.length > 0 &&
977
+ expr.elements[0].span.start.line > expr.span.start.line);
978
+
979
+ const elements = expr.elements
980
+ .map((e) => this.formatExpression(e, indent))
981
+ .join(", ");
982
+ if (
983
+ !isMultiline &&
984
+ indent + elements.length + 2 <= this.lineWidth &&
985
+ !elements.includes("\n")
986
+ ) {
987
+ return `[${elements}]`;
988
+ }
989
+
990
+ const elemIndent = this.spaces(indent + this.indentSize);
991
+ const closingIndent = this.spaces(indent);
992
+ const lines = expr.elements.map(
993
+ (e) =>
994
+ `${elemIndent}${this.formatExpression(e, indent + this.indentSize)}`,
995
+ );
996
+ return `[\n${lines.join(",\n")}\n${closingIndent}]`;
997
+ }
998
+
999
+ private formatBinaryExpression(
1000
+ expr: BinaryExpression,
1001
+ indent = 0,
1002
+ parentOp?: string,
1003
+ isRightSide?: boolean,
1004
+ ): string {
1005
+ const currentPrec = PRECEDENCE[expr.operator] ?? 0;
1006
+ const parentPrec = parentOp ? (PRECEDENCE[parentOp] ?? 0) : 0;
1007
+
1008
+ let needsParens = false;
1009
+ if (parentOp) {
1010
+ if (currentPrec < parentPrec) {
1011
+ needsParens = true;
1012
+ } else if (currentPrec === parentPrec) {
1013
+ const associativity = ASSOCIATIVITY[parentOp] ?? "left";
1014
+ if (
1015
+ (associativity === "left" && isRightSide) ||
1016
+ (associativity === "right" && !isRightSide)
1017
+ ) {
1018
+ needsParens = true;
1019
+ }
1020
+ }
1021
+ }
1022
+
1023
+ const left = this.formatBinaryOperand(
1024
+ expr.left,
1025
+ indent,
1026
+ expr.operator,
1027
+ false,
1028
+ );
1029
+ const right = this.formatBinaryOperand(
1030
+ expr.right,
1031
+ indent,
1032
+ expr.operator,
1033
+ true,
1034
+ );
1035
+ const res = `${left} ${expr.operator} ${right}`;
1036
+ return needsParens ? `(${res})` : res;
1037
+ }
1038
+
1039
+ private formatBinaryOperand(
1040
+ expr: Expression,
1041
+ indent: number,
1042
+ parentOp: string,
1043
+ isRightSide: boolean,
1044
+ ): string {
1045
+ if (expr.kind === "BinaryExpression") {
1046
+ return this.formatBinaryExpression(expr, indent, parentOp, isRightSide);
1047
+ }
1048
+ if (expr.kind === "PipelineExpression") {
1049
+ return `(${this.formatPipelineExpression(expr, indent)})`;
1050
+ }
1051
+ return this.formatExpression(expr, indent);
1052
+ }
1053
+
1054
+ private formatUnaryExpression(expr: UnaryExpression, indent = 0): string {
1055
+ const op = expr.operator === "not" ? "not " : "-";
1056
+ let arg: string;
1057
+ if (
1058
+ expr.argument.kind === "BinaryExpression" ||
1059
+ expr.argument.kind === "PipelineExpression"
1060
+ ) {
1061
+ arg = `(${this.formatExpression(expr.argument, indent)})`;
1062
+ } else {
1063
+ arg = this.formatExpression(expr.argument, indent);
1064
+ }
1065
+ return `${op}${arg}`;
1066
+ }
1067
+
1068
+ private formatCallExpression(expr: CallExpression, indent = 0): string {
1069
+ let callee: string;
1070
+ if (expr.callee.kind === "FunctionExpression") {
1071
+ callee = `(${this.formatExpression(expr.callee, indent)})`;
1072
+ } else {
1073
+ callee = this.formatExpression(expr.callee, indent);
1074
+ }
1075
+
1076
+ if (expr.args.length === 0) return `${callee}()`;
1077
+
1078
+ const lastArg = expr.args[expr.args.length - 1];
1079
+ const isMultilineBlock =
1080
+ lastArg.kind === "FunctionExpression" &&
1081
+ (lastArg.body.kind === "BlockStatement" ||
1082
+ lastArg.body.kind === "MatchExpression" ||
1083
+ lastArg.body.kind === "EffectBlockExpression" ||
1084
+ lastArg.body.kind === "ParBlockExpression" ||
1085
+ (lastArg.body.kind === "PipelineExpression" &&
1086
+ this.shouldExpandPipeline(
1087
+ this.unrollPipeline(lastArg.body),
1088
+ indent + this.indentSize,
1089
+ )));
1090
+
1091
+ if (isMultilineBlock) {
1092
+ const formattedLast = this.formatExpression(lastArg, indent);
1093
+ const firstLine = formattedLast.split("\n")[0] ?? "";
1094
+ const close =
1095
+ formattedLast.endsWith("}") && firstLine.includes("{")
1096
+ ? ")"
1097
+ : `\n${this.spaces(indent)})`;
1098
+ if (expr.args.length === 1) {
1099
+ return `${callee}(${formattedLast}${close}`;
1100
+ }
1101
+ const otherArgs = expr.args
1102
+ .slice(0, -1)
1103
+ .map((a) => this.formatExpression(a, indent))
1104
+ .join(", ");
1105
+ return `${callee}(${otherArgs}, ${formattedLast}${close}`;
1106
+ }
1107
+
1108
+ const args = expr.args
1109
+ .map((a) => this.formatExpression(a, indent))
1110
+ .join(", ");
1111
+ return `${callee}(${args})`;
1112
+ }
1113
+
1114
+ private formatExpression(expr: Expression, indent = 0): string {
1115
+ switch (expr.kind) {
1116
+ case "PipelineExpression":
1117
+ return this.formatPipelineExpression(expr, indent);
1118
+ case "BinaryExpression":
1119
+ return this.formatBinaryExpression(expr, indent);
1120
+ case "UnaryExpression":
1121
+ return this.formatUnaryExpression(expr, indent);
1122
+ case "CallExpression":
1123
+ return this.formatCallExpression(expr, indent);
1124
+ case "MemberExpression":
1125
+ return `${this.formatExpression(expr.object, indent)}.${expr.property.name}`;
1126
+ case "IndexExpression":
1127
+ return `${this.formatExpression(expr.object, indent)}[${this.formatExpression(expr.index, indent)}]`;
1128
+ case "RecordExpression":
1129
+ return this.formatRecordExpression(expr, indent);
1130
+ case "ArrayExpression":
1131
+ return this.formatArrayExpression(expr, indent);
1132
+ case "FunctionExpression":
1133
+ return this.formatFunctionExpression(expr, indent);
1134
+ case "MutBlockExpression":
1135
+ return `mut (${expr.draftParam.name}) =>\n${this.formatFunctionBody(expr.body, indent + this.indentSize)}`;
1136
+ case "MatchExpression":
1137
+ return this.formatMatchExpression(expr, indent);
1138
+ case "EffectBlockExpression":
1139
+ return this.formatEffectBlock(expr, indent);
1140
+ case "ParBlockExpression":
1141
+ return this.formatParBlock(expr, indent);
1142
+ case "TypedHole":
1143
+ return expr.label
1144
+ ? `??? "${this.escapeStringPart(expr.label)}"`
1145
+ : "???";
1146
+ case "Placeholder":
1147
+ return "_";
1148
+ case "NumberLiteral":
1149
+ return expr.raw ?? expr.value.toString();
1150
+ case "BooleanLiteral":
1151
+ return expr.value ? "true" : "false";
1152
+ case "StringLiteral":
1153
+ return this.formatStringLiteral(expr);
1154
+ case "TemplateLiteral":
1155
+ return this.formatTemplateLiteral(expr);
1156
+ case "TaggedTemplateExpression":
1157
+ return this.formatTaggedTemplateExpression(expr, indent);
1158
+ case "Identifier":
1159
+ case "UpperIdentifier":
1160
+ return expr.name;
1161
+ case "JSXElement":
1162
+ case "JSXSelfClosingElement":
1163
+ return this.source.slice(expr.span.start.offset, expr.span.end.offset);
1164
+ case "JsExpression":
1165
+ return `js ${this.formatExpression(expr.expression, indent)}`;
1166
+ case "TsEscapeExpression":
1167
+ return this.formatTsEscape(expr.code);
1168
+ case "SpreadElement":
1169
+ return `...${this.formatExpression(expr.argument, indent)}`;
1170
+ }
1171
+ }
1172
+
1173
+ private formatFunctionBody(body: FunctionBody, indent: number): string {
1174
+ if (body.kind === "BlockStatement") {
1175
+ return this.formatBlockStatement(body, indent);
1176
+ }
1177
+ return `${this.spaces(indent)}${this.formatExpression(body, indent)}`;
1178
+ }
1179
+
1180
+ private formatStringLiteral(str: StringLiteral): string {
1181
+ let result = '"';
1182
+ for (const part of str.parts) {
1183
+ if (part.kind === "StringRawPart") {
1184
+ result += this.escapeStringPart(part.value);
1185
+ } else {
1186
+ result += `{${this.formatExpression(part.expression, 0)}}`;
1187
+ }
1188
+ }
1189
+ result += '"';
1190
+ return result;
1191
+ }
1192
+
1193
+ private formatTemplateLiteral(tpl: TemplateLiteral): string {
1194
+ let result = "`";
1195
+ for (const part of tpl.parts) {
1196
+ if (part.kind === "StringRawPart") {
1197
+ result += part.value;
1198
+ } else {
1199
+ result += `\${${this.formatExpression(part.expression, 0)}}`;
1200
+ }
1201
+ }
1202
+ result += "`";
1203
+ return result;
1204
+ }
1205
+
1206
+ private formatTaggedTemplateExpression(
1207
+ expr: TaggedTemplateExpression,
1208
+ indent: number,
1209
+ ): string {
1210
+ const tag = this.formatExpression(expr.tag, indent);
1211
+ let typeArgs = "";
1212
+ if (expr.typeArguments && expr.typeArguments.length > 0) {
1213
+ typeArgs =
1214
+ "<" +
1215
+ expr.typeArguments.map((t) => this.formatType(t)).join(", ") +
1216
+ ">";
1217
+ }
1218
+ return `${tag}${typeArgs}${this.formatTemplateLiteral(expr.template)}`;
1219
+ }
1220
+
1221
+ private formatLiteral(lit: Literal): string {
1222
+ switch (lit.kind) {
1223
+ case "NumberLiteral":
1224
+ return lit.raw ?? lit.value.toString();
1225
+ case "BooleanLiteral":
1226
+ return lit.value ? "true" : "false";
1227
+ case "StringLiteral":
1228
+ return this.formatStringLiteral(lit);
1229
+ }
1230
+ }
1231
+
1232
+ private formatFunctionExpression(
1233
+ expr: FunctionExpression,
1234
+ indent = 0,
1235
+ ): string {
1236
+ const typeParams = this.formatTypeParameters(expr.typeParams);
1237
+ const params = expr.params
1238
+ .map((p) => {
1239
+ const rest = p.isRest ? "..." : "";
1240
+ const type = p.typeAnnotation
1241
+ ? `: ${this.formatType(p.typeAnnotation)}`
1242
+ : "";
1243
+ const defaultValue = p.defaultValue
1244
+ ? ` = ${this.formatExpression(p.defaultValue, indent)}`
1245
+ : "";
1246
+ return `${rest}${this.formatBindingTarget(p.name)}${type}${defaultValue}`;
1247
+ })
1248
+ .join(", ");
1249
+ const ret = expr.returnType ? `: ${this.formatType(expr.returnType)}` : "";
1250
+ const isArrowOnly =
1251
+ expr.params.length === 0 &&
1252
+ !expr.returnType &&
1253
+ expr.span &&
1254
+ this.source.slice(expr.span.start.offset, expr.span.start.offset + 2) ===
1255
+ "=>";
1256
+ const sig = isArrowOnly ? `=>` : `${typeParams}(${params})${ret} =>`;
1257
+
1258
+ if (expr.body.kind === "BlockStatement") {
1259
+ if (expr.body.statements.length === 1 && !expr.body.finalExpression) {
1260
+ const single = expr.body.statements[0];
1261
+ const singleExpr =
1262
+ single?.kind === "ReturnStatement"
1263
+ ? single.argument
1264
+ : single?.kind === "ExpressionStatement"
1265
+ ? single.expression
1266
+ : null;
1267
+ if (
1268
+ singleExpr &&
1269
+ singleExpr.kind === "PipelineExpression" &&
1270
+ this.shouldExpandPipeline(
1271
+ this.unrollPipeline(singleExpr),
1272
+ indent + this.indentSize,
1273
+ )
1274
+ ) {
1275
+ const body = this.formatPipelineExpression(
1276
+ singleExpr,
1277
+ indent + this.indentSize,
1278
+ );
1279
+ return `${sig}\n${this.spaces(indent + this.indentSize)}${body}`;
1280
+ } else if (singleExpr) {
1281
+ return `${sig} ${this.formatExpression(singleExpr, indent)}`;
1282
+ }
1283
+ } else if (
1284
+ expr.body.statements.length === 0 &&
1285
+ expr.body.finalExpression
1286
+ ) {
1287
+ if (
1288
+ expr.body.finalExpression.kind === "PipelineExpression" &&
1289
+ this.shouldExpandPipeline(
1290
+ this.unrollPipeline(expr.body.finalExpression),
1291
+ indent + this.indentSize,
1292
+ )
1293
+ ) {
1294
+ const body = this.formatPipelineExpression(
1295
+ expr.body.finalExpression,
1296
+ indent + this.indentSize,
1297
+ );
1298
+ return `${sig}\n${this.spaces(indent + this.indentSize)}${body}`;
1299
+ }
1300
+ const bodyStartsWithBrace =
1301
+ this.source.length === 0 ||
1302
+ this.source[expr.body.span?.start.offset ?? -1] === "{";
1303
+ if (!bodyStartsWithBrace) {
1304
+ const body = this.formatBlockStatement(
1305
+ expr.body,
1306
+ indent + this.indentSize,
1307
+ );
1308
+ return `${sig}\n${body}`;
1309
+ }
1310
+ }
1311
+ const body = this.formatBlockStatement(
1312
+ expr.body,
1313
+ indent + this.indentSize,
1314
+ );
1315
+ return body ? `${sig} {\n${body}\n${this.spaces(indent)}}` : `${sig} {}`;
1316
+ } else if (
1317
+ expr.body.kind === "PipelineExpression" &&
1318
+ this.shouldExpandPipeline(
1319
+ this.unrollPipeline(expr.body),
1320
+ indent + this.indentSize,
1321
+ )
1322
+ ) {
1323
+ const body = this.formatPipelineExpression(
1324
+ expr.body,
1325
+ indent + this.indentSize,
1326
+ );
1327
+ return `${sig}\n${this.spaces(indent + this.indentSize)}${body}`;
1328
+ } else {
1329
+ const body = this.formatExpression(expr.body, indent);
1330
+ return `${sig} ${body}`;
1331
+ }
1332
+ }
1333
+
1334
+ public formatType(node: TypeNode, indent = 0): string {
1335
+ switch (node.kind) {
1336
+ case "QualifiedTypeReferenceNode": {
1337
+ const args = node.typeArgs?.length
1338
+ ? `<${node.typeArgs.map((arg) => this.formatType(arg, indent)).join(", ")}>`
1339
+ : "";
1340
+ return `${node.parts.map((part) => part.name).join(".")}${args}`;
1341
+ }
1342
+ case "TypeReferenceNode": {
1343
+ const typeArgs =
1344
+ node.typeArgs && node.typeArgs.length > 0
1345
+ ? `<${node.typeArgs.map((t) => this.formatType(t, indent)).join(", ")}>`
1346
+ : "";
1347
+ return `${node.name.name[0].toUpperCase()}${node.name.name.slice(1)}${typeArgs}`;
1348
+ }
1349
+ case "FunctionTypeNode": {
1350
+ const params = node.params
1351
+ .map((p) => {
1352
+ const rest = p.isRest ? "..." : "";
1353
+ const typeStr = p.typeAnnotation
1354
+ ? this.formatType(p.typeAnnotation, indent)
1355
+ : "Unknown";
1356
+ return p.name
1357
+ ? `${rest}${this.formatBindingTarget(p.name)}: ${typeStr}`
1358
+ : `${rest}${typeStr}`;
1359
+ })
1360
+ .join(", ");
1361
+ return `${this.formatTypeParameters(node.typeParams)}(${params}) => ${this.formatType(node.returnType, indent)}`;
1362
+ }
1363
+ case "EffectTypeNode": {
1364
+ const args = [this.formatType(node.success, indent)];
1365
+ if (node.error) {
1366
+ args.push(this.formatType(node.error, indent));
1367
+ if (node.requirements) {
1368
+ args.push(this.formatType(node.requirements, indent));
1369
+ }
1370
+ }
1371
+ return `Effect<${args.join(", ")}>`;
1372
+ }
1373
+ case "RecordTypeNode": {
1374
+ if (node.fields.length === 0) return "{}";
1375
+ const isMultiline =
1376
+ node.fields.length >= 3 ||
1377
+ node.span.start.line !== node.span.end.line;
1378
+ const inlineFields = node.fields
1379
+ .map((f) => {
1380
+ const key = RESERVED_IDENTIFIERS.has(f.name.name)
1381
+ ? `"${f.name.name}"`
1382
+ : f.name.name;
1383
+ return `${key}: ${this.formatType(f.typeAnnotation, indent)}${f.defaultValue ? ` = ${this.formatExpression(f.defaultValue, indent)}` : ""}`;
1384
+ })
1385
+ .join(", ");
1386
+ const maxInline = Math.min(60, this.lineWidth);
1387
+ if (
1388
+ !isMultiline &&
1389
+ indent + inlineFields.length + 4 <= maxInline &&
1390
+ !inlineFields.includes("\n")
1391
+ ) {
1392
+ return `{ ${inlineFields} }`;
1393
+ }
1394
+ const fieldIndent = this.spaces(indent + this.indentSize);
1395
+ const closingIndent = this.spaces(indent);
1396
+ const fields = node.fields
1397
+ .map((f) => {
1398
+ const key = RESERVED_IDENTIFIERS.has(f.name.name)
1399
+ ? `"${f.name.name}"`
1400
+ : f.name.name;
1401
+ return `${fieldIndent}${key}: ${this.formatType(f.typeAnnotation, indent + this.indentSize)}${f.defaultValue ? ` = ${this.formatExpression(f.defaultValue, indent + this.indentSize)}` : ""}`;
1402
+ })
1403
+ .join("\n");
1404
+ return `{\n${fields}\n${closingIndent}}`;
1405
+ }
1406
+ case "ArrayTypeNode":
1407
+ return `Array<${this.formatType(node.elementType, indent)}>`;
1408
+ case "UnionTypeNode":
1409
+ return node.types.map((t) => this.formatType(t, indent)).join(" | ");
1410
+ case "IntersectionTypeNode":
1411
+ return node.types.map((t) => this.formatType(t, indent)).join(" & ");
1412
+ case "LiteralTypeNode":
1413
+ return JSON.stringify(node.value);
1414
+ case "TypeQueryNode":
1415
+ return `typeof ${this.formatExpression(node.expr, indent)}`;
1416
+ case "IndexedAccessTypeNode":
1417
+ return `${this.formatType(node.objectType, indent)}[${this.formatType(node.indexType, indent)}]`;
1418
+ case "ParenthesizedTypeNode":
1419
+ return `(${this.formatType(node.typeNode, indent)})`;
1420
+ case "TsEscapeTypeNode":
1421
+ return this.formatTsEscape(node.code);
1422
+ }
1423
+ }
1424
+
1425
+ private formatTsEscape(code: string): string {
1426
+ return code.includes("\n") ? `ts {\n${code}\n}` : `ts { ${code} }`;
1427
+ }
1428
+ }
1429
+
1430
+ export function format(source: string, options?: FormatOptions): string {
1431
+ const filename = options?.filename ?? "<anonymous>";
1432
+ // Type-aware shortening is optional for incomplete or unresolvable source;
1433
+ // formatting itself must remain available while editing.
1434
+ let input = source;
1435
+ try {
1436
+ input = convertPipelineStyle(source, filename, "bare").text;
1437
+ } catch {
1438
+ // Preserve the original stage whenever its resolution cannot be proven.
1439
+ }
1440
+ const lexer = new Lexer(input, filename);
1441
+ const tokens = lexer.tokenize();
1442
+ const parser = new Parser(tokens, filename);
1443
+ const ast = parser.parse();
1444
+ const formatter = new Formatter(input, ast, options);
1445
+ return formatter.format();
1446
+ }
1447
+
1448
+ export function checkFormat(
1449
+ source: string,
1450
+ options?: FormatOptions,
1451
+ ): { isFormatted: boolean; formatted: string } {
1452
+ const formatted = format(source, options);
1453
+ return {
1454
+ isFormatted: formatted === source,
1455
+ formatted,
1456
+ };
1457
+ }