@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,1502 @@
1
+ import ts from 'typescript';
2
+
3
+ import { ERROR_STDLIB_DECLARATION_FILE } from './error_stdlib_support.ts';
4
+ import { RESULT_STDLIB_DECLARATION_FILE, STDLIB_DECLARATION_FILE } from './std_package_support.ts';
5
+ import { toSourceFileName } from './project_frontend.ts';
6
+ import type {
7
+ CanonicalFailureInfo,
8
+ MacroDependencyReference,
9
+ MacroDependencySet,
10
+ CanonicalResultCarrierInfo,
11
+ CanonicalResultInfo,
12
+ MacroFiniteCase,
13
+ MacroFunctionContext,
14
+ MacroRuntimeKind,
15
+ MacroTryCarrierInfo,
16
+ MacroType,
17
+ } from './macro_semantic_types.ts';
18
+ import type { SourceSpan } from './macro_types.ts';
19
+
20
+ const INTERNAL_TS_TYPE = Symbol('macroTsType');
21
+ const INTERNAL_TS_CHECKER = Symbol('macroTsChecker');
22
+
23
+ type InternalMacroType = MacroType & {
24
+ [INTERNAL_TS_CHECKER]: ts.TypeChecker;
25
+ [INTERNAL_TS_TYPE]: ts.Type;
26
+ };
27
+
28
+ const MAX_FINITE_CASE_COMBINATIONS = 64;
29
+ const BUILTIN_RUNTIME_CONSTRUCTOR_NAMES = new Set([
30
+ 'AggregateError',
31
+ 'ArrayBuffer',
32
+ 'BigInt64Array',
33
+ 'BigUint64Array',
34
+ 'DataView',
35
+ 'Date',
36
+ 'Error',
37
+ 'EvalError',
38
+ 'Float32Array',
39
+ 'Float64Array',
40
+ 'Int16Array',
41
+ 'Int32Array',
42
+ 'Int8Array',
43
+ 'Map',
44
+ 'Promise',
45
+ 'RangeError',
46
+ 'ReferenceError',
47
+ 'RegExp',
48
+ 'Set',
49
+ 'SharedArrayBuffer',
50
+ 'SyntaxError',
51
+ 'TypeError',
52
+ 'URIError',
53
+ 'URL',
54
+ 'URLSearchParams',
55
+ 'Uint16Array',
56
+ 'Uint32Array',
57
+ 'Uint8Array',
58
+ 'Uint8ClampedArray',
59
+ 'WeakMap',
60
+ 'WeakSet',
61
+ ]);
62
+
63
+ export interface MacroSemantics {
64
+ awaitedType(type: MacroType): MacroType;
65
+ canonicalResultOfEnclosingFunctionNode(node: ts.Node): CanonicalResultInfo | undefined;
66
+ classDeclarationOfTypeNode(node: ts.TypeNode): ts.ClassDeclaration | null;
67
+ classifyCanonicalFailureType(type: MacroType): CanonicalFailureInfo | null;
68
+ classifyCanonicalResultCarrierType(type: MacroType): CanonicalResultCarrierInfo | null;
69
+ classifyCanonicalResultType(type: MacroType): CanonicalResultInfo | null;
70
+ classifyTryCarrierType(type: MacroType): MacroTryCarrierInfo | null;
71
+ enclosingFunctionOfNode(node: ts.Node): MacroFunctionContext | undefined;
72
+ finiteCases(type: MacroType): readonly MacroFiniteCase[] | null;
73
+ isAssignable(from: MacroType, to: MacroType): boolean;
74
+ nullType(): MacroType;
75
+ readSetOfNode(node: ts.Node): MacroDependencySet;
76
+ typeOfNode(node: ts.Node): MacroType;
77
+ undefinedType(): MacroType;
78
+ valueBindingCallableInScope(name: string, node: ts.Node): boolean;
79
+ valueBindingInScope(name: string, node: ts.Node): boolean;
80
+ writeSetOfNode(node: ts.Node): MacroDependencySet;
81
+ }
82
+
83
+ function createSourceSpan(node: ts.Node): SourceSpan {
84
+ const sourceFile = node.getSourceFile();
85
+ return {
86
+ fileName: sourceFile.fileName,
87
+ start: node.getStart(sourceFile),
88
+ end: node.getEnd(),
89
+ };
90
+ }
91
+
92
+ function createMacroType(checker: ts.TypeChecker, type: ts.Type): InternalMacroType {
93
+ return {
94
+ [INTERNAL_TS_CHECKER]: checker,
95
+ [INTERNAL_TS_TYPE]: type,
96
+ displayText: checker.typeToString(type),
97
+ };
98
+ }
99
+
100
+ function getInternalType(type: MacroType): ts.Type {
101
+ return (type as InternalMacroType)[INTERNAL_TS_TYPE];
102
+ }
103
+
104
+ function getInternalChecker(type: MacroType): ts.TypeChecker {
105
+ return (type as InternalMacroType)[INTERNAL_TS_CHECKER];
106
+ }
107
+
108
+ function getNodeType(checker: ts.TypeChecker, node: ts.Node): ts.Type {
109
+ return ts.isTypeNode(node) ? checker.getTypeFromTypeNode(node) : checker.getTypeAtLocation(node);
110
+ }
111
+
112
+ function getFunctionReturnType(
113
+ checker: ts.TypeChecker,
114
+ node: ts.Node,
115
+ ): ts.Type | undefined {
116
+ if (
117
+ !ts.isFunctionLike(node) ||
118
+ node.kind === ts.SyntaxKind.Constructor
119
+ ) {
120
+ return undefined;
121
+ }
122
+
123
+ const signature = checker.getSignatureFromDeclaration(node);
124
+ return signature ? checker.getReturnTypeOfSignature(signature) : undefined;
125
+ }
126
+
127
+ function isAsyncFunctionLike(node: ts.Node): boolean {
128
+ return ts.canHaveModifiers(node) &&
129
+ ts.getModifiers(node)?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword) ===
130
+ true;
131
+ }
132
+
133
+ function isGeneratorFunctionLike(node: ts.Node): boolean {
134
+ return 'asteriskToken' in node && !!node.asteriskToken;
135
+ }
136
+
137
+ function getDeclarationNameText(name: ts.DeclarationName | undefined): string | undefined {
138
+ if (!name) {
139
+ return undefined;
140
+ }
141
+
142
+ if (ts.isIdentifier(name) || ts.isStringLiteralLike(name) || ts.isNumericLiteral(name)) {
143
+ return name.text;
144
+ }
145
+
146
+ return undefined;
147
+ }
148
+
149
+ function getFunctionLikeName(node: ts.Node): string | undefined {
150
+ if (
151
+ ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node) || ts.isMethodDeclaration(node)
152
+ ) {
153
+ const directName = getDeclarationNameText(node.name);
154
+ if (directName) {
155
+ return directName;
156
+ }
157
+ }
158
+
159
+ if (ts.isArrowFunction(node) || ts.isFunctionExpression(node)) {
160
+ const parent = node.parent;
161
+ if (parent && ts.isVariableDeclaration(parent) && ts.isIdentifier(parent.name)) {
162
+ return parent.name.text;
163
+ }
164
+ if (parent && ts.isPropertyAssignment(parent) && ts.isIdentifier(parent.name)) {
165
+ return parent.name.text;
166
+ }
167
+ }
168
+
169
+ return undefined;
170
+ }
171
+
172
+ function getTypeSymbol(type: ts.Type): ts.Symbol | undefined {
173
+ return type.aliasSymbol ?? type.getSymbol();
174
+ }
175
+
176
+ function resolveClassDeclarationForTypeNode(
177
+ checker: ts.TypeChecker,
178
+ node: ts.TypeNode,
179
+ ): ts.ClassDeclaration | null {
180
+ const type = checker.getTypeFromTypeNode(node);
181
+ const symbol = (() => {
182
+ if (ts.isTypeReferenceNode(node)) {
183
+ return checker.getSymbolAtLocation(node.typeName) ?? getTypeSymbol(type);
184
+ }
185
+ return getTypeSymbol(type);
186
+ })();
187
+ if (!symbol) {
188
+ return null;
189
+ }
190
+ return resolveAliasedSymbol(checker, symbol).declarations?.find(ts.isClassDeclaration) ?? null;
191
+ }
192
+
193
+ function resolveAliasedSymbol(checker: ts.TypeChecker, symbol: ts.Symbol): ts.Symbol {
194
+ return (symbol.flags & ts.SymbolFlags.Alias) !== 0 ? checker.getAliasedSymbol(symbol) : symbol;
195
+ }
196
+
197
+ function symbolHasValueMeaning(checker: ts.TypeChecker, symbol: ts.Symbol): boolean {
198
+ return (resolveAliasedSymbol(checker, symbol).flags & ts.SymbolFlags.Value) !== 0;
199
+ }
200
+
201
+ function getValuePathBindingInScope(
202
+ checker: ts.TypeChecker,
203
+ node: ts.Node,
204
+ name: string,
205
+ ): { readonly fromAlias: boolean; readonly symbol: ts.Symbol; readonly type: ts.Type } | null {
206
+ const segments = name.split('.').map((segment) => segment.trim()).filter((segment) =>
207
+ segment.length > 0
208
+ );
209
+ const [root, ...rest] = segments;
210
+ if (!root) {
211
+ return null;
212
+ }
213
+
214
+ const rootSymbol = checker.getSymbolsInScope(node, ts.SymbolFlags.Value | ts.SymbolFlags.Alias)
215
+ .find((symbol) => symbol.getName() === root && symbolHasValueMeaning(checker, symbol));
216
+ if (!rootSymbol) {
217
+ return null;
218
+ }
219
+
220
+ const fromAlias = (rootSymbol.flags & ts.SymbolFlags.Alias) !== 0;
221
+ let currentType = checker.getTypeOfSymbolAtLocation(rootSymbol, node);
222
+ let currentSymbol = resolveAliasedSymbol(checker, rootSymbol);
223
+ for (const segment of rest) {
224
+ const property = checker.getPropertyOfType(currentType, segment);
225
+ if (!property) {
226
+ return null;
227
+ }
228
+ currentSymbol = property;
229
+ currentType = checker.getTypeOfSymbolAtLocation(property, node);
230
+ }
231
+
232
+ return {
233
+ fromAlias,
234
+ symbol: resolveAliasedSymbol(checker, currentSymbol),
235
+ type: currentType,
236
+ };
237
+ }
238
+
239
+ function declarationIsCallable(declaration: ts.Declaration): boolean {
240
+ return ts.isFunctionDeclaration(declaration) ||
241
+ ts.isMethodDeclaration(declaration) ||
242
+ ts.isMethodSignature(declaration) ||
243
+ ts.isCallSignatureDeclaration(declaration) ||
244
+ (ts.isVariableDeclaration(declaration) &&
245
+ declaration.initializer !== undefined &&
246
+ (ts.isArrowFunction(declaration.initializer) ||
247
+ ts.isFunctionExpression(declaration.initializer)));
248
+ }
249
+
250
+ function unwrapDependencyNode(node: ts.Node): ts.Node {
251
+ let current = node;
252
+ while (
253
+ ts.isParenthesizedExpression(current) ||
254
+ ts.isAsExpression(current) ||
255
+ ts.isSatisfiesExpression(current) ||
256
+ ts.isNonNullExpression(current) ||
257
+ ts.isPartiallyEmittedExpression(current) ||
258
+ ts.isTypeAssertionExpression(current)
259
+ ) {
260
+ current = current.expression;
261
+ }
262
+ return current;
263
+ }
264
+
265
+ function isAssignmentOperatorToken(kind: ts.SyntaxKind): boolean {
266
+ return kind >= ts.SyntaxKind.FirstAssignment && kind <= ts.SyntaxKind.LastAssignment;
267
+ }
268
+
269
+ function isCompoundAssignmentOperatorToken(kind: ts.SyntaxKind): boolean {
270
+ return kind !== ts.SyntaxKind.EqualsToken && isAssignmentOperatorToken(kind);
271
+ }
272
+
273
+ function isDeclarationNameIdentifier(node: ts.Identifier): boolean {
274
+ const parent = node.parent;
275
+ if (!parent) {
276
+ return false;
277
+ }
278
+ return (
279
+ (ts.isVariableDeclaration(parent) && parent.name === node) ||
280
+ (ts.isParameter(parent) && parent.name === node) ||
281
+ (ts.isBindingElement(parent) && parent.name === node) ||
282
+ (ts.isFunctionDeclaration(parent) && parent.name === node) ||
283
+ (ts.isFunctionExpression(parent) && parent.name === node) ||
284
+ (ts.isClassDeclaration(parent) && parent.name === node) ||
285
+ (ts.isClassExpression(parent) && parent.name === node) ||
286
+ (ts.isMethodDeclaration(parent) && parent.name === node) ||
287
+ (ts.isPropertyDeclaration(parent) && parent.name === node) ||
288
+ (ts.isPropertySignature(parent) && parent.name === node) ||
289
+ (ts.isMethodSignature(parent) && parent.name === node) ||
290
+ (ts.isPropertyAssignment(parent) && parent.name === node) ||
291
+ (ts.isShorthandPropertyAssignment(parent) && parent.name === node && !!parent.objectAssignmentInitializer) ||
292
+ (ts.isEnumMember(parent) && parent.name === node) ||
293
+ (ts.isTypeAliasDeclaration(parent) && parent.name === node) ||
294
+ (ts.isInterfaceDeclaration(parent) && parent.name === node) ||
295
+ (ts.isTypeParameterDeclaration(parent) && parent.name === node) ||
296
+ (ts.isImportSpecifier(parent) && (parent.name === node || parent.propertyName === node)) ||
297
+ (ts.isImportClause(parent) && parent.name === node) ||
298
+ (ts.isNamespaceImport(parent) && parent.name === node) ||
299
+ (ts.isImportEqualsDeclaration(parent) && parent.name === node) ||
300
+ (ts.isLabeledStatement(parent) && parent.label === node) ||
301
+ (ts.isBreakOrContinueStatement(parent) && parent.label === node) ||
302
+ (ts.isJsxAttribute(parent) && parent.name === node) ||
303
+ (ts.isJsxOpeningElement(parent) && parent.tagName === node) ||
304
+ (ts.isJsxSelfClosingElement(parent) && parent.tagName === node) ||
305
+ (ts.isJsxClosingElement(parent) && parent.tagName === node)
306
+ );
307
+ }
308
+
309
+ function isReferenceIdentifier(node: ts.Identifier): boolean {
310
+ const parent = node.parent;
311
+ if (!parent) {
312
+ return true;
313
+ }
314
+ if (isDeclarationNameIdentifier(node)) {
315
+ return false;
316
+ }
317
+ if (ts.isPropertyAccessExpression(parent) && parent.name === node) {
318
+ return false;
319
+ }
320
+ if (ts.isQualifiedName(parent)) {
321
+ return false;
322
+ }
323
+ if (ts.isTypeReferenceNode(parent) || ts.isExpressionWithTypeArguments(parent)) {
324
+ return false;
325
+ }
326
+ return true;
327
+ }
328
+
329
+ function collectDeclaredValueSymbols(
330
+ checker: ts.TypeChecker,
331
+ root: ts.Node,
332
+ ): { readonly names: ReadonlySet<string>; readonly symbols: ReadonlySet<ts.Symbol> } {
333
+ const names = new Set<string>();
334
+ const symbols = new Set<ts.Symbol>();
335
+
336
+ function addBindingName(name: ts.BindingName | ts.DeclarationName | undefined) {
337
+ if (!name) {
338
+ return;
339
+ }
340
+ if (ts.isIdentifier(name)) {
341
+ names.add(name.text);
342
+ const symbol = checker.getSymbolAtLocation(name);
343
+ if (symbol) {
344
+ symbols.add(resolveAliasedSymbol(checker, symbol));
345
+ }
346
+ return;
347
+ }
348
+ if (ts.isObjectBindingPattern(name) || ts.isArrayBindingPattern(name)) {
349
+ for (const element of name.elements) {
350
+ if (ts.isOmittedExpression(element)) {
351
+ continue;
352
+ }
353
+ addBindingName(element.name);
354
+ }
355
+ }
356
+ }
357
+
358
+ function visit(node: ts.Node) {
359
+ if (ts.isVariableDeclaration(node)) {
360
+ addBindingName(node.name);
361
+ } else if (ts.isParameter(node)) {
362
+ addBindingName(node.name);
363
+ } else if (ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node)) {
364
+ addBindingName(node.name);
365
+ } else if (ts.isClassDeclaration(node) || ts.isClassExpression(node)) {
366
+ addBindingName(node.name);
367
+ } else if (ts.isImportClause(node)) {
368
+ addBindingName(node.name);
369
+ } else if (ts.isNamespaceImport(node)) {
370
+ addBindingName(node.name);
371
+ } else if (ts.isImportSpecifier(node)) {
372
+ addBindingName(node.name);
373
+ }
374
+
375
+ ts.forEachChild(node, visit);
376
+ }
377
+
378
+ visit(root);
379
+ return { names, symbols };
380
+ }
381
+
382
+ function createDependencySetBuilder() {
383
+ const dependencies = new Map<string, MacroDependencyReference>();
384
+ let unknown = false;
385
+
386
+ return {
387
+ add(kind: MacroDependencyReference['kind'], name: string) {
388
+ dependencies.set(`${kind}:${name}`, { kind, name });
389
+ },
390
+ markUnknown() {
391
+ unknown = true;
392
+ },
393
+ build(): MacroDependencySet {
394
+ return {
395
+ dependencies: [...dependencies.values()],
396
+ unknown,
397
+ };
398
+ },
399
+ };
400
+ }
401
+
402
+ function collectDependencySets(
403
+ checker: ts.TypeChecker,
404
+ root: ts.Node,
405
+ ): { readonly readSet: MacroDependencySet; readonly writeSet: MacroDependencySet } {
406
+ const localDeclarations = collectDeclaredValueSymbols(checker, root);
407
+ const rootSourceFile = root.getSourceFile();
408
+ const rootStart = root.getStart(rootSourceFile, false);
409
+ const rootEnd = root.end;
410
+ const reads = createDependencySetBuilder();
411
+ const writes = createDependencySetBuilder();
412
+
413
+ function symbolDeclaredWithinRoot(symbol: ts.Symbol): boolean {
414
+ return (resolveAliasedSymbol(checker, symbol).declarations ?? []).some((declaration) => {
415
+ const sourceFile = declaration.getSourceFile();
416
+ return sourceFile === rootSourceFile &&
417
+ declaration.getStart(sourceFile, false) >= rootStart &&
418
+ declaration.end <= rootEnd;
419
+ });
420
+ }
421
+
422
+ function addBinding(builder: ReturnType<typeof createDependencySetBuilder>, identifier: ts.Identifier) {
423
+ if (!isReferenceIdentifier(identifier)) {
424
+ return;
425
+ }
426
+ if (localDeclarations.names.has(identifier.text)) {
427
+ return;
428
+ }
429
+ const symbol = checker.getSymbolAtLocation(identifier);
430
+ if (symbol) {
431
+ const resolved = resolveAliasedSymbol(checker, symbol);
432
+ if (
433
+ localDeclarations.symbols.has(resolved) ||
434
+ symbolDeclaredWithinRoot(resolved)
435
+ ) {
436
+ return;
437
+ }
438
+ }
439
+ builder.add('binding', identifier.text);
440
+ }
441
+
442
+ function collectAccessRoot(
443
+ node: ts.Expression,
444
+ builder: ReturnType<typeof createDependencySetBuilder>,
445
+ includeDynamicReads: boolean,
446
+ ): void {
447
+ const current = unwrapDependencyNode(node);
448
+ if (ts.isIdentifier(current)) {
449
+ addBinding(builder, current);
450
+ return;
451
+ }
452
+ if (current.kind === ts.SyntaxKind.ThisKeyword) {
453
+ return;
454
+ }
455
+ if (ts.isPropertyAccessExpression(current)) {
456
+ const base = unwrapDependencyNode(current.expression);
457
+ if (base.kind === ts.SyntaxKind.ThisKeyword) {
458
+ builder.add('this-member', current.name.text);
459
+ return;
460
+ }
461
+ collectAccessRoot(current.expression, builder, includeDynamicReads);
462
+ return;
463
+ }
464
+ if (ts.isElementAccessExpression(current)) {
465
+ const base = unwrapDependencyNode(current.expression);
466
+ if (base.kind === ts.SyntaxKind.ThisKeyword) {
467
+ builder.markUnknown();
468
+ } else {
469
+ collectAccessRoot(current.expression, builder, includeDynamicReads);
470
+ builder.markUnknown();
471
+ }
472
+ if (includeDynamicReads && current.argumentExpression) {
473
+ collectReadDependencies(current.argumentExpression);
474
+ }
475
+ return;
476
+ }
477
+ collectReadDependencies(current);
478
+ }
479
+
480
+ function collectAssignmentTarget(node: ts.Node): void {
481
+ const current = unwrapDependencyNode(node);
482
+ if (ts.isIdentifier(current)) {
483
+ addBinding(writes, current);
484
+ return;
485
+ }
486
+ if (ts.isPropertyAccessExpression(current) || ts.isElementAccessExpression(current)) {
487
+ collectAccessRoot(current, writes, false);
488
+ if (ts.isElementAccessExpression(current)) {
489
+ writes.markUnknown();
490
+ }
491
+ return;
492
+ }
493
+ if (ts.isArrayLiteralExpression(current)) {
494
+ for (const element of current.elements) {
495
+ if (ts.isOmittedExpression(element)) {
496
+ continue;
497
+ }
498
+ if (ts.isSpreadElement(element)) {
499
+ collectAssignmentTarget(element.expression);
500
+ } else {
501
+ collectAssignmentTarget(element);
502
+ }
503
+ }
504
+ return;
505
+ }
506
+ if (ts.isObjectLiteralExpression(current)) {
507
+ for (const property of current.properties) {
508
+ if (ts.isShorthandPropertyAssignment(property)) {
509
+ collectAssignmentTarget(property.name);
510
+ } else if (ts.isPropertyAssignment(property)) {
511
+ collectAssignmentTarget(property.initializer);
512
+ } else if (ts.isSpreadAssignment(property)) {
513
+ collectAssignmentTarget(property.expression);
514
+ }
515
+ }
516
+ return;
517
+ }
518
+ writes.markUnknown();
519
+ }
520
+
521
+ function collectReadDependencies(node: ts.Node): void {
522
+ const current = unwrapDependencyNode(node);
523
+ if (ts.isIdentifier(current)) {
524
+ addBinding(reads, current);
525
+ return;
526
+ }
527
+ if (ts.isPropertyAccessExpression(current)) {
528
+ const parent = current.parent;
529
+ if (
530
+ (ts.isPropertyAccessExpression(parent) || ts.isElementAccessExpression(parent)) &&
531
+ parent.expression === current
532
+ ) {
533
+ return;
534
+ }
535
+ collectAccessRoot(current, reads, true);
536
+ return;
537
+ }
538
+ if (ts.isElementAccessExpression(current)) {
539
+ const parent = current.parent;
540
+ if (
541
+ (ts.isPropertyAccessExpression(parent) || ts.isElementAccessExpression(parent)) &&
542
+ parent.expression === current
543
+ ) {
544
+ return;
545
+ }
546
+ collectAccessRoot(current, reads, true);
547
+ return;
548
+ }
549
+ if (ts.isBinaryExpression(current) && isAssignmentOperatorToken(current.operatorToken.kind)) {
550
+ if (isCompoundAssignmentOperatorToken(current.operatorToken.kind)) {
551
+ collectAccessRoot(current.left as ts.Expression, reads, true);
552
+ }
553
+ collectAssignmentTarget(current.left);
554
+ collectReadDependencies(current.right);
555
+ return;
556
+ }
557
+ if (
558
+ ts.isPrefixUnaryExpression(current) || ts.isPostfixUnaryExpression(current)
559
+ ) {
560
+ if (
561
+ current.operator === ts.SyntaxKind.PlusPlusToken ||
562
+ current.operator === ts.SyntaxKind.MinusMinusToken
563
+ ) {
564
+ collectAccessRoot(current.operand, reads, true);
565
+ collectAssignmentTarget(current.operand);
566
+ return;
567
+ }
568
+ }
569
+ if (ts.isVariableDeclaration(current)) {
570
+ if (current.initializer) {
571
+ collectReadDependencies(current.initializer);
572
+ }
573
+ return;
574
+ }
575
+ if (ts.isParameter(current)) {
576
+ if (current.initializer) {
577
+ collectReadDependencies(current.initializer);
578
+ }
579
+ return;
580
+ }
581
+ if (ts.isPropertyAssignment(current)) {
582
+ collectReadDependencies(current.initializer);
583
+ return;
584
+ }
585
+ if (ts.isShorthandPropertyAssignment(current)) {
586
+ addBinding(reads, current.name);
587
+ if (current.objectAssignmentInitializer) {
588
+ collectReadDependencies(current.objectAssignmentInitializer);
589
+ }
590
+ return;
591
+ }
592
+ ts.forEachChild(current, collectReadDependencies);
593
+ }
594
+
595
+ function collectWriteDependencies(node: ts.Node): void {
596
+ const current = unwrapDependencyNode(node);
597
+ if (ts.isBinaryExpression(current) && isAssignmentOperatorToken(current.operatorToken.kind)) {
598
+ collectAssignmentTarget(current.left);
599
+ collectWriteDependencies(current.right);
600
+ return;
601
+ }
602
+ if (
603
+ ts.isPrefixUnaryExpression(current) || ts.isPostfixUnaryExpression(current)
604
+ ) {
605
+ if (
606
+ current.operator === ts.SyntaxKind.PlusPlusToken ||
607
+ current.operator === ts.SyntaxKind.MinusMinusToken
608
+ ) {
609
+ collectAssignmentTarget(current.operand);
610
+ return;
611
+ }
612
+ }
613
+ if (ts.isVariableDeclaration(current)) {
614
+ if (current.initializer) {
615
+ collectWriteDependencies(current.initializer);
616
+ }
617
+ return;
618
+ }
619
+ if (ts.isParameter(current)) {
620
+ if (current.initializer) {
621
+ collectWriteDependencies(current.initializer);
622
+ }
623
+ return;
624
+ }
625
+ ts.forEachChild(current, collectWriteDependencies);
626
+ }
627
+
628
+ collectReadDependencies(root);
629
+ collectWriteDependencies(root);
630
+ return {
631
+ readSet: reads.build(),
632
+ writeSet: writes.build(),
633
+ };
634
+ }
635
+
636
+ function normalizeFileNameForComparison(fileName: string): string {
637
+ return fileName.replaceAll('\\', '/');
638
+ }
639
+
640
+ function isCheckedInResultStdlibFile(fileName: string): boolean {
641
+ return fileName.endsWith('/src/stdlib/result.d.ts') ||
642
+ fileName.endsWith('/src/stdlib/result.ts') ||
643
+ fileName.endsWith('/src/stdlib/index.d.ts') ||
644
+ fileName.endsWith('/src/stdlib/index.ts');
645
+ }
646
+
647
+ function isInstalledResultStdlibFile(fileName: string): boolean {
648
+ return fileName.endsWith('/node_modules/@soundscript/soundscript/result.d.ts') ||
649
+ fileName.endsWith('/node_modules/@soundscript/soundscript/result.js') ||
650
+ fileName.endsWith('/node_modules/@soundscript/soundscript/index.d.ts') ||
651
+ fileName.endsWith('/node_modules/@soundscript/soundscript/index.js');
652
+ }
653
+
654
+ function isCheckedInErrorStdlibFile(fileName: string): boolean {
655
+ return fileName.endsWith('/src/stdlib/failures.d.ts') ||
656
+ fileName.endsWith('/src/stdlib/failures.ts') ||
657
+ fileName.endsWith('/src/stdlib/index.d.ts') ||
658
+ fileName.endsWith('/src/stdlib/index.ts');
659
+ }
660
+
661
+ function isInstalledErrorStdlibFile(fileName: string): boolean {
662
+ return fileName.endsWith('/node_modules/@soundscript/soundscript/failures.d.ts') ||
663
+ fileName.endsWith('/node_modules/@soundscript/soundscript/failures.js') ||
664
+ fileName.endsWith('/node_modules/@soundscript/soundscript/index.d.ts') ||
665
+ fileName.endsWith('/node_modules/@soundscript/soundscript/index.js');
666
+ }
667
+
668
+ function isCheckedInRootStdlibFile(fileName: string): boolean {
669
+ return fileName.endsWith('/src/stdlib/index.d.ts') || fileName.endsWith('/src/stdlib/index.ts');
670
+ }
671
+
672
+ function isInstalledRootStdlibFile(fileName: string): boolean {
673
+ return fileName.endsWith('/node_modules/@soundscript/soundscript/index.d.ts') ||
674
+ fileName.endsWith('/node_modules/@soundscript/soundscript/index.js');
675
+ }
676
+
677
+ function symbolIsOwnedByResultStdlibModule(
678
+ checker: ts.TypeChecker,
679
+ symbol: ts.Symbol,
680
+ exportName: string,
681
+ ): boolean {
682
+ const resolved = resolveAliasedSymbol(checker, symbol);
683
+ if (resolved.getName() !== exportName) {
684
+ return false;
685
+ }
686
+
687
+ return (resolved.declarations ?? []).some((declaration) => {
688
+ const fileName = normalizeFileNameForComparison(declaration.getSourceFile().fileName);
689
+ return fileName === RESULT_STDLIB_DECLARATION_FILE ||
690
+ fileName === STDLIB_DECLARATION_FILE ||
691
+ isCheckedInResultStdlibFile(fileName) ||
692
+ isInstalledResultStdlibFile(fileName) ||
693
+ isCheckedInRootStdlibFile(fileName) ||
694
+ isInstalledRootStdlibFile(fileName);
695
+ });
696
+ }
697
+
698
+ function symbolIsOwnedByErrorStdlibModule(
699
+ checker: ts.TypeChecker,
700
+ symbol: ts.Symbol,
701
+ exportName: string,
702
+ ): boolean {
703
+ const resolved = resolveAliasedSymbol(checker, symbol);
704
+ if (resolved.getName() !== exportName) {
705
+ return false;
706
+ }
707
+
708
+ return (resolved.declarations ?? []).some((declaration) => {
709
+ const fileName = normalizeFileNameForComparison(declaration.getSourceFile().fileName);
710
+ return fileName === ERROR_STDLIB_DECLARATION_FILE ||
711
+ fileName === STDLIB_DECLARATION_FILE ||
712
+ isCheckedInErrorStdlibFile(fileName) ||
713
+ isInstalledErrorStdlibFile(fileName);
714
+ });
715
+ }
716
+
717
+ function getBaseTypes(checker: ts.TypeChecker, type: ts.Type): readonly ts.Type[] {
718
+ if ((type.flags & ts.TypeFlags.Object) === 0) {
719
+ return [];
720
+ }
721
+
722
+ const objectType = type as ts.ObjectType;
723
+ if (
724
+ (objectType.objectFlags & (ts.ObjectFlags.Class | ts.ObjectFlags.Interface |
725
+ ts.ObjectFlags.Reference)) === 0
726
+ ) {
727
+ return [];
728
+ }
729
+
730
+ return checker.getBaseTypes(objectType as ts.InterfaceType);
731
+ }
732
+
733
+ function typeExtendsCanonicalFailure(
734
+ checker: ts.TypeChecker,
735
+ type: ts.Type,
736
+ seen = new Set<ts.Type>(),
737
+ ): boolean {
738
+ if (seen.has(type)) {
739
+ return false;
740
+ }
741
+ seen.add(type);
742
+
743
+ const symbol = getTypeSymbol(type);
744
+ if (symbol && symbolIsOwnedByErrorStdlibModule(checker, symbol, 'Failure')) {
745
+ return true;
746
+ }
747
+
748
+ if ((type.flags & ts.TypeFlags.Union) !== 0) {
749
+ return (type as ts.UnionType).types.every((member) =>
750
+ typeExtendsCanonicalFailure(checker, member, seen)
751
+ );
752
+ }
753
+
754
+ if ((type.flags & ts.TypeFlags.Intersection) !== 0) {
755
+ return (type as ts.IntersectionType).types.some((member) =>
756
+ typeExtendsCanonicalFailure(checker, member, seen)
757
+ );
758
+ }
759
+
760
+ return getBaseTypes(checker, type).some((baseType) =>
761
+ typeExtendsCanonicalFailure(checker, baseType, seen)
762
+ );
763
+ }
764
+
765
+ function getTypeArguments(checker: ts.TypeChecker, type: ts.Type): readonly ts.Type[] {
766
+ if (type.aliasTypeArguments && type.aliasTypeArguments.length > 0) {
767
+ return type.aliasTypeArguments;
768
+ }
769
+
770
+ return checker.getTypeArguments(type as ts.TypeReference);
771
+ }
772
+
773
+ function getLiteralCodeForType(checker: ts.TypeChecker, type: ts.Type): string | null {
774
+ if ((type.flags & ts.TypeFlags.StringLiteral) !== 0) {
775
+ return JSON.stringify((type as ts.StringLiteralType).value);
776
+ }
777
+
778
+ if ((type.flags & ts.TypeFlags.NumberLiteral) !== 0) {
779
+ return String((type as ts.NumberLiteralType).value);
780
+ }
781
+
782
+ if ((type.flags & ts.TypeFlags.BooleanLiteral) !== 0) {
783
+ return checker.typeToString(type);
784
+ }
785
+
786
+ if ((type.flags & ts.TypeFlags.Null) !== 0) {
787
+ return 'null';
788
+ }
789
+
790
+ return null;
791
+ }
792
+
793
+ function isClassInstanceType(checker: ts.TypeChecker, type: ts.Type): string | null {
794
+ const symbol = getTypeSymbol(type);
795
+ if (!symbol) {
796
+ return null;
797
+ }
798
+
799
+ const resolved = resolveAliasedSymbol(checker, symbol);
800
+ const declaration = resolved.declarations?.find((candidate) => ts.isClassLike(candidate));
801
+ if (!declaration) {
802
+ return null;
803
+ }
804
+
805
+ return resolved.getName();
806
+ }
807
+
808
+ function builtinRuntimeConstructorName(checker: ts.TypeChecker, type: ts.Type): string | null {
809
+ const displayText = checker.typeToString(type);
810
+ const baseName = displayText.match(/^[A-Za-z_$][A-Za-z0-9_$]*/)?.[0] ?? null;
811
+ if (!baseName || !BUILTIN_RUNTIME_CONSTRUCTOR_NAMES.has(baseName)) {
812
+ return null;
813
+ }
814
+
815
+ return baseName;
816
+ }
817
+
818
+ function combineFiniteCaseOptions(
819
+ optionSets: readonly (readonly (MacroFiniteCase | null)[])[],
820
+ ): readonly (readonly (MacroFiniteCase | null)[])[] | null {
821
+ let combinations: (MacroFiniteCase | null)[][] = [[]];
822
+
823
+ for (const optionSet of optionSets) {
824
+ const next: (MacroFiniteCase | null)[][] = [];
825
+ for (const combination of combinations) {
826
+ for (const option of optionSet) {
827
+ next.push([...combination, option]);
828
+ if (next.length > MAX_FINITE_CASE_COMBINATIONS) {
829
+ return null;
830
+ }
831
+ }
832
+ }
833
+ combinations = next;
834
+ }
835
+
836
+ return combinations;
837
+ }
838
+
839
+ function getSingleFiniteTupleCase(
840
+ checker: ts.TypeChecker,
841
+ type: ts.Type,
842
+ visiting: Set<ts.Type>,
843
+ ): MacroFiniteCase | null {
844
+ if (!checker.isTupleType(type) || (type.flags & ts.TypeFlags.Object) === 0) {
845
+ return null;
846
+ }
847
+
848
+ const tupleType = type as ts.TypeReference;
849
+ const tupleTarget = tupleType.target as ts.TupleType;
850
+ if (
851
+ (tupleTarget.combinedFlags & ts.ElementFlags.Variable) ||
852
+ tupleTarget.minLength !== tupleTarget.fixedLength
853
+ ) {
854
+ return null;
855
+ }
856
+ if (visiting.has(type)) {
857
+ return null;
858
+ }
859
+
860
+ visiting.add(type);
861
+ try {
862
+ const elements = checker.getTypeArguments(tupleType).map((elementType) => ({
863
+ finiteCase: getSingleFiniteCaseForTsType(checker, elementType, visiting),
864
+ }));
865
+ return {
866
+ elements,
867
+ exactLength: tupleTarget.fixedLength,
868
+ kind: 'array',
869
+ };
870
+ } finally {
871
+ visiting.delete(type);
872
+ }
873
+ }
874
+
875
+ function getFiniteTupleCases(
876
+ checker: ts.TypeChecker,
877
+ type: ts.Type,
878
+ visiting: Set<ts.Type>,
879
+ ): readonly MacroFiniteCase[] | null {
880
+ if (!checker.isTupleType(type) || (type.flags & ts.TypeFlags.Object) === 0) {
881
+ return null;
882
+ }
883
+
884
+ const tupleType = type as ts.TypeReference;
885
+ const tupleTarget = tupleType.target as ts.TupleType;
886
+ if (tupleTarget.combinedFlags & ts.ElementFlags.Variable) {
887
+ return null;
888
+ }
889
+ if (visiting.has(type)) {
890
+ return null;
891
+ }
892
+
893
+ visiting.add(type);
894
+ try {
895
+ const elementTypes = checker.getTypeArguments(tupleType);
896
+ const finiteCases: MacroFiniteCase[] = [];
897
+ for (let length = tupleTarget.minLength; length <= tupleTarget.fixedLength; length += 1) {
898
+ const elementOptions = elementTypes
899
+ .slice(0, length)
900
+ .map((elementType) =>
901
+ getFiniteCasesForTsTypeInternal(checker, elementType, visiting) ?? [null]
902
+ );
903
+ const elementCombinations = combineFiniteCaseOptions(elementOptions);
904
+ if (!elementCombinations) {
905
+ return null;
906
+ }
907
+ for (const elementCombination of elementCombinations) {
908
+ finiteCases.push({
909
+ elements: elementCombination.map((finiteCase) => ({ finiteCase })),
910
+ exactLength: length,
911
+ kind: 'array',
912
+ });
913
+ }
914
+ }
915
+ return finiteCases;
916
+ } finally {
917
+ visiting.delete(type);
918
+ }
919
+ }
920
+
921
+ function getFiniteObjectCases(
922
+ checker: ts.TypeChecker,
923
+ type: ts.Type,
924
+ visiting: Set<ts.Type>,
925
+ ): readonly MacroFiniteCase[] | null {
926
+ if (visiting.has(type)) {
927
+ return null;
928
+ }
929
+
930
+ visiting.add(type);
931
+ try {
932
+ const properties = checker.getPropertiesOfType(type);
933
+ if (properties.length === 0) {
934
+ return null;
935
+ }
936
+
937
+ const propertyOptions: (readonly (MacroFiniteCase | null)[])[] = [];
938
+ const propertyKeys: string[] = [];
939
+ let hasFiniteDetail = false;
940
+ for (const property of properties) {
941
+ if ((property.flags & ts.SymbolFlags.Optional) !== 0) {
942
+ return null;
943
+ }
944
+
945
+ const declaration = property.valueDeclaration ?? property.declarations?.[0];
946
+ if (!declaration) {
947
+ propertyKeys.push(property.getName());
948
+ propertyOptions.push([null]);
949
+ continue;
950
+ }
951
+ const propertyType = checker.getTypeOfSymbolAtLocation(property, declaration);
952
+ const finiteCases = getFiniteCasesForTsTypeInternal(checker, propertyType, visiting);
953
+ if (finiteCases !== null) {
954
+ hasFiniteDetail = true;
955
+ }
956
+ propertyKeys.push(property.getName());
957
+ propertyOptions.push(finiteCases ?? [null]);
958
+ }
959
+
960
+ if (!hasFiniteDetail) {
961
+ return null;
962
+ }
963
+
964
+ const propertyCombinations = combineFiniteCaseOptions(propertyOptions);
965
+ if (!propertyCombinations) {
966
+ return null;
967
+ }
968
+
969
+ return propertyCombinations.map((propertyCombination) => ({
970
+ kind: 'object',
971
+ properties: propertyCombination.map((finiteCase, index) => ({
972
+ finiteCase,
973
+ key: propertyKeys[index]!,
974
+ })),
975
+ }));
976
+ } finally {
977
+ visiting.delete(type);
978
+ }
979
+ }
980
+
981
+ function getRuntimeKindFiniteCase(
982
+ checker: ts.TypeChecker,
983
+ type: ts.Type,
984
+ ): Extract<MacroFiniteCase, { kind: 'runtime' }> | null {
985
+ const runtimeKind = getRuntimeKindForTsType(checker, type);
986
+ return runtimeKind ? { kind: 'runtime', typeName: runtimeKind } : null;
987
+ }
988
+
989
+ function getRuntimeKindForTsType(
990
+ checker: ts.TypeChecker,
991
+ type: ts.Type,
992
+ ): MacroRuntimeKind | null {
993
+ const aliasName = type.aliasSymbol?.getName();
994
+ if (
995
+ aliasName === 'f64' ||
996
+ aliasName === 'f32' ||
997
+ aliasName === 'i8' ||
998
+ aliasName === 'i16' ||
999
+ aliasName === 'i32' ||
1000
+ aliasName === 'i64' ||
1001
+ aliasName === 'u8' ||
1002
+ aliasName === 'u16' ||
1003
+ aliasName === 'u32' ||
1004
+ aliasName === 'u64'
1005
+ ) {
1006
+ return aliasName;
1007
+ }
1008
+
1009
+ if ((type.flags & (ts.TypeFlags.Undefined | ts.TypeFlags.Void)) !== 0) {
1010
+ return 'undefined';
1011
+ }
1012
+
1013
+ if ((type.flags & ts.TypeFlags.StringLike) !== 0) {
1014
+ return 'string';
1015
+ }
1016
+
1017
+ if ((type.flags & ts.TypeFlags.NumberLike) !== 0) {
1018
+ return 'number';
1019
+ }
1020
+
1021
+ if ((type.flags & ts.TypeFlags.BigIntLike) !== 0) {
1022
+ return 'bigint';
1023
+ }
1024
+
1025
+ if ((type.flags & ts.TypeFlags.ESSymbolLike) !== 0) {
1026
+ return 'symbol';
1027
+ }
1028
+
1029
+ if (checker.getSignaturesOfType(type, ts.SignatureKind.Call).length > 0) {
1030
+ return 'function';
1031
+ }
1032
+
1033
+ if ((type.flags & (ts.TypeFlags.Object | ts.TypeFlags.NonPrimitive)) !== 0) {
1034
+ return 'object';
1035
+ }
1036
+
1037
+ return null;
1038
+ }
1039
+
1040
+ function getSingleFiniteCaseForTsType(
1041
+ checker: ts.TypeChecker,
1042
+ type: ts.Type,
1043
+ visiting: Set<ts.Type> = new Set(),
1044
+ ): MacroFiniteCase | null {
1045
+ const literalCode = getLiteralCodeForType(checker, type);
1046
+ if (literalCode !== null) {
1047
+ return { kind: 'literal', code: literalCode };
1048
+ }
1049
+
1050
+ const className = isClassInstanceType(checker, type);
1051
+ if (className) {
1052
+ return { kind: 'class', className };
1053
+ }
1054
+
1055
+ const builtinClassName = builtinRuntimeConstructorName(checker, type);
1056
+ if (builtinClassName) {
1057
+ return { kind: 'class', className: builtinClassName };
1058
+ }
1059
+
1060
+ return getSingleFiniteTupleCase(checker, type, visiting) ??
1061
+ getRuntimeKindFiniteCase(checker, type);
1062
+ }
1063
+
1064
+ function getFiniteCasesForTsTypeInternal(
1065
+ checker: ts.TypeChecker,
1066
+ type: ts.Type,
1067
+ visiting: Set<ts.Type>,
1068
+ ): readonly MacroFiniteCase[] | null {
1069
+ if ((type.flags & ts.TypeFlags.Boolean) !== 0) {
1070
+ return [
1071
+ { kind: 'literal', code: 'true' },
1072
+ { kind: 'literal', code: 'false' },
1073
+ ];
1074
+ }
1075
+
1076
+ if (type.isUnion()) {
1077
+ const finiteCases: MacroFiniteCase[] = [];
1078
+ for (const member of type.types) {
1079
+ const memberCases = getFiniteCasesForTsTypeInternal(checker, member, visiting);
1080
+ if (!memberCases) {
1081
+ return null;
1082
+ }
1083
+ finiteCases.push(...memberCases);
1084
+ if (finiteCases.length > MAX_FINITE_CASE_COMBINATIONS) {
1085
+ return null;
1086
+ }
1087
+ }
1088
+ return finiteCases;
1089
+ }
1090
+
1091
+ const baseFiniteCase = getSingleFiniteCaseForTsType(checker, type, visiting);
1092
+ if (baseFiniteCase?.kind === 'literal' || baseFiniteCase?.kind === 'class') {
1093
+ return [baseFiniteCase];
1094
+ }
1095
+ if (baseFiniteCase?.kind === 'runtime' && baseFiniteCase.typeName !== 'object') {
1096
+ return [baseFiniteCase];
1097
+ }
1098
+
1099
+ const tupleCases = getFiniteTupleCases(checker, type, visiting);
1100
+ if (tupleCases) {
1101
+ return tupleCases;
1102
+ }
1103
+
1104
+ const objectCases = getFiniteObjectCases(checker, type, visiting);
1105
+ if (objectCases) {
1106
+ return objectCases;
1107
+ }
1108
+
1109
+ return baseFiniteCase ? [baseFiniteCase] : null;
1110
+ }
1111
+
1112
+ function getFiniteCasesForTsType(
1113
+ checker: ts.TypeChecker,
1114
+ type: ts.Type,
1115
+ ): readonly MacroFiniteCase[] | null {
1116
+ return getFiniteCasesForTsTypeInternal(checker, type, new Set());
1117
+ }
1118
+
1119
+ function classifyCanonicalResultTsType(
1120
+ checker: ts.TypeChecker,
1121
+ tsType: ts.Type,
1122
+ ): CanonicalResultInfo | null {
1123
+ const symbol = getTypeSymbol(tsType);
1124
+ if (!symbol) {
1125
+ return null;
1126
+ }
1127
+
1128
+ if (symbolIsOwnedByResultStdlibModule(checker, symbol, 'Result')) {
1129
+ const typeArguments = getTypeArguments(checker, tsType);
1130
+ if (typeArguments.length !== 2) {
1131
+ return null;
1132
+ }
1133
+
1134
+ const [okType, errType] = typeArguments;
1135
+ return {
1136
+ errType: createMacroType(checker, errType),
1137
+ family: 'result',
1138
+ okType: createMacroType(checker, okType),
1139
+ resultType: createMacroType(checker, tsType),
1140
+ };
1141
+ }
1142
+
1143
+ if (symbolIsOwnedByResultStdlibModule(checker, symbol, 'Option')) {
1144
+ const typeArguments = getTypeArguments(checker, tsType);
1145
+ if (typeArguments.length !== 1) {
1146
+ return null;
1147
+ }
1148
+
1149
+ const [okType] = typeArguments;
1150
+ const voidType = checker.getVoidType();
1151
+ return {
1152
+ errType: createMacroType(checker, voidType),
1153
+ family: 'option',
1154
+ okType: createMacroType(checker, okType),
1155
+ resultType: createMacroType(checker, tsType),
1156
+ };
1157
+ }
1158
+
1159
+ return null;
1160
+ }
1161
+
1162
+ function classifyCanonicalPromiseResultTsType(
1163
+ checker: ts.TypeChecker,
1164
+ tsType: ts.Type,
1165
+ ): CanonicalResultInfo | null {
1166
+ const symbol = getTypeSymbol(tsType);
1167
+ if (!symbol || resolveAliasedSymbol(checker, symbol).getName() !== 'Promise') {
1168
+ return null;
1169
+ }
1170
+
1171
+ const [promisedType] = getTypeArguments(checker, tsType);
1172
+ return promisedType ? classifyCanonicalResultTsType(checker, promisedType) : null;
1173
+ }
1174
+
1175
+ function mergeInferredResultSideTypes(
1176
+ checker: ts.TypeChecker,
1177
+ current: ts.Type,
1178
+ next: ts.Type,
1179
+ ): ts.Type | null {
1180
+ if ((current.flags & ts.TypeFlags.Never) !== 0) {
1181
+ return next;
1182
+ }
1183
+ if ((next.flags & ts.TypeFlags.Never) !== 0) {
1184
+ return current;
1185
+ }
1186
+ if (checker.isTypeAssignableTo(next, current)) {
1187
+ return current;
1188
+ }
1189
+ if (checker.isTypeAssignableTo(current, next)) {
1190
+ return next;
1191
+ }
1192
+ return null;
1193
+ }
1194
+
1195
+ function inferCanonicalResultTsTypeFromFunctionReturns(
1196
+ checker: ts.TypeChecker,
1197
+ node: ts.Node,
1198
+ ): CanonicalResultInfo | null {
1199
+ if (
1200
+ !ts.isFunctionLike(node) ||
1201
+ node.kind === ts.SyntaxKind.Constructor
1202
+ ) {
1203
+ return null;
1204
+ }
1205
+
1206
+ if (ts.isArrowFunction(node) && !ts.isBlock(node.body)) {
1207
+ return classifyCanonicalResultTsType(checker, checker.getTypeAtLocation(node.body));
1208
+ }
1209
+
1210
+ const body = 'body' in node ? node.body : undefined;
1211
+ if (!body || !ts.isBlock(body)) {
1212
+ return null;
1213
+ }
1214
+
1215
+ let inferred: CanonicalResultInfo | null = null;
1216
+ let sawReturn = false;
1217
+ let failed = false;
1218
+
1219
+ function visit(current: ts.Node) {
1220
+ if (failed) {
1221
+ return;
1222
+ }
1223
+ if (ts.isFunctionLike(current) && current !== node) {
1224
+ return;
1225
+ }
1226
+ if (ts.isReturnStatement(current)) {
1227
+ sawReturn = true;
1228
+ if (!current.expression) {
1229
+ failed = true;
1230
+ return;
1231
+ }
1232
+
1233
+ const info = classifyCanonicalResultTsType(
1234
+ checker,
1235
+ checker.getTypeAtLocation(current.expression),
1236
+ );
1237
+ if (!info) {
1238
+ failed = true;
1239
+ return;
1240
+ }
1241
+
1242
+ if (!inferred) {
1243
+ inferred = info;
1244
+ return;
1245
+ }
1246
+
1247
+ if (inferred.family !== info.family) {
1248
+ failed = true;
1249
+ return;
1250
+ }
1251
+
1252
+ const mergedOkType = mergeInferredResultSideTypes(
1253
+ checker,
1254
+ getInternalType(inferred.okType),
1255
+ getInternalType(info.okType),
1256
+ );
1257
+ const mergedErrType = mergeInferredResultSideTypes(
1258
+ checker,
1259
+ getInternalType(inferred.errType),
1260
+ getInternalType(info.errType),
1261
+ );
1262
+ if (!mergedOkType || !mergedErrType) {
1263
+ failed = true;
1264
+ return;
1265
+ }
1266
+
1267
+ inferred = {
1268
+ errType: createMacroType(checker, mergedErrType),
1269
+ family: inferred.family,
1270
+ okType: createMacroType(checker, mergedOkType),
1271
+ resultType: inferred.resultType,
1272
+ };
1273
+ return;
1274
+ }
1275
+
1276
+ ts.forEachChild(current, visit);
1277
+ }
1278
+
1279
+ visit(body);
1280
+ return failed || !sawReturn ? null : inferred;
1281
+ }
1282
+
1283
+ function classifyEffectiveFunctionResultTsType(
1284
+ checker: ts.TypeChecker,
1285
+ node: ts.Node,
1286
+ ): CanonicalResultInfo | null {
1287
+ if (
1288
+ !ts.isFunctionLike(node) ||
1289
+ node.kind === ts.SyntaxKind.Constructor
1290
+ ) {
1291
+ return null;
1292
+ }
1293
+
1294
+ const returnType = getFunctionReturnType(checker, node);
1295
+ if (returnType) {
1296
+ const signatureResult = isAsyncFunctionLike(node)
1297
+ ? classifyCanonicalPromiseResultTsType(checker, returnType)
1298
+ : classifyCanonicalResultTsType(checker, returnType);
1299
+ if (signatureResult || node.type) {
1300
+ return signatureResult;
1301
+ }
1302
+ }
1303
+
1304
+ return inferCanonicalResultTsTypeFromFunctionReturns(checker, node);
1305
+ }
1306
+
1307
+ function classifyTryCarrierTsType(
1308
+ checker: ts.TypeChecker,
1309
+ tsType: ts.Type,
1310
+ ): MacroTryCarrierInfo | null {
1311
+ const resultInfo = classifyCanonicalResultTsType(checker, tsType);
1312
+ if (resultInfo) {
1313
+ return {
1314
+ ...resultInfo,
1315
+ kind: 'result',
1316
+ };
1317
+ }
1318
+
1319
+ const hasNull = checker.isTypeAssignableTo(checker.getNullType(), tsType);
1320
+ const hasUndefined = checker.isTypeAssignableTo(checker.getUndefinedType(), tsType);
1321
+ if (!hasNull && !hasUndefined) {
1322
+ return null;
1323
+ }
1324
+
1325
+ const valueType = checker.getNonNullableType(tsType);
1326
+ if ((valueType.flags & ts.TypeFlags.Never) !== 0) {
1327
+ return null;
1328
+ }
1329
+ if (valueType === tsType) {
1330
+ return null;
1331
+ }
1332
+
1333
+ return {
1334
+ carrierType: createMacroType(checker, tsType),
1335
+ kind: 'nullish',
1336
+ nullishKinds: [
1337
+ ...(hasNull ? ['null' as const] : []),
1338
+ ...(hasUndefined ? ['undefined' as const] : []),
1339
+ ],
1340
+ valueType: createMacroType(checker, valueType),
1341
+ };
1342
+ }
1343
+
1344
+ export function createMacroSemantics(program: ts.Program): MacroSemantics {
1345
+ const checker = program.getTypeChecker();
1346
+
1347
+ return {
1348
+ awaitedType(type: MacroType): MacroType {
1349
+ const typeChecker = getInternalChecker(type);
1350
+ const tsType = getInternalType(type);
1351
+ return createMacroType(typeChecker, typeChecker.getAwaitedType(tsType) ?? tsType);
1352
+ },
1353
+
1354
+ classDeclarationOfTypeNode(node: ts.TypeNode): ts.ClassDeclaration | null {
1355
+ return resolveClassDeclarationForTypeNode(checker, node);
1356
+ },
1357
+
1358
+ canonicalResultOfEnclosingFunctionNode(node: ts.Node): CanonicalResultInfo | undefined {
1359
+ let current: ts.Node | undefined = node;
1360
+ while (current) {
1361
+ if (ts.isFunctionLike(current) && current.kind !== ts.SyntaxKind.Constructor) {
1362
+ return classifyEffectiveFunctionResultTsType(checker, current) ?? undefined;
1363
+ }
1364
+
1365
+ current = current.parent;
1366
+ }
1367
+
1368
+ return undefined;
1369
+ },
1370
+
1371
+ classifyCanonicalFailureType(type: MacroType): CanonicalFailureInfo | null {
1372
+ const tsType = getInternalType(type);
1373
+ if (!typeExtendsCanonicalFailure(checker, tsType)) {
1374
+ return null;
1375
+ }
1376
+ return {
1377
+ failureType: createMacroType(getInternalChecker(type), tsType),
1378
+ };
1379
+ },
1380
+
1381
+ classifyCanonicalResultCarrierType(type: MacroType): CanonicalResultCarrierInfo | null {
1382
+ const typeChecker = getInternalChecker(type);
1383
+ const tsType = getInternalType(type);
1384
+ const directResult = classifyCanonicalResultTsType(typeChecker, tsType);
1385
+ if (directResult) {
1386
+ return {
1387
+ ...directResult,
1388
+ requiresAwait: false,
1389
+ };
1390
+ }
1391
+
1392
+ const awaitedType = typeChecker.getAwaitedType(tsType);
1393
+ if (!awaitedType || awaitedType === tsType) {
1394
+ const promisedResult = classifyCanonicalPromiseResultTsType(typeChecker, tsType);
1395
+ return promisedResult
1396
+ ? {
1397
+ ...promisedResult,
1398
+ requiresAwait: true,
1399
+ }
1400
+ : null;
1401
+ }
1402
+
1403
+ const awaitedResult = classifyCanonicalResultTsType(typeChecker, awaitedType);
1404
+ if (!awaitedResult) {
1405
+ const promisedResult = classifyCanonicalPromiseResultTsType(typeChecker, tsType);
1406
+ return promisedResult
1407
+ ? {
1408
+ ...promisedResult,
1409
+ requiresAwait: true,
1410
+ }
1411
+ : null;
1412
+ }
1413
+
1414
+ return {
1415
+ ...awaitedResult,
1416
+ requiresAwait: true,
1417
+ };
1418
+ },
1419
+
1420
+ classifyCanonicalResultType(type: MacroType): CanonicalResultInfo | null {
1421
+ return classifyCanonicalResultTsType(getInternalChecker(type), getInternalType(type));
1422
+ },
1423
+
1424
+ classifyTryCarrierType(type: MacroType): MacroTryCarrierInfo | null {
1425
+ return classifyTryCarrierTsType(getInternalChecker(type), getInternalType(type));
1426
+ },
1427
+
1428
+ finiteCases(type: MacroType): readonly MacroFiniteCase[] | null {
1429
+ return getFiniteCasesForTsType(getInternalChecker(type), getInternalType(type));
1430
+ },
1431
+
1432
+ enclosingFunctionOfNode(node: ts.Node): MacroFunctionContext | undefined {
1433
+ let current: ts.Node | undefined = node;
1434
+ while (current) {
1435
+ if (ts.isFunctionLike(current) && current.kind !== ts.SyntaxKind.Constructor) {
1436
+ const returnType = getFunctionReturnType(checker, current);
1437
+ if (!returnType) {
1438
+ return undefined;
1439
+ }
1440
+
1441
+ return {
1442
+ fileName: toSourceFileName(current.getSourceFile().fileName),
1443
+ hasDeclaredReturnType: current.type !== undefined,
1444
+ isAsync: isAsyncFunctionLike(current),
1445
+ isGenerator: isGeneratorFunctionLike(current),
1446
+ name: getFunctionLikeName(current),
1447
+ returnType: createMacroType(checker, returnType),
1448
+ span: createSourceSpan(current),
1449
+ };
1450
+ }
1451
+
1452
+ current = current.parent;
1453
+ }
1454
+
1455
+ return undefined;
1456
+ },
1457
+
1458
+ isAssignable(from: MacroType, to: MacroType): boolean {
1459
+ const fromChecker = getInternalChecker(from);
1460
+ const toChecker = getInternalChecker(to);
1461
+ if (fromChecker === toChecker) {
1462
+ return fromChecker.isTypeAssignableTo(getInternalType(from), getInternalType(to));
1463
+ }
1464
+
1465
+ return checker.isTypeAssignableTo(getInternalType(from), getInternalType(to));
1466
+ },
1467
+
1468
+ nullType(): MacroType {
1469
+ return createMacroType(checker, checker.getNullType());
1470
+ },
1471
+
1472
+ readSetOfNode(node: ts.Node): MacroDependencySet {
1473
+ return collectDependencySets(checker, node).readSet;
1474
+ },
1475
+
1476
+ typeOfNode(node: ts.Node): MacroType {
1477
+ return createMacroType(checker, getNodeType(checker, node));
1478
+ },
1479
+
1480
+ undefinedType(): MacroType {
1481
+ return createMacroType(checker, checker.getUndefinedType());
1482
+ },
1483
+
1484
+ valueBindingCallableInScope(name: string, node: ts.Node): boolean {
1485
+ const binding = getValuePathBindingInScope(checker, node, name);
1486
+ if (!binding) {
1487
+ return false;
1488
+ }
1489
+ return checker.getSignaturesOfType(binding.type, ts.SignatureKind.Call).length > 0 ||
1490
+ (binding.fromAlias && (binding.type.flags & ts.TypeFlags.Any) !== 0) ||
1491
+ (binding.symbol.declarations ?? []).some(declarationIsCallable);
1492
+ },
1493
+
1494
+ valueBindingInScope(name: string, node: ts.Node): boolean {
1495
+ return getValuePathBindingInScope(checker, node, name) !== null;
1496
+ },
1497
+
1498
+ writeSetOfNode(node: ts.Node): MacroDependencySet {
1499
+ return collectDependencySets(checker, node).writeSet;
1500
+ },
1501
+ };
1502
+ }