@geastack/compiler 1.0.20 → 1.0.21

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 (534) hide show
  1. package/dist/cli-emit.js +11 -1
  2. package/dist/cli-package-source.d.ts +0 -4
  3. package/dist/cli-package-source.js +8 -112
  4. package/dist/cli-refusal-location.d.ts +6 -0
  5. package/dist/cli-refusal-location.js +5 -0
  6. package/dist/cli.js +2 -1
  7. package/dist/compiler.d.ts +4 -0
  8. package/dist/compiler.js +118 -28
  9. package/dist/conversion/algebra.d.ts +16 -1
  10. package/dist/conversion/algebra.js +2 -0
  11. package/dist/conversion/build.js +16 -6
  12. package/dist/conversion/constructor-identity-family.d.ts +49 -0
  13. package/dist/conversion/constructor-identity-family.js +67 -0
  14. package/dist/conversion/derive.js +39 -6
  15. package/dist/conversion/iterable-object-view.d.ts +44 -0
  16. package/dist/conversion/iterable-object-view.js +55 -0
  17. package/dist/conversion/iterator-object-view.d.ts +77 -0
  18. package/dist/conversion/iterator-object-view.js +112 -0
  19. package/dist/conversion/nodes.d.ts +93 -0
  20. package/dist/conversion/nodes.js +262 -5
  21. package/dist/conversion/operand-view.js +41 -0
  22. package/dist/conversion/protocol-iterator.d.ts +84 -0
  23. package/dist/conversion/protocol-iterator.js +88 -0
  24. package/dist/conversion/record-view.d.ts +152 -8
  25. package/dist/conversion/record-view.js +518 -37
  26. package/dist/conversion/registry.d.ts +32 -0
  27. package/dist/ir/borrowed-call-arguments.d.ts +22 -1
  28. package/dist/ir/borrowed-call-arguments.js +102 -1
  29. package/dist/ir/borrowed-callable-uses.d.ts +62 -0
  30. package/dist/ir/borrowed-callable-uses.js +135 -0
  31. package/dist/ir/boxed-bind-assumptions.d.ts +24 -0
  32. package/dist/ir/boxed-bind-assumptions.js +93 -0
  33. package/dist/ir/build.d.ts +18 -7
  34. package/dist/ir/build.js +71 -13
  35. package/dist/ir/call-dispatch.js +474 -10
  36. package/dist/ir/call-entry.d.ts +24 -1
  37. package/dist/ir/call-entry.js +106 -1
  38. package/dist/ir/callable-class-flow.js +15 -5
  39. package/dist/ir/callable-identity-demand.d.ts +20 -1
  40. package/dist/ir/callable-identity-demand.js +157 -18
  41. package/dist/ir/callable-member-candidates.d.ts +11 -1
  42. package/dist/ir/callable-member-candidates.js +82 -3
  43. package/dist/ir/callable-records.js +2 -2
  44. package/dist/ir/callable-site-escape.d.ts +4 -0
  45. package/dist/ir/callable-site-escape.js +228 -0
  46. package/dist/ir/captures.d.ts +10 -1
  47. package/dist/ir/captures.js +219 -4
  48. package/dist/ir/certify/carrier-keys.d.ts +58 -1
  49. package/dist/ir/certify/carrier-keys.js +158 -6
  50. package/dist/ir/certify/property-access-keys.d.ts +17 -0
  51. package/dist/ir/certify/property-access-keys.js +36 -0
  52. package/dist/ir/certify/property-access.js +49 -4
  53. package/dist/ir/certify/runtime-helper.d.ts +4 -0
  54. package/dist/ir/certify/runtime-helper.js +17 -5
  55. package/dist/ir/certify.js +132 -79
  56. package/dist/ir/class-static-fields.d.ts +11 -0
  57. package/dist/ir/class-static-fields.js +28 -0
  58. package/dist/ir/construct-entry.d.ts +2 -1
  59. package/dist/ir/construct-entry.js +3 -2
  60. package/dist/ir/construction-only-fields.d.ts +73 -0
  61. package/dist/ir/construction-only-fields.js +211 -0
  62. package/dist/ir/dominance.d.ts +14 -0
  63. package/dist/ir/dominance.js +18 -4
  64. package/dist/ir/emission-representations.js +4 -1
  65. package/dist/ir/exception-edges.d.ts +17 -0
  66. package/dist/ir/exception-edges.js +75 -0
  67. package/dist/ir/facts.d.ts +54 -3
  68. package/dist/ir/facts.js +182 -5
  69. package/dist/ir/generator-split.js +2 -2
  70. package/dist/ir/instance-reparenting.d.ts +83 -0
  71. package/dist/ir/instance-reparenting.js +167 -0
  72. package/dist/ir/integer-storage.d.ts +29 -0
  73. package/dist/ir/integer-storage.js +188 -79
  74. package/dist/ir/integers.d.ts +16 -0
  75. package/dist/ir/integers.js +327 -8
  76. package/dist/ir/integrity-restrictions.d.ts +42 -9
  77. package/dist/ir/integrity-restrictions.js +335 -42
  78. package/dist/ir/key-order-observation.d.ts +38 -0
  79. package/dist/ir/key-order-observation.js +173 -0
  80. package/dist/ir/literal-set-membership.d.ts +30 -0
  81. package/dist/ir/literal-set-membership.js +277 -0
  82. package/dist/ir/lower-allocation.js +68 -8
  83. package/dist/ir/lower-completion.d.ts +0 -9
  84. package/dist/ir/lower-completion.js +14 -5
  85. package/dist/ir/lower-destructuring.js +24 -2
  86. package/dist/ir/lower-exceptions.js +4 -4
  87. package/dist/ir/lower-flow.d.ts +21 -0
  88. package/dist/ir/lower-flow.js +37 -3
  89. package/dist/ir/lower-graph.d.ts +19 -1
  90. package/dist/ir/lower-graph.js +91 -67
  91. package/dist/ir/lower-invocation.d.ts +16 -0
  92. package/dist/ir/lower-invocation.js +182 -28
  93. package/dist/ir/lower-narrow.d.ts +0 -46
  94. package/dist/ir/lower-narrow.js +63 -1
  95. package/dist/ir/lower-operands.d.ts +52 -2
  96. package/dist/ir/lower-operands.js +491 -8
  97. package/dist/ir/lower-property.d.ts +2 -1
  98. package/dist/ir/lower-property.js +140 -31
  99. package/dist/ir/lower-protocol.js +16 -4
  100. package/dist/ir/lower-proxy.d.ts +65 -0
  101. package/dist/ir/lower-proxy.js +181 -0
  102. package/dist/ir/lower.js +112 -17
  103. package/dist/ir/model.d.ts +228 -18
  104. package/dist/ir/native-class-accessor.d.ts +9 -2
  105. package/dist/ir/native-class-accessor.js +12 -3
  106. package/dist/ir/native-class-construction.d.ts +1 -1
  107. package/dist/ir/native-class-construction.js +2 -1
  108. package/dist/ir/native-class-initialization.js +2 -2
  109. package/dist/ir/object-value-conversions.js +20 -5
  110. package/dist/ir/operation-index.d.ts +16 -0
  111. package/dist/ir/operation-index.js +17 -0
  112. package/dist/ir/pcm-loops.d.ts +11 -0
  113. package/dist/ir/pcm-loops.js +284 -0
  114. package/dist/ir/presence-proof.js +5 -0
  115. package/dist/ir/program-facts.d.ts +10 -20
  116. package/dist/ir/program-facts.js +80 -6
  117. package/dist/ir/proven-branches.d.ts +2 -2
  118. package/dist/ir/proven-branches.js +196 -3
  119. package/dist/ir/queries.js +35 -1
  120. package/dist/ir/reflection-demand.d.ts +9 -4
  121. package/dist/ir/reflection-demand.js +400 -28
  122. package/dist/ir/refusal.d.ts +1 -0
  123. package/dist/ir/refusal.js +2 -1
  124. package/dist/ir/shake.d.ts +94 -1
  125. package/dist/ir/shake.js +209 -12
  126. package/dist/ir/shared-empty-records.d.ts +12 -0
  127. package/dist/ir/shared-empty-records.js +301 -0
  128. package/dist/ir/spread-copy-elision.d.ts +59 -0
  129. package/dist/ir/spread-copy-elision.js +431 -0
  130. package/dist/ir/stable-cell-reads.d.ts +40 -0
  131. package/dist/ir/stable-cell-reads.js +123 -0
  132. package/dist/ir/static-callables.js +50 -0
  133. package/dist/ir/string-layout-reuse.d.ts +16 -1
  134. package/dist/ir/string-layout-reuse.js +66 -0
  135. package/dist/ir/transfer.d.ts +72 -3
  136. package/dist/ir/transfer.js +511 -19
  137. package/dist/ir/typed-property-access.d.ts +18 -2
  138. package/dist/ir/typed-property-access.js +109 -12
  139. package/dist/ir/verify.js +2 -1
  140. package/dist/plugins/apple/plugin.js +3 -2
  141. package/dist/plugins/gea/host.d.ts +7 -0
  142. package/dist/plugins/gea/host.js +30 -1
  143. package/dist/plugins/gea/lower.js +6 -1
  144. package/dist/plugins/gea/plugin.js +7 -4
  145. package/dist/plugins/gea/render-bridge.d.ts +4 -2
  146. package/dist/plugins/gea/render-bridge.js +23 -7
  147. package/dist/plugins/model.d.ts +2 -0
  148. package/dist/project-preparation.d.ts +5 -1
  149. package/dist/project-preparation.js +164 -7
  150. package/dist/projection/bindings.d.ts +5 -2
  151. package/dist/projection/bindings.js +40 -1
  152. package/dist/projection/callee.d.ts +16 -0
  153. package/dist/projection/callee.js +35 -6
  154. package/dist/projection/class-prototype.js +5 -1
  155. package/dist/projection/classes.d.ts +65 -2
  156. package/dist/projection/classes.js +159 -2
  157. package/dist/projection/dispatch.d.ts +69 -18
  158. package/dist/projection/dispatch.js +391 -19
  159. package/dist/projection/fields.d.ts +4 -3
  160. package/dist/projection/fields.js +89 -21
  161. package/dist/projection/instance-test.d.ts +12 -0
  162. package/dist/projection/instance-test.js +40 -10
  163. package/dist/projection/method-value-escapes.d.ts +43 -0
  164. package/dist/projection/method-value-escapes.js +113 -0
  165. package/dist/projection/slots.d.ts +34 -1
  166. package/dist/projection/slots.js +82 -5
  167. package/dist/representation/any-null-record-members.d.ts +41 -0
  168. package/dist/representation/any-null-record-members.js +115 -0
  169. package/dist/representation/collections.d.ts +3 -1
  170. package/dist/representation/collections.js +5 -2
  171. package/dist/representation/derive.d.ts +9 -1
  172. package/dist/representation/derive.js +366 -25
  173. package/dist/representation/difference.js +14 -0
  174. package/dist/representation/embedded-carriers.js +1 -0
  175. package/dist/representation/host-abi.d.ts +15 -0
  176. package/dist/representation/host-abi.js +28 -0
  177. package/dist/representation/host-templates.d.ts +1 -1
  178. package/dist/representation/host-templates.js +7 -3
  179. package/dist/representation/intersection.js +22 -0
  180. package/dist/representation/json-nullable.d.ts +18 -0
  181. package/dist/representation/json-nullable.js +22 -0
  182. package/dist/representation/model.d.ts +141 -0
  183. package/dist/representation/model.js +47 -3
  184. package/dist/representation/object-shape.d.ts +5 -1
  185. package/dist/representation/object-shape.js +5 -2
  186. package/dist/representation/policies.d.ts +66 -1
  187. package/dist/representation/proxy-carriers.d.ts +71 -0
  188. package/dist/representation/proxy-carriers.js +330 -0
  189. package/dist/representation/publish.d.ts +9 -1
  190. package/dist/representation/publish.js +186 -18
  191. package/dist/representation/record-fields.d.ts +5 -0
  192. package/dist/representation/record-fields.js +7 -1
  193. package/dist/representation/static-field-cells.d.ts +61 -0
  194. package/dist/representation/static-field-cells.js +80 -0
  195. package/dist/representation/unassigned-binding-cells.d.ts +32 -0
  196. package/dist/representation/unassigned-binding-cells.js +44 -0
  197. package/dist/representation/union.js +17 -1
  198. package/dist/representation/unwritten-record-members.d.ts +54 -0
  199. package/dist/representation/unwritten-record-members.js +167 -0
  200. package/dist/representation/value-records.js +153 -11
  201. package/dist/semantics/ambient.d.ts +102 -0
  202. package/dist/semantics/ambient.js +183 -0
  203. package/dist/semantics/any-null-member-declarations.d.ts +32 -0
  204. package/dist/semantics/any-null-member-declarations.js +90 -0
  205. package/dist/semantics/borrowed-builtin-call-bind-source-transform.js +14 -0
  206. package/dist/semantics/borrowed-method-receiver-copy-source-transform.d.ts +51 -0
  207. package/dist/semantics/borrowed-method-receiver-copy-source-transform.js +367 -0
  208. package/dist/semantics/call-invalidated-narrowing.d.ts +3 -0
  209. package/dist/semantics/call-invalidated-narrowing.js +715 -0
  210. package/dist/semantics/callable-origins.d.ts +39 -0
  211. package/dist/semantics/callable-origins.js +73 -2
  212. package/dist/semantics/class-alias.d.ts +21 -0
  213. package/dist/semantics/class-alias.js +45 -14
  214. package/dist/semantics/class-heritage.d.ts +64 -0
  215. package/dist/semantics/class-heritage.js +278 -2
  216. package/dist/semantics/commonjs-wrapper.d.ts +1 -1
  217. package/dist/semantics/commonjs-wrapper.js +24 -7
  218. package/dist/semantics/constructor-function-class-source-transform.d.ts +4 -0
  219. package/dist/semantics/constructor-function-class-source-transform.js +327 -0
  220. package/dist/semantics/constructor-slot-subclasses.js +163 -4
  221. package/dist/semantics/declaration-overlay-transform.js +197 -3
  222. package/dist/semantics/frontend.d.ts +31 -0
  223. package/dist/semantics/frontend.js +177 -19
  224. package/dist/semantics/host-methods.d.ts +27 -2
  225. package/dist/semantics/host-methods.js +57 -4
  226. package/dist/semantics/host-protocols.d.ts +19 -8
  227. package/dist/semantics/host-protocols.js +145 -31
  228. package/dist/semantics/interface-families.d.ts +51 -2
  229. package/dist/semantics/interface-families.js +609 -23
  230. package/dist/semantics/interface-implementors.d.ts +20 -0
  231. package/dist/semantics/interface-implementors.js +130 -6
  232. package/dist/semantics/known-caller-predicate-parameters.d.ts +2 -0
  233. package/dist/semantics/known-caller-predicate-parameters.js +161 -0
  234. package/dist/semantics/model/intrinsic-accessor-getters.d.ts +10 -0
  235. package/dist/semantics/model/intrinsic-accessor-getters.js +4 -0
  236. package/dist/semantics/model/operands.d.ts +10 -0
  237. package/dist/semantics/model/operations.d.ts +107 -0
  238. package/dist/semantics/model/selected-signature.d.ts +33 -0
  239. package/dist/semantics/model/structural-type-table.d.ts +15 -0
  240. package/dist/semantics/model/structural-type-table.js +19 -1
  241. package/dist/semantics/model/structural-types.d.ts +99 -0
  242. package/dist/semantics/model/structural-types.js +31 -2
  243. package/dist/semantics/normalize/absent-globals.d.ts +1 -1
  244. package/dist/semantics/normalize/absent-globals.js +3 -3
  245. package/dist/semantics/normalize/bivariant-slot-parameter.d.ts +46 -0
  246. package/dist/semantics/normalize/bivariant-slot-parameter.js +357 -0
  247. package/dist/semantics/normalize/callback-parameter-contracts.d.ts +2 -0
  248. package/dist/semantics/normalize/callback-parameter-contracts.js +21 -0
  249. package/dist/semantics/normalize/census.d.ts +2 -1
  250. package/dist/semantics/normalize/census.js +25 -2
  251. package/dist/semantics/normalize/class-instance-record-statement.d.ts +37 -0
  252. package/dist/semantics/normalize/class-instance-record-statement.js +74 -0
  253. package/dist/semantics/normalize/collection-bindings.js +242 -5
  254. package/dist/semantics/normalize/commonjs-require.d.ts +26 -0
  255. package/dist/semantics/normalize/commonjs-require.js +355 -121
  256. package/dist/semantics/normalize/dead-event-emissions.d.ts +6 -0
  257. package/dist/semantics/normalize/dead-event-emissions.js +866 -0
  258. package/dist/semantics/normalize/dead-method-copies.d.ts +56 -0
  259. package/dist/semantics/normalize/dead-method-copies.js +239 -0
  260. package/dist/semantics/normalize/derived-expression-type.d.ts +149 -2
  261. package/dist/semantics/normalize/derived-expression-type.js +418 -14
  262. package/dist/semantics/normalize/field-bindings.js +139 -4
  263. package/dist/semantics/normalize/flow/callable-reach.js +162 -9
  264. package/dist/semantics/normalize/flow/member-call-forwarding.js +6 -0
  265. package/dist/semantics/normalize/flow/owned-class-receivers.js +40 -2
  266. package/dist/semantics/normalize/flow/source-class-data.d.ts +7 -1
  267. package/dist/semantics/normalize/flow/source-class-data.js +28 -9
  268. package/dist/semantics/normalize/flow/source-value-session.js +32 -13
  269. package/dist/semantics/normalize/flow/targets.d.ts +32 -0
  270. package/dist/semantics/normalize/flow/targets.js +65 -6
  271. package/dist/semantics/normalize/flow/value-flow.d.ts +7 -0
  272. package/dist/semantics/normalize/flow/value-flow.js +30 -2
  273. package/dist/semantics/normalize/fresh-array-result.d.ts +32 -0
  274. package/dist/semantics/normalize/fresh-array-result.js +125 -0
  275. package/dist/semantics/normalize/gating.js +2 -0
  276. package/dist/semantics/normalize/global-host-mutations.d.ts +11 -1
  277. package/dist/semantics/normalize/global-host-mutations.js +691 -27
  278. package/dist/semantics/normalize/host-effect-contracts.d.ts +43 -1
  279. package/dist/semantics/normalize/host-effect-contracts.js +60 -3
  280. package/dist/semantics/normalize/identities.js +49 -3
  281. package/dist/semantics/normalize/intrinsic-accessor-getter.d.ts +59 -0
  282. package/dist/semantics/normalize/intrinsic-accessor-getter.js +129 -0
  283. package/dist/semantics/normalize/local-bindings.js +117 -21
  284. package/dist/semantics/normalize/merged-declaration.d.ts +41 -0
  285. package/dist/semantics/normalize/merged-declaration.js +81 -0
  286. package/dist/semantics/normalize/never-override-result.d.ts +21 -0
  287. package/dist/semantics/normalize/never-override-result.js +64 -0
  288. package/dist/semantics/normalize/null-blind-parameter.d.ts +21 -0
  289. package/dist/semantics/normalize/null-blind-parameter.js +77 -0
  290. package/dist/semantics/normalize/object-bag-bindings.js +10 -0
  291. package/dist/semantics/normalize/override-field-arms.d.ts +33 -0
  292. package/dist/semantics/normalize/override-field-arms.js +362 -0
  293. package/dist/semantics/normalize/parameter-bindings.d.ts +64 -1
  294. package/dist/semantics/normalize/parameter-bindings.js +801 -81
  295. package/dist/semantics/normalize/parameter-slot.d.ts +1 -0
  296. package/dist/semantics/normalize/parameter-slot.js +69 -0
  297. package/dist/semantics/normalize/physical-overload-result.d.ts +45 -0
  298. package/dist/semantics/normalize/physical-overload-result.js +132 -0
  299. package/dist/semantics/normalize/producer-context.d.ts +23 -0
  300. package/dist/semantics/normalize/producers/allocations.d.ts +13 -0
  301. package/dist/semantics/normalize/producers/allocations.js +8 -15
  302. package/dist/semantics/normalize/producers/binding-kind.d.ts +11 -0
  303. package/dist/semantics/normalize/producers/binding-kind.js +31 -0
  304. package/dist/semantics/normalize/producers/bindings.js +16 -1
  305. package/dist/semantics/normalize/producers/boundary.js +21 -1
  306. package/dist/semantics/normalize/producers/class-lifecycle.js +23 -14
  307. package/dist/semantics/normalize/producers/computations.js +8 -67
  308. package/dist/semantics/normalize/producers/control-loops.d.ts +1 -1
  309. package/dist/semantics/normalize/producers/control-loops.js +25 -1
  310. package/dist/semantics/normalize/producers/control.js +87 -60
  311. package/dist/semantics/normalize/producers/declaration-lifecycle.js +5 -0
  312. package/dist/semantics/normalize/producers/enums.d.ts +10 -0
  313. package/dist/semantics/normalize/producers/enums.js +169 -0
  314. package/dist/semantics/normalize/producers/erasure.d.ts +10 -0
  315. package/dist/semantics/normalize/producers/erasure.js +23 -0
  316. package/dist/semantics/normalize/producers/intrinsic-accessor-getter.d.ts +15 -0
  317. package/dist/semantics/normalize/producers/intrinsic-accessor-getter.js +33 -0
  318. package/dist/semantics/normalize/producers/invocations.js +255 -18
  319. package/dist/semantics/normalize/producers/iteration-yield.js +6 -1
  320. package/dist/semantics/normalize/producers/logical-merge-type.d.ts +36 -0
  321. package/dist/semantics/normalize/producers/logical-merge-type.js +163 -0
  322. package/dist/semantics/normalize/producers/properties.js +207 -18
  323. package/dist/semantics/normalize/producers/protocol.d.ts +4 -0
  324. package/dist/semantics/normalize/producers/protocol.js +147 -12
  325. package/dist/semantics/normalize/producers/references.js +50 -9
  326. package/dist/semantics/normalize/producers/shared.d.ts +120 -0
  327. package/dist/semantics/normalize/producers/shared.js +359 -10
  328. package/dist/semantics/normalize/producers/spread-arguments.js +85 -4
  329. package/dist/semantics/normalize/producers/tuple-spread.d.ts +1 -0
  330. package/dist/semantics/normalize/producers/tuple-spread.js +1 -1
  331. package/dist/semantics/normalize/property-key-domain.js +6 -0
  332. package/dist/semantics/normalize/reachability.d.ts +7 -0
  333. package/dist/semantics/normalize/reachability.js +166 -9
  334. package/dist/semantics/normalize/record-home-arms.d.ts +25 -0
  335. package/dist/semantics/normalize/record-home-arms.js +87 -0
  336. package/dist/semantics/normalize/record-link-families.d.ts +74 -0
  337. package/dist/semantics/normalize/record-link-families.js +327 -0
  338. package/dist/semantics/normalize/record-stand-in-arms.d.ts +56 -0
  339. package/dist/semantics/normalize/record-stand-in-arms.js +484 -0
  340. package/dist/semantics/normalize/return-bindings.d.ts +6 -2
  341. package/dist/semantics/normalize/return-bindings.js +60 -7
  342. package/dist/semantics/normalize/script-global-redefinition.js +5 -0
  343. package/dist/semantics/normalize/shared-array-storage.d.ts +42 -0
  344. package/dist/semantics/normalize/shared-array-storage.js +373 -0
  345. package/dist/semantics/normalize/sloppy-absence.d.ts +41 -0
  346. package/dist/semantics/normalize/sloppy-absence.js +199 -0
  347. package/dist/semantics/normalize/specialization.d.ts +40 -0
  348. package/dist/semantics/normalize/specialization.js +770 -36
  349. package/dist/semantics/normalize/structural-array-element.js +196 -6
  350. package/dist/semantics/normalize/structural-array-read.js +12 -0
  351. package/dist/semantics/normalize/structural-creation-order.d.ts +15 -0
  352. package/dist/semantics/normalize/structural-creation-order.js +51 -48
  353. package/dist/semantics/normalize/structural-declarations.d.ts +16 -0
  354. package/dist/semantics/normalize/structural-declarations.js +43 -0
  355. package/dist/semantics/normalize/structural-generics.d.ts +13 -0
  356. package/dist/semantics/normalize/structural-generics.js +22 -0
  357. package/dist/semantics/normalize/structural-indexed-access.js +16 -0
  358. package/dist/semantics/normalize/structural-instantiated-member.js +68 -2
  359. package/dist/semantics/normalize/structural-joins.js +2 -0
  360. package/dist/semantics/normalize/structural-layout-relevance.d.ts +30 -0
  361. package/dist/semantics/normalize/structural-layout-relevance.js +164 -0
  362. package/dist/semantics/normalize/structural-layout-type.js +270 -14
  363. package/dist/semantics/normalize/structural-leaves.d.ts +1 -1
  364. package/dist/semantics/normalize/structural-leaves.js +17 -3
  365. package/dist/semantics/normalize/structural-mutable-method.js +5 -1
  366. package/dist/semantics/normalize/structural-parts.d.ts +18 -5
  367. package/dist/semantics/normalize/structural-parts.js +68 -19
  368. package/dist/semantics/normalize/structural-receiver.d.ts +1 -1
  369. package/dist/semantics/normalize/structural-receiver.js +23 -1
  370. package/dist/semantics/normalize/structural-self-reference.d.ts +1 -1
  371. package/dist/semantics/normalize/structural-self-reference.js +23 -2
  372. package/dist/semantics/normalize/structural.d.ts +12 -1
  373. package/dist/semantics/normalize/structural.js +1420 -53
  374. package/dist/semantics/normalize/suppressed-write-arms.d.ts +47 -0
  375. package/dist/semantics/normalize/suppressed-write-arms.js +393 -0
  376. package/dist/semantics/normalize/unresolvable-names.d.ts +15 -2
  377. package/dist/semantics/normalize/unresolvable-names.js +30 -6
  378. package/dist/semantics/normalize/unshared-array-result.d.ts +32 -0
  379. package/dist/semantics/normalize/unshared-array-result.js +125 -0
  380. package/dist/semantics/package-sources.d.ts +1 -1
  381. package/dist/semantics/package-sources.js +120 -8
  382. package/dist/semantics/program.d.ts +20 -0
  383. package/dist/semantics/program.js +145 -14
  384. package/dist/semantics/stable-checker.d.ts +1 -1
  385. package/dist/semantics/stable-checker.js +7 -2
  386. package/dist/semantics/unchecked-guard-argument-copies.d.ts +67 -0
  387. package/dist/semantics/unchecked-guard-argument-copies.js +163 -0
  388. package/dist/semantics/unchecked-write-member-declarations.d.ts +46 -0
  389. package/dist/semantics/unchecked-write-member-declarations.js +331 -0
  390. package/dist/targets/cpp/balanced-units.d.ts +31 -0
  391. package/dist/targets/cpp/balanced-units.js +57 -0
  392. package/dist/targets/cpp/borrowed-arm-projections.d.ts +31 -0
  393. package/dist/targets/cpp/borrowed-arm-projections.js +106 -0
  394. package/dist/targets/cpp/borrowed-call-entry.d.ts +1 -1
  395. package/dist/targets/cpp/borrowed-call-entry.js +24 -20
  396. package/dist/targets/cpp/captures.js +314 -8
  397. package/dist/targets/cpp/char-code-buffers.d.ts +36 -0
  398. package/dist/targets/cpp/char-code-buffers.js +214 -0
  399. package/dist/targets/cpp/class-layout.d.ts +7 -1
  400. package/dist/targets/cpp/class-layout.js +42 -17
  401. package/dist/targets/cpp/class-properties/computed-method-value.d.ts +17 -2
  402. package/dist/targets/cpp/class-properties/computed-method-value.js +50 -4
  403. package/dist/targets/cpp/class-properties/emit-class-properties.d.ts +31 -1
  404. package/dist/targets/cpp/class-properties/emit-class-properties.js +174 -17
  405. package/dist/targets/cpp/class-ref-transport.d.ts +32 -0
  406. package/dist/targets/cpp/class-ref-transport.js +56 -1
  407. package/dist/targets/cpp/conversions.js +1827 -1502
  408. package/dist/targets/cpp/coroutine-bodies.d.ts +97 -0
  409. package/dist/targets/cpp/coroutine-bodies.js +187 -0
  410. package/dist/targets/cpp/document.js +11 -1
  411. package/dist/targets/cpp/emit-allocation.js +535 -21
  412. package/dist/targets/cpp/emit-arrays.d.ts +10 -0
  413. package/dist/targets/cpp/emit-arrays.js +147 -16
  414. package/dist/targets/cpp/emit-binding-reference.d.ts +26 -2
  415. package/dist/targets/cpp/emit-binding-reference.js +97 -5
  416. package/dist/targets/cpp/emit-bindings.d.ts +9 -1
  417. package/dist/targets/cpp/emit-bindings.js +108 -8
  418. package/dist/targets/cpp/emit-buffers.d.ts +2 -16
  419. package/dist/targets/cpp/emit-buffers.js +124 -25
  420. package/dist/targets/cpp/emit-callable.d.ts +35 -72
  421. package/dist/targets/cpp/emit-callable.js +647 -82
  422. package/dist/targets/cpp/emit-carrier-members.d.ts +13 -2
  423. package/dist/targets/cpp/emit-carrier-members.js +50 -9
  424. package/dist/targets/cpp/emit-constructor-identity-family.d.ts +20 -0
  425. package/dist/targets/cpp/emit-constructor-identity-family.js +45 -0
  426. package/dist/targets/cpp/emit-context.d.ts +329 -15
  427. package/dist/targets/cpp/emit-context.js +104 -13
  428. package/dist/targets/cpp/emit-dynamic-properties.d.ts +44 -0
  429. package/dist/targets/cpp/emit-dynamic-properties.js +256 -37
  430. package/dist/targets/cpp/emit-equality.js +117 -2
  431. package/dist/targets/cpp/emit-error-constructor.d.ts +19 -0
  432. package/dist/targets/cpp/emit-error-constructor.js +73 -0
  433. package/dist/targets/cpp/emit-exceptions.d.ts +25 -3
  434. package/dist/targets/cpp/emit-exceptions.js +215 -26
  435. package/dist/targets/cpp/emit-in.js +52 -5
  436. package/dist/targets/cpp/emit-instanceof.js +59 -11
  437. package/dist/targets/cpp/emit-integers.d.ts +6 -0
  438. package/dist/targets/cpp/emit-integers.js +10 -0
  439. package/dist/targets/cpp/emit-iterable-object-view.d.ts +15 -0
  440. package/dist/targets/cpp/emit-iterable-object-view.js +51 -0
  441. package/dist/targets/cpp/emit-iterator-object-view.d.ts +15 -0
  442. package/dist/targets/cpp/emit-iterator-object-view.js +125 -0
  443. package/dist/targets/cpp/emit-iterator.d.ts +7 -10
  444. package/dist/targets/cpp/emit-iterator.js +361 -24
  445. package/dist/targets/cpp/emit-json.d.ts +7 -1
  446. package/dist/targets/cpp/emit-json.js +595 -55
  447. package/dist/targets/cpp/emit-jsx.d.ts +0 -37
  448. package/dist/targets/cpp/emit-jsx.js +70 -8
  449. package/dist/targets/cpp/emit-narrowing.d.ts +192 -28
  450. package/dist/targets/cpp/emit-narrowing.js +1595 -120
  451. package/dist/targets/cpp/emit-native-selection.js +8 -2
  452. package/dist/targets/cpp/emit-presence.js +13 -0
  453. package/dist/targets/cpp/emit-properties.d.ts +3 -1
  454. package/dist/targets/cpp/emit-properties.js +303 -54
  455. package/dist/targets/cpp/emit-protocol-iterator.d.ts +14 -0
  456. package/dist/targets/cpp/emit-protocol-iterator.js +203 -0
  457. package/dist/targets/cpp/emit-proxy.d.ts +17 -0
  458. package/dist/targets/cpp/emit-proxy.js +35 -0
  459. package/dist/targets/cpp/emit-record-view.d.ts +12 -1
  460. package/dist/targets/cpp/emit-record-view.js +429 -18
  461. package/dist/targets/cpp/emit-return.d.ts +38 -0
  462. package/dist/targets/cpp/emit-return.js +143 -21
  463. package/dist/targets/cpp/emit-scopes.d.ts +78 -0
  464. package/dist/targets/cpp/emit-scopes.js +196 -0
  465. package/dist/targets/cpp/emit-sum-widening.js +6 -3
  466. package/dist/targets/cpp/emit-tonumber.d.ts +40 -1
  467. package/dist/targets/cpp/emit-tonumber.js +75 -4
  468. package/dist/targets/cpp/emit-tostring.d.ts +28 -2
  469. package/dist/targets/cpp/emit-tostring.js +115 -12
  470. package/dist/targets/cpp/emit-typeof.js +2 -2
  471. package/dist/targets/cpp/emit-union-properties.d.ts +9 -0
  472. package/dist/targets/cpp/emit-union-properties.js +375 -46
  473. package/dist/targets/cpp/emit.d.ts +3 -1
  474. package/dist/targets/cpp/emit.js +725 -90
  475. package/dist/targets/cpp/evaluated-once.d.ts +51 -0
  476. package/dist/targets/cpp/evaluated-once.js +140 -0
  477. package/dist/targets/cpp/host/core-globals.d.ts +0 -21
  478. package/dist/targets/cpp/host/core-globals.js +10 -2
  479. package/dist/targets/cpp/host/emit-host-invoke.d.ts +0 -10
  480. package/dist/targets/cpp/host/emit-host-invoke.js +454 -36
  481. package/dist/targets/cpp/host/emit-host-object.js +733 -88
  482. package/dist/targets/cpp/host/emit-host-properties.js +26 -2
  483. package/dist/targets/cpp/host/emit-host-reflect.d.ts +1 -0
  484. package/dist/targets/cpp/host/emit-host-reflect.js +26 -7
  485. package/dist/targets/cpp/host/emit-host-value.d.ts +13 -0
  486. package/dist/targets/cpp/host/emit-host-value.js +47 -3
  487. package/dist/targets/cpp/host/host-members.d.ts +11 -0
  488. package/dist/targets/cpp/host/host-members.js +50 -4
  489. package/dist/targets/cpp/host/native-protocols.js +3 -0
  490. package/dist/targets/cpp/host/object-protocol.d.ts +8 -0
  491. package/dist/targets/cpp/host/object-protocol.js +55 -13
  492. package/dist/targets/cpp/key-order-tracking.d.ts +11 -0
  493. package/dist/targets/cpp/key-order-tracking.js +18 -0
  494. package/dist/targets/cpp/manifest/capabilities.js +107 -8
  495. package/dist/targets/cpp/manifest.js +14 -0
  496. package/dist/targets/cpp/native-collection-base.d.ts +27 -0
  497. package/dist/targets/cpp/native-collection-base.js +202 -0
  498. package/dist/targets/cpp/native-error-base.d.ts +14 -0
  499. package/dist/targets/cpp/native-error-base.js +19 -4
  500. package/dist/targets/cpp/native-promise-base.d.ts +23 -0
  501. package/dist/targets/cpp/native-promise-base.js +30 -0
  502. package/dist/targets/cpp/native-symbol-keys.js +1 -1
  503. package/dist/targets/cpp/prototype/emit-prototype-array.js +258 -20
  504. package/dist/targets/cpp/prototype/emit-prototype-date.js +5 -4
  505. package/dist/targets/cpp/prototype/emit-prototype-invoke.d.ts +9 -1
  506. package/dist/targets/cpp/prototype/emit-prototype-invoke.js +232 -26
  507. package/dist/targets/cpp/prototype/emit-prototype-iterator.d.ts +1 -1
  508. package/dist/targets/cpp/prototype/emit-prototype-iterator.js +111 -13
  509. package/dist/targets/cpp/prototype/emit-prototype-promise.d.ts +33 -4
  510. package/dist/targets/cpp/prototype/emit-prototype-promise.js +218 -10
  511. package/dist/targets/cpp/prototype/emit-prototype-regexp.js +8 -5
  512. package/dist/targets/cpp/prototype/emit-prototype-string.js +24 -7
  513. package/dist/targets/cpp/prototype/prototype-method-reads.js +18 -3
  514. package/dist/targets/cpp/reactive-dependencies.d.ts +2 -0
  515. package/dist/targets/cpp/reactive-dependencies.js +94 -3
  516. package/dist/targets/cpp/realm-storage.d.ts +5 -0
  517. package/dist/targets/cpp/realm-storage.js +12 -0
  518. package/dist/targets/cpp/records.d.ts +151 -1
  519. package/dist/targets/cpp/records.js +1321 -122
  520. package/dist/targets/cpp/recursive-containers.d.ts +4 -1
  521. package/dist/targets/cpp/recursive-containers.js +28 -9
  522. package/dist/targets/cpp/translation-unit.d.ts +10 -0
  523. package/dist/targets/cpp/translation-unit.js +686 -140
  524. package/dist/targets/cpp/types.d.ts +56 -6
  525. package/dist/targets/cpp/types.js +218 -12
  526. package/dist/targets/cpp/virtual-methods.d.ts +3 -3
  527. package/dist/targets/cpp/virtual-methods.js +174 -17
  528. package/package.json +6 -5
  529. package/src/targets/cpp/runtime/gea_dynamic_proxy.h +155 -25
  530. package/src/targets/cpp/runtime/gea_eval.h +3 -0
  531. package/src/targets/cpp/runtime/gea_pcm.h +116 -0
  532. package/src/targets/cpp/runtime/gea_runtime.h +18096 -4876
  533. package/src/targets/cpp/runtime/gea_runtime_builtins.cpp +7 -45
  534. package/src/targets/cpp/runtime/gea_runtime_rtc.h +247 -0
@@ -1,13 +1,13 @@
1
1
  import { resolve } from 'node:path';
2
2
  import ts from 'typescript';
3
- import { valueSymbolAt } from './normalize/unresolvable-names.js';
4
- const resolvedSymbolAt = (checker, node) => {
3
+ import { noHostProvidedNames, valueSymbolAt } from './normalize/unresolvable-names.js';
4
+ const resolvedSymbolAt = (checker, node, hostProvided) => {
5
5
  // One rule for what a name in value position denotes (`valueSymbolAt`),
6
6
  // including checked JavaScript's `module`/`exports`, which the checker binds
7
7
  // to the file's own export synthesis rather than to the wrapper parameter
8
8
  // the host declared. Provenance is a question about that lexical
9
9
  // declaration, and the reference producer keys the same declaration.
10
- const local = valueSymbolAt(checker, node);
10
+ const local = valueSymbolAt(checker, node, hostProvided);
11
11
  if (!local)
12
12
  return null;
13
13
  return (local.flags & ts.SymbolFlags.Alias) !== 0 ? checker.getAliasedSymbol(local) : local;
@@ -70,6 +70,11 @@ const exactCompatibleDeclaration = (declaration, configured) => (configured.comp
70
70
  * nothing is never CommonJS.
71
71
  */
72
72
  export const isCommonJsSourceFile = (file, formats) => {
73
+ // A `.json` module is loaded by the CommonJS loader whatever its package's
74
+ // `type` (`program.ts`'s `jsonModuleAsTypeScript`), and the checker's walk
75
+ // places no `.json` at all.
76
+ if (file.fileName.endsWith('.json'))
77
+ return true;
73
78
  let format = file.impliedNodeFormat;
74
79
  if (format === undefined) {
75
80
  if (formats.has(file))
@@ -149,7 +154,7 @@ const wrapperRedeclarationFor = (declaration, configured, formats) => {
149
154
  * already introduced that exact parameter cell. No block binding, nested var,
150
155
  * local function, import, or caller-owned ambient declaration is admitted.
151
156
  */
152
- export const createCommonJsWrapperIdentity = (checker, files, globals) => {
157
+ export const createCommonJsWrapperIdentity = (checker, files, globals, hostProvided = noHostProvidedNames) => {
153
158
  const configuredByName = new Map();
154
159
  for (const [name, configured] of globals) {
155
160
  if (name === configured.declarationName && name === configured.global)
@@ -163,7 +168,7 @@ export const createCommonJsWrapperIdentity = (checker, files, globals) => {
163
168
  if (ts.isIdentifier(node) && ts.isVariableDeclaration(node.parent) && node.parent.name === node) {
164
169
  const configured = configuredByName.get(node.text);
165
170
  if (configured && exactConfiguredDeclaration(node.parent, configured)) {
166
- const symbol = resolvedSymbolAt(checker, node);
171
+ const symbol = resolvedSymbolAt(checker, node, hostProvided);
167
172
  if (symbol) {
168
173
  const declarations = symbol.declarations ?? [];
169
174
  const complete = declarations.length > 0 &&
@@ -194,7 +199,7 @@ export const createCommonJsWrapperIdentity = (checker, files, globals) => {
194
199
  const configured = configuredByName.get(identifier.text);
195
200
  if (!configured || !authenticatedGlobals.has(configured.global))
196
201
  continue;
197
- const symbol = resolvedSymbolAt(checker, identifier);
202
+ const symbol = resolvedSymbolAt(checker, identifier, hostProvided);
198
203
  if (!symbol)
199
204
  continue;
200
205
  const perFile = new Map(redeclarations.get(symbol) ?? []);
@@ -210,8 +215,20 @@ export const createCommonJsWrapperIdentity = (checker, files, globals) => {
210
215
  };
211
216
  visit(file);
212
217
  }
218
+ // Every census pass classifies every identifier it evaluates again, and the
219
+ // answer is a function of the identifier alone (its resolved symbol and its
220
+ // file, against tables fixed above), so it is asked of the checker once.
221
+ const classified = new Map();
213
222
  const classify = (identifier) => {
214
- const symbol = resolvedSymbolAt(checker, identifier);
223
+ const held = classified.get(identifier);
224
+ if (held !== undefined)
225
+ return held;
226
+ const identity = classifyUncached(identifier);
227
+ classified.set(identifier, identity);
228
+ return identity;
229
+ };
230
+ const classifyUncached = (identifier) => {
231
+ const symbol = resolvedSymbolAt(checker, identifier, hostProvided);
215
232
  if (!symbol)
216
233
  return { kind: 'ordinary' };
217
234
  const redeclared = redeclarations.get(symbol)?.get(identifier.getSourceFile());
@@ -0,0 +1,4 @@
1
+ export declare const constructorFunctionClassSourceTransform: (input: {
2
+ readonly fileName: string;
3
+ readonly text: string;
4
+ }) => string | null;
@@ -0,0 +1,327 @@
1
+ import ts from 'typescript';
2
+ /**
3
+ * A pre-class constructor function and the methods its script assigns onto
4
+ * `F.prototype`, rewritten into the class it is.
5
+ *
6
+ * ## The shape
7
+ *
8
+ * `sparse-bitfield` and `memory-pager` (under `@mongodb-js/saslprep`, which the
9
+ * MongoDB driver's SCRAM authentication needs) are ES5:
10
+ *
11
+ * function Pager (pageSize, opts) {
12
+ * if (!(this instanceof Pager)) return new Pager(pageSize, opts)
13
+ * this.length = 0
14
+ * ...
15
+ * }
16
+ * Pager.prototype.get = function (i, noAllocate) { ... }
17
+ *
18
+ * The checker infers the instance type from the `this.x =` writes and the
19
+ * prototype assignments, but an instance of such a function is a record with
20
+ * no prototype chain and no method table, so every method read is refused
21
+ * (`producers/properties.ts`, "model the constructor as the class it is").
22
+ * A class states the same members in the one form every later layer already
23
+ * compiles: the constructor body becomes `constructor`, each prototype
24
+ * assignment becomes a method.
25
+ *
26
+ * ## What the rewrite preserves, and what it does not
27
+ *
28
+ * - Every body keeps its text, parameters and JSDoc.
29
+ * - When the class exists: a function declaration is hoisted, a class is not,
30
+ * and memory-pager's first line is `module.exports = Pager`. The class goes
31
+ * at the head of the file, after the directive prologue. Its definition has
32
+ * no heritage clause, no computed key and no field, so evaluating it runs
33
+ * nothing and reads nothing -- exactly as early and as inert as the hoisted
34
+ * function it replaces.
35
+ * - The call-without-`new` guard (`if (!(this instanceof F)) return new F(a,
36
+ * b)`, the whole guard, forwarding every parameter in order) says a plain
37
+ * `F(a, b)` IS `new F(a, b)`. A class cannot be called, so a guarded `F`
38
+ * stays a function -- one whose body is that construction -- and the class
39
+ * takes the name `F$class`. Every `new F(...)` and `instanceof F` in the file
40
+ * names the class. `new F(...)` from another module still answers the class
41
+ * instance the factory returns; `x instanceof F` there reads the factory's
42
+ * own, unrelated `prototype` and answers `false`.
43
+ * - Class bodies are strict code. A sloppy-mode body that relied on sloppy
44
+ * semantics would change; neither package does.
45
+ *
46
+ * Refused, leaving the file as written: a prototype member that is not a
47
+ * plain `function` expression (a data value, an arrow), one assigned twice or
48
+ * named `constructor`, `F.prototype` read anywhere else, `F` used as a value
49
+ * (passed, stored, given static members, inherited from), a constructor that
50
+ * returns a value or reads `new.target`, a named method expression that reads
51
+ * its own name, and an unguarded `F` called without `new`.
52
+ *
53
+ * Text in, text out, spliced from the end backward, like the other transforms
54
+ * in this directory.
55
+ */
56
+ const isJavaScriptFile = (fileName) => fileName.endsWith('.js') || fileName.endsWith('.mjs') || fileName.endsWith('.cjs');
57
+ const unwrap = (expression) => ts.isParenthesizedExpression(expression) ? unwrap(expression.expression) : expression;
58
+ /** `F.prototype.name = function (...) {...}` as a top-level statement. */
59
+ const prototypeMethodOf = (statement, className) => {
60
+ if (!ts.isExpressionStatement(statement))
61
+ return null;
62
+ const assignment = statement.expression;
63
+ if (!ts.isBinaryExpression(assignment) || assignment.operatorToken.kind !== ts.SyntaxKind.EqualsToken)
64
+ return null;
65
+ const target = assignment.left;
66
+ if (!ts.isPropertyAccessExpression(target) || !ts.isIdentifier(target.name))
67
+ return null;
68
+ const owner = target.expression;
69
+ if (!ts.isPropertyAccessExpression(owner) ||
70
+ owner.name.text !== 'prototype' ||
71
+ !ts.isIdentifier(owner.expression) ||
72
+ owner.expression.text !== className)
73
+ return null;
74
+ const value = unwrap(assignment.right);
75
+ if (!ts.isFunctionExpression(value))
76
+ return null;
77
+ return { statement, name: target.name.text, value };
78
+ };
79
+ /** Whether `node` reads `name` as an identifier anywhere, ignoring property-name positions. */
80
+ const readsName = (node, name) => {
81
+ const visit = (child) => {
82
+ if (ts.isIdentifier(child) && child.text === name) {
83
+ const parent = child.parent;
84
+ if (ts.isPropertyAccessExpression(parent) && parent.name === child)
85
+ return false;
86
+ if ((ts.isPropertyAssignment(parent) || ts.isMethodDeclaration(parent)) && parent.name === child)
87
+ return false;
88
+ return true;
89
+ }
90
+ return ts.forEachChild(child, visit) ?? false;
91
+ };
92
+ return visit(node);
93
+ };
94
+ /** Whether the function's own body (not a nested function's) returns a value or reads `new.target`. */
95
+ const constructorBodyRefused = (body, guard) => {
96
+ const visit = (node) => {
97
+ if (node === guard)
98
+ return false;
99
+ if (ts.isMetaProperty(node) && node.keywordToken === ts.SyntaxKind.NewKeyword)
100
+ return true;
101
+ if (ts.isReturnStatement(node) && node.expression !== undefined)
102
+ return true;
103
+ if (ts.isFunctionLike(node) && !ts.isArrowFunction(node))
104
+ return false;
105
+ if (ts.isClassLike(node))
106
+ return false;
107
+ return ts.forEachChild(node, visit) ?? false;
108
+ };
109
+ return ts.forEachChild(body, visit) ?? false;
110
+ };
111
+ const readsNewTarget = (node) => (ts.isMetaProperty(node) && node.keywordToken === ts.SyntaxKind.NewKeyword) || (ts.forEachChild(node, readsNewTarget) ?? false);
112
+ /** `if (!(this instanceof F)) return new F(<every parameter, in order>)`, the whole first statement. */
113
+ const callGuardOf = (fn, className) => {
114
+ const first = fn.body?.statements[0];
115
+ if (!first || !ts.isIfStatement(first) || first.elseStatement)
116
+ return null;
117
+ const test = unwrap(first.expression);
118
+ if (!ts.isPrefixUnaryExpression(test) || test.operator !== ts.SyntaxKind.ExclamationToken)
119
+ return null;
120
+ const check = unwrap(test.operand);
121
+ if (!ts.isBinaryExpression(check) ||
122
+ check.operatorToken.kind !== ts.SyntaxKind.InstanceOfKeyword ||
123
+ check.left.kind !== ts.SyntaxKind.ThisKeyword ||
124
+ !ts.isIdentifier(check.right) ||
125
+ check.right.text !== className)
126
+ return null;
127
+ let then = first.thenStatement;
128
+ if (ts.isBlock(then)) {
129
+ if (then.statements.length !== 1)
130
+ return null;
131
+ then = then.statements[0];
132
+ }
133
+ if (!ts.isReturnStatement(then) || !then.expression)
134
+ return null;
135
+ const construction = unwrap(then.expression);
136
+ if (!ts.isNewExpression(construction) || !ts.isIdentifier(construction.expression) || construction.expression.text !== className)
137
+ return null;
138
+ const args = construction.arguments ?? ts.factory.createNodeArray();
139
+ if (args.length !== fn.parameters.length)
140
+ return null;
141
+ for (const [index, parameter] of fn.parameters.entries()) {
142
+ const arg = args[index];
143
+ if (!ts.isIdentifier(parameter.name) || parameter.dotDotDotToken || parameter.initializer)
144
+ return null;
145
+ if (!ts.isIdentifier(arg) || arg.text !== parameter.name.text)
146
+ return null;
147
+ }
148
+ return first;
149
+ };
150
+ /** How one read of the name `F` uses it. */
151
+ const useOf = (identifier, fn, methods) => {
152
+ const parent = identifier.parent;
153
+ if (parent === fn)
154
+ return 'declaration';
155
+ if (ts.isNewExpression(parent) && parent.expression === identifier)
156
+ return 'construct';
157
+ if (ts.isCallExpression(parent) && parent.expression === identifier)
158
+ return 'call';
159
+ if (ts.isBinaryExpression(parent) && parent.right === identifier && parent.operatorToken.kind === ts.SyntaxKind.InstanceOfKeyword)
160
+ return 'instanceof';
161
+ if (ts.isPropertyAccessExpression(parent) && parent.expression === identifier && parent.name.text === 'prototype') {
162
+ const member = parent.parent;
163
+ return ts.isPropertyAccessExpression(member) && methods.has(member.parent) ? 'prototype-member' : 'other';
164
+ }
165
+ if (ts.isExportAssignment(parent))
166
+ return 'export';
167
+ // `module.exports = F`, `exports.name = F`, `module.exports.name = F`.
168
+ if (ts.isBinaryExpression(parent) && parent.right === identifier && parent.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
169
+ const text = parent.left.getText();
170
+ if (/^(?:module\.exports|exports|module\.exports\.[\w$]+|exports\.[\w$]+)$/.test(text))
171
+ return 'export';
172
+ }
173
+ return 'other';
174
+ };
175
+ /** Every identifier reading `name` outside property-name positions. */
176
+ const identifiersNamed = (file, name) => {
177
+ const found = [];
178
+ const visit = (node) => {
179
+ if (ts.isIdentifier(node) && node.text === name) {
180
+ const parent = node.parent;
181
+ const namePosition = (ts.isPropertyAccessExpression(parent) && parent.name === node) ||
182
+ ((ts.isPropertyAssignment(parent) || ts.isMethodDeclaration(parent) || ts.isPropertyDeclaration(parent)) && parent.name === node);
183
+ if (!namePosition)
184
+ found.push(node);
185
+ }
186
+ ts.forEachChild(node, visit);
187
+ };
188
+ visit(file);
189
+ return found;
190
+ };
191
+ /** A statement's own leading comments (its JSDoc), from its full start to its first token. */
192
+ const leadingText = (node, file) => {
193
+ const text = file.text.slice(node.getFullStart(), node.getStart(file)).trim();
194
+ return text.length > 0 ? `${text}\n ` : '';
195
+ };
196
+ /** Source text of `[start, end)` with the rename edits inside it applied. */
197
+ const sliceWith = (file, start, end, renames) => {
198
+ let text = '';
199
+ let at = start;
200
+ for (const rename of renames) {
201
+ if (rename.start < start || rename.end > end)
202
+ continue;
203
+ text += file.text.slice(at, rename.start) + rename.text;
204
+ at = rename.end;
205
+ }
206
+ return text + file.text.slice(at, end);
207
+ };
208
+ const parameterListText = (parameters, file, renames) => parameters.length === 0 ? '' : sliceWith(file, parameters[0].getStart(file), parameters.end, renames);
209
+ /** The rewrite of one constructor function, or null when any use falls outside what the class states. */
210
+ const rewriteOf = (fn, file) => {
211
+ const className = fn.name.text;
212
+ if (fn.typeParameters || fn.asteriskToken || fn.modifiers?.length)
213
+ return null;
214
+ const methods = [];
215
+ for (const statement of file.statements) {
216
+ const method = prototypeMethodOf(statement, className);
217
+ if (method)
218
+ methods.push(method);
219
+ }
220
+ if (methods.length === 0)
221
+ return null;
222
+ const names = new Set();
223
+ for (const method of methods) {
224
+ if (method.name === 'constructor' || names.has(method.name))
225
+ return null;
226
+ names.add(method.name);
227
+ if (readsNewTarget(method.value.body))
228
+ return null;
229
+ if (method.value.name && readsName(method.value.body, method.value.name.text))
230
+ return null;
231
+ }
232
+ const guard = callGuardOf(fn, className);
233
+ if (constructorBodyRefused(fn.body, guard))
234
+ return null;
235
+ const methodStatements = new Set(methods.map((method) => method.statement.expression));
236
+ const uses = identifiersNamed(file, className).map((identifier) => ({ identifier, use: useOf(identifier, fn, methodStatements) }));
237
+ if (uses.some(({ use }) => use === 'other'))
238
+ return null;
239
+ if (!guard && uses.some(({ use }) => use === 'call'))
240
+ return null;
241
+ const instanceName = guard ? `${className}$class` : className;
242
+ if (guard && identifiersNamed(file, instanceName).length > 0)
243
+ return null;
244
+ const renames = guard
245
+ ? uses
246
+ .filter(({ identifier, use }) => {
247
+ if (use !== 'construct' && use !== 'instanceof')
248
+ return false;
249
+ // The guard's own `this instanceof F` / `new F(...)` is dropped with the guard.
250
+ return identifier.getStart(file) < guard.getStart(file) || identifier.getStart(file) >= guard.end;
251
+ })
252
+ .map(({ identifier }) => ({ start: identifier.getStart(file), end: identifier.end, text: instanceName }))
253
+ .sort((a, b) => a.start - b.start)
254
+ : [];
255
+ const body = fn.body;
256
+ const bodyText = guard
257
+ ? `{${sliceWith(file, body.getStart(file) + 1, guard.getFullStart(), renames)}${sliceWith(file, guard.end, body.end, renames)}`
258
+ : sliceWith(file, body.getStart(file), body.end, renames);
259
+ const members = [
260
+ `${leadingText(fn, file)}constructor(${parameterListText(fn.parameters, file, renames)}) ${bodyText}`,
261
+ ...methods.map((method) => {
262
+ const value = method.value;
263
+ const prefix = `${value.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword) ? 'async ' : ''}${value.asteriskToken ? '*' : ''}`;
264
+ return `${leadingText(method.statement, file)}${prefix}${method.name}(${parameterListText(value.parameters, file, renames)}) ${sliceWith(file, value.body.getStart(file), value.body.end, renames)}`;
265
+ })
266
+ ];
267
+ const classText = `class ${instanceName} {\n ${members.join('\n\n ')}\n}\n`;
268
+ const edits = [
269
+ ...methods.map((method) => ({ start: method.statement.getFullStart(), end: method.statement.end, text: '' })),
270
+ ...renames
271
+ .filter((rename) => rename.start < fn.getFullStart() || rename.start >= fn.end)
272
+ .filter((rename) => !methods.some((method) => rename.start >= method.statement.getFullStart() && rename.end <= method.statement.end)),
273
+ {
274
+ start: fn.getFullStart(),
275
+ end: fn.end,
276
+ text: guard
277
+ ? `${file.text.slice(fn.getFullStart(), fn.getStart(file))}function ${className}(${parameterListText(fn.parameters, file, [])}) {\n return new ${instanceName}(${fn.parameters
278
+ .map((parameter) => parameter.name.getText(file))
279
+ .join(', ')})\n}`
280
+ : ''
281
+ }
282
+ ];
283
+ return { classText, edits };
284
+ };
285
+ /** Where a hoisted class goes: after a shebang line and the directive prologue (`'use strict'`), so both stay what they are. */
286
+ const headOf = (file) => {
287
+ let at = file.text.startsWith('#!') ? file.text.indexOf('\n') + 1 || file.text.length : 0;
288
+ for (const statement of file.statements) {
289
+ if (!ts.isExpressionStatement(statement) || !ts.isStringLiteral(statement.expression))
290
+ break;
291
+ at = statement.end;
292
+ }
293
+ return at;
294
+ };
295
+ /**
296
+ * One constructor per pass, re-parsed between passes: a second constructor's
297
+ * body can name the first (`new Page(...)` inside `Pager`), and those renames
298
+ * must land in the text the second rewrite slices, not in a region it deletes.
299
+ */
300
+ const rewriteOne = (fileName, text, done) => {
301
+ const file = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, ts.ScriptKind.JS);
302
+ for (const statement of file.statements) {
303
+ if (!ts.isFunctionDeclaration(statement) || !statement.name || !statement.body || done.has(statement.name.text))
304
+ continue;
305
+ const rewrite = rewriteOf(statement, file);
306
+ if (!rewrite)
307
+ continue;
308
+ const head = headOf(file);
309
+ const edits = [...rewrite.edits, { start: head, end: head, text: `${head > 0 ? '\n' : ''}${rewrite.classText}` }];
310
+ let rewritten = text;
311
+ for (const edit of edits.sort((a, b) => b.start - a.start || b.end - a.end))
312
+ rewritten = `${rewritten.slice(0, edit.start)}${edit.text}${rewritten.slice(edit.end)}`;
313
+ return { text: rewritten, name: statement.name.text };
314
+ }
315
+ return null;
316
+ };
317
+ export const constructorFunctionClassSourceTransform = (input) => {
318
+ if (!isJavaScriptFile(input.fileName) || !input.text.includes('.prototype.'))
319
+ return null;
320
+ const done = new Set();
321
+ let text = input.text;
322
+ for (let pass = rewriteOne(input.fileName, text, done); pass; pass = rewriteOne(input.fileName, text, done)) {
323
+ done.add(pass.name);
324
+ text = pass.text;
325
+ }
326
+ return done.size === 0 ? null : text;
327
+ };
@@ -36,12 +36,93 @@ export const constructorSlotSubclassesOf = (checker, identities, files, heritage
36
36
  return null;
37
37
  return identities.declarationIdOf(node);
38
38
  };
39
+ // `x.constructor` is the constructor of whichever class `x` was allocated
40
+ // as: its static class or any class extending it. `lib.es5.d.ts` types the
41
+ // read `Function`, so a program stores it into a `typeof Base` slot through
42
+ // `as any` -- `mongodb-connection-string-url` hands
43
+ // `this.searchParams.constructor as any` to its `typeof URLSearchParams`
44
+ // mixin parameter -- and every class it can be is a write of its own.
45
+ const constructorReadClassOf = (source) => {
46
+ let inner = source;
47
+ while (ts.isParenthesizedExpression(inner) ||
48
+ ts.isAsExpression(inner) ||
49
+ ts.isNonNullExpression(inner) ||
50
+ ts.isSatisfiesExpression(inner))
51
+ inner = inner.expression;
52
+ if (!ts.isPropertyAccessExpression(inner) || inner.name.text !== 'constructor')
53
+ return null;
54
+ const instance = checker.getTypeAtLocation(inner.expression);
55
+ const symbol = instance.getSymbol();
56
+ if (!symbol || (symbol.flags & ts.SymbolFlags.Class) === 0)
57
+ return null;
58
+ const node = symbol.declarations?.find((candidate) => ts.isClassLike(candidate));
59
+ if (!node || (instance.target ?? instance) !== checker.getDeclaredTypeOfSymbol(symbol))
60
+ return null;
61
+ return identities.declarationIdOf(node);
62
+ };
39
63
  const record = (target, source) => {
40
64
  if (!target)
41
65
  return;
42
- const derived = classOfConstructorType(checker.getTypeAtLocation(source));
66
+ const read = constructorReadClassOf(source);
67
+ if (read !== null) {
68
+ for (const [candidate, ancestors] of heritage)
69
+ if (candidate === read || ancestors.includes(read))
70
+ recordDerived(target, candidate);
71
+ recordDerived(target, read);
72
+ return;
73
+ }
74
+ const derivedType = checker.getTypeAtLocation(source);
75
+ const derived = classOfConstructorType(derivedType);
43
76
  if (derived === null)
44
77
  return;
78
+ recordDerived(target, derived);
79
+ recordThroughConvention(target, derivedType, derived);
80
+ };
81
+ /**
82
+ * A class stored into a STRUCTURAL constructor slot -- `{ new (bytes):
83
+ * Reply; make(bytes): Reply }` -- is a write into every ancestor's
84
+ * `typeof Base` that fits the same slot.
85
+ *
86
+ * The statement names no class, but the census may hold the slot to the
87
+ * classes its callers pass (`narrowsStructuralConstructorToClasses`), and
88
+ * the checker then subtype-reduces `typeof Derived | typeof Base` to
89
+ * `typeof Base`: mongodb's `responseType ?? MongoDBResponse` over a
90
+ * `CursorResponse` argument is typed `typeof MongoDBResponse`. That family
91
+ * has to name the subclass, or the checked projection into it refuses the
92
+ * very class the caller handed in.
93
+ */
94
+ const recordThroughConvention = (target, derivedType, derived) => {
95
+ const conventions = (target.isUnion() ? target.types : [target]).filter((arm) => (arm.flags & ts.TypeFlags.Object) !== 0 &&
96
+ classOfConstructorType(arm) === null &&
97
+ arm.getConstructSignatures().length > 0 &&
98
+ arm.getCallSignatures().length === 0);
99
+ if (conventions.length === 0)
100
+ return;
101
+ const symbol = derivedType.getSymbol();
102
+ if (!symbol)
103
+ return;
104
+ const seen = new Set();
105
+ let frontier = checker.getBaseTypes(checker.getDeclaredTypeOfSymbol(symbol));
106
+ while (frontier.length > 0) {
107
+ const next = [];
108
+ for (const base of frontier) {
109
+ const target = base.objectFlags & ts.ObjectFlags.Reference ? base.target : base;
110
+ if (seen.has(target))
111
+ continue;
112
+ seen.add(target);
113
+ const baseSymbol = target.getSymbol();
114
+ if (!baseSymbol || (baseSymbol.flags & ts.SymbolFlags.Class) === 0)
115
+ continue;
116
+ const baseConstructor = checker.getTypeOfSymbol(baseSymbol);
117
+ if (conventions.some((convention) => checker.isTypeAssignableTo(baseConstructor, convention)))
118
+ recordDerived(baseConstructor, derived);
119
+ if (target.isClassOrInterface())
120
+ next.push(...checker.getBaseTypes(target));
121
+ }
122
+ frontier = next;
123
+ }
124
+ };
125
+ const recordDerived = (target, derived) => {
45
126
  const ancestors = heritage.get(derived) ?? [];
46
127
  for (const arm of target.isUnion() ? target.types : [target]) {
47
128
  const base = classOfConstructorType(arm);
@@ -77,7 +158,84 @@ export const constructorSlotSubclassesOf = (checker, identities, files, heritage
77
158
  let inner = node;
78
159
  while (ts.isParenthesizedExpression(inner))
79
160
  inner = inner.expression;
80
- return ts.isIdentifier(inner) || ts.isPropertyAccessExpression(inner) || ts.isClassExpression(inner);
161
+ return (ts.isIdentifier(inner) ||
162
+ ts.isPropertyAccessExpression(inner) ||
163
+ ts.isClassExpression(inner) ||
164
+ constructorReadClassOf(inner) !== null);
165
+ };
166
+ // A conditional or a `??`/`||`/`&&` chain stores whichever arm it picks, so
167
+ // each arm that names a constructor is a write of its own:
168
+ // `this.RESPONSE_TYPE = explain ? ExplainedResponse : Response` puts either
169
+ // class in the slot.
170
+ const namedArmsOf = (node) => {
171
+ let inner = node;
172
+ while (ts.isParenthesizedExpression(inner))
173
+ inner = inner.expression;
174
+ if (ts.isConditionalExpression(inner))
175
+ return [...namedArmsOf(inner.whenTrue), ...namedArmsOf(inner.whenFalse)];
176
+ if (ts.isBinaryExpression(inner)) {
177
+ const operator = inner.operatorToken.kind;
178
+ if (operator === ts.SyntaxKind.AmpersandAmpersandToken)
179
+ return namedArmsOf(inner.right);
180
+ if (operator === ts.SyntaxKind.QuestionQuestionToken || operator === ts.SyntaxKind.BarBarToken) {
181
+ return [...namedArmsOf(inner.left), ...namedArmsOf(inner.right)];
182
+ }
183
+ }
184
+ return isNamedValue(inner) ? [inner] : [];
185
+ };
186
+ // A field that overrides a base member is the SAME slot as far as the base's
187
+ // code is concerned: `abstract RESPONSE_TYPE: typeof Response` read by the
188
+ // base's `new this.RESPONSE_TYPE(bytes)` sees what a subclass's
189
+ // `override RESPONSE_TYPE = ListResponse` (whose own type the checker
190
+ // narrows to `typeof ListResponse`) or `this.RESPONSE_TYPE = ...` stores. So
191
+ // a write into a class's own field is also a write into every same-named
192
+ // member of its base classes, at that member's declared type.
193
+ const overriddenSlotTypesOf = (owner, name, at) => {
194
+ const types = [];
195
+ const seen = new Set();
196
+ const pending = [];
197
+ const ownerType = checker.getTypeAtLocation(owner);
198
+ const pushBases = (type) => {
199
+ if (!(type.flags & ts.TypeFlags.Object) || !(type.objectFlags & ts.ObjectFlags.ClassOrInterface))
200
+ return;
201
+ for (const base of checker.getBaseTypes(type))
202
+ pending.push(base);
203
+ };
204
+ pushBases(ownerType);
205
+ while (pending.length > 0) {
206
+ const base = pending.pop();
207
+ if (seen.has(base))
208
+ continue;
209
+ seen.add(base);
210
+ const member = checker.getPropertyOfType(base, name);
211
+ if (member)
212
+ types.push(checker.getTypeOfSymbolAtLocation(member, at));
213
+ const target = base.target ?? base;
214
+ pushBases(target);
215
+ }
216
+ return types;
217
+ };
218
+ const recordArms = (target, value) => {
219
+ for (const arm of namedArmsOf(value))
220
+ record(target, arm);
221
+ };
222
+ const recordOverriddenWrites = (node) => {
223
+ if (ts.isPropertyDeclaration(node) && node.initializer && ts.isClassLike(node.parent) && !ts.isComputedPropertyName(node.name)) {
224
+ const name = node.name.getText();
225
+ for (const type of overriddenSlotTypesOf(node.parent, name, node))
226
+ recordArms(type, node.initializer);
227
+ return;
228
+ }
229
+ if (ts.isBinaryExpression(node) &&
230
+ node.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
231
+ ts.isPropertyAccessExpression(node.left) &&
232
+ node.left.expression.kind === ts.SyntaxKind.ThisKeyword) {
233
+ const owner = checker.getSymbolAtLocation(node.left.name)?.declarations?.find((declaration) => ts.isClassLike(declaration.parent));
234
+ if (!owner || !ts.isClassLike(owner.parent))
235
+ return;
236
+ for (const type of overriddenSlotTypesOf(owner.parent, node.left.name.text, node))
237
+ recordArms(type, node.right);
238
+ }
81
239
  };
82
240
  const valuePosition = (node) => {
83
241
  if (ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.EqualsToken)
@@ -95,8 +253,9 @@ export const constructorSlotSubclassesOf = (checker, identities, files, heritage
95
253
  for (const file of files) {
96
254
  const visit = (node) => {
97
255
  const value = valuePosition(node);
98
- if (value && isNamedValue(value))
99
- record(checker.getContextualType(value), value);
256
+ if (value && namedArmsOf(value).length > 0)
257
+ recordArms(checker.getContextualType(value), value);
258
+ recordOverriddenWrites(node);
100
259
  if (ts.isCallExpression(node) || ts.isNewExpression(node)) {
101
260
  for (const argument of node.arguments ?? [])
102
261
  if (isNamedValue(argument))