@soundscript/soundscript 0.1.12 → 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,1276 @@
1
+ import ts from 'typescript';
2
+
3
+ import { SOUND_DIAGNOSTIC_CODES, SOUND_DIAGNOSTIC_MESSAGES } from '../engine/diagnostic_codes.ts';
4
+ import type { AnalysisContext } from '../engine/types.ts';
5
+ import { getNodeDiagnosticRange, type SoundDiagnostic } from '../diagnostics.ts';
6
+
7
+ const enum FieldInitializationState {
8
+ Uninitialized = 0,
9
+ MaybeInitialized = 1,
10
+ DefinitelyInitialized = 2,
11
+ }
12
+
13
+ const enum TrackedValueMask {
14
+ None = 0,
15
+ ThisAlias = 1 << 0,
16
+ CapturedThisClosure = 1 << 1,
17
+ }
18
+
19
+ type ClassLikeDeclaration = ts.ClassDeclaration | ts.ClassExpression;
20
+
21
+ interface ClassLifecycleInfo {
22
+ fieldNames: ReadonlySet<string>;
23
+ hasBaseClass: boolean;
24
+ }
25
+
26
+ interface LifecycleState {
27
+ readonly fieldStates: Map<string, FieldInitializationState>;
28
+ readonly trackedValues: Map<number, TrackedValueMask>;
29
+ }
30
+
31
+ type ReceiverKind = 'super' | 'this';
32
+
33
+ type ConstructionHazardKind =
34
+ | 'captured closure call'
35
+ | 'receiver accessor dispatch'
36
+ | 'receiver method dispatch'
37
+ | 'tracked this alias escape'
38
+ | 'tracked this argument escape'
39
+ | 'tracked this return'
40
+ | 'tracked this throw';
41
+
42
+ interface LifecycleDiagnosticInfo {
43
+ hazardKind: ConstructionHazardKind;
44
+ memberName?: string;
45
+ node: ts.Node;
46
+ receiverKind?: ReceiverKind;
47
+ }
48
+
49
+ interface FieldInitializationDiagnosticInfo {
50
+ accessKind: 'object destructuring' | 'this property access';
51
+ fieldName?: string;
52
+ node: ts.Node;
53
+ }
54
+
55
+ function createLifecycleExample(info: LifecycleDiagnosticInfo): string {
56
+ if (
57
+ info.hazardKind === 'receiver method dispatch' ||
58
+ info.hazardKind === 'receiver accessor dispatch'
59
+ ) {
60
+ return `Write fields directly during construction, then call \`${info.memberName ?? 'the member'}\` from a post-construction method or factory step instead of from the constructor.`;
61
+ }
62
+
63
+ return 'Finish initialization first, then pass `this` or code that captures it to other routines only after construction completes.';
64
+ }
65
+
66
+ function createLifecycleNote(info: LifecycleDiagnosticInfo): string {
67
+ if (
68
+ info.hazardKind === 'receiver method dispatch' ||
69
+ info.hazardKind === 'receiver accessor dispatch'
70
+ ) {
71
+ const receiver = info.receiverKind ?? 'this';
72
+ return `This constructor dispatches through \`${receiver}.${info.memberName ?? 'member'}\` before construction completes.`;
73
+ }
74
+
75
+ if (info.hazardKind === 'captured closure call') {
76
+ return 'This constructor calls a closure that captures `this` before construction completes.';
77
+ }
78
+
79
+ if (info.hazardKind === 'tracked this argument escape') {
80
+ return 'This constructor passes `this` or a value that captures it into another call before construction completes.';
81
+ }
82
+
83
+ if (info.hazardKind === 'tracked this return') {
84
+ return 'This constructor returns `this` or a value that captures it before construction completes.';
85
+ }
86
+
87
+ if (info.hazardKind === 'tracked this throw') {
88
+ return 'This constructor throws `this` or a value that captures it before construction completes.';
89
+ }
90
+
91
+ return 'This constructor stores or forwards `this` before construction completes.';
92
+ }
93
+
94
+ function createLifecycleDiagnostic(info: LifecycleDiagnosticInfo): SoundDiagnostic {
95
+ const example = createLifecycleExample(info);
96
+
97
+ return {
98
+ source: 'sound',
99
+ code: SOUND_DIAGNOSTIC_CODES.constructionLifecycleViolation,
100
+ category: 'error',
101
+ message: SOUND_DIAGNOSTIC_MESSAGES.constructionLifecycleViolation,
102
+ metadata: {
103
+ rule: 'construction_lifecycle_violation',
104
+ primarySymbol: info.memberName,
105
+ fixability: 'local_rewrite',
106
+ invariant:
107
+ 'Constructors and field initializers must finish establishing instance state before they dispatch through members or let `this` escape.',
108
+ replacementFamily: 'finish_initialization_before_dispatch',
109
+ evidence: [
110
+ { label: 'hazardKind', value: info.hazardKind },
111
+ ...(info.receiverKind ? [{ label: 'receiver', value: info.receiverKind }] : []),
112
+ ...(info.memberName ? [{ label: 'memberName', value: info.memberName }] : []),
113
+ ],
114
+ counterexample:
115
+ 'Dispatching through instance members before construction completes can observe partially initialized state or overridden subclass behavior.',
116
+ example,
117
+ },
118
+ notes: [
119
+ createLifecycleNote(info),
120
+ `Example: ${example}`,
121
+ ],
122
+ hint: 'Finish initialization before calling instance members or letting `this` escape.',
123
+ ...getNodeDiagnosticRange(info.node),
124
+ };
125
+ }
126
+
127
+ function createFieldInitializationDiagnostic(info: FieldInitializationDiagnosticInfo): SoundDiagnostic {
128
+ const fieldName = info.fieldName ?? 'this field';
129
+ const example =
130
+ `Assign \`${fieldName}\` on every path before reading it, or move the read after the initializing assignment.`;
131
+
132
+ return {
133
+ source: 'sound',
134
+ code: SOUND_DIAGNOSTIC_CODES.fieldReadBeforeInitialization,
135
+ category: 'error',
136
+ message: SOUND_DIAGNOSTIC_MESSAGES.fieldReadBeforeInitialization,
137
+ metadata: {
138
+ rule: 'field_read_before_initialization',
139
+ primarySymbol: info.fieldName,
140
+ fixability: 'local_rewrite',
141
+ invariant:
142
+ 'Instance fields must be definitely initialized on every path before reads during construction or field initialization.',
143
+ replacementFamily: 'initialize_before_read',
144
+ evidence: [
145
+ ...(info.fieldName ? [{ label: 'fieldName', value: info.fieldName }] : []),
146
+ { label: 'accessKind', value: info.accessKind },
147
+ ],
148
+ counterexample:
149
+ 'A read before definite initialization can observe an uninitialized field or depend on constructor ordering that soundscript cannot prove safe.',
150
+ example,
151
+ },
152
+ notes: [
153
+ `The read of \`${fieldName}\` can happen before that field is definitely initialized on every path.`,
154
+ `Example: ${example}`,
155
+ ],
156
+ hint: 'Initialize the field before reading it, or defer the read until after construction establishes the value.',
157
+ ...getNodeDiagnosticRange(info.node),
158
+ };
159
+ }
160
+
161
+ function cloneState(state: LifecycleState): LifecycleState {
162
+ return {
163
+ fieldStates: new Map(state.fieldStates),
164
+ trackedValues: new Map(state.trackedValues),
165
+ };
166
+ }
167
+
168
+ function mergeFieldState(
169
+ left: FieldInitializationState,
170
+ right: FieldInitializationState,
171
+ ): FieldInitializationState {
172
+ if (left === right) {
173
+ return left;
174
+ }
175
+
176
+ if (
177
+ left === FieldInitializationState.DefinitelyInitialized &&
178
+ right === FieldInitializationState.DefinitelyInitialized
179
+ ) {
180
+ return FieldInitializationState.DefinitelyInitialized;
181
+ }
182
+
183
+ if (
184
+ left === FieldInitializationState.Uninitialized &&
185
+ right === FieldInitializationState.Uninitialized
186
+ ) {
187
+ return FieldInitializationState.Uninitialized;
188
+ }
189
+
190
+ return FieldInitializationState.MaybeInitialized;
191
+ }
192
+
193
+ function mergeStates(states: readonly LifecycleState[]): LifecycleState | null {
194
+ if (states.length === 0) {
195
+ return null;
196
+ }
197
+
198
+ const mergedFieldStates = new Map(states[0]!.fieldStates);
199
+ const mergedTrackedValues = new Map(states[0]!.trackedValues);
200
+
201
+ for (const state of states.slice(1)) {
202
+ for (const [fieldName, fieldState] of state.fieldStates) {
203
+ mergedFieldStates.set(
204
+ fieldName,
205
+ mergeFieldState(mergedFieldStates.get(fieldName) ?? FieldInitializationState.Uninitialized, fieldState),
206
+ );
207
+ }
208
+
209
+ for (const [symbolId, mask] of state.trackedValues) {
210
+ mergedTrackedValues.set(symbolId, (mergedTrackedValues.get(symbolId) ?? TrackedValueMask.None) | mask);
211
+ }
212
+ }
213
+
214
+ return {
215
+ fieldStates: mergedFieldStates,
216
+ trackedValues: mergedTrackedValues,
217
+ };
218
+ }
219
+
220
+ function getSimpleNameText(name: ts.PropertyName | ts.BindingName | undefined): string | undefined {
221
+ if (!name) {
222
+ return undefined;
223
+ }
224
+
225
+ if (
226
+ ts.isIdentifier(name) ||
227
+ ts.isPrivateIdentifier(name) ||
228
+ ts.isStringLiteralLike(name) ||
229
+ ts.isNumericLiteral(name)
230
+ ) {
231
+ return name.text;
232
+ }
233
+
234
+ return undefined;
235
+ }
236
+
237
+ function hasStaticModifier(node: ts.Node): boolean {
238
+ return ts.canHaveModifiers(node) &&
239
+ ts.getModifiers(node)?.some((modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword) === true;
240
+ }
241
+
242
+ function isParameterProperty(parameter: ts.ParameterDeclaration): boolean {
243
+ if (!ts.isIdentifier(parameter.name)) {
244
+ return false;
245
+ }
246
+
247
+ const modifierFlags = ts.getCombinedModifierFlags(parameter);
248
+ return (modifierFlags & (
249
+ ts.ModifierFlags.Public |
250
+ ts.ModifierFlags.Private |
251
+ ts.ModifierFlags.Protected |
252
+ ts.ModifierFlags.Readonly
253
+ )) !== 0;
254
+ }
255
+
256
+ function collectClassLifecycleInfo(classLike: ClassLikeDeclaration): ClassLifecycleInfo {
257
+ const fieldNames = new Set<string>();
258
+ let constructorDeclaration: ts.ConstructorDeclaration | undefined;
259
+
260
+ for (const member of classLike.members) {
261
+ if (ts.isConstructorDeclaration(member)) {
262
+ constructorDeclaration = member;
263
+ continue;
264
+ }
265
+
266
+ if (hasStaticModifier(member)) {
267
+ continue;
268
+ }
269
+
270
+ if (
271
+ (ts.isPropertyDeclaration(member) || ts.isAccessor(member)) &&
272
+ getSimpleNameText(member.name)
273
+ ) {
274
+ fieldNames.add(getSimpleNameText(member.name)!);
275
+ }
276
+ }
277
+
278
+ for (const parameter of constructorDeclaration?.parameters ?? []) {
279
+ const parameterName = getSimpleNameText(parameter.name);
280
+ if (isParameterProperty(parameter) && parameterName) {
281
+ fieldNames.add(parameterName);
282
+ }
283
+ }
284
+
285
+ return {
286
+ fieldNames,
287
+ hasBaseClass: (classLike.heritageClauses ?? []).some((clause) =>
288
+ clause.token === ts.SyntaxKind.ExtendsKeyword && clause.types.length > 0
289
+ ),
290
+ };
291
+ }
292
+
293
+ function createInitialState(classInfo: ClassLifecycleInfo): LifecycleState {
294
+ return {
295
+ fieldStates: new Map(
296
+ [...classInfo.fieldNames].map((fieldName) =>
297
+ [fieldName, FieldInitializationState.Uninitialized] as const
298
+ ),
299
+ ),
300
+ trackedValues: new Map<number, TrackedValueMask>(),
301
+ };
302
+ }
303
+
304
+ function getTrackedMaskForSymbol(
305
+ context: AnalysisContext,
306
+ state: LifecycleState,
307
+ symbol: ts.Symbol | undefined,
308
+ ): TrackedValueMask {
309
+ return symbol ? state.trackedValues.get(context.getSymbolId(symbol)) ?? TrackedValueMask.None : TrackedValueMask.None;
310
+ }
311
+
312
+ function setTrackedMaskForSymbol(
313
+ context: AnalysisContext,
314
+ state: LifecycleState,
315
+ symbol: ts.Symbol | undefined,
316
+ mask: TrackedValueMask,
317
+ ): void {
318
+ if (!symbol) {
319
+ return;
320
+ }
321
+
322
+ const symbolId = context.getSymbolId(symbol);
323
+ if (mask === TrackedValueMask.None) {
324
+ state.trackedValues.delete(symbolId);
325
+ return;
326
+ }
327
+
328
+ state.trackedValues.set(symbolId, mask);
329
+ }
330
+
331
+ function unwrapExpression(expression: ts.Expression): ts.Expression {
332
+ let current = expression;
333
+
334
+ while (
335
+ ts.isParenthesizedExpression(current) ||
336
+ ts.isNonNullExpression(current) ||
337
+ ts.isAsExpression(current) ||
338
+ ts.isSatisfiesExpression(current) ||
339
+ ts.isTypeAssertionExpression(current)
340
+ ) {
341
+ current = current.expression;
342
+ }
343
+
344
+ return current;
345
+ }
346
+
347
+ function functionCapturesTrackedThis(
348
+ context: AnalysisContext,
349
+ state: LifecycleState,
350
+ node: ts.FunctionExpression | ts.ArrowFunction,
351
+ ): boolean {
352
+ let captures = false;
353
+
354
+ function visit(current: ts.Node, allowThisKeyword: boolean): void {
355
+ if (captures) {
356
+ return;
357
+ }
358
+
359
+ if (current !== node && ts.isClassLike(current)) {
360
+ return;
361
+ }
362
+
363
+ if (current !== node && ts.isFunctionLike(current)) {
364
+ if (ts.isArrowFunction(current)) {
365
+ visit(current.body, allowThisKeyword);
366
+ } else if (
367
+ ts.isFunctionDeclaration(current) ||
368
+ ts.isFunctionExpression(current) ||
369
+ ts.isMethodDeclaration(current) ||
370
+ ts.isConstructorDeclaration(current) ||
371
+ ts.isGetAccessorDeclaration(current) ||
372
+ ts.isSetAccessorDeclaration(current)
373
+ ) {
374
+ if (current.body) {
375
+ visit(current.body, false);
376
+ }
377
+ }
378
+ return;
379
+ }
380
+
381
+ if (allowThisKeyword && current.kind === ts.SyntaxKind.ThisKeyword) {
382
+ captures = true;
383
+ return;
384
+ }
385
+
386
+ if (ts.isIdentifier(current)) {
387
+ const symbol = context.checker.getSymbolAtLocation(current);
388
+ if ((getTrackedMaskForSymbol(context, state, symbol) & TrackedValueMask.ThisAlias) !== 0) {
389
+ captures = true;
390
+ return;
391
+ }
392
+ }
393
+
394
+ ts.forEachChild(current, (child) => visit(child, allowThisKeyword));
395
+ }
396
+
397
+ visit(node.body, ts.isArrowFunction(node));
398
+ return captures;
399
+ }
400
+
401
+ function classifyExpressionValueMask(
402
+ context: AnalysisContext,
403
+ state: LifecycleState,
404
+ expression: ts.Expression,
405
+ ): TrackedValueMask {
406
+ const current = unwrapExpression(expression);
407
+
408
+ if (current.kind === ts.SyntaxKind.ThisKeyword) {
409
+ return TrackedValueMask.ThisAlias;
410
+ }
411
+
412
+ if (ts.isIdentifier(current)) {
413
+ return getTrackedMaskForSymbol(context, state, context.checker.getSymbolAtLocation(current));
414
+ }
415
+
416
+ if (ts.isFunctionExpression(current) || ts.isArrowFunction(current)) {
417
+ return functionCapturesTrackedThis(context, state, current)
418
+ ? TrackedValueMask.CapturedThisClosure
419
+ : TrackedValueMask.None;
420
+ }
421
+
422
+ return TrackedValueMask.None;
423
+ }
424
+
425
+ function expressionContainsTrackedValue(
426
+ context: AnalysisContext,
427
+ state: LifecycleState,
428
+ expression: ts.Expression,
429
+ ): TrackedValueMask {
430
+ const directMask = classifyExpressionValueMask(context, state, expression);
431
+ if (directMask !== TrackedValueMask.None) {
432
+ return directMask;
433
+ }
434
+
435
+ if (
436
+ ts.isFunctionExpression(expression) ||
437
+ ts.isArrowFunction(expression) ||
438
+ ts.isClassExpression(expression)
439
+ ) {
440
+ return TrackedValueMask.None;
441
+ }
442
+
443
+ let result = TrackedValueMask.None;
444
+ ts.forEachChild(expression, (child) => {
445
+ if (result !== TrackedValueMask.None || !ts.isExpression(child)) {
446
+ return;
447
+ }
448
+ result |= expressionContainsTrackedValue(context, state, child);
449
+ });
450
+ return result;
451
+ }
452
+
453
+ function getReceiverKind(
454
+ context: AnalysisContext,
455
+ state: LifecycleState,
456
+ expression: ts.Expression,
457
+ ): ReceiverKind | undefined {
458
+ const current = unwrapExpression(expression);
459
+ if (current.kind === ts.SyntaxKind.ThisKeyword) {
460
+ return 'this';
461
+ }
462
+ if (current.kind === ts.SyntaxKind.SuperKeyword) {
463
+ return 'super';
464
+ }
465
+
466
+ if (ts.isIdentifier(current)) {
467
+ const symbol = context.checker.getSymbolAtLocation(current);
468
+ return (getTrackedMaskForSymbol(context, state, symbol) & TrackedValueMask.ThisAlias) !== 0
469
+ ? 'this'
470
+ : undefined;
471
+ }
472
+
473
+ return undefined;
474
+ }
475
+
476
+ function getMemberSymbol(
477
+ context: AnalysisContext,
478
+ node: ts.PropertyAccessExpression | ts.ElementAccessExpression,
479
+ ): ts.Symbol | undefined {
480
+ if (ts.isPropertyAccessExpression(node)) {
481
+ return context.checker.getSymbolAtLocation(node.name);
482
+ }
483
+
484
+ const memberName = node.argumentExpression && (
485
+ ts.isStringLiteralLike(node.argumentExpression) || ts.isNumericLiteral(node.argumentExpression)
486
+ )
487
+ ? node.argumentExpression.text
488
+ : undefined;
489
+ if (!memberName) {
490
+ return undefined;
491
+ }
492
+
493
+ return context.checker.getTypeAtLocation(node.expression).getProperty(memberName);
494
+ }
495
+
496
+ function getMemberName(
497
+ node: ts.PropertyAccessExpression | ts.ElementAccessExpression,
498
+ ): string | undefined {
499
+ return ts.isPropertyAccessExpression(node)
500
+ ? getSimpleNameText(node.name)
501
+ : node.argumentExpression &&
502
+ (ts.isStringLiteralLike(node.argumentExpression) || ts.isNumericLiteral(node.argumentExpression))
503
+ ? node.argumentExpression.text
504
+ : undefined;
505
+ }
506
+
507
+ function isInstanceMethodSymbol(symbol: ts.Symbol | undefined): boolean {
508
+ if (!symbol) {
509
+ return false;
510
+ }
511
+
512
+ return (symbol.declarations ?? []).some((declaration) =>
513
+ !declaration.getSourceFile().isDeclarationFile &&
514
+ (ts.isMethodDeclaration(declaration) || ts.isMethodSignature(declaration)) &&
515
+ !hasStaticModifier(declaration)
516
+ );
517
+ }
518
+
519
+ function isInstanceAccessorSymbol(symbol: ts.Symbol | undefined): boolean {
520
+ if (!symbol) {
521
+ return false;
522
+ }
523
+
524
+ return (symbol.declarations ?? []).some((declaration) =>
525
+ !declaration.getSourceFile().isDeclarationFile &&
526
+ (ts.isGetAccessorDeclaration(declaration) || ts.isSetAccessorDeclaration(declaration)) &&
527
+ !hasStaticModifier(declaration)
528
+ );
529
+ }
530
+
531
+ function getTrackedFieldName(
532
+ classInfo: ClassLifecycleInfo,
533
+ node: ts.PropertyAccessExpression | ts.ElementAccessExpression,
534
+ ): string | undefined {
535
+ const memberName = ts.isPropertyAccessExpression(node)
536
+ ? getSimpleNameText(node.name)
537
+ : node.argumentExpression &&
538
+ (ts.isStringLiteralLike(node.argumentExpression) || ts.isNumericLiteral(node.argumentExpression))
539
+ ? node.argumentExpression.text
540
+ : undefined;
541
+ if (!memberName || !classInfo.fieldNames.has(memberName)) {
542
+ return undefined;
543
+ }
544
+
545
+ return memberName;
546
+ }
547
+
548
+ function isSimpleAssignmentOperator(token: ts.SyntaxKind): boolean {
549
+ return token === ts.SyntaxKind.EqualsToken;
550
+ }
551
+
552
+ function isAssignmentOperator(token: ts.SyntaxKind): boolean {
553
+ return token >= ts.SyntaxKind.FirstAssignment && token <= ts.SyntaxKind.LastAssignment;
554
+ }
555
+
556
+ function getFieldAccessMode(
557
+ node: ts.PropertyAccessExpression | ts.ElementAccessExpression,
558
+ ): 'read' | 'readwrite' | 'write' {
559
+ const parent = node.parent;
560
+ if (ts.isBinaryExpression(parent) && parent.left === node && isAssignmentOperator(parent.operatorToken.kind)) {
561
+ return isSimpleAssignmentOperator(parent.operatorToken.kind) ? 'write' : 'readwrite';
562
+ }
563
+
564
+ if (
565
+ (ts.isPrefixUnaryExpression(parent) || ts.isPostfixUnaryExpression(parent)) &&
566
+ (parent.operator === ts.SyntaxKind.PlusPlusToken || parent.operator === ts.SyntaxKind.MinusMinusToken)
567
+ ) {
568
+ return 'readwrite';
569
+ }
570
+
571
+ return 'read';
572
+ }
573
+
574
+ function isDirectMethodCall(
575
+ node: ts.PropertyAccessExpression | ts.ElementAccessExpression,
576
+ ): boolean {
577
+ const parent = node.parent;
578
+ return (ts.isCallExpression(parent) || ts.isNewExpression(parent)) && parent.expression === node;
579
+ }
580
+
581
+ function getDirectSuperCallStatement(
582
+ statement: ts.Statement,
583
+ ): ts.CallExpression | undefined {
584
+ if (!ts.isExpressionStatement(statement) || !ts.isCallExpression(statement.expression)) {
585
+ return undefined;
586
+ }
587
+
588
+ const callee = unwrapExpression(statement.expression.expression);
589
+ return callee.kind === ts.SyntaxKind.SuperKeyword ? statement.expression : undefined;
590
+ }
591
+
592
+ function pushDiagnostic(
593
+ diagnostics: SoundDiagnostic[],
594
+ seen: Set<string>,
595
+ diagnostic: SoundDiagnostic,
596
+ ): void {
597
+ const key =
598
+ `${diagnostic.code}:${diagnostic.filePath}:${diagnostic.line}:${diagnostic.column}:` +
599
+ `${diagnostic.endLine}:${diagnostic.endColumn}`;
600
+ if (seen.has(key)) {
601
+ return;
602
+ }
603
+ seen.add(key);
604
+ diagnostics.push(diagnostic);
605
+ }
606
+
607
+ function analyzeExpression(
608
+ context: AnalysisContext,
609
+ classInfo: ClassLifecycleInfo,
610
+ state: LifecycleState,
611
+ expression: ts.Expression,
612
+ diagnostics: SoundDiagnostic[],
613
+ seen: Set<string>,
614
+ ): void {
615
+ const current = unwrapExpression(expression);
616
+
617
+ if (ts.isFunctionExpression(current) || ts.isArrowFunction(current) || ts.isClassExpression(current)) {
618
+ return;
619
+ }
620
+
621
+ if (ts.isCallExpression(current) || ts.isNewExpression(current)) {
622
+ const callee = current.expression;
623
+ const calleeMask = classifyExpressionValueMask(context, state, callee);
624
+ if ((calleeMask & TrackedValueMask.CapturedThisClosure) !== 0) {
625
+ pushDiagnostic(
626
+ diagnostics,
627
+ seen,
628
+ createLifecycleDiagnostic({ node: callee, hazardKind: 'captured closure call' }),
629
+ );
630
+ }
631
+
632
+ if (
633
+ (ts.isPropertyAccessExpression(callee) || ts.isElementAccessExpression(callee)) &&
634
+ (getReceiverKind(context, state, callee.expression) === 'this' ||
635
+ getReceiverKind(context, state, callee.expression) === 'super')
636
+ ) {
637
+ const receiverKind = getReceiverKind(context, state, callee.expression);
638
+ const memberSymbol = getMemberSymbol(context, callee);
639
+ if (isInstanceMethodSymbol(memberSymbol) || isInstanceAccessorSymbol(memberSymbol)) {
640
+ pushDiagnostic(
641
+ diagnostics,
642
+ seen,
643
+ createLifecycleDiagnostic({
644
+ node: callee,
645
+ hazardKind: isInstanceAccessorSymbol(memberSymbol)
646
+ ? 'receiver accessor dispatch'
647
+ : 'receiver method dispatch',
648
+ memberName: getMemberName(callee),
649
+ receiverKind,
650
+ }),
651
+ );
652
+ }
653
+ } else if (
654
+ (ts.isPropertyAccessExpression(callee) || ts.isElementAccessExpression(callee)) &&
655
+ (classifyExpressionValueMask(context, state, callee.expression) &
656
+ TrackedValueMask.CapturedThisClosure) !== 0
657
+ ) {
658
+ pushDiagnostic(
659
+ diagnostics,
660
+ seen,
661
+ createLifecycleDiagnostic({
662
+ node: callee.expression,
663
+ hazardKind: 'tracked this alias escape',
664
+ }),
665
+ );
666
+ } else {
667
+ analyzeExpression(context, classInfo, state, callee, diagnostics, seen);
668
+ }
669
+
670
+ for (const argument of current.arguments ?? []) {
671
+ analyzeExpression(context, classInfo, state, argument, diagnostics, seen);
672
+ if (expressionContainsTrackedValue(context, state, argument) !== TrackedValueMask.None) {
673
+ pushDiagnostic(
674
+ diagnostics,
675
+ seen,
676
+ createLifecycleDiagnostic({ node: argument, hazardKind: 'tracked this argument escape' }),
677
+ );
678
+ }
679
+ }
680
+
681
+ return;
682
+ }
683
+
684
+ if (ts.isBinaryExpression(current) && isAssignmentOperator(current.operatorToken.kind)) {
685
+ analyzeExpression(context, classInfo, state, current.right, diagnostics, seen);
686
+
687
+ const rightValueMask = classifyExpressionValueMask(context, state, current.right);
688
+ const rightContainsTrackedValue = expressionContainsTrackedValue(context, state, current.right);
689
+
690
+ if (ts.isIdentifier(current.left)) {
691
+ const leftSymbol = context.checker.getSymbolAtLocation(current.left);
692
+ if (rightValueMask !== TrackedValueMask.None) {
693
+ setTrackedMaskForSymbol(context, state, leftSymbol, rightValueMask);
694
+ } else {
695
+ setTrackedMaskForSymbol(context, state, leftSymbol, TrackedValueMask.None);
696
+ if (rightContainsTrackedValue !== TrackedValueMask.None) {
697
+ pushDiagnostic(
698
+ diagnostics,
699
+ seen,
700
+ createLifecycleDiagnostic({ node: current.right, hazardKind: 'tracked this alias escape' }),
701
+ );
702
+ }
703
+ }
704
+ return;
705
+ }
706
+
707
+ if (ts.isPropertyAccessExpression(current.left) || ts.isElementAccessExpression(current.left)) {
708
+ analyzePropertyAccess(context, classInfo, state, current.left, diagnostics, seen);
709
+ if (rightContainsTrackedValue !== TrackedValueMask.None) {
710
+ pushDiagnostic(
711
+ diagnostics,
712
+ seen,
713
+ createLifecycleDiagnostic({ node: current.right, hazardKind: 'tracked this alias escape' }),
714
+ );
715
+ }
716
+ return;
717
+ }
718
+
719
+ analyzeExpression(context, classInfo, state, current.left, diagnostics, seen);
720
+ if (rightContainsTrackedValue !== TrackedValueMask.None) {
721
+ pushDiagnostic(
722
+ diagnostics,
723
+ seen,
724
+ createLifecycleDiagnostic({ node: current.right, hazardKind: 'tracked this alias escape' }),
725
+ );
726
+ }
727
+ return;
728
+ }
729
+
730
+ if (ts.isPropertyAccessExpression(current) || ts.isElementAccessExpression(current)) {
731
+ analyzePropertyAccess(context, classInfo, state, current, diagnostics, seen);
732
+ return;
733
+ }
734
+
735
+ if (ts.isConditionalExpression(current)) {
736
+ analyzeExpression(context, classInfo, state, current.condition, diagnostics, seen);
737
+ analyzeExpression(context, classInfo, state, current.whenTrue, diagnostics, seen);
738
+ analyzeExpression(context, classInfo, state, current.whenFalse, diagnostics, seen);
739
+ return;
740
+ }
741
+
742
+ if (ts.isPrefixUnaryExpression(current) || ts.isPostfixUnaryExpression(current)) {
743
+ analyzeExpression(context, classInfo, state, current.operand, diagnostics, seen);
744
+ return;
745
+ }
746
+
747
+ if (ts.isArrayLiteralExpression(current) || ts.isObjectLiteralExpression(current)) {
748
+ for (const child of current.getChildren(current.getSourceFile())) {
749
+ if (ts.isExpression(child)) {
750
+ analyzeExpression(context, classInfo, state, child, diagnostics, seen);
751
+ }
752
+ }
753
+ return;
754
+ }
755
+
756
+ ts.forEachChild(current, (child) => {
757
+ if (ts.isExpression(child)) {
758
+ analyzeExpression(context, classInfo, state, child, diagnostics, seen);
759
+ }
760
+ });
761
+ }
762
+
763
+ function analyzePropertyAccess(
764
+ context: AnalysisContext,
765
+ classInfo: ClassLifecycleInfo,
766
+ state: LifecycleState,
767
+ node: ts.PropertyAccessExpression | ts.ElementAccessExpression,
768
+ diagnostics: SoundDiagnostic[],
769
+ seen: Set<string>,
770
+ ): void {
771
+ analyzeExpression(context, classInfo, state, node.expression, diagnostics, seen);
772
+ if (ts.isElementAccessExpression(node) && node.argumentExpression) {
773
+ analyzeExpression(context, classInfo, state, node.argumentExpression, diagnostics, seen);
774
+ }
775
+
776
+ const receiverKind = getReceiverKind(context, state, node.expression);
777
+ if (!receiverKind) {
778
+ return;
779
+ }
780
+
781
+ const memberSymbol = getMemberSymbol(context, node);
782
+ if (isInstanceAccessorSymbol(memberSymbol)) {
783
+ pushDiagnostic(
784
+ diagnostics,
785
+ seen,
786
+ createLifecycleDiagnostic({
787
+ node,
788
+ hazardKind: 'receiver accessor dispatch',
789
+ memberName: getMemberName(node),
790
+ receiverKind,
791
+ }),
792
+ );
793
+ return;
794
+ }
795
+
796
+ if (isDirectMethodCall(node) && isInstanceMethodSymbol(memberSymbol)) {
797
+ pushDiagnostic(
798
+ diagnostics,
799
+ seen,
800
+ createLifecycleDiagnostic({
801
+ node,
802
+ hazardKind: 'receiver method dispatch',
803
+ memberName: getMemberName(node),
804
+ receiverKind,
805
+ }),
806
+ );
807
+ return;
808
+ }
809
+
810
+ if (receiverKind !== 'this') {
811
+ return;
812
+ }
813
+
814
+ const fieldName = getTrackedFieldName(classInfo, node);
815
+ if (!fieldName) {
816
+ return;
817
+ }
818
+
819
+ const accessMode = getFieldAccessMode(node);
820
+ if (accessMode !== 'write' &&
821
+ (state.fieldStates.get(fieldName) ?? FieldInitializationState.Uninitialized) !==
822
+ FieldInitializationState.DefinitelyInitialized) {
823
+ pushDiagnostic(
824
+ diagnostics,
825
+ seen,
826
+ createFieldInitializationDiagnostic({
827
+ node,
828
+ fieldName,
829
+ accessKind: 'this property access',
830
+ }),
831
+ );
832
+ }
833
+
834
+ if (accessMode !== 'read') {
835
+ state.fieldStates.set(fieldName, FieldInitializationState.DefinitelyInitialized);
836
+ }
837
+ }
838
+
839
+ function analyzeObjectBindingPatternFromThis(
840
+ context: AnalysisContext,
841
+ classInfo: ClassLifecycleInfo,
842
+ state: LifecycleState,
843
+ declaration: ts.VariableDeclaration,
844
+ diagnostics: SoundDiagnostic[],
845
+ seen: Set<string>,
846
+ ): void {
847
+ if (!declaration.initializer || !ts.isObjectBindingPattern(declaration.name)) {
848
+ return;
849
+ }
850
+
851
+ const receiverKind = getReceiverKind(context, state, declaration.initializer);
852
+ if (receiverKind !== 'this' && receiverKind !== 'super') {
853
+ return;
854
+ }
855
+
856
+ for (const element of declaration.name.elements) {
857
+ const memberName = getSimpleNameText(element.propertyName ?? element.name);
858
+ if (!memberName) {
859
+ continue;
860
+ }
861
+
862
+ const receiverType = context.checker.getTypeAtLocation(declaration.initializer);
863
+ const memberSymbol = receiverType.getProperty(memberName);
864
+ if (isInstanceAccessorSymbol(memberSymbol)) {
865
+ pushDiagnostic(
866
+ diagnostics,
867
+ seen,
868
+ createLifecycleDiagnostic({
869
+ node: element,
870
+ hazardKind: 'receiver accessor dispatch',
871
+ memberName,
872
+ receiverKind,
873
+ }),
874
+ );
875
+ continue;
876
+ }
877
+
878
+ if (receiverKind === 'this' && classInfo.fieldNames.has(memberName)) {
879
+ const fieldState = state.fieldStates.get(memberName) ?? FieldInitializationState.Uninitialized;
880
+ if (fieldState !== FieldInitializationState.DefinitelyInitialized) {
881
+ pushDiagnostic(
882
+ diagnostics,
883
+ seen,
884
+ createFieldInitializationDiagnostic({
885
+ node: element,
886
+ fieldName: memberName,
887
+ accessKind: 'object destructuring',
888
+ }),
889
+ );
890
+ }
891
+ }
892
+ }
893
+ }
894
+
895
+ function analyzeVariableDeclaration(
896
+ context: AnalysisContext,
897
+ classInfo: ClassLifecycleInfo,
898
+ state: LifecycleState,
899
+ declaration: ts.VariableDeclaration,
900
+ diagnostics: SoundDiagnostic[],
901
+ seen: Set<string>,
902
+ ): void {
903
+ if (!declaration.initializer) {
904
+ return;
905
+ }
906
+
907
+ analyzeExpression(context, classInfo, state, declaration.initializer, diagnostics, seen);
908
+ analyzeObjectBindingPatternFromThis(context, classInfo, state, declaration, diagnostics, seen);
909
+
910
+ const directMask = classifyExpressionValueMask(context, state, declaration.initializer);
911
+ const containedMask = expressionContainsTrackedValue(context, state, declaration.initializer);
912
+
913
+ if (ts.isIdentifier(declaration.name)) {
914
+ const symbol = context.checker.getSymbolAtLocation(declaration.name);
915
+ if (directMask !== TrackedValueMask.None) {
916
+ setTrackedMaskForSymbol(context, state, symbol, directMask);
917
+ return;
918
+ }
919
+
920
+ setTrackedMaskForSymbol(context, state, symbol, TrackedValueMask.None);
921
+ if (containedMask !== TrackedValueMask.None) {
922
+ pushDiagnostic(
923
+ diagnostics,
924
+ seen,
925
+ createLifecycleDiagnostic({
926
+ node: declaration.initializer,
927
+ hazardKind: 'tracked this alias escape',
928
+ }),
929
+ );
930
+ }
931
+ return;
932
+ }
933
+
934
+ if (containedMask !== TrackedValueMask.None) {
935
+ pushDiagnostic(
936
+ diagnostics,
937
+ seen,
938
+ createLifecycleDiagnostic({
939
+ node: declaration.initializer,
940
+ hazardKind: 'tracked this alias escape',
941
+ }),
942
+ );
943
+ }
944
+ }
945
+
946
+ function analyzeStatement(
947
+ context: AnalysisContext,
948
+ classInfo: ClassLifecycleInfo,
949
+ state: LifecycleState,
950
+ statement: ts.Statement,
951
+ diagnostics: SoundDiagnostic[],
952
+ seen: Set<string>,
953
+ ): LifecycleState | null {
954
+ if (ts.isBlock(statement)) {
955
+ return analyzeStatements(context, classInfo, cloneState(state), statement.statements, diagnostics, seen);
956
+ }
957
+
958
+ if (ts.isVariableStatement(statement)) {
959
+ for (const declaration of statement.declarationList.declarations) {
960
+ analyzeVariableDeclaration(context, classInfo, state, declaration, diagnostics, seen);
961
+ }
962
+ return state;
963
+ }
964
+
965
+ if (ts.isExpressionStatement(statement)) {
966
+ analyzeExpression(context, classInfo, state, statement.expression, diagnostics, seen);
967
+ return state;
968
+ }
969
+
970
+ if (ts.isReturnStatement(statement) || ts.isThrowStatement(statement)) {
971
+ if (statement.expression) {
972
+ analyzeExpression(context, classInfo, state, statement.expression, diagnostics, seen);
973
+ if (expressionContainsTrackedValue(context, state, statement.expression) !== TrackedValueMask.None) {
974
+ pushDiagnostic(
975
+ diagnostics,
976
+ seen,
977
+ createLifecycleDiagnostic({
978
+ node: statement.expression,
979
+ hazardKind: ts.isReturnStatement(statement) ? 'tracked this return' : 'tracked this throw',
980
+ }),
981
+ );
982
+ }
983
+ }
984
+ return null;
985
+ }
986
+
987
+ if (ts.isIfStatement(statement)) {
988
+ analyzeExpression(context, classInfo, state, statement.expression, diagnostics, seen);
989
+ const thenState = analyzeStatement(
990
+ context,
991
+ classInfo,
992
+ cloneState(state),
993
+ statement.thenStatement,
994
+ diagnostics,
995
+ seen,
996
+ );
997
+ const elseState = statement.elseStatement
998
+ ? analyzeStatement(context, classInfo, cloneState(state), statement.elseStatement, diagnostics, seen)
999
+ : cloneState(state);
1000
+ return mergeStates([thenState, elseState].filter((value): value is LifecycleState => value !== null));
1001
+ }
1002
+
1003
+ if (ts.isWhileStatement(statement)) {
1004
+ analyzeExpression(context, classInfo, state, statement.expression, diagnostics, seen);
1005
+ const bodyState = analyzeStatement(
1006
+ context,
1007
+ classInfo,
1008
+ cloneState(state),
1009
+ statement.statement,
1010
+ diagnostics,
1011
+ seen,
1012
+ );
1013
+ return mergeStates([state, bodyState].filter((value): value is LifecycleState => value !== null));
1014
+ }
1015
+
1016
+ if (ts.isDoStatement(statement)) {
1017
+ const bodyState = analyzeStatement(
1018
+ context,
1019
+ classInfo,
1020
+ cloneState(state),
1021
+ statement.statement,
1022
+ diagnostics,
1023
+ seen,
1024
+ );
1025
+ if (bodyState) {
1026
+ analyzeExpression(context, classInfo, bodyState, statement.expression, diagnostics, seen);
1027
+ }
1028
+ return mergeStates([state, bodyState].filter((value): value is LifecycleState => value !== null));
1029
+ }
1030
+
1031
+ if (ts.isForStatement(statement)) {
1032
+ if (statement.initializer) {
1033
+ if (ts.isVariableDeclarationList(statement.initializer)) {
1034
+ for (const declaration of statement.initializer.declarations) {
1035
+ analyzeVariableDeclaration(context, classInfo, state, declaration, diagnostics, seen);
1036
+ }
1037
+ } else {
1038
+ analyzeExpression(context, classInfo, state, statement.initializer, diagnostics, seen);
1039
+ }
1040
+ }
1041
+ if (statement.condition) {
1042
+ analyzeExpression(context, classInfo, state, statement.condition, diagnostics, seen);
1043
+ }
1044
+ const bodyState = analyzeStatement(
1045
+ context,
1046
+ classInfo,
1047
+ cloneState(state),
1048
+ statement.statement,
1049
+ diagnostics,
1050
+ seen,
1051
+ );
1052
+ if (bodyState && statement.incrementor) {
1053
+ analyzeExpression(context, classInfo, bodyState, statement.incrementor, diagnostics, seen);
1054
+ }
1055
+ return mergeStates([state, bodyState].filter((value): value is LifecycleState => value !== null));
1056
+ }
1057
+
1058
+ if (ts.isForOfStatement(statement) || ts.isForInStatement(statement)) {
1059
+ analyzeExpression(context, classInfo, state, statement.expression, diagnostics, seen);
1060
+ const bodyState = analyzeStatement(
1061
+ context,
1062
+ classInfo,
1063
+ cloneState(state),
1064
+ statement.statement,
1065
+ diagnostics,
1066
+ seen,
1067
+ );
1068
+ return mergeStates([state, bodyState].filter((value): value is LifecycleState => value !== null));
1069
+ }
1070
+
1071
+ if (ts.isSwitchStatement(statement)) {
1072
+ analyzeExpression(context, classInfo, state, statement.expression, diagnostics, seen);
1073
+ const clauseStates: LifecycleState[] = [];
1074
+ for (const clause of statement.caseBlock.clauses) {
1075
+ if (ts.isCaseClause(clause)) {
1076
+ analyzeExpression(context, classInfo, state, clause.expression, diagnostics, seen);
1077
+ }
1078
+ const clauseState = analyzeStatements(
1079
+ context,
1080
+ classInfo,
1081
+ cloneState(state),
1082
+ clause.statements,
1083
+ diagnostics,
1084
+ seen,
1085
+ );
1086
+ if (clauseState) {
1087
+ clauseStates.push(clauseState);
1088
+ }
1089
+ }
1090
+ clauseStates.push(state);
1091
+ return mergeStates(clauseStates);
1092
+ }
1093
+
1094
+ if (ts.isTryStatement(statement)) {
1095
+ const tryState = analyzeStatement(
1096
+ context,
1097
+ classInfo,
1098
+ cloneState(state),
1099
+ statement.tryBlock,
1100
+ diagnostics,
1101
+ seen,
1102
+ );
1103
+ const catchState = statement.catchClause
1104
+ ? analyzeStatement(
1105
+ context,
1106
+ classInfo,
1107
+ cloneState(state),
1108
+ statement.catchClause.block,
1109
+ diagnostics,
1110
+ seen,
1111
+ )
1112
+ : null;
1113
+ const mergedBeforeFinally = mergeStates(
1114
+ [tryState, catchState].filter((value): value is LifecycleState => value !== null),
1115
+ ) ?? cloneState(state);
1116
+ return statement.finallyBlock
1117
+ ? analyzeStatement(
1118
+ context,
1119
+ classInfo,
1120
+ mergedBeforeFinally,
1121
+ statement.finallyBlock,
1122
+ diagnostics,
1123
+ seen,
1124
+ )
1125
+ : mergedBeforeFinally;
1126
+ }
1127
+
1128
+ ts.forEachChild(statement, (child) => {
1129
+ if (ts.isExpression(child)) {
1130
+ analyzeExpression(context, classInfo, state, child, diagnostics, seen);
1131
+ }
1132
+ });
1133
+ return state;
1134
+ }
1135
+
1136
+ function analyzeStatements(
1137
+ context: AnalysisContext,
1138
+ classInfo: ClassLifecycleInfo,
1139
+ initialState: LifecycleState,
1140
+ statements: readonly ts.Statement[],
1141
+ diagnostics: SoundDiagnostic[],
1142
+ seen: Set<string>,
1143
+ ): LifecycleState | null {
1144
+ let currentState: LifecycleState | null = initialState;
1145
+
1146
+ for (const statement of statements) {
1147
+ if (context.isGeneratedNode(statement)) {
1148
+ continue;
1149
+ }
1150
+
1151
+ if (!currentState) {
1152
+ return null;
1153
+ }
1154
+ currentState = analyzeStatement(context, classInfo, currentState, statement, diagnostics, seen);
1155
+ }
1156
+
1157
+ return currentState;
1158
+ }
1159
+
1160
+ function applyParameterPropertyInitialization(
1161
+ constructorDeclaration: ts.ConstructorDeclaration | undefined,
1162
+ state: LifecycleState,
1163
+ ): void {
1164
+ for (const parameter of constructorDeclaration?.parameters ?? []) {
1165
+ const parameterName = getSimpleNameText(parameter.name);
1166
+ if (isParameterProperty(parameter) && parameterName) {
1167
+ state.fieldStates.set(parameterName, FieldInitializationState.DefinitelyInitialized);
1168
+ }
1169
+ }
1170
+ }
1171
+
1172
+ function analyzeFieldInitializers(
1173
+ context: AnalysisContext,
1174
+ classInfo: ClassLifecycleInfo,
1175
+ classLike: ClassLikeDeclaration,
1176
+ state: LifecycleState,
1177
+ diagnostics: SoundDiagnostic[],
1178
+ seen: Set<string>,
1179
+ ): void {
1180
+ for (const member of classLike.members) {
1181
+ if (!ts.isPropertyDeclaration(member) || hasStaticModifier(member) || !member.initializer) {
1182
+ continue;
1183
+ }
1184
+
1185
+ analyzeExpression(context, classInfo, state, member.initializer, diagnostics, seen);
1186
+ const fieldName = getSimpleNameText(member.name);
1187
+ if (fieldName) {
1188
+ state.fieldStates.set(fieldName, FieldInitializationState.DefinitelyInitialized);
1189
+ }
1190
+ }
1191
+ }
1192
+
1193
+ function analyzeClassLike(
1194
+ context: AnalysisContext,
1195
+ classLike: ClassLikeDeclaration,
1196
+ diagnostics: SoundDiagnostic[],
1197
+ seen: Set<string>,
1198
+ ): void {
1199
+ const classInfo = collectClassLifecycleInfo(classLike);
1200
+ const constructorDeclaration = classLike.members.find(ts.isConstructorDeclaration);
1201
+
1202
+ if (!classInfo.hasBaseClass) {
1203
+ const state = createInitialState(classInfo);
1204
+ analyzeFieldInitializers(context, classInfo, classLike, state, diagnostics, seen);
1205
+ applyParameterPropertyInitialization(constructorDeclaration, state);
1206
+ if (constructorDeclaration?.body) {
1207
+ analyzeStatements(
1208
+ context,
1209
+ classInfo,
1210
+ state,
1211
+ constructorDeclaration.body.statements,
1212
+ diagnostics,
1213
+ seen,
1214
+ );
1215
+ }
1216
+ return;
1217
+ }
1218
+
1219
+ const state = createInitialState(classInfo);
1220
+ if (!constructorDeclaration?.body) {
1221
+ analyzeFieldInitializers(context, classInfo, classLike, state, diagnostics, seen);
1222
+ return;
1223
+ }
1224
+
1225
+ const statements = constructorDeclaration.body.statements;
1226
+ const superStatementIndex = statements.findIndex((statement) => getDirectSuperCallStatement(statement) !== undefined);
1227
+ if (superStatementIndex === -1) {
1228
+ return;
1229
+ }
1230
+
1231
+ if (superStatementIndex > 0) {
1232
+ analyzeStatements(
1233
+ context,
1234
+ classInfo,
1235
+ state,
1236
+ statements.slice(0, superStatementIndex + 1),
1237
+ diagnostics,
1238
+ seen,
1239
+ );
1240
+ } else {
1241
+ analyzeExpression(
1242
+ context,
1243
+ classInfo,
1244
+ state,
1245
+ getDirectSuperCallStatement(statements[superStatementIndex]!)!,
1246
+ diagnostics,
1247
+ seen,
1248
+ );
1249
+ }
1250
+
1251
+ analyzeFieldInitializers(context, classInfo, classLike, state, diagnostics, seen);
1252
+ applyParameterPropertyInitialization(constructorDeclaration, state);
1253
+ analyzeStatements(
1254
+ context,
1255
+ classInfo,
1256
+ state,
1257
+ statements.slice(superStatementIndex + 1),
1258
+ diagnostics,
1259
+ seen,
1260
+ );
1261
+ }
1262
+
1263
+ export function runClassLifecycleRules(context: AnalysisContext): SoundDiagnostic[] {
1264
+ const diagnostics: SoundDiagnostic[] = [];
1265
+ const seen = new Set<string>();
1266
+
1267
+ context.forEachSourceFile((sourceFile) => {
1268
+ context.traverse(sourceFile, (node) => {
1269
+ if (ts.isClassDeclaration(node) || ts.isClassExpression(node)) {
1270
+ analyzeClassLike(context, node, diagnostics, seen);
1271
+ }
1272
+ });
1273
+ });
1274
+
1275
+ return diagnostics;
1276
+ }