@soundscript/soundscript 0.1.13 → 0.1.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (441) hide show
  1. package/package.json +15 -6
  2. package/project-transform/index.js +2 -0
  3. package/project-transform/index.ts +8 -0
  4. package/project-transform/src/annotation_syntax.js +948 -0
  5. package/project-transform/src/annotation_syntax.ts +1217 -0
  6. package/project-transform/src/build_package.js +475 -0
  7. package/project-transform/src/build_package.ts +683 -0
  8. package/project-transform/src/bundled/portable-web-globals.d.ts +153 -0
  9. package/project-transform/src/bundled/runtime_externs.js +220 -0
  10. package/project-transform/src/bundled/runtime_externs.ts +237 -0
  11. package/project-transform/src/bundled/sound-libs/lib.decorators.d.ts +385 -0
  12. package/project-transform/src/bundled/sound-libs/lib.decorators.legacy.d.ts +22 -0
  13. package/project-transform/src/bundled/sound-libs/lib.dom.asynciterable.d.ts +42 -0
  14. package/project-transform/src/bundled/sound-libs/lib.dom.d.ts +39440 -0
  15. package/project-transform/src/bundled/sound-libs/lib.es2015.collection.d.ts +149 -0
  16. package/project-transform/src/bundled/sound-libs/lib.es2015.core.d.ts +657 -0
  17. package/project-transform/src/bundled/sound-libs/lib.es2015.d.ts +28 -0
  18. package/project-transform/src/bundled/sound-libs/lib.es2015.generator.d.ts +77 -0
  19. package/project-transform/src/bundled/sound-libs/lib.es2015.iterable.d.ts +616 -0
  20. package/project-transform/src/bundled/sound-libs/lib.es2015.promise.d.ts +80 -0
  21. package/project-transform/src/bundled/sound-libs/lib.es2015.proxy.d.ts +128 -0
  22. package/project-transform/src/bundled/sound-libs/lib.es2015.reflect.d.ts +144 -0
  23. package/project-transform/src/bundled/sound-libs/lib.es2015.symbol.d.ts +46 -0
  24. package/project-transform/src/bundled/sound-libs/lib.es2015.symbol.wellknown.d.ts +170 -0
  25. package/project-transform/src/bundled/sound-libs/lib.es2016.array.include.d.ts +116 -0
  26. package/project-transform/src/bundled/sound-libs/lib.es2016.d.ts +21 -0
  27. package/project-transform/src/bundled/sound-libs/lib.es2017.arraybuffer.d.ts +21 -0
  28. package/project-transform/src/bundled/sound-libs/lib.es2017.d.ts +26 -0
  29. package/project-transform/src/bundled/sound-libs/lib.es2017.date.d.ts +31 -0
  30. package/project-transform/src/bundled/sound-libs/lib.es2017.object.d.ts +49 -0
  31. package/project-transform/src/bundled/sound-libs/lib.es2017.string.d.ts +45 -0
  32. package/project-transform/src/bundled/sound-libs/lib.es2017.typedarrays.d.ts +53 -0
  33. package/project-transform/src/bundled/sound-libs/lib.es2018.asyncgenerator.d.ts +77 -0
  34. package/project-transform/src/bundled/sound-libs/lib.es2018.asynciterable.d.ts +57 -0
  35. package/project-transform/src/bundled/sound-libs/lib.es2018.d.ts +24 -0
  36. package/project-transform/src/bundled/sound-libs/lib.es2018.promise.d.ts +30 -0
  37. package/project-transform/src/bundled/sound-libs/lib.es2018.regexp.d.ts +37 -0
  38. package/project-transform/src/bundled/sound-libs/lib.es2019.array.d.ts +79 -0
  39. package/project-transform/src/bundled/sound-libs/lib.es2019.d.ts +24 -0
  40. package/project-transform/src/bundled/sound-libs/lib.es2019.object.d.ts +47 -0
  41. package/project-transform/src/bundled/sound-libs/lib.es2019.string.d.ts +37 -0
  42. package/project-transform/src/bundled/sound-libs/lib.es2019.symbol.d.ts +24 -0
  43. package/project-transform/src/bundled/sound-libs/lib.es2020.bigint.d.ts +765 -0
  44. package/project-transform/src/bundled/sound-libs/lib.es2020.d.ts +27 -0
  45. package/project-transform/src/bundled/sound-libs/lib.es2020.date.d.ts +42 -0
  46. package/project-transform/src/bundled/sound-libs/lib.es2020.number.d.ts +28 -0
  47. package/project-transform/src/bundled/sound-libs/lib.es2020.promise.d.ts +49 -0
  48. package/project-transform/src/bundled/sound-libs/lib.es2020.string.d.ts +44 -0
  49. package/project-transform/src/bundled/sound-libs/lib.es2020.symbol.wellknown.d.ts +41 -0
  50. package/project-transform/src/bundled/sound-libs/lib.es2021.d.ts +23 -0
  51. package/project-transform/src/bundled/sound-libs/lib.es2021.promise.d.ts +48 -0
  52. package/project-transform/src/bundled/sound-libs/lib.es2021.string.d.ts +33 -0
  53. package/project-transform/src/bundled/sound-libs/lib.es2021.weakref.d.ts +78 -0
  54. package/project-transform/src/bundled/sound-libs/lib.es2022.array.d.ts +121 -0
  55. package/project-transform/src/bundled/sound-libs/lib.es2022.d.ts +25 -0
  56. package/project-transform/src/bundled/sound-libs/lib.es2022.error.d.ts +75 -0
  57. package/project-transform/src/bundled/sound-libs/lib.es2022.object.d.ts +26 -0
  58. package/project-transform/src/bundled/sound-libs/lib.es2022.regexp.d.ts +39 -0
  59. package/project-transform/src/bundled/sound-libs/lib.es2022.string.d.ts +25 -0
  60. package/project-transform/src/bundled/sound-libs/lib.es2023.array.d.ts +924 -0
  61. package/project-transform/src/bundled/sound-libs/lib.es2023.collection.d.ts +21 -0
  62. package/project-transform/src/bundled/sound-libs/lib.es2023.d.ts +22 -0
  63. package/project-transform/src/bundled/sound-libs/lib.es2024.arraybuffer.d.ts +65 -0
  64. package/project-transform/src/bundled/sound-libs/lib.es2024.collection.d.ts +29 -0
  65. package/project-transform/src/bundled/sound-libs/lib.es2024.d.ts +26 -0
  66. package/project-transform/src/bundled/sound-libs/lib.es2024.object.d.ts +33 -0
  67. package/project-transform/src/bundled/sound-libs/lib.es2024.promise.d.ts +35 -0
  68. package/project-transform/src/bundled/sound-libs/lib.es2024.regexp.d.ts +25 -0
  69. package/project-transform/src/bundled/sound-libs/lib.es2024.string.d.ts +29 -0
  70. package/project-transform/src/bundled/sound-libs/lib.es5.d.ts +4924 -0
  71. package/project-transform/src/bundled/sound_stdlib.js +142 -0
  72. package/project-transform/src/bundled/sound_stdlib.ts +180 -0
  73. package/project-transform/src/checker/analyze_project.js +1361 -0
  74. package/project-transform/src/checker/analyze_project.ts +2246 -0
  75. package/project-transform/src/checker/diagnostics.js +112 -0
  76. package/project-transform/src/checker/diagnostics.ts +222 -0
  77. package/project-transform/src/checker/engine/context.js +235 -0
  78. package/project-transform/src/checker/engine/context.ts +340 -0
  79. package/project-transform/src/checker/engine/diagnostic_codes.js +72 -0
  80. package/project-transform/src/checker/engine/diagnostic_codes.ts +95 -0
  81. package/project-transform/src/checker/engine/facts.js +35 -0
  82. package/project-transform/src/checker/engine/facts.ts +48 -0
  83. package/project-transform/src/checker/engine/types.js +1 -0
  84. package/project-transform/src/checker/engine/types.ts +485 -0
  85. package/project-transform/src/checker/proof_escape_hatch_diagnostics.js +104 -0
  86. package/project-transform/src/checker/proof_escape_hatch_diagnostics.ts +173 -0
  87. package/project-transform/src/checker/rules/async_surface.js +231 -0
  88. package/project-transform/src/checker/rules/async_surface.ts +335 -0
  89. package/project-transform/src/checker/rules/class_lifecycle.js +798 -0
  90. package/project-transform/src/checker/rules/class_lifecycle.ts +1276 -0
  91. package/project-transform/src/checker/rules/directive_validation.js +571 -0
  92. package/project-transform/src/checker/rules/directive_validation.ts +938 -0
  93. package/project-transform/src/checker/rules/directives.js +23 -0
  94. package/project-transform/src/checker/rules/directives.ts +25 -0
  95. package/project-transform/src/checker/rules/flow.js +202 -0
  96. package/project-transform/src/checker/rules/flow.ts +333 -0
  97. package/project-transform/src/checker/rules/flow_facts.js +601 -0
  98. package/project-transform/src/checker/rules/flow_facts.ts +978 -0
  99. package/project-transform/src/checker/rules/flow_invalidation.js +1119 -0
  100. package/project-transform/src/checker/rules/flow_invalidation.ts +2150 -0
  101. package/project-transform/src/checker/rules/flow_shared.js +2822 -0
  102. package/project-transform/src/checker/rules/flow_shared.ts +4383 -0
  103. package/project-transform/src/checker/rules/foreign_boundary.js +120 -0
  104. package/project-transform/src/checker/rules/foreign_boundary.ts +196 -0
  105. package/project-transform/src/checker/rules/foreign_projection.js +279 -0
  106. package/project-transform/src/checker/rules/foreign_projection.ts +425 -0
  107. package/project-transform/src/checker/rules/generated_helpers.js +13 -0
  108. package/project-transform/src/checker/rules/generated_helpers.ts +18 -0
  109. package/project-transform/src/checker/rules/index.js +35 -0
  110. package/project-transform/src/checker/rules/index.ts +49 -0
  111. package/project-transform/src/checker/rules/namespace_object.js +845 -0
  112. package/project-transform/src/checker/rules/namespace_object.ts +1224 -0
  113. package/project-transform/src/checker/rules/non_ordinary_recovery.js +1328 -0
  114. package/project-transform/src/checker/rules/non_ordinary_recovery.ts +2391 -0
  115. package/project-transform/src/checker/rules/null_prototype.js +3 -0
  116. package/project-transform/src/checker/rules/null_prototype.ts +6 -0
  117. package/project-transform/src/checker/rules/overloads.js +181 -0
  118. package/project-transform/src/checker/rules/overloads.ts +317 -0
  119. package/project-transform/src/checker/rules/predicate_verification.js +691 -0
  120. package/project-transform/src/checker/rules/predicate_verification.ts +1088 -0
  121. package/project-transform/src/checker/rules/prototype_hardening.js +237 -0
  122. package/project-transform/src/checker/rules/prototype_hardening.ts +343 -0
  123. package/project-transform/src/checker/rules/receiver_discipline.js +263 -0
  124. package/project-transform/src/checker/rules/receiver_discipline.ts +356 -0
  125. package/project-transform/src/checker/rules/relations.js +6861 -0
  126. package/project-transform/src/checker/rules/relations.ts +12158 -0
  127. package/project-transform/src/checker/rules/resolved_builtins.js +274 -0
  128. package/project-transform/src/checker/rules/resolved_builtins.ts +438 -0
  129. package/project-transform/src/checker/rules/trust.js +217 -0
  130. package/project-transform/src/checker/rules/trust.ts +301 -0
  131. package/project-transform/src/checker/rules/type_guards.js +173 -0
  132. package/project-transform/src/checker/rules/type_guards.ts +257 -0
  133. package/project-transform/src/checker/rules/universal.js +17 -0
  134. package/project-transform/src/checker/rules/universal.ts +22 -0
  135. package/project-transform/src/checker/rules/unsafe_value_origin.js +80 -0
  136. package/project-transform/src/checker/rules/unsafe_value_origin.ts +125 -0
  137. package/project-transform/src/checker/rules/unsound_imports.js +218 -0
  138. package/project-transform/src/checker/rules/unsound_imports.ts +301 -0
  139. package/project-transform/src/checker/rules/unsound_syntax.js +1695 -0
  140. package/project-transform/src/checker/rules/unsound_syntax.ts +2540 -0
  141. package/project-transform/src/checker/rules/value_types.js +206 -0
  142. package/project-transform/src/checker/rules/value_types.ts +407 -0
  143. package/project-transform/src/checker/timing.js +43 -0
  144. package/project-transform/src/checker/timing.ts +78 -0
  145. package/project-transform/src/checker/unsupported_feature_messages.js +337 -0
  146. package/project-transform/src/checker/unsupported_feature_messages.ts +531 -0
  147. package/project-transform/src/cli.js +892 -0
  148. package/project-transform/src/cli.ts +1476 -0
  149. package/project-transform/src/compiler/compile_project.js +319 -0
  150. package/project-transform/src/compiler/compile_project.ts +508 -0
  151. package/project-transform/src/compiler/errors.js +10 -0
  152. package/project-transform/src/compiler/errors.ts +29 -0
  153. package/project-transform/src/compiler/ir.js +1 -0
  154. package/project-transform/src/compiler/ir.ts +1526 -0
  155. package/project-transform/src/compiler/lower.js +30550 -0
  156. package/project-transform/src/compiler/lower.ts +43645 -0
  157. package/project-transform/src/compiler/lower_arrays.js +140 -0
  158. package/project-transform/src/compiler/lower_arrays.ts +190 -0
  159. package/project-transform/src/compiler/lower_strings.js +121 -0
  160. package/project-transform/src/compiler/lower_strings.ts +198 -0
  161. package/project-transform/src/compiler/lower_tagged.js +329 -0
  162. package/project-transform/src/compiler/lower_tagged.ts +427 -0
  163. package/project-transform/src/compiler/lower_views.js +171 -0
  164. package/project-transform/src/compiler/lower_views.ts +251 -0
  165. package/project-transform/src/compiler/object_keys.js +25 -0
  166. package/project-transform/src/compiler/object_keys.ts +35 -0
  167. package/project-transform/src/compiler/runtime_ir.js +30 -0
  168. package/project-transform/src/compiler/runtime_ir.ts +727 -0
  169. package/project-transform/src/compiler/tagged_boundary.js +18 -0
  170. package/project-transform/src/compiler/tagged_boundary.ts +37 -0
  171. package/project-transform/src/compiler/toolchain.js +170 -0
  172. package/project-transform/src/compiler/toolchain.ts +229 -0
  173. package/project-transform/src/compiler/unicode_case_data.js +2102 -0
  174. package/project-transform/src/compiler/unicode_case_data.ts +2112 -0
  175. package/project-transform/src/compiler/wasm_js_host_runtime.js +656 -0
  176. package/project-transform/src/compiler/wasm_js_host_runtime.ts +762 -0
  177. package/project-transform/src/compiler/wat_arrays.js +3132 -0
  178. package/project-transform/src/compiler/wat_arrays.ts +3768 -0
  179. package/project-transform/src/compiler/wat_emitter.js +17952 -0
  180. package/project-transform/src/compiler/wat_emitter.ts +22812 -0
  181. package/project-transform/src/compiler/wat_strings.js +129 -0
  182. package/project-transform/src/compiler/wat_strings.ts +187 -0
  183. package/project-transform/src/compiler/wat_tagged.js +548 -0
  184. package/project-transform/src/compiler/wat_tagged.ts +674 -0
  185. package/project-transform/src/compiler_generator_runner.js +153 -0
  186. package/project-transform/src/compiler_generator_runner.ts +171 -0
  187. package/project-transform/src/compiler_object_test_helpers.js +69 -0
  188. package/project-transform/src/compiler_object_test_helpers.ts +96 -0
  189. package/project-transform/src/compiler_promise_runner.js +2116 -0
  190. package/project-transform/src/compiler_promise_runner.ts +2184 -0
  191. package/project-transform/src/compiler_test_helpers.js +854 -0
  192. package/project-transform/src/compiler_test_helpers.ts +1087 -0
  193. package/project-transform/src/config.js +568 -0
  194. package/project-transform/src/config.ts +892 -0
  195. package/project-transform/src/diagnostic_metadata.js +67 -0
  196. package/project-transform/src/diagnostic_metadata.ts +99 -0
  197. package/project-transform/src/diagnostic_reference.js +1368 -0
  198. package/project-transform/src/diagnostic_reference.ts +1523 -0
  199. package/project-transform/src/editor_diagnostics_worker.js +176 -0
  200. package/project-transform/src/editor_diagnostics_worker.ts +250 -0
  201. package/project-transform/src/editor_projection.js +224 -0
  202. package/project-transform/src/editor_projection.ts +421 -0
  203. package/project-transform/src/frontend/builtin_expanded_program_test_cleanup.js +47 -0
  204. package/project-transform/src/frontend/builtin_expanded_program_test_cleanup.ts +72 -0
  205. package/project-transform/src/frontend/builtin_macro_support.js +842 -0
  206. package/project-transform/src/frontend/builtin_macro_support.ts +1386 -0
  207. package/project-transform/src/frontend/builtin_macros.js +409 -0
  208. package/project-transform/src/frontend/builtin_macros.ts +542 -0
  209. package/project-transform/src/frontend/component_poc_runtime.js +279 -0
  210. package/project-transform/src/frontend/component_poc_runtime.ts +372 -0
  211. package/project-transform/src/frontend/css_macro.js +148 -0
  212. package/project-transform/src/frontend/css_macro.ts +222 -0
  213. package/project-transform/src/frontend/derive_macros.js +2072 -0
  214. package/project-transform/src/frontend/derive_macros.ts +3188 -0
  215. package/project-transform/src/frontend/embedded_fragment_support.js +106 -0
  216. package/project-transform/src/frontend/embedded_fragment_support.ts +172 -0
  217. package/project-transform/src/frontend/error_normalization.js +403 -0
  218. package/project-transform/src/frontend/error_normalization.ts +832 -0
  219. package/project-transform/src/frontend/error_stdlib_support.js +1 -0
  220. package/project-transform/src/frontend/error_stdlib_support.ts +6 -0
  221. package/project-transform/src/frontend/expand_project.js +169 -0
  222. package/project-transform/src/frontend/expand_project.ts +248 -0
  223. package/project-transform/src/frontend/format_soundscript.js +297 -0
  224. package/project-transform/src/frontend/format_soundscript.ts +582 -0
  225. package/project-transform/src/frontend/graphql_macro.js +174 -0
  226. package/project-transform/src/frontend/graphql_macro.ts +253 -0
  227. package/project-transform/src/frontend/hash_context.js +83 -0
  228. package/project-transform/src/frontend/hash_context.ts +113 -0
  229. package/project-transform/src/frontend/hkt_macro.js +448 -0
  230. package/project-transform/src/frontend/hkt_macro.ts +897 -0
  231. package/project-transform/src/frontend/import_binding_usage.js +190 -0
  232. package/project-transform/src/frontend/import_binding_usage.ts +277 -0
  233. package/project-transform/src/frontend/macro_advanced_backend_adapter.js +58 -0
  234. package/project-transform/src/frontend/macro_advanced_backend_adapter.ts +123 -0
  235. package/project-transform/src/frontend/macro_advanced_context.js +826 -0
  236. package/project-transform/src/frontend/macro_advanced_context.ts +1102 -0
  237. package/project-transform/src/frontend/macro_advanced_output.js +21 -0
  238. package/project-transform/src/frontend/macro_advanced_output.ts +41 -0
  239. package/project-transform/src/frontend/macro_api.js +353 -0
  240. package/project-transform/src/frontend/macro_api.ts +1722 -0
  241. package/project-transform/src/frontend/macro_api_internal.js +35 -0
  242. package/project-transform/src/frontend/macro_api_internal.ts +80 -0
  243. package/project-transform/src/frontend/macro_api_module_support.js +39 -0
  244. package/project-transform/src/frontend/macro_api_module_support.ts +65 -0
  245. package/project-transform/src/frontend/macro_backend_adapter.js +272 -0
  246. package/project-transform/src/frontend/macro_backend_adapter.ts +420 -0
  247. package/project-transform/src/frontend/macro_context.js +816 -0
  248. package/project-transform/src/frontend/macro_context.ts +1105 -0
  249. package/project-transform/src/frontend/macro_debug.js +99 -0
  250. package/project-transform/src/frontend/macro_debug.ts +157 -0
  251. package/project-transform/src/frontend/macro_definition_support.js +28 -0
  252. package/project-transform/src/frontend/macro_definition_support.ts +73 -0
  253. package/project-transform/src/frontend/macro_errors.js +40 -0
  254. package/project-transform/src/frontend/macro_errors.ts +70 -0
  255. package/project-transform/src/frontend/macro_expander.js +919 -0
  256. package/project-transform/src/frontend/macro_expander.ts +1611 -0
  257. package/project-transform/src/frontend/macro_factory_support.js +176 -0
  258. package/project-transform/src/frontend/macro_factory_support.ts +263 -0
  259. package/project-transform/src/frontend/macro_host_ast_internal.js +64 -0
  260. package/project-transform/src/frontend/macro_host_ast_internal.ts +109 -0
  261. package/project-transform/src/frontend/macro_index.js +27 -0
  262. package/project-transform/src/frontend/macro_index.ts +50 -0
  263. package/project-transform/src/frontend/macro_loader.js +281 -0
  264. package/project-transform/src/frontend/macro_loader.ts +506 -0
  265. package/project-transform/src/frontend/macro_operand_semantics.js +838 -0
  266. package/project-transform/src/frontend/macro_operand_semantics.ts +1489 -0
  267. package/project-transform/src/frontend/macro_output.js +54 -0
  268. package/project-transform/src/frontend/macro_output.ts +123 -0
  269. package/project-transform/src/frontend/macro_parser.js +611 -0
  270. package/project-transform/src/frontend/macro_parser.ts +832 -0
  271. package/project-transform/src/frontend/macro_resolver.js +69 -0
  272. package/project-transform/src/frontend/macro_resolver.ts +125 -0
  273. package/project-transform/src/frontend/macro_rewrite.js +285 -0
  274. package/project-transform/src/frontend/macro_rewrite.ts +442 -0
  275. package/project-transform/src/frontend/macro_runtime_support.js +232 -0
  276. package/project-transform/src/frontend/macro_runtime_support.ts +324 -0
  277. package/project-transform/src/frontend/macro_scanner.js +393 -0
  278. package/project-transform/src/frontend/macro_scanner.ts +455 -0
  279. package/project-transform/src/frontend/macro_semantic_backend_adapter.js +87 -0
  280. package/project-transform/src/frontend/macro_semantic_backend_adapter.ts +166 -0
  281. package/project-transform/src/frontend/macro_semantic_context.js +5 -0
  282. package/project-transform/src/frontend/macro_semantic_context.ts +12 -0
  283. package/project-transform/src/frontend/macro_semantic_output.js +24 -0
  284. package/project-transform/src/frontend/macro_semantic_output.ts +47 -0
  285. package/project-transform/src/frontend/macro_semantic_types.js +1 -0
  286. package/project-transform/src/frontend/macro_semantic_types.ts +98 -0
  287. package/project-transform/src/frontend/macro_semantics.js +1172 -0
  288. package/project-transform/src/frontend/macro_semantics.ts +1502 -0
  289. package/project-transform/src/frontend/macro_site_kind_support.js +164 -0
  290. package/project-transform/src/frontend/macro_site_kind_support.ts +255 -0
  291. package/project-transform/src/frontend/macro_syntax_internal.js +1950 -0
  292. package/project-transform/src/frontend/macro_syntax_internal.ts +3338 -0
  293. package/project-transform/src/frontend/macro_templates.js +57 -0
  294. package/project-transform/src/frontend/macro_templates.ts +143 -0
  295. package/project-transform/src/frontend/macro_test_helpers.js +82 -0
  296. package/project-transform/src/frontend/macro_test_helpers.ts +136 -0
  297. package/project-transform/src/frontend/macro_types.js +1 -0
  298. package/project-transform/src/frontend/macro_types.ts +103 -0
  299. package/project-transform/src/frontend/macro_vm.js +39 -0
  300. package/project-transform/src/frontend/macro_vm.ts +113 -0
  301. package/project-transform/src/frontend/match_macro.js +885 -0
  302. package/project-transform/src/frontend/match_macro.ts +1220 -0
  303. package/project-transform/src/frontend/numeric_normalization.js +824 -0
  304. package/project-transform/src/frontend/numeric_normalization.ts +1380 -0
  305. package/project-transform/src/frontend/numeric_prelude.js +278 -0
  306. package/project-transform/src/frontend/numeric_prelude.ts +370 -0
  307. package/project-transform/src/frontend/project_frontend.js +2396 -0
  308. package/project-transform/src/frontend/project_frontend.ts +3776 -0
  309. package/project-transform/src/frontend/project_macro_support.js +1401 -0
  310. package/project-transform/src/frontend/project_macro_support.ts +2137 -0
  311. package/project-transform/src/frontend/sql_macro.js +175 -0
  312. package/project-transform/src/frontend/sql_macro.ts +254 -0
  313. package/project-transform/src/frontend/sql_stdlib_support.js +1 -0
  314. package/project-transform/src/frontend/sql_stdlib_support.ts +6 -0
  315. package/project-transform/src/frontend/std_package_support.js +228 -0
  316. package/project-transform/src/frontend/std_package_support.ts +400 -0
  317. package/project-transform/src/frontend/value_normalization.js +306 -0
  318. package/project-transform/src/frontend/value_normalization.ts +599 -0
  319. package/project-transform/src/lsp/project_service.js +4771 -0
  320. package/project-transform/src/lsp/project_service.ts +7580 -0
  321. package/project-transform/src/lsp/protocol.js +9 -0
  322. package/project-transform/src/lsp/protocol.ts +38 -0
  323. package/project-transform/src/lsp/server.js +355 -0
  324. package/project-transform/src/lsp/server.ts +671 -0
  325. package/project-transform/src/lsp/session.js +49 -0
  326. package/project-transform/src/lsp/session.ts +48 -0
  327. package/project-transform/src/lsp/timing.js +43 -0
  328. package/project-transform/src/lsp/timing.ts +76 -0
  329. package/project-transform/src/lsp/transport.js +205 -0
  330. package/project-transform/src/lsp/transport.ts +253 -0
  331. package/project-transform/src/lsp_main.js +5 -0
  332. package/project-transform/src/lsp_main.ts +7 -0
  333. package/project-transform/src/macros.d.ts +1 -0
  334. package/project-transform/src/macros.js +1 -0
  335. package/project-transform/src/macros.ts +1 -0
  336. package/project-transform/src/main.js +24 -0
  337. package/project-transform/src/main.ts +28 -0
  338. package/project-transform/src/platform/host.js +264 -0
  339. package/project-transform/src/platform/host.ts +343 -0
  340. package/project-transform/src/platform/path.js +8 -0
  341. package/project-transform/src/platform/path.ts +20 -0
  342. package/project-transform/src/public_macro_api/macro_api.d.ts +1054 -0
  343. package/project-transform/src/public_macro_api/macro_semantic_types.d.ts +66 -0
  344. package/project-transform/src/public_macro_api/macro_types.d.ts +70 -0
  345. package/project-transform/src/run_program.js +14 -0
  346. package/project-transform/src/run_program.ts +33 -0
  347. package/project-transform/src/runtime/materialize.js +371 -0
  348. package/project-transform/src/runtime/materialize.ts +502 -0
  349. package/project-transform/src/runtime/on_demand.js +203 -0
  350. package/project-transform/src/runtime/on_demand.ts +305 -0
  351. package/project-transform/src/runtime/source_maps.js +205 -0
  352. package/project-transform/src/runtime/source_maps.ts +297 -0
  353. package/project-transform/src/runtime/transform.js +148 -0
  354. package/project-transform/src/runtime/transform.ts +295 -0
  355. package/project-transform/src/service/types.js +1 -0
  356. package/project-transform/src/service/types.ts +22 -0
  357. package/project-transform/src/soundscript_packages.js +477 -0
  358. package/project-transform/src/soundscript_packages.ts +754 -0
  359. package/project-transform/src/soundscript_runtime_specifiers.js +88 -0
  360. package/project-transform/src/soundscript_runtime_specifiers.ts +96 -0
  361. package/project-transform/src/stdlib/async.d.ts +81 -0
  362. package/project-transform/src/stdlib/async.js +213 -0
  363. package/project-transform/src/stdlib/async.ts +315 -0
  364. package/project-transform/src/stdlib/codec.d.ts +32 -0
  365. package/project-transform/src/stdlib/codec.js +30 -0
  366. package/project-transform/src/stdlib/codec.ts +76 -0
  367. package/project-transform/src/stdlib/compare.d.ts +28 -0
  368. package/project-transform/src/stdlib/compare.js +115 -0
  369. package/project-transform/src/stdlib/compare.ts +151 -0
  370. package/project-transform/src/stdlib/css.d.ts +16 -0
  371. package/project-transform/src/stdlib/css.js +9 -0
  372. package/project-transform/src/stdlib/css.ts +28 -0
  373. package/project-transform/src/stdlib/debug.d.ts +2 -0
  374. package/project-transform/src/stdlib/debug.js +9 -0
  375. package/project-transform/src/stdlib/debug.ts +10 -0
  376. package/project-transform/src/stdlib/decode.d.ts +86 -0
  377. package/project-transform/src/stdlib/decode.js +254 -0
  378. package/project-transform/src/stdlib/decode.ts +390 -0
  379. package/project-transform/src/stdlib/derive.d.ts +6 -0
  380. package/project-transform/src/stdlib/derive.js +7 -0
  381. package/project-transform/src/stdlib/derive.ts +7 -0
  382. package/project-transform/src/stdlib/encode.d.ts +100 -0
  383. package/project-transform/src/stdlib/encode.js +130 -0
  384. package/project-transform/src/stdlib/encode.ts +259 -0
  385. package/project-transform/src/stdlib/failures.d.ts +23 -0
  386. package/project-transform/src/stdlib/failures.js +41 -0
  387. package/project-transform/src/stdlib/failures.ts +64 -0
  388. package/project-transform/src/stdlib/fetch.d.ts +67 -0
  389. package/project-transform/src/stdlib/fetch.js +5 -0
  390. package/project-transform/src/stdlib/fetch.ts +11 -0
  391. package/project-transform/src/stdlib/graphql.d.ts +16 -0
  392. package/project-transform/src/stdlib/graphql.js +9 -0
  393. package/project-transform/src/stdlib/graphql.ts +28 -0
  394. package/project-transform/src/stdlib/hash.d.ts +34 -0
  395. package/project-transform/src/stdlib/hash.js +110 -0
  396. package/project-transform/src/stdlib/hash.ts +188 -0
  397. package/project-transform/src/stdlib/hkt.d.ts +40 -0
  398. package/project-transform/src/stdlib/hkt.js +3 -0
  399. package/project-transform/src/stdlib/hkt.ts +41 -0
  400. package/project-transform/src/stdlib/index.d.ts +9 -0
  401. package/project-transform/src/stdlib/index.js +15 -0
  402. package/project-transform/src/stdlib/index.ts +23 -0
  403. package/project-transform/src/stdlib/json.d.ts +125 -0
  404. package/project-transform/src/stdlib/json.js +764 -0
  405. package/project-transform/src/stdlib/json.ts +1034 -0
  406. package/project-transform/src/stdlib/match.d.ts +11 -0
  407. package/project-transform/src/stdlib/match.js +13 -0
  408. package/project-transform/src/stdlib/match.ts +26 -0
  409. package/project-transform/src/stdlib/numerics.d.ts +523 -0
  410. package/project-transform/src/stdlib/numerics.js +1356 -0
  411. package/project-transform/src/stdlib/numerics.ts +1937 -0
  412. package/project-transform/src/stdlib/random.d.ts +19 -0
  413. package/project-transform/src/stdlib/random.js +3 -0
  414. package/project-transform/src/stdlib/random.ts +5 -0
  415. package/project-transform/src/stdlib/result.d.ts +68 -0
  416. package/project-transform/src/stdlib/result.js +139 -0
  417. package/project-transform/src/stdlib/result.ts +248 -0
  418. package/project-transform/src/stdlib/sql.d.ts +22 -0
  419. package/project-transform/src/stdlib/sql.js +23 -0
  420. package/project-transform/src/stdlib/sql.ts +53 -0
  421. package/project-transform/src/stdlib/text.d.ts +24 -0
  422. package/project-transform/src/stdlib/text.js +3 -0
  423. package/project-transform/src/stdlib/text.ts +4 -0
  424. package/project-transform/src/stdlib/thunk.d.ts +2 -0
  425. package/project-transform/src/stdlib/thunk.js +9 -0
  426. package/project-transform/src/stdlib/thunk.ts +15 -0
  427. package/project-transform/src/stdlib/typeclasses.d.ts +57 -0
  428. package/project-transform/src/stdlib/typeclasses.js +78 -0
  429. package/project-transform/src/stdlib/typeclasses.ts +173 -0
  430. package/project-transform/src/stdlib/url.d.ts +37 -0
  431. package/project-transform/src/stdlib/url.js +3 -0
  432. package/project-transform/src/stdlib/url.ts +4 -0
  433. package/project-transform/src/stdlib/value.d.ts +9 -0
  434. package/project-transform/src/stdlib/value.js +104 -0
  435. package/project-transform/src/stdlib/value.ts +133 -0
  436. package/project-transform/src/test_installed_stdlib.js +147 -0
  437. package/project-transform/src/test_installed_stdlib.ts +245 -0
  438. package/project-transform/src/test_macro_package_fixture.js +50 -0
  439. package/project-transform/src/test_macro_package_fixture.ts +68 -0
  440. package/project-transform/src/value_deep_safe.js +191 -0
  441. package/project-transform/src/value_deep_safe.ts +273 -0
@@ -0,0 +1,897 @@
1
+ import ts from 'typescript';
2
+
3
+ import { fromFileUrl } from '../platform/path.ts';
4
+ import { STS_HKT_MODULE_SPECIFIER } from '../soundscript_runtime_specifiers.ts';
5
+ import type { MacroDefinition } from './macro_api.ts';
6
+ import { macroSignature } from './macro_api.ts';
7
+ import { attachMacroFactoryMetadata } from './macro_api_internal.ts';
8
+ import {
9
+ createDeclSyntaxFromText,
10
+ createExprSyntaxFromText,
11
+ getHostDeclaration,
12
+ getHostExpression,
13
+ } from './macro_syntax_internal.ts';
14
+
15
+ const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
16
+ const DO_BIND_HELPER_PROPERTY = 'macroBind';
17
+ const DO_HELPER_PROPERTY = 'macroGen';
18
+ const HKT_MODULE_SPECIFIER = STS_HKT_MODULE_SPECIFIER;
19
+ const HKT_MACRO_FILE_NAME = fromFileUrl(import.meta.url);
20
+ const HKT_DECL_SIGNATURE = macroSignature.of(macroSignature.interfaceDecl('target'));
21
+ const DO_SIGNATURE = macroSignature.of(
22
+ macroSignature.expr('monad'),
23
+ macroSignature.functionExpr('body'),
24
+ );
25
+
26
+ function attachHktFactory<T extends () => MacroDefinition>(factory: T, form: 'call' | 'decl'): T {
27
+ return attachMacroFactoryMetadata(factory, { form, moduleFileName: HKT_MACRO_FILE_NAME }) as T;
28
+ }
29
+
30
+ function printNode(node: ts.Node, sourceFile: ts.SourceFile): string {
31
+ return printer.printNode(ts.EmitHint.Unspecified, node, sourceFile);
32
+ }
33
+
34
+ function indent(text: string, prefix = ' '): string {
35
+ return text
36
+ .trim()
37
+ .split('\n')
38
+ .map((line) => `${prefix}${line}`)
39
+ .join('\n');
40
+ }
41
+
42
+ function createTypeSlot(index: number): ts.TypeNode {
43
+ return ts.factory.createIndexedAccessTypeNode(
44
+ ts.factory.createIndexedAccessTypeNode(
45
+ ts.factory.createThisTypeNode(),
46
+ ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral('Args')),
47
+ ),
48
+ ts.factory.createLiteralTypeNode(ts.factory.createNumericLiteral(index)),
49
+ );
50
+ }
51
+
52
+ function rewriteHktMemberType(
53
+ node: ts.TypeNode,
54
+ typeParameters: ReadonlyMap<string, number>,
55
+ ): ts.TypeNode {
56
+ const transformed = ts.transform(node, [(
57
+ context: ts.TransformationContext,
58
+ ) =>
59
+ (root: ts.Node) =>
60
+ ts.visitNode(root, function visit(current): ts.VisitResult<ts.Node> {
61
+ if (
62
+ ts.isTypeReferenceNode(current) &&
63
+ current.typeArguments === undefined &&
64
+ ts.isIdentifier(current.typeName)
65
+ ) {
66
+ const index = typeParameters.get(current.typeName.text);
67
+ if (index !== undefined) {
68
+ return createTypeSlot(index);
69
+ }
70
+ }
71
+ return ts.visitEachChild(current, visit, context);
72
+ })]);
73
+ try {
74
+ const [rewritten] = transformed.transformed;
75
+ if (!rewritten || !ts.isTypeNode(rewritten)) {
76
+ throw new Error('Expected HKT member type rewrite to produce a type node.');
77
+ }
78
+ return rewritten;
79
+ } finally {
80
+ transformed.dispose();
81
+ }
82
+ }
83
+
84
+ function hasReadonlyModifier(node: ts.Node): boolean {
85
+ const modifiers = ts.canHaveModifiers(node) ? ts.getModifiers(node) : undefined;
86
+ return modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ReadonlyKeyword) ?? false;
87
+ }
88
+
89
+ function modifierText(
90
+ modifiers: readonly ts.ModifierLike[] | undefined,
91
+ sourceFile: ts.SourceFile,
92
+ ): string {
93
+ const printed = modifiers
94
+ ?.filter((modifier): modifier is ts.Modifier => !ts.isDecorator(modifier))
95
+ .map((modifier) => printNode(modifier, sourceFile))
96
+ .join(' ') ?? '';
97
+ return printed.length > 0 ? `${printed} ` : '';
98
+ }
99
+
100
+ function bindingIdentifiers(name: ts.BindingName): readonly ts.Identifier[] {
101
+ if (ts.isIdentifier(name)) {
102
+ return [name];
103
+ }
104
+
105
+ return name.elements.flatMap((element) =>
106
+ ts.isOmittedExpression(element) ? [] : bindingIdentifiers(element.name)
107
+ );
108
+ }
109
+
110
+ function isDeclarationName(node: ts.Identifier): boolean {
111
+ const parent = node.parent;
112
+ return (
113
+ (ts.isVariableDeclaration(parent) && parent.name === node) ||
114
+ (ts.isParameter(parent) && parent.name === node) ||
115
+ (ts.isBindingElement(parent) && parent.name === node) ||
116
+ (ts.isFunctionDeclaration(parent) && parent.name === node) ||
117
+ (ts.isFunctionExpression(parent) && parent.name === node) ||
118
+ (ts.isArrowFunction(parent) &&
119
+ parent.parameters.some((parameter) => parameter.name === node)) ||
120
+ (ts.isClassDeclaration(parent) && parent.name === node) ||
121
+ (ts.isClassExpression(parent) && parent.name === node) ||
122
+ (ts.isInterfaceDeclaration(parent) && parent.name === node)
123
+ );
124
+ }
125
+
126
+ function isIgnoredValueName(node: ts.Identifier): boolean {
127
+ const parent = node.parent;
128
+ return (
129
+ (ts.isPropertyAccessExpression(parent) && parent.name === node) ||
130
+ (ts.isPropertyAssignment(parent) && parent.name === node) ||
131
+ (ts.isMethodDeclaration(parent) && parent.name === node) ||
132
+ (ts.isPropertyDeclaration(parent) && parent.name === node) ||
133
+ (ts.isGetAccessorDeclaration(parent) && parent.name === node) ||
134
+ (ts.isSetAccessorDeclaration(parent) && parent.name === node) ||
135
+ (ts.isBindingElement(parent) && parent.propertyName === node)
136
+ );
137
+ }
138
+
139
+ function assertNoBindInNestedFunction(
140
+ node: ts.SignatureDeclaration,
141
+ bindName: string,
142
+ error: (message: string) => never,
143
+ ) {
144
+ const found = ts.forEachChild(node, function visit(current): true | undefined {
145
+ if (ts.isTypeNode(current)) {
146
+ return undefined;
147
+ }
148
+ if (ts.isIdentifier(current) && current.text === bindName && !isIgnoredValueName(current)) {
149
+ return true;
150
+ }
151
+ return ts.forEachChild(current, visit);
152
+ });
153
+
154
+ if (found) {
155
+ error(`Do does not allow \`${bindName}\` inside nested functions.`);
156
+ }
157
+ }
158
+
159
+ function assertNoBindShadowing(
160
+ name: ts.BindingName,
161
+ bindName: string,
162
+ error: (message: string) => never,
163
+ ) {
164
+ for (const identifier of bindingIdentifiers(name)) {
165
+ if (identifier.text === bindName) {
166
+ error(`Do does not allow rebinding \`${bindName}\` inside the callback body.`);
167
+ }
168
+ }
169
+ }
170
+
171
+ function isNestedLabel(node: ts.LabeledStatement): boolean {
172
+ return ts.isLabeledStatement(node.parent) && node.parent.statement === node;
173
+ }
174
+
175
+ function collectLabelChain(node: ts.LabeledStatement): {
176
+ labels: readonly string[];
177
+ statement: ts.Statement;
178
+ } {
179
+ const labels: string[] = [node.label.text];
180
+ let statement = node.statement;
181
+
182
+ while (ts.isLabeledStatement(statement)) {
183
+ labels.push(statement.label.text);
184
+ statement = statement.statement;
185
+ }
186
+
187
+ return { labels, statement };
188
+ }
189
+
190
+ function createThrowTypeError(message: string): ts.Expression {
191
+ return ts.factory.createCallExpression(
192
+ ts.factory.createParenthesizedExpression(
193
+ ts.factory.createArrowFunction(
194
+ undefined,
195
+ undefined,
196
+ [],
197
+ undefined,
198
+ ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
199
+ ts.factory.createBlock(
200
+ [
201
+ ts.factory.createThrowStatement(
202
+ ts.factory.createNewExpression(
203
+ ts.factory.createIdentifier('TypeError'),
204
+ undefined,
205
+ [ts.factory.createStringLiteral(message)],
206
+ ),
207
+ ),
208
+ ],
209
+ true,
210
+ ),
211
+ ),
212
+ ),
213
+ undefined,
214
+ [],
215
+ );
216
+ }
217
+
218
+ function createAsyncIteratorValueStatement(
219
+ initializer: ts.ForInitializer,
220
+ stepValue: ts.Expression,
221
+ bindName: string,
222
+ error: (message: string) => never,
223
+ ): ts.Statement {
224
+ if (ts.isVariableDeclarationList(initializer)) {
225
+ if ((initializer.flags & ts.NodeFlags.BlockScoped) === 0) {
226
+ error('Do only supports `const` and `let` declarations inside the callback body.');
227
+ }
228
+ if (initializer.declarations.length !== 1) {
229
+ error('Do requires `for await...of` declarations to bind exactly one value.');
230
+ }
231
+ const [declaration] = initializer.declarations;
232
+ if (!declaration) {
233
+ error('Do requires `for await...of` declarations to bind exactly one value.');
234
+ }
235
+ if (declaration.initializer) {
236
+ error('Do does not support initializers in `for await...of` declarations.');
237
+ }
238
+ assertNoBindShadowing(declaration.name, bindName, error);
239
+ return ts.factory.createVariableStatement(
240
+ undefined,
241
+ ts.factory.createVariableDeclarationList(
242
+ [
243
+ ts.factory.createVariableDeclaration(
244
+ declaration.name,
245
+ undefined,
246
+ declaration.type,
247
+ stepValue,
248
+ ),
249
+ ],
250
+ initializer.flags,
251
+ ),
252
+ );
253
+ }
254
+
255
+ return ts.factory.createExpressionStatement(
256
+ ts.factory.createBinaryExpression(
257
+ initializer,
258
+ ts.factory.createToken(ts.SyntaxKind.EqualsToken),
259
+ stepValue,
260
+ ),
261
+ );
262
+ }
263
+
264
+ function lowerForAwaitStatement(
265
+ loop: ts.ForOfStatement,
266
+ labels: readonly string[],
267
+ bindName: string,
268
+ monadBindingName: string,
269
+ bindHelperName: string,
270
+ allocateBindTempName: () => string,
271
+ allocateAsyncName: (kind: string) => string,
272
+ visit: (node: ts.Node) => ts.VisitResult<ts.Node>,
273
+ allowAwait: boolean,
274
+ error: (message: string) => never,
275
+ ): ts.Statement {
276
+ if (!allowAwait) {
277
+ error('Do only supports `for await...of` inside async callbacks.');
278
+ }
279
+ if (
280
+ ts.isVariableDeclarationList(loop.initializer) &&
281
+ (loop.initializer.flags & ts.NodeFlags.BlockScoped) === 0
282
+ ) {
283
+ error('Do only supports `const` and `let` declarations inside the callback body.');
284
+ }
285
+
286
+ const iterableName = allocateAsyncName('iterable');
287
+ const iteratorMethodName = allocateAsyncName('iterator_method');
288
+ const iteratorName = allocateAsyncName('iterator');
289
+ const doneName = allocateAsyncName('done');
290
+ const stepName = allocateAsyncName('step');
291
+ const bindTempName = allocateBindTempName();
292
+ const iteratorCloseTempName = allocateBindTempName();
293
+
294
+ const iterableExpression = ts.visitNode(loop.expression, visit) as ts.Expression;
295
+ const bodyStatement = ts.visitNode(loop.statement, visit) as ts.Statement;
296
+ const bodyStatements = ts.isBlock(bodyStatement)
297
+ ? [...bodyStatement.statements]
298
+ : [bodyStatement];
299
+
300
+ const stepValueExpression = ts.factory.createPropertyAccessExpression(
301
+ ts.factory.createIdentifier(stepName),
302
+ 'value',
303
+ );
304
+ const stepBindingStatement = ts.factory.createVariableStatement(
305
+ undefined,
306
+ ts.factory.createVariableDeclarationList(
307
+ [
308
+ ts.factory.createVariableDeclaration(
309
+ stepName,
310
+ undefined,
311
+ undefined,
312
+ ts.factory.createCallExpression(
313
+ ts.factory.createIdentifier(bindHelperName),
314
+ undefined,
315
+ [
316
+ ts.factory.createBinaryExpression(
317
+ ts.factory.createIdentifier(bindTempName),
318
+ ts.factory.createToken(ts.SyntaxKind.EqualsToken),
319
+ ts.factory.createCallExpression(
320
+ ts.factory.createPropertyAccessExpression(
321
+ ts.factory.createIdentifier(monadBindingName),
322
+ 'fromPromise',
323
+ ),
324
+ undefined,
325
+ [
326
+ ts.factory.createCallExpression(
327
+ ts.factory.createPropertyAccessExpression(
328
+ ts.factory.createIdentifier(iteratorName),
329
+ 'next',
330
+ ),
331
+ undefined,
332
+ [],
333
+ ),
334
+ ],
335
+ ),
336
+ ),
337
+ ts.factory.createYieldExpression(
338
+ undefined,
339
+ ts.factory.createIdentifier(bindTempName),
340
+ ),
341
+ ],
342
+ ),
343
+ ),
344
+ ],
345
+ ts.NodeFlags.Const,
346
+ ),
347
+ );
348
+
349
+ const loopStatements: ts.Statement[] = [
350
+ stepBindingStatement,
351
+ ts.factory.createIfStatement(
352
+ ts.factory.createPropertyAccessExpression(
353
+ ts.factory.createIdentifier(stepName),
354
+ 'done',
355
+ ),
356
+ ts.factory.createBlock(
357
+ [
358
+ ts.factory.createExpressionStatement(
359
+ ts.factory.createBinaryExpression(
360
+ ts.factory.createIdentifier(doneName),
361
+ ts.factory.createToken(ts.SyntaxKind.EqualsToken),
362
+ ts.factory.createTrue(),
363
+ ),
364
+ ),
365
+ ts.factory.createBreakStatement(),
366
+ ],
367
+ true,
368
+ ),
369
+ ),
370
+ createAsyncIteratorValueStatement(
371
+ loop.initializer,
372
+ stepValueExpression,
373
+ bindName,
374
+ error,
375
+ ),
376
+ ...bodyStatements,
377
+ ];
378
+
379
+ let loopStatement: ts.Statement = ts.factory.createWhileStatement(
380
+ ts.factory.createTrue(),
381
+ ts.factory.createBlock(loopStatements, true),
382
+ );
383
+ for (const label of [...labels].reverse()) {
384
+ loopStatement = ts.factory.createLabeledStatement(label, loopStatement);
385
+ }
386
+
387
+ return ts.factory.createBlock(
388
+ [
389
+ ts.factory.createVariableStatement(
390
+ undefined,
391
+ ts.factory.createVariableDeclarationList(
392
+ [
393
+ ts.factory.createVariableDeclaration(
394
+ iterableName,
395
+ undefined,
396
+ undefined,
397
+ iterableExpression,
398
+ ),
399
+ ],
400
+ ts.NodeFlags.Const,
401
+ ),
402
+ ),
403
+ ts.factory.createVariableStatement(
404
+ undefined,
405
+ ts.factory.createVariableDeclarationList(
406
+ [
407
+ ts.factory.createVariableDeclaration(
408
+ iteratorMethodName,
409
+ undefined,
410
+ undefined,
411
+ ts.factory.createElementAccessExpression(
412
+ ts.factory.createIdentifier(iterableName),
413
+ ts.factory.createPropertyAccessExpression(
414
+ ts.factory.createIdentifier('Symbol'),
415
+ 'asyncIterator',
416
+ ),
417
+ ),
418
+ ),
419
+ ],
420
+ ts.NodeFlags.Const,
421
+ ),
422
+ ),
423
+ ts.factory.createVariableStatement(
424
+ undefined,
425
+ ts.factory.createVariableDeclarationList(
426
+ [
427
+ ts.factory.createVariableDeclaration(
428
+ iteratorName,
429
+ undefined,
430
+ undefined,
431
+ ts.factory.createConditionalExpression(
432
+ ts.factory.createBinaryExpression(
433
+ ts.factory.createTypeOfExpression(
434
+ ts.factory.createIdentifier(iteratorMethodName),
435
+ ),
436
+ ts.factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken),
437
+ ts.factory.createStringLiteral('function'),
438
+ ),
439
+ ts.factory.createToken(ts.SyntaxKind.QuestionToken),
440
+ ts.factory.createCallExpression(
441
+ ts.factory.createElementAccessExpression(
442
+ ts.factory.createIdentifier(iterableName),
443
+ ts.factory.createPropertyAccessExpression(
444
+ ts.factory.createIdentifier('Symbol'),
445
+ 'asyncIterator',
446
+ ),
447
+ ),
448
+ undefined,
449
+ [],
450
+ ),
451
+ ts.factory.createToken(ts.SyntaxKind.ColonToken),
452
+ createThrowTypeError(
453
+ 'Do `for await...of` requires an async iterable value.',
454
+ ),
455
+ ),
456
+ ),
457
+ ],
458
+ ts.NodeFlags.Const,
459
+ ),
460
+ ),
461
+ ts.factory.createVariableStatement(
462
+ undefined,
463
+ ts.factory.createVariableDeclarationList(
464
+ [
465
+ ts.factory.createVariableDeclaration(
466
+ doneName,
467
+ undefined,
468
+ undefined,
469
+ ts.factory.createFalse(),
470
+ ),
471
+ ],
472
+ ts.NodeFlags.Let,
473
+ ),
474
+ ),
475
+ ts.factory.createTryStatement(
476
+ ts.factory.createBlock([loopStatement], true),
477
+ undefined,
478
+ ts.factory.createBlock(
479
+ [
480
+ ts.factory.createIfStatement(
481
+ ts.factory.createBinaryExpression(
482
+ ts.factory.createPrefixUnaryExpression(
483
+ ts.SyntaxKind.ExclamationToken,
484
+ ts.factory.createIdentifier(doneName),
485
+ ),
486
+ ts.factory.createToken(ts.SyntaxKind.AmpersandAmpersandToken),
487
+ ts.factory.createBinaryExpression(
488
+ ts.factory.createTypeOfExpression(
489
+ ts.factory.createPropertyAccessExpression(
490
+ ts.factory.createIdentifier(iteratorName),
491
+ 'return',
492
+ ),
493
+ ),
494
+ ts.factory.createToken(ts.SyntaxKind.EqualsEqualsEqualsToken),
495
+ ts.factory.createStringLiteral('function'),
496
+ ),
497
+ ),
498
+ ts.factory.createBlock(
499
+ [
500
+ ts.factory.createExpressionStatement(
501
+ ts.factory.createCallExpression(
502
+ ts.factory.createIdentifier(bindHelperName),
503
+ undefined,
504
+ [
505
+ ts.factory.createBinaryExpression(
506
+ ts.factory.createIdentifier(iteratorCloseTempName),
507
+ ts.factory.createToken(ts.SyntaxKind.EqualsToken),
508
+ ts.factory.createCallExpression(
509
+ ts.factory.createPropertyAccessExpression(
510
+ ts.factory.createIdentifier(monadBindingName),
511
+ 'fromPromise',
512
+ ),
513
+ undefined,
514
+ [
515
+ ts.factory.createCallExpression(
516
+ ts.factory.createPropertyAccessExpression(
517
+ ts.factory.createIdentifier(iteratorName),
518
+ 'return',
519
+ ),
520
+ undefined,
521
+ [],
522
+ ),
523
+ ],
524
+ ),
525
+ ),
526
+ ts.factory.createYieldExpression(
527
+ undefined,
528
+ ts.factory.createIdentifier(iteratorCloseTempName),
529
+ ),
530
+ ],
531
+ ),
532
+ ),
533
+ ],
534
+ true,
535
+ ),
536
+ ),
537
+ ],
538
+ true,
539
+ ),
540
+ ),
541
+ ],
542
+ true,
543
+ );
544
+ }
545
+
546
+ function lowerDoBlock(
547
+ body: ts.Block,
548
+ bindName: string,
549
+ monadBindingName: string,
550
+ bindHelperName: string,
551
+ helperSuffix: string,
552
+ bindTempPrefix: string,
553
+ allowAwait: boolean,
554
+ error: (message: string) => never,
555
+ ): ts.Block {
556
+ const bindTempNames: string[] = [];
557
+ const allocateBindTempName = () => {
558
+ const name = `${bindTempPrefix}${bindTempNames.length}`;
559
+ bindTempNames.push(name);
560
+ return name;
561
+ };
562
+ let asyncTempIndex = 0;
563
+ const allocateAsyncName = (kind: string) =>
564
+ `__sts_do_async_${kind}_${helperSuffix}_${asyncTempIndex++}`;
565
+ const transformed = ts.transform(body, [(
566
+ context: ts.TransformationContext,
567
+ ) =>
568
+ (root: ts.Node) =>
569
+ ts.visitNode(root, function visit(node): ts.VisitResult<ts.Node> {
570
+ if (ts.isTypeNode(node)) {
571
+ return node;
572
+ }
573
+ if (ts.isFunctionLike(node)) {
574
+ assertNoBindInNestedFunction(node, bindName, error);
575
+ return node;
576
+ }
577
+ if (ts.isAwaitExpression(node)) {
578
+ if (!allowAwait) {
579
+ error('Do only supports `await` inside async callbacks.');
580
+ }
581
+ const tempName = allocateBindTempName();
582
+ return ts.factory.createCallExpression(
583
+ ts.factory.createIdentifier(bindHelperName),
584
+ undefined,
585
+ [
586
+ ts.factory.createBinaryExpression(
587
+ ts.factory.createIdentifier(tempName),
588
+ ts.factory.createToken(ts.SyntaxKind.EqualsToken),
589
+ ts.factory.createCallExpression(
590
+ ts.factory.createPropertyAccessExpression(
591
+ ts.factory.createIdentifier(monadBindingName),
592
+ 'fromPromise',
593
+ ),
594
+ undefined,
595
+ [ts.visitNode(node.expression, visit) as ts.Expression],
596
+ ),
597
+ ),
598
+ ts.factory.createYieldExpression(
599
+ undefined,
600
+ ts.factory.createIdentifier(tempName),
601
+ ),
602
+ ],
603
+ );
604
+ }
605
+ if (ts.isYieldExpression(node)) {
606
+ error('Do does not support explicit `yield` inside the callback body.');
607
+ }
608
+ if (ts.isMetaProperty(node)) {
609
+ error('Do does not support `new.target` inside the callback body.');
610
+ }
611
+ if (node.kind === ts.SyntaxKind.SuperKeyword) {
612
+ error('Do does not support `super` inside the callback body.');
613
+ }
614
+ if (ts.isIdentifier(node) && node.text === 'arguments' && !isIgnoredValueName(node)) {
615
+ error('Do does not support `arguments` inside the callback body.');
616
+ }
617
+ if (ts.isVariableDeclarationList(node) && (node.flags & ts.NodeFlags.BlockScoped) === 0) {
618
+ error('Do only supports `const` and `let` declarations inside the callback body.');
619
+ }
620
+ if (ts.isVariableDeclaration(node)) {
621
+ assertNoBindShadowing(node.name, bindName, error);
622
+ }
623
+ if (ts.isLabeledStatement(node) && !isNestedLabel(node)) {
624
+ const { labels, statement } = collectLabelChain(node);
625
+ if (ts.isForOfStatement(statement) && statement.awaitModifier) {
626
+ return lowerForAwaitStatement(
627
+ statement,
628
+ labels,
629
+ bindName,
630
+ monadBindingName,
631
+ bindHelperName,
632
+ allocateBindTempName,
633
+ allocateAsyncName,
634
+ visit,
635
+ allowAwait,
636
+ error,
637
+ );
638
+ }
639
+ }
640
+ if (ts.isForInStatement(node) || ts.isForOfStatement(node)) {
641
+ if (
642
+ ts.isVariableDeclarationList(node.initializer) &&
643
+ (node.initializer.flags & ts.NodeFlags.BlockScoped) === 0
644
+ ) {
645
+ error('Do only supports `const` and `let` declarations inside the callback body.');
646
+ }
647
+ }
648
+ if (ts.isForOfStatement(node) && node.awaitModifier) {
649
+ if (ts.isLabeledStatement(node.parent)) {
650
+ return node;
651
+ }
652
+ return lowerForAwaitStatement(
653
+ node,
654
+ [],
655
+ bindName,
656
+ monadBindingName,
657
+ bindHelperName,
658
+ allocateBindTempName,
659
+ allocateAsyncName,
660
+ visit,
661
+ allowAwait,
662
+ error,
663
+ );
664
+ }
665
+ if (
666
+ ts.isCallExpression(node) && ts.isIdentifier(node.expression) &&
667
+ node.expression.text === bindName
668
+ ) {
669
+ if ((node.typeArguments?.length ?? 0) > 0 || node.arguments.length !== 1) {
670
+ error('Do bind sites must use the form `bind(expr)`.');
671
+ }
672
+ const tempName = allocateBindTempName();
673
+ return ts.factory.createCallExpression(
674
+ ts.factory.createIdentifier(bindHelperName),
675
+ undefined,
676
+ [
677
+ ts.factory.createBinaryExpression(
678
+ ts.factory.createIdentifier(tempName),
679
+ ts.factory.createToken(ts.SyntaxKind.EqualsToken),
680
+ ts.visitNode(node.arguments[0]!, visit) as ts.Expression,
681
+ ),
682
+ ts.factory.createYieldExpression(
683
+ undefined,
684
+ ts.factory.createIdentifier(tempName),
685
+ ),
686
+ ],
687
+ );
688
+ }
689
+ if (ts.isIdentifier(node) && node.text === bindName) {
690
+ if (isDeclarationName(node)) {
691
+ error(`Do does not allow rebinding \`${bindName}\` inside the callback body.`);
692
+ }
693
+ if (!isIgnoredValueName(node)) {
694
+ error('Do bind sites must use the form `bind(expr)`.');
695
+ }
696
+ }
697
+ return ts.visitEachChild(node, visit, context);
698
+ })]);
699
+ try {
700
+ const [firstRewritten] = transformed.transformed;
701
+ if (!firstRewritten || !ts.isBlock(firstRewritten)) {
702
+ throw new Error('Expected Do lowering to produce a block.');
703
+ }
704
+ let rewritten: ts.Block = firstRewritten;
705
+ if (bindTempNames.length > 0) {
706
+ rewritten = ts.factory.updateBlock(rewritten, [
707
+ ts.factory.createVariableStatement(
708
+ undefined,
709
+ ts.factory.createVariableDeclarationList(
710
+ bindTempNames.map((name) => ts.factory.createVariableDeclaration(name)),
711
+ ts.NodeFlags.Let,
712
+ ),
713
+ ),
714
+ ...rewritten.statements,
715
+ ]);
716
+ }
717
+ return rewritten;
718
+ } finally {
719
+ transformed.dispose();
720
+ }
721
+ }
722
+
723
+ // #[macro(decl)]
724
+ export function hkt(): MacroDefinition<typeof HKT_DECL_SIGNATURE> {
725
+ return {
726
+ declarationKinds: ['interface'],
727
+ expand(ctx, signature) {
728
+ const declaration = getHostDeclaration(signature.args.target);
729
+ const interfaceDeclaration = ts.isInterfaceDeclaration(declaration)
730
+ ? declaration
731
+ : ctx.error('hkt only supports interface declarations.');
732
+
733
+ if ((interfaceDeclaration.typeParameters?.length ?? 0) < 1) {
734
+ ctx.error('hkt requires at least one type parameter.');
735
+ }
736
+ if ((interfaceDeclaration.heritageClauses?.length ?? 0) > 0) {
737
+ ctx.error('hkt does not yet support interface extends clauses.');
738
+ }
739
+
740
+ const typeParameters = interfaceDeclaration.typeParameters ?? [];
741
+ for (const typeParameter of typeParameters) {
742
+ if (typeParameter.constraint || typeParameter.default) {
743
+ ctx.error('hkt does not yet support constrained or defaulted type parameters.');
744
+ }
745
+ }
746
+
747
+ if (interfaceDeclaration.members.length !== 1) {
748
+ ctx.error('hkt requires exactly one `readonly type: ...` member.');
749
+ }
750
+
751
+ const [memberNode] = interfaceDeclaration.members;
752
+ const member = memberNode && ts.isPropertySignature(memberNode)
753
+ ? memberNode
754
+ : ctx.error('hkt requires exactly one `readonly type: ...` member.');
755
+ if (!ts.isIdentifier(member.name) || member.name.text !== 'type') {
756
+ ctx.error('hkt requires the interface member to be named `type`.');
757
+ }
758
+ if (!hasReadonlyModifier(member)) {
759
+ ctx.error('hkt requires `type` to be declared as a readonly property.');
760
+ }
761
+ if (!member.type || member.questionToken) {
762
+ ctx.error('hkt requires `type` to have an explicit non-optional type.');
763
+ }
764
+ const memberType = member.type ??
765
+ ctx.error('hkt requires `type` to have an explicit non-optional type.');
766
+
767
+ const typeParameterIndexes = new Map(
768
+ typeParameters.map((typeParameter, index) => [typeParameter.name.text, index] as const),
769
+ );
770
+ const rewrittenMemberType = rewriteHktMemberType(memberType, typeParameterIndexes);
771
+ const updatedMember = ts.factory.updatePropertySignature(
772
+ member,
773
+ member.modifiers,
774
+ member.name,
775
+ undefined,
776
+ rewrittenMemberType,
777
+ );
778
+ const sourceFile = interfaceDeclaration.getSourceFile();
779
+ const declarationText = [
780
+ `${
781
+ modifierText(interfaceDeclaration.modifiers, sourceFile)
782
+ }interface ${interfaceDeclaration.name.text} {`,
783
+ ' readonly Args: readonly unknown[];',
784
+ indent(printNode(updatedMember, sourceFile)),
785
+ '}',
786
+ ].join('\n');
787
+
788
+ return ctx.output.stmt(
789
+ createDeclSyntaxFromText(
790
+ ctx.invocationSpan().fileName,
791
+ signature.args.target.span,
792
+ declarationText,
793
+ ),
794
+ );
795
+ },
796
+ signature: HKT_DECL_SIGNATURE,
797
+ };
798
+ }
799
+ attachHktFactory(hkt, 'decl');
800
+
801
+ // #[macro(call)]
802
+ export function Do(): MacroDefinition<typeof DO_SIGNATURE> {
803
+ return {
804
+ expand(ctx, signature) {
805
+ const callbackText = ctx.semantics.argExpanded(1)?.text() ?? signature.args.body.text();
806
+ const callbackExpr = createExprSyntaxFromText(
807
+ ctx.invocationSpan().fileName,
808
+ signature.args.body.span,
809
+ callbackText,
810
+ );
811
+ const callbackNode = getHostExpression(callbackExpr);
812
+ const callback = ts.isArrowFunction(callbackNode)
813
+ ? callbackNode
814
+ : ctx.error('Do only supports callbacks of the form `(bind) => { ... }`.');
815
+ const isAsyncCallback =
816
+ ts.getModifiers(callback)?.some((modifier) =>
817
+ modifier.kind === ts.SyntaxKind.AsyncKeyword
818
+ ) ??
819
+ false;
820
+ if ((callback.typeParameters?.length ?? 0) > 0) {
821
+ ctx.error('Do does not support generic callbacks.');
822
+ }
823
+ if (callback.parameters.length !== 1 || !ts.isIdentifier(callback.parameters[0]!.name)) {
824
+ ctx.error('Do only supports callbacks of the form `(bind) => { ... }`.');
825
+ }
826
+
827
+ const [bindParameter] = callback.parameters;
828
+ if (!bindParameter || bindParameter.dotDotDotToken || bindParameter.initializer) {
829
+ ctx.error('Do only supports callbacks of the form `(bind) => { ... }`.');
830
+ }
831
+ const bindParameterName = ts.isIdentifier(bindParameter.name)
832
+ ? bindParameter.name
833
+ : ctx.error('Do only supports callbacks of the form `(bind) => { ... }`.');
834
+ if (bindParameterName.text !== 'bind') {
835
+ ctx.error('Do requires the callback parameter to be named `bind`.');
836
+ }
837
+ const callbackBody = ts.isBlock(callback.body)
838
+ ? callback.body
839
+ : ctx.error('Do requires a block-bodied callback.');
840
+
841
+ const bindName = bindParameterName.text;
842
+ const helperSuffix = String(ctx.invocationSpan().start);
843
+ const monadBindingName = `__sts_do_monad_${helperSuffix}`;
844
+ const bindHelperName = `__sts_do_bind_${helperSuffix}`;
845
+ const callbackBindingName = `__sts_do_callback_${helperSuffix}`;
846
+ const bindTempPrefix = `__sts_do_effect_${helperSuffix}_`;
847
+ const loweredBody = lowerDoBlock(
848
+ callbackBody,
849
+ bindName,
850
+ monadBindingName,
851
+ bindHelperName,
852
+ helperSuffix,
853
+ bindTempPrefix,
854
+ isAsyncCallback,
855
+ (message) => ctx.error(message),
856
+ );
857
+ const sourceFile = callback.getSourceFile();
858
+ const monadText = ctx.semantics.argExpanded(0)?.text() ?? signature.args.monad.text();
859
+ const callbackBodyText = printNode(callbackBody, sourceFile);
860
+ const monadTypeText = `import(${
861
+ JSON.stringify(HKT_MODULE_SPECIFIER)
862
+ }).MonadTypeLambda<typeof ${monadBindingName}>`;
863
+ const callbackReturnTypeText = isAsyncCallback
864
+ ? `Awaited<ReturnType<typeof ${callbackBindingName}>>`
865
+ : `ReturnType<typeof ${callbackBindingName}>`;
866
+ const loweredText = [
867
+ '(() => {',
868
+ ` const ${monadBindingName} = ${monadText};`,
869
+ ` const ${bindHelperName} = ${ctx.name}.${DO_BIND_HELPER_PROPERTY}<${monadTypeText}>(${monadBindingName});`,
870
+ ` const ${callbackBindingName} = ${isAsyncCallback ? 'async ' : ''}(bind: import(${
871
+ JSON.stringify(HKT_MODULE_SPECIFIER)
872
+ }).Binder<${monadTypeText}>) => ${callbackBodyText};`,
873
+ ` return ${ctx.name}.${DO_HELPER_PROPERTY}<`,
874
+ ` ${monadTypeText},`,
875
+ ` ${callbackReturnTypeText}`,
876
+ ' >(',
877
+ ` ${monadBindingName},`,
878
+ ` () => (function* (): Generator<`,
879
+ ` import(${JSON.stringify(HKT_MODULE_SPECIFIER)}).Kind<`,
880
+ ` ${monadTypeText},`,
881
+ ' unknown',
882
+ ' >,',
883
+ ` ${callbackReturnTypeText},`,
884
+ ' unknown',
885
+ ` > ${printNode(loweredBody, sourceFile)}).call(this),`,
886
+ ' );',
887
+ '})()',
888
+ ].join('\n');
889
+
890
+ return ctx.output.expr(
891
+ createExprSyntaxFromText(ctx.invocationSpan().fileName, ctx.invocationSpan(), loweredText),
892
+ );
893
+ },
894
+ signature: DO_SIGNATURE,
895
+ };
896
+ }
897
+ attachHktFactory(Do, 'call');