@homepages/concrete 7.0.0 → 9.0.0

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 (331) hide show
  1. package/CHANGELOG.md +1007 -0
  2. package/README.md +20 -9
  3. package/codemods/list-grammar.mjs +923 -0
  4. package/codemods/manifest.json +66 -0
  5. package/codemods/process-builders.mjs +198 -0
  6. package/codemods/slot-object.mjs +1097 -0
  7. package/codemods/steps/content-object-fields.mjs +324 -0
  8. package/codemods/steps/list-object-form.mjs +6 -7
  9. package/codemods/table-rows.mjs +568 -0
  10. package/codemods/unwrap-object-fields.mjs +310 -0
  11. package/codemods/viewer-slot-document.mjs +131 -0
  12. package/codemods/write-property-as-facts-document.mjs +716 -0
  13. package/codemods/write-the-address-line.mjs +255 -0
  14. package/codemods/write-the-registry-members.mjs +653 -0
  15. package/codemods/write-the-select-from.mjs +141 -0
  16. package/codemods/write-the-sole-from.mjs +369 -0
  17. package/codemods/write-the-table-from.mjs +267 -0
  18. package/codemods/write-the-whole-item.mjs +512 -0
  19. package/codemods/writeback-drop.mjs +168 -0
  20. package/codemods/written-contact-address.mjs +162 -0
  21. package/codemods/written-media-category.mjs +227 -0
  22. package/codemods/written-ref-kinds-name-the-layer.mjs +172 -0
  23. package/codemods/written-uploads-replace-pool-reads.mjs +454 -0
  24. package/codemods/written-uploads-sort-into-lists.mjs +405 -0
  25. package/codemods/written-uploads-stated-in-sorted-lists.mjs +295 -0
  26. package/codemods/written-video-sources-drop.mjs +144 -0
  27. package/codemods/written-without-pick-none.mjs +146 -0
  28. package/codemods/written-without-retired-knowledge.mjs +223 -0
  29. package/codemods/written-without-the-media-key.mjs +202 -0
  30. package/codemods/written-without-the-property-type.mjs +249 -0
  31. package/dist/artifact/playground/client/assets/index-BUmdDDX5.js +1259 -0
  32. package/dist/artifact/playground/client/assets/index-DDOcM0zA.css +1 -0
  33. package/dist/artifact/playground/client/index.html +2 -2
  34. package/dist/artifact/playground/client/island-canvas-host.mjs +1 -1
  35. package/dist/artifact/playground/host.mjs +61 -27
  36. package/dist/base.css +58 -1
  37. package/dist/cli/build/artifact.js +6 -9
  38. package/dist/cli/build/captures.js +2 -12
  39. package/dist/cli/build/esbuild.js +1 -25
  40. package/dist/cli/build/fixtures.js +2 -1
  41. package/dist/cli/build/icons.js +1 -1
  42. package/dist/cli/build/static.js +2 -2
  43. package/dist/cli/capture/capture.js +8 -9
  44. package/dist/cli/check/analyze.js +8 -1
  45. package/dist/cli/check/load-outdir.js +47 -0
  46. package/dist/cli/check/load-workspace.js +4 -4
  47. package/dist/cli/check/render/html.js +1 -2
  48. package/dist/cli/check/render/inputs.js +10 -3
  49. package/dist/cli/check/render/island-frames.js +2 -1
  50. package/dist/cli/check/render/marker-coverage.js +23 -15
  51. package/dist/cli/check/report.js +5 -0
  52. package/dist/cli/check/stages/bundles.js +1 -1
  53. package/dist/cli/check/stages/relations.js +1 -1
  54. package/dist/cli/check/stages/render.js +48 -33
  55. package/dist/cli/check/stages/sections.js +83 -31
  56. package/dist/cli/commands.js +1 -1
  57. package/dist/cli/dev/crop.js +1 -1
  58. package/dist/cli/dev/fixture-ladders.js +96 -0
  59. package/dist/cli/dev/islands.js +12 -123
  60. package/dist/cli/dev/listen.js +20 -1
  61. package/dist/cli/dev/load-template.js +1 -1
  62. package/dist/cli/dev/server.js +35 -10
  63. package/dist/cli/dev/vite.js +9 -7
  64. package/dist/cli/dev/workspace-reader.js +16 -16
  65. package/dist/cli/media/derive.js +43 -57
  66. package/dist/cli/media/frame-rung-floor.js +0 -1
  67. package/dist/cli/property/index.js +1 -6
  68. package/dist/css/inline.js +1 -22
  69. package/dist/eslint/rules/no-marker-literal.js +1 -1
  70. package/dist/fixtures/load.js +16 -7
  71. package/dist/fixtures/media-plugin.js +17 -12
  72. package/dist/fixtures/media-store.js +115 -0
  73. package/dist/fixtures/property.js +345 -126
  74. package/dist/fixtures/resolve.js +263 -251
  75. package/dist/fixtures/rows.js +4 -11
  76. package/dist/fixtures.d.ts +234 -109
  77. package/dist/index.d.ts +25 -16
  78. package/dist/index.js +10 -6
  79. package/dist/islands/marker.js +2 -1
  80. package/dist/packages/external/concrete/package.js +1 -1
  81. package/dist/packages/internal/canvas-core/dist/island-host.js +62 -0
  82. package/dist/packages/internal/canvas-core/dist/island-shields.js +30 -0
  83. package/dist/packages/internal/capture-settle/dist/index.js +3 -0
  84. package/dist/packages/internal/capture-settle/dist/island-markers.d.ts +13 -0
  85. package/dist/packages/internal/capture-settle/dist/island-markers.js +6 -0
  86. package/dist/packages/internal/capture-settle/dist/settle.js +150 -0
  87. package/dist/packages/internal/{image-ladder/dist/crop.js → crop-region/dist/index.js} +21 -20
  88. package/dist/packages/internal/fill-bake/dist/bake-property-ref.js +252 -35
  89. package/dist/packages/internal/fill-bake/dist/compute-reads.js +365 -4
  90. package/dist/packages/internal/fill-bake/dist/derive-source.js +35 -21
  91. package/dist/packages/internal/fill-bake/dist/index.js +8 -3
  92. package/dist/packages/internal/fill-bake/dist/item-merge.js +119 -0
  93. package/dist/packages/internal/fill-bake/dist/item-overrides.js +28 -0
  94. package/dist/packages/internal/fill-bake/dist/linked-merge.js +40 -0
  95. package/dist/packages/internal/fill-bake/dist/list-grammar.js +175 -0
  96. package/dist/packages/internal/fill-vocabulary/dist/holder.js +51 -0
  97. package/dist/packages/internal/fill-vocabulary/dist/resolved-decision.js +390 -0
  98. package/dist/packages/internal/fill-vocabulary/dist/row-planners.js +1 -23
  99. package/dist/packages/internal/fill-vocabulary/dist/row-reads.js +1 -1
  100. package/dist/packages/internal/fill-vocabulary/dist/verbs.js +75 -0
  101. package/dist/packages/internal/format-geometry/dist/capture.js +11 -147
  102. package/dist/packages/internal/format-geometry/dist/format.d.ts +8 -2
  103. package/dist/packages/internal/format-geometry/dist/geometry.js +13 -2
  104. package/dist/packages/internal/image-ladder/dist/encode.js +27 -14
  105. package/dist/packages/internal/image-ladder/dist/index.js +3 -3
  106. package/dist/packages/internal/image-ladder/dist/policy.js +28 -53
  107. package/dist/packages/internal/island-runtime/dist/capture-holds.js +1 -1
  108. package/dist/packages/internal/island-runtime/dist/contract.d.ts +3 -12
  109. package/dist/packages/internal/island-runtime/dist/contract.js +2 -2
  110. package/dist/packages/internal/island-runtime/dist/index.d.ts +3 -7
  111. package/dist/packages/internal/island-runtime/dist/index.js +2 -1
  112. package/dist/packages/internal/island-runtime/dist/viewport-schedule.js +1 -1
  113. package/dist/packages/internal/media-resolution/dist/crop-key.js +7 -0
  114. package/dist/packages/internal/media-resolution/dist/document.js +63 -0
  115. package/dist/packages/internal/media-resolution/dist/image.js +228 -0
  116. package/dist/packages/internal/media-resolution/dist/index.js +11 -0
  117. package/dist/packages/internal/media-resolution/dist/list.js +112 -0
  118. package/dist/packages/internal/media-resolution/dist/media.js +59 -0
  119. package/dist/packages/internal/media-resolution/dist/records.js +7 -0
  120. package/dist/packages/internal/media-resolution/dist/slot.js +34 -0
  121. package/dist/packages/internal/media-resolution/dist/store.js +19 -0
  122. package/dist/packages/internal/media-resolution/dist/tour.js +59 -0
  123. package/dist/packages/internal/media-resolution/dist/video.js +93 -0
  124. package/dist/packages/internal/property-facts/dist/registry-facts.js +244 -0
  125. package/dist/packages/internal/section-contracts/dist/cap-contract.js +8 -17
  126. package/dist/packages/internal/section-contracts/dist/catalog.js +35 -27
  127. package/dist/packages/internal/section-contracts/dist/count-bounds-contract.js +11 -2
  128. package/dist/packages/internal/section-contracts/dist/directly-linked-list-contract.js +27 -0
  129. package/dist/packages/internal/section-contracts/dist/fill-options.js +12 -0
  130. package/dist/packages/internal/section-contracts/dist/fill-spec-view.js +1 -0
  131. package/dist/packages/internal/section-contracts/dist/fill-verb-contract.js +95 -0
  132. package/dist/packages/internal/section-contracts/dist/grammar-scope.js +154 -0
  133. package/dist/packages/internal/section-contracts/dist/index.js +7 -5
  134. package/dist/packages/internal/section-contracts/dist/list-fill-contract.js +23 -19
  135. package/dist/packages/internal/section-contracts/dist/list-scalar-contract.js +12 -7
  136. package/dist/packages/internal/section-contracts/dist/list-source-contract.js +46 -0
  137. package/dist/packages/internal/section-contracts/dist/nested-slot-contract.js +11 -2
  138. package/dist/packages/internal/section-contracts/dist/object-contract.js +92 -0
  139. package/dist/packages/internal/section-contracts/dist/poi-curation-contract.js +5 -5
  140. package/dist/packages/internal/section-contracts/dist/reads-acyclic-contract.js +63 -0
  141. package/dist/packages/internal/section-contracts/dist/row-fill-contract.js +7 -4
  142. package/dist/packages/internal/section-contracts/dist/row-rule-contract.js +83 -0
  143. package/dist/packages/internal/section-contracts/dist/row-source-contract.js +67 -0
  144. package/dist/packages/internal/section-contracts/dist/runners.js +77 -33
  145. package/dist/packages/internal/section-contracts/dist/select-contract.js +0 -8
  146. package/dist/packages/internal/section-contracts/dist/source-contract.js +120 -66
  147. package/dist/packages/internal/section-contracts/dist/sources-contract.js +47 -17
  148. package/dist/packages/internal/section-contracts/dist/table-member.js +16 -0
  149. package/dist/packages/internal/section-contracts/dist/writer-reads-fact-contract.js +43 -18
  150. package/dist/packages/internal/source-resolution/dist/index.js +113 -118
  151. package/dist/packages/internal/static-assets-binding/dist/section-asset-url.js +16 -0
  152. package/dist/packages/internal/street-address/dist/index.js +72 -0
  153. package/dist/packages/internal/template-artifact/dist/asset-mime.js +30 -0
  154. package/dist/packages/internal/template-artifact/dist/layout.js +1 -2
  155. package/dist/packages/internal/template-artifact/dist/manifest.js +1 -1
  156. package/dist/packages/internal/template-artifact/dist/verify.js +12 -29
  157. package/dist/packages/internal/template-contracts/dist/address-value.d.ts +52 -0
  158. package/dist/packages/internal/template-contracts/dist/address-value.js +90 -0
  159. package/dist/packages/internal/template-contracts/dist/comparable-address.js +19 -0
  160. package/dist/{schema → packages/internal/template-contracts/dist}/compute.d.ts +29 -6
  161. package/dist/{schema → packages/internal/template-contracts/dist}/compute.js +11 -13
  162. package/dist/packages/internal/template-contracts/dist/content-pick.js +51 -0
  163. package/dist/packages/internal/template-contracts/dist/decision-fills.js +85 -0
  164. package/dist/packages/internal/template-contracts/dist/display-overrides.js +7 -0
  165. package/dist/packages/internal/template-contracts/dist/document-value.d.ts +16 -5
  166. package/dist/packages/internal/template-contracts/dist/fact-compute.d.ts +24 -0
  167. package/dist/packages/internal/template-contracts/dist/fact-compute.js +30 -0
  168. package/dist/packages/internal/template-contracts/dist/fact-keys.d.ts +44 -0
  169. package/dist/packages/internal/template-contracts/dist/fact-keys.js +88 -0
  170. package/dist/packages/internal/template-contracts/dist/fact-registry.d.ts +290 -0
  171. package/dist/packages/internal/template-contracts/dist/fact-registry.js +598 -0
  172. package/dist/packages/internal/template-contracts/dist/facts-view.d.ts +12 -36
  173. package/dist/packages/internal/template-contracts/dist/fill-builders.d.ts +157 -0
  174. package/dist/packages/internal/template-contracts/dist/fill-builders.js +79 -0
  175. package/dist/packages/internal/template-contracts/dist/fill-declaration.d.ts +14 -0
  176. package/dist/{schema → packages/internal/template-contracts/dist}/fill-declaration.js +6 -5
  177. package/dist/packages/internal/template-contracts/dist/fill-spec-schema.js +158 -0
  178. package/dist/packages/internal/template-contracts/dist/fill-spec.js +291 -0
  179. package/dist/packages/internal/template-contracts/dist/findable.js +8 -0
  180. package/dist/packages/internal/template-contracts/dist/floor-presets.js +13 -0
  181. package/dist/packages/internal/template-contracts/dist/icon-names.d.ts +5 -0
  182. package/dist/packages/internal/template-contracts/dist/icon-names.js +70 -0
  183. package/dist/packages/internal/template-contracts/dist/legacy-facts.js +167 -0
  184. package/dist/packages/internal/template-contracts/dist/list-editing.js +94 -0
  185. package/dist/packages/internal/template-contracts/dist/list.d.ts +215 -0
  186. package/dist/packages/internal/template-contracts/dist/list.js +258 -0
  187. package/dist/packages/internal/template-contracts/dist/markers.d.ts +2 -2
  188. package/dist/packages/internal/template-contracts/dist/media-candidates.js +38 -0
  189. package/dist/packages/internal/template-contracts/dist/mirror-kind.js +48 -0
  190. package/dist/packages/internal/template-contracts/dist/number-display.js +10 -0
  191. package/dist/packages/internal/template-contracts/dist/object.d.ts +89 -0
  192. package/dist/packages/internal/template-contracts/dist/object.js +131 -0
  193. package/dist/packages/internal/template-contracts/dist/poi-row.d.ts +1 -1
  194. package/dist/packages/internal/template-contracts/dist/primitive-descriptors.d.ts +1 -0
  195. package/dist/packages/internal/template-contracts/dist/primitive-descriptors.js +162 -0
  196. package/dist/packages/internal/template-contracts/dist/process-builders.d.ts +77 -0
  197. package/dist/packages/internal/template-contracts/dist/process-builders.js +73 -0
  198. package/dist/packages/internal/template-contracts/dist/read-graph.js +250 -0
  199. package/dist/packages/internal/template-contracts/dist/ref-locate.js +192 -0
  200. package/dist/packages/internal/template-contracts/dist/registry-aliases.js +77 -0
  201. package/dist/packages/internal/template-contracts/dist/registry-facts.d.ts +268 -0
  202. package/dist/packages/internal/template-contracts/dist/registry-facts.js +202 -0
  203. package/dist/packages/internal/template-contracts/dist/registry-shape.d.ts +41 -0
  204. package/dist/packages/internal/template-contracts/dist/registry-shape.js +135 -0
  205. package/dist/packages/internal/template-contracts/dist/resolve-scope.js +103 -0
  206. package/dist/packages/internal/{image-descriptor/dist/index.d.ts → template-contracts/dist/responsive-image.d.ts} +1 -1
  207. package/dist/packages/internal/template-contracts/dist/row-planners.js +29 -0
  208. package/dist/packages/internal/template-contracts/dist/row-ref.d.ts +133 -0
  209. package/dist/packages/internal/template-contracts/dist/row-ref.js +101 -0
  210. package/dist/packages/internal/template-contracts/dist/row-sources.js +48 -8
  211. package/dist/packages/internal/template-contracts/dist/row.d.ts +155 -0
  212. package/dist/packages/internal/template-contracts/dist/row.js +84 -0
  213. package/dist/packages/internal/template-contracts/dist/runtime-values.d.ts +97 -0
  214. package/dist/packages/internal/template-contracts/dist/slot-builders.d.ts +782 -0
  215. package/dist/packages/internal/template-contracts/dist/slot-builders.js +233 -0
  216. package/dist/packages/internal/template-contracts/dist/slot-catalog.d.ts +243 -384
  217. package/dist/packages/internal/template-contracts/dist/slot-catalog.js +334 -230
  218. package/dist/packages/internal/template-contracts/dist/slot-overrides.js +21 -0
  219. package/dist/packages/internal/template-contracts/dist/slot-ref.d.ts +155 -0
  220. package/dist/packages/internal/template-contracts/dist/slot-ref.js +76 -0
  221. package/dist/packages/internal/template-contracts/dist/slot-types.d.ts +163 -0
  222. package/dist/packages/internal/template-contracts/dist/slot-types.js +140 -0
  223. package/dist/packages/internal/template-contracts/dist/slot-value.d.ts +97 -0
  224. package/dist/packages/internal/template-contracts/dist/table-members.d.ts +25 -0
  225. package/dist/packages/internal/template-contracts/dist/table-members.js +90 -0
  226. package/dist/packages/internal/template-contracts/dist/tour-value.d.ts +5 -5
  227. package/dist/packages/internal/template-contracts/dist/value-contracts.js +15 -0
  228. package/dist/packages/internal/template-contracts/dist/video-value.d.ts +2 -2
  229. package/dist/platform/fs.js +5 -2
  230. package/dist/playground/canvas-island-host.d.ts +11 -0
  231. package/dist/playground/canvas-island-host.js +29 -0
  232. package/dist/playground/canvas-markers.js +14 -0
  233. package/dist/playground-island-runtime.d.ts +3 -2
  234. package/dist/playground-island-runtime.js +3 -2
  235. package/dist/primitives/FrameShell.js +0 -1
  236. package/dist/primitives/Image.d.ts +5 -4
  237. package/dist/primitives/Tour.d.ts +1 -1
  238. package/dist/primitives/Video.d.ts +2 -2
  239. package/dist/primitives/index.d.ts +2 -3
  240. package/dist/primitives/picture-sources.js +2 -2
  241. package/dist/primitives/probe-context.js +2 -12
  242. package/dist/primitives/slot-address.d.ts +44 -0
  243. package/dist/primitives/slot-address.js +19 -0
  244. package/dist/primitives/slot-bindings.d.ts +65 -26
  245. package/dist/primitives/slot-bindings.js +104 -46
  246. package/dist/primitives/slot-document.d.ts +23 -18
  247. package/dist/primitives/slot-document.js +39 -14
  248. package/dist/primitives/slot-download.d.ts +39 -0
  249. package/dist/primitives/slot-download.js +39 -0
  250. package/dist/primitives/slot-list.d.ts +3 -3
  251. package/dist/primitives/slot-list.js +0 -2
  252. package/dist/primitives/slot-media.d.ts +24 -5
  253. package/dist/primitives/slot-media.js +6 -9
  254. package/dist/primitives/slot-object.d.ts +49 -0
  255. package/dist/primitives/slot-object.js +20 -0
  256. package/dist/primitives/slot-scalar.d.ts +27 -2
  257. package/dist/primitives/slot-scalar.js +50 -7
  258. package/dist/primitives/slot.d.ts +13 -6
  259. package/dist/primitives/slot.js +10 -3
  260. package/dist/rules/registry.js +19 -30
  261. package/dist/schema/define-schema.d.ts +4 -8
  262. package/dist/schema/define-schema.js +178 -79
  263. package/dist/schema/extract-fill-spec.js +2 -158
  264. package/dist/schema/fact.d.ts +57 -15
  265. package/dist/schema/fact.js +42 -7
  266. package/dist/schema/facts-view.d.ts +14 -0
  267. package/dist/schema/format.d.ts +1 -0
  268. package/dist/schema/format.js +2 -1
  269. package/dist/schema/manifest.js +24 -0
  270. package/dist/schema/property-facts.d.ts +8 -11
  271. package/dist/schema/section-props.d.ts +2 -66
  272. package/dist/schema/section-schema.d.ts +8 -11
  273. package/dist/schema/section-schema.js +27 -10
  274. package/dist/section/declare.d.ts +18 -7
  275. package/dist/section/declare.js +18 -5
  276. package/dist/section/materialize.js +3 -8
  277. package/dist/section/section.d.ts +12 -8
  278. package/dist/section/section.js +1 -1
  279. package/dist/template/logic-shim.d.ts +14 -7
  280. package/dist/template/logic-shim.js +14 -7
  281. package/dist/template/materialize.js +17 -18
  282. package/dist/template/mirror-types.js +30 -31
  283. package/dist/template/section-reads.js +85 -9
  284. package/guide/reference/cli/INDEX.md +2 -2
  285. package/guide/reference/cli/build.md +1 -1
  286. package/guide/reference/cli/check.md +1 -1
  287. package/guide/reference/cli/dev.md +1 -1
  288. package/guide/reference/cli/property-add.md +1 -1
  289. package/guide/reference/cli/property-list.md +2 -2
  290. package/guide/reference/cli/publish.md +1 -1
  291. package/guide/reference/cli/upgrade.md +1 -1
  292. package/guide/reference/rules/INDEX.md +29 -29
  293. package/package.json +9 -8
  294. package/dist/artifact/playground/client/assets/index-CckS2ONm.css +0 -1
  295. package/dist/artifact/playground/client/assets/index-D8eepVr-.js +0 -1101
  296. package/dist/fixtures/canonical.d.ts +0 -166
  297. package/dist/fixtures/media.js +0 -163
  298. package/dist/packages/internal/fill-vocabulary/dist/origination.js +0 -111
  299. package/dist/packages/internal/format-geometry/dist/index.js +0 -4
  300. package/dist/packages/internal/property-facts/dist/derive-facts-view.js +0 -10
  301. package/dist/packages/internal/property-facts/dist/index.js +0 -3
  302. package/dist/packages/internal/property-facts/dist/property-facts-Cy7-O4p0.js +0 -189
  303. package/dist/packages/internal/section-contracts/dist/choose-select-contract.js +0 -20
  304. package/dist/packages/internal/section-contracts/dist/name-title-contract.js +0 -20
  305. package/dist/packages/internal/section-contracts/dist/pooling-field-contract.js +0 -22
  306. package/dist/packages/internal/section-contracts/dist/record-collection-contract.js +0 -24
  307. package/dist/packages/internal/section-contracts/dist/source-filter-contract.js +0 -15
  308. package/dist/packages/internal/template-contracts/dist/collection-editing.js +0 -22
  309. package/dist/packages/internal/template-contracts/dist/facts-view.js +0 -111
  310. package/dist/packages/internal/template-contracts/dist/select-key.js +0 -7
  311. package/dist/primitives/slot-group.d.ts +0 -40
  312. package/dist/primitives/slot-group.js +0 -36
  313. package/dist/schema/extract-used-facts.js +0 -34
  314. package/dist/schema/fill-builders.d.ts +0 -115
  315. package/dist/schema/fill-builders.js +0 -25
  316. package/dist/schema/fill-declaration.d.ts +0 -14
  317. package/dist/schema/fill-spec.js +0 -89
  318. package/dist/schema/layout.d.ts +0 -30
  319. package/dist/schema/layout.js +0 -14
  320. package/dist/schema/row-fields.js +0 -88
  321. package/dist/schema/row-ref.d.ts +0 -59
  322. package/dist/schema/row-ref.js +0 -28
  323. package/dist/schema/runtime-values.d.ts +0 -71
  324. package/dist/schema/section-nav.js +0 -14
  325. package/dist/schema/slot-builders.d.ts +0 -613
  326. package/dist/schema/slot-builders.js +0 -177
  327. package/dist/schema/slot-ref.d.ts +0 -79
  328. package/dist/schema/slot-ref.js +0 -41
  329. package/dist/schema/slot-types.d.ts +0 -111
  330. package/dist/schema/slot-types.js +0 -214
  331. package/dist/section/groups.js +0 -68
@@ -0,0 +1,1097 @@
1
+ // 9.0.0 — `Slot.Object` in binding form: slots that belong together are one `object()`
2
+ // in the section's `slots`, and the markup marks it from its binding. `SlotGroup` and
3
+ // `Slot.Group` are gone, and with them the card a render declared.
4
+ //
5
+ // What this codemod does, in every `.ts` and `.tsx` under the workspace:
6
+ //
7
+ // - A declared card — `<SlotGroup id="address" label="Property address">` (or
8
+ // `<Slot.Group id label>`) — becomes an object: the slots it marks move out of
9
+ // `slots` into `address: object({ label: compute(() => "Property address"), fields:
10
+ // { … } })`, placed where the first of them was declared; every copy of the card
11
+ // becomes `<Slot.Object slot={…address}>`; and every read of a moved slot becomes a
12
+ // read of the object's field (`slot.address.fields.street`). A slot belongs to the NEAREST card
13
+ // around its marker, as the groups probe counted it, so a card nested in another
14
+ // holds its own slots and the outer card the rest.
15
+ // - A card in a component rather than in the section's render is the same object, in
16
+ // the section the component's slots come from:
17
+ // · a component handed the section's whole binding map (a parameter typed
18
+ // `SlotBindingMap<typeof Hero.schema>`, or an alias of it) marks the object off
19
+ // that map — `<Slot.Object slot={bindings.address}>` — and every
20
+ // `bindings.street` in the file reads `bindings.address.fields.street`;
21
+ // · a component handed single slots (`streetSlot: SlotBinding<typeof
22
+ // Hero.schema.slots.street>`) gains a prop for the object
23
+ // (`address: ObjectBinding<typeof Hero.schema.slots.address>`), every place that
24
+ // renders the component passes it down (`address={slot.address}` from the
25
+ // section's render), and each single slot's type becomes the field's
26
+ // (`FieldBinding<typeof Hero.schema.slots.address.fields.street>`).
27
+ // - A card that marks no slot is deleted, and so is a card whose one slot is the
28
+ // slot it is named for or a slot no object may hold (a list of objects or rows) —
29
+ // that slot is already a card of its own. The element it rendered stays
30
+ // (`<SlotGroup id label as="ol" className>` → `<ol className>`).
31
+ // - An item's container — `<SlotGroup slot={item}>`, `<Slot.Group slot={item}>` —
32
+ // becomes `<Slot.Object slot={item}>`.
33
+ // - Every fixture `content(Section, { … })` states a moved slot under its object
34
+ // (`./steps/content-object-fields.mjs`).
35
+ // - Imports `object`, `compute`, `Slot`, `type ObjectBinding` and `type FieldBinding`
36
+ // where a rewrite uses them, and drops `SlotGroup` once nothing names it.
37
+ //
38
+ // What it leaves to you:
39
+ //
40
+ // - Reported per file as `Manual:` — another section reading a moved slot
41
+ // (`Hero.slots.street`): it is now a field of the object, so read what the field
42
+ // reads instead, or keep the slot out of the object.
43
+ // - Refused, by line — a card whose id or label is not a plain string, a card whose
44
+ // slots this codemod cannot trace to a section, a slot marked in two cards neither of
45
+ // which holds the other, a card id that names another slot, a card under two labels,
46
+ // a slot that cannot be an object's field beside others, and a component it cannot
47
+ // pass the object down to. Each is a decision the markup cannot make for you.
48
+
49
+ import { readdir, readFile, writeFile } from "node:fs/promises";
50
+ import { dirname, join, relative, sep } from "node:path";
51
+
52
+ import ts from "typescript";
53
+
54
+ import contentObjectFields from "./steps/content-object-fields.mjs";
55
+
56
+ const CONCRETE = "@homepages/concrete";
57
+
58
+ const EXTENSIONS = [".ts", ".tsx"];
59
+ const SKIP_DIRS = new Set(["node_modules", ".concrete", ".git", "dist", "build"]);
60
+
61
+ /** A slot whose declaration cannot sit in an object's `fields`. */
62
+ const NOT_A_FIELD = /^\s*(object\(|list\([\s\S]*(\bobject\(|\brow\(|fact\.property\.(units|contacts|comparables)\b))/;
63
+
64
+ /** How far a component's own markers are followed into the components it renders. */
65
+ const TRACE_DEPTH = 4;
66
+
67
+ async function sourceFiles(root) {
68
+ const found = [];
69
+ async function walk(dir) {
70
+ for (const entry of await readdir(dir, { withFileTypes: true })) {
71
+ if (entry.isDirectory()) {
72
+ if (!SKIP_DIRS.has(entry.name)) await walk(join(dir, entry.name));
73
+ } else if (EXTENSIONS.some((ext) => entry.name.endsWith(ext)) && !entry.name.endsWith(".d.ts")) {
74
+ found.push(join(dir, entry.name));
75
+ }
76
+ }
77
+ }
78
+ await walk(root);
79
+ return found.sort();
80
+ }
81
+
82
+ /** Non-overlapping text replacements, applied back to front; insertions at one point keep their order. */
83
+ class Edits {
84
+ constructor() {
85
+ this.list = [];
86
+ this.inserts = new Map();
87
+ }
88
+ add(start, end, text) {
89
+ if (start === end) {
90
+ this.insert(start, text);
91
+ return;
92
+ }
93
+ if (this.list.some((edit) => edit.start === start && edit.end === end)) return;
94
+ this.list.push({ start, end, text });
95
+ }
96
+ insert(at, text) {
97
+ this.inserts.set(at, [...(this.inserts.get(at) ?? []), text]);
98
+ }
99
+ get size() {
100
+ return this.list.length + this.inserts.size;
101
+ }
102
+ apply(source) {
103
+ const all = [...this.list, ...[...this.inserts].map(([at, texts]) => ({ start: at, end: at, text: texts.join("") }))];
104
+ // Back to front; at one offset the insertion lands before the replacement that starts there.
105
+ const sorted = all.sort((a, b) => b.start - a.start || b.end - a.end);
106
+ let out = source;
107
+ let floor = Infinity;
108
+ for (const edit of sorted) {
109
+ if (edit.end > floor) throw new Error(`slot-object: overlapping rewrites at ${String(edit.start)}`);
110
+ out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
111
+ floor = edit.start;
112
+ }
113
+ return out;
114
+ }
115
+ }
116
+
117
+ function lineIndent(text, pos) {
118
+ const start = text.lastIndexOf("\n", pos - 1) + 1;
119
+ return /^[ \t]*/.exec(text.slice(start, pos))[0];
120
+ }
121
+
122
+ /** Shift every line after the first of `fragment` right by `by`. */
123
+ function indentTail(fragment, by) {
124
+ return fragment
125
+ .split("\n")
126
+ .map((line, index) => (index === 0 || line === "" ? line : `${by}${line}`))
127
+ .join("\n");
128
+ }
129
+
130
+ function lineOf(sf, node) {
131
+ return sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1;
132
+ }
133
+
134
+ /** A JSX attribute's value when it is a plain string (`id="a"`, `id={"a"}`), else `undefined`. */
135
+ function stringValue(attribute) {
136
+ const init = attribute.initializer;
137
+ if (init === undefined) return undefined;
138
+ if (ts.isStringLiteral(init)) return init.text;
139
+ if (ts.isJsxExpression(init) && init.expression !== undefined) {
140
+ const expr = init.expression;
141
+ if (ts.isStringLiteral(expr) || ts.isNoSubstitutionTemplateLiteral(expr)) return expr.text;
142
+ }
143
+ return undefined;
144
+ }
145
+
146
+ /** A JSX attribute's `{expression}`, or `undefined`. */
147
+ function expressionValue(attribute) {
148
+ const init = attribute?.initializer;
149
+ return init !== undefined && ts.isJsxExpression(init) ? init.expression : undefined;
150
+ }
151
+
152
+ function propertyKey(property) {
153
+ if (ts.isShorthandPropertyAssignment(property)) return property.name.text;
154
+ if (!ts.isPropertyAssignment(property)) return undefined;
155
+ const { name } = property;
156
+ return ts.isIdentifier(name) || ts.isStringLiteral(name) ? name.text : undefined;
157
+ }
158
+
159
+ /** The `slots` object literal of a `section(options, render)` call, or `undefined`. */
160
+ function slotsLiteral(call) {
161
+ const options = call.arguments[0];
162
+ if (options === undefined || !ts.isObjectLiteralExpression(options)) return undefined;
163
+ const slots = options.properties.find((p) => propertyKey(p) === "slots");
164
+ return slots !== undefined && ts.isPropertyAssignment(slots) && ts.isObjectLiteralExpression(slots.initializer) ? slots.initializer : undefined;
165
+ }
166
+
167
+ /** The local name the render binds `slot` to (`({ slot }) =>`, `({ slot: s }) =>`), or `undefined`. */
168
+ function slotBindingName(call) {
169
+ const render = call.arguments[1];
170
+ if (render === undefined || !(ts.isArrowFunction(render) || ts.isFunctionExpression(render))) return undefined;
171
+ const param = render.parameters[0];
172
+ if (param === undefined || !ts.isObjectBindingPattern(param.name)) return undefined;
173
+ for (const element of param.name.elements) {
174
+ const key = element.propertyName ?? element.name;
175
+ if (ts.isIdentifier(key) && key.text === "slot" && ts.isIdentifier(element.name)) return element.name.text;
176
+ }
177
+ return undefined;
178
+ }
179
+
180
+ /** The removal range of one property of an object literal: itself, its leading trivia and its comma. */
181
+ function propertyRemoval(text, property, sf) {
182
+ let end = property.getEnd();
183
+ let at = end;
184
+ while (at < text.length && /\s/.test(text[at])) at += 1;
185
+ if (text[at] === ",") return { start: property.getFullStart(), end: at + 1 };
186
+ // The last property with no trailing comma: take the comma before it instead.
187
+ let before = property.getFullStart() - 1;
188
+ while (before >= 0 && /\s/.test(text[before])) before -= 1;
189
+ return text[before] === "," ? { start: before, end } : { start: property.getStart(sf), end };
190
+ }
191
+
192
+ /**
193
+ * Where to add one more entry after `last`, the final entry of a list separated by
194
+ * `separator` (`,` in a binding pattern, `;` in a type literal), and the text to add —
195
+ * on a line of its own when the list puts each entry on one.
196
+ */
197
+ function appendEntry(text, last, entry, separator) {
198
+ let at = last.getEnd();
199
+ // A type member carries its own `;`; a pattern element's `,` follows it.
200
+ let trailing = text[at - 1] === "," || text[at - 1] === ";" ? text[at - 1] : undefined;
201
+ let probe = at;
202
+ while (probe < text.length && /[ \t]/.test(text[probe])) probe += 1;
203
+ if (trailing === undefined && (text[probe] === "," || text[probe] === ";")) {
204
+ trailing = text[probe];
205
+ at = probe + 1;
206
+ probe += 1;
207
+ while (probe < text.length && /[ \t]/.test(text[probe])) probe += 1;
208
+ }
209
+ const lead = trailing === undefined ? separator : "";
210
+ if (text[probe] === "\n" || text[probe] === "\r") return { at, text: `${lead}\n${lineIndent(text, last.getStart())}${entry}${trailing ?? separator}` };
211
+ return { at, text: `${lead} ${entry}${trailing ?? ""}` };
212
+ }
213
+
214
+ /** A relative import's file in the workspace, or `undefined`. */
215
+ function resolveImport(from, specifier, known) {
216
+ if (!specifier.startsWith(".")) return undefined;
217
+ const base = join(dirname(from), specifier);
218
+ const stem = base.replace(/\.(m?js|jsx)$/, "");
219
+ for (const candidate of [base, `${stem}.ts`, `${stem}.tsx`, join(base, "index.ts"), join(base, "index.tsx")]) {
220
+ if (known.has(candidate)) return candidate;
221
+ }
222
+ return undefined;
223
+ }
224
+
225
+ /** One source file: its tree, the framework names it imports, and the edits it collects. */
226
+ function moduleOf(file, text, known) {
227
+ const sf = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, file.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS);
228
+ const mod = { file, text, sf, imported: new Map(), hosts: [], localImports: new Map(), sections: new Map(), exports: new Map() };
229
+ for (const statement of sf.statements) {
230
+ if (ts.isImportDeclaration(statement) && ts.isStringLiteral(statement.moduleSpecifier)) {
231
+ const specifier = statement.moduleSpecifier.text;
232
+ const clause = statement.importClause;
233
+ if (clause === undefined) continue;
234
+ if (specifier === CONCRETE) {
235
+ const bindings = clause.namedBindings;
236
+ if (bindings === undefined || !ts.isNamedImports(bindings)) continue;
237
+ mod.hosts.push(statement);
238
+ for (const element of bindings.elements) mod.imported.set((element.propertyName ?? element.name).text, element);
239
+ continue;
240
+ }
241
+ const target = resolveImport(file, specifier, known);
242
+ if (target === undefined) continue;
243
+ if (clause.name !== undefined) mod.localImports.set(clause.name.text, { file: target, name: "default" });
244
+ if (clause.namedBindings !== undefined && ts.isNamedImports(clause.namedBindings)) {
245
+ for (const element of clause.namedBindings.elements) mod.localImports.set(element.name.text, { file: target, name: (element.propertyName ?? element.name).text });
246
+ }
247
+ }
248
+ const exported = ts.canHaveModifiers(statement) && (ts.getModifiers(statement) ?? []).some((m) => m.kind === ts.SyntaxKind.ExportKeyword);
249
+ const isDefault = ts.canHaveModifiers(statement) && (ts.getModifiers(statement) ?? []).some((m) => m.kind === ts.SyntaxKind.DefaultKeyword);
250
+ if (ts.isFunctionDeclaration(statement) && statement.name !== undefined && exported) mod.exports.set(isDefault ? "default" : statement.name.text, statement.name.text);
251
+ if (ts.isVariableStatement(statement) && exported) {
252
+ for (const declaration of statement.declarationList.declarations) if (ts.isIdentifier(declaration.name)) mod.exports.set(declaration.name.text, declaration.name.text);
253
+ }
254
+ if (ts.isExportAssignment(statement) && !statement.isExportEquals && ts.isIdentifier(statement.expression)) mod.exports.set("default", statement.expression.text);
255
+ if (ts.isExportDeclaration(statement) && statement.moduleSpecifier === undefined && statement.exportClause !== undefined && ts.isNamedExports(statement.exportClause)) {
256
+ for (const element of statement.exportClause.elements) mod.exports.set(element.name.text, (element.propertyName ?? element.name).text);
257
+ }
258
+ }
259
+ mod.name = (exported) => mod.imported.get(exported)?.name.text;
260
+ return mod;
261
+ }
262
+
263
+ /** Every `const X = section(…)` of a module, with what the rewrite reads off it. */
264
+ function indexSections(mod) {
265
+ const sectionName = mod.name("section");
266
+ if (sectionName === undefined) return;
267
+ const visit = (node) => {
268
+ if (
269
+ ts.isVariableDeclaration(node) &&
270
+ ts.isIdentifier(node.name) &&
271
+ node.initializer !== undefined &&
272
+ ts.isCallExpression(node.initializer) &&
273
+ ts.isIdentifier(node.initializer.expression) &&
274
+ node.initializer.expression.text === sectionName
275
+ ) {
276
+ const call = node.initializer;
277
+ const slots = slotsLiteral(call);
278
+ const declaredSlots = new Map();
279
+ for (const property of slots?.properties ?? []) {
280
+ const key = propertyKey(property);
281
+ if (key !== undefined) declaredSlots.set(key, property);
282
+ }
283
+ mod.sections.set(node.name.text, { mod, name: node.name.text, call, slots, binding: slotBindingName(call), declaredSlots, render: call.arguments[1] });
284
+ }
285
+ ts.forEachChild(node, visit);
286
+ };
287
+ visit(mod.sf);
288
+ }
289
+
290
+ function isGroupTag(mod, tag) {
291
+ const groupName = mod.name("SlotGroup");
292
+ const slotName = mod.name("Slot");
293
+ return (
294
+ (groupName !== undefined && ts.isIdentifier(tag) && tag.text === groupName) ||
295
+ (slotName !== undefined && ts.isPropertyAccessExpression(tag) && ts.isIdentifier(tag.expression) && tag.expression.text === slotName && tag.name.text === "Group")
296
+ );
297
+ }
298
+
299
+ const openingOf = (element) => (ts.isJsxElement(element) ? element.openingElement : element);
300
+ const attributeOf = (element, name) =>
301
+ openingOf(element).attributes.properties.find((a) => ts.isJsxAttribute(a) && ts.isIdentifier(a.name) && a.name.text === name);
302
+ const isCard = (mod, node) => (ts.isJsxElement(node) || ts.isJsxSelfClosingElement(node)) && isGroupTag(mod, openingOf(node).tagName);
303
+
304
+ /** The nearest function around `node` (a component, a callback), or `undefined`. */
305
+ function enclosingFunction(node) {
306
+ for (let at = node.parent; at !== undefined; at = at.parent) {
307
+ if (ts.isFunctionDeclaration(at) || ts.isArrowFunction(at) || ts.isFunctionExpression(at)) return at;
308
+ }
309
+ return undefined;
310
+ }
311
+
312
+ /** The name a function is declared under (`function X`, `const X = () =>`), or `undefined`. */
313
+ function functionName(fn) {
314
+ if (ts.isFunctionDeclaration(fn)) return fn.name?.text;
315
+ const parent = fn.parent;
316
+ return parent !== undefined && ts.isVariableDeclaration(parent) && ts.isIdentifier(parent.name) ? parent.name.text : undefined;
317
+ }
318
+
319
+ export default async function slotObject(root) {
320
+ const files = await sourceFiles(root);
321
+ const known = new Set(files);
322
+ const name = (file) => relative(root, file).split(sep).join("/");
323
+
324
+ const modules = new Map();
325
+ for (const file of files) {
326
+ // Every file, not only those importing the framework: one that reads a section's
327
+ // schema (`Hero.schema.slots.street`) or renders its components is rewritten too.
328
+ modules.set(file, moduleOf(file, await readFile(file, "utf8"), known));
329
+ }
330
+ for (const mod of modules.values()) indexSections(mod);
331
+ for (const mod of modules.values()) {
332
+ mod.edits = new Edits();
333
+ mod.refused = [];
334
+ mod.manual = [];
335
+ mod.uses = new Set();
336
+ mod.retyped = new Set();
337
+ mod.groupLeft = false;
338
+ }
339
+
340
+ const sectionNamed = (mod, ident) => {
341
+ const local = mod.sections.get(ident);
342
+ if (local !== undefined) return local;
343
+ const imported = mod.localImports.get(ident);
344
+ if (imported === undefined) return undefined;
345
+ const target = modules.get(imported.file);
346
+ return target?.sections.get(imported.name);
347
+ };
348
+
349
+ // `typeof X.schema` / `typeof X.schema.slots.k` → the section X names and the path read off it.
350
+ const schemaQuery = (mod, node) => {
351
+ if (node === undefined || !ts.isTypeQueryNode(node)) return undefined;
352
+ const parts = [];
353
+ let at = node.exprName;
354
+ while (ts.isQualifiedName(at)) {
355
+ parts.unshift(at.right.text);
356
+ at = at.left;
357
+ }
358
+ if (!ts.isIdentifier(at) || parts[0] !== "schema") return undefined;
359
+ const section = sectionNamed(mod, at.text);
360
+ return section === undefined ? undefined : { section, ident: at.text, path: parts.slice(1) };
361
+ };
362
+
363
+ // Each module's binding-map names and single-slot props, read off their type
364
+ // annotations — on the name itself, or on the props type a component destructures it
365
+ // from, wherever in the workspace that type is declared.
366
+ for (const mod of modules.values()) {
367
+ mod.aliases = new Map();
368
+ mod.propsTypes = new Map();
369
+ for (const statement of mod.sf.statements) {
370
+ if (ts.isInterfaceDeclaration(statement)) mod.propsTypes.set(statement.name.text, statement);
371
+ if (ts.isTypeAliasDeclaration(statement) && ts.isTypeLiteralNode(statement.type)) mod.propsTypes.set(statement.name.text, statement.type);
372
+ }
373
+ }
374
+ /** A type name `mod` can see: the module that declares it and its local name there. */
375
+ const typeDeclaration = (mod, ref, table) => {
376
+ if (mod[table].has(ref)) return { mod, local: ref };
377
+ const imported = mod.localImports.get(ref);
378
+ const target = imported === undefined ? undefined : modules.get(imported.file);
379
+ return target?.[table].has(imported.name) ? { mod: target, local: imported.name } : undefined;
380
+ };
381
+ /** The section whose `SlotBindingMap` a type is (through aliases and `Pick`), or `undefined`. */
382
+ const mapOf = (mod, type) => {
383
+ if (type === undefined) return undefined;
384
+ if (ts.isParenthesizedTypeNode(type)) return mapOf(mod, type.type);
385
+ if (!ts.isTypeReferenceNode(type) || !ts.isIdentifier(type.typeName)) return undefined;
386
+ const ref = type.typeName.text;
387
+ if (ref === mod.name("SlotBindingMap")) {
388
+ const query = schemaQuery(mod, type.typeArguments?.[0]);
389
+ return query !== undefined && query.path.length === 0 ? query.section : undefined;
390
+ }
391
+ const alias = typeDeclaration(mod, ref, "aliases");
392
+ if (alias !== undefined) return alias.mod.aliases.get(alias.local);
393
+ if ((ref === "Pick" || ref === "Omit" || ref === "Readonly") && type.typeArguments?.[0] !== undefined) return mapOf(mod, type.typeArguments[0]);
394
+ return undefined;
395
+ };
396
+ for (let pass = 0; pass < 2; pass += 1) {
397
+ for (const mod of modules.values()) {
398
+ for (const statement of mod.sf.statements) {
399
+ if (!ts.isTypeAliasDeclaration(statement)) continue;
400
+ const section = mapOf(mod, statement.type);
401
+ if (section !== undefined) mod.aliases.set(statement.name.text, section);
402
+ }
403
+ }
404
+ }
405
+ /** A props type's members (an inline literal, an interface or type literal anywhere, under `Pick`), with the module declaring them. */
406
+ const membersOf = (mod, type) => {
407
+ if (type === undefined) return undefined;
408
+ if (ts.isParenthesizedTypeNode(type)) return membersOf(mod, type.type);
409
+ if (ts.isTypeLiteralNode(type)) return { mod, owner: type, members: type.members };
410
+ if (!ts.isTypeReferenceNode(type) || !ts.isIdentifier(type.typeName)) return undefined;
411
+ const ref = type.typeName.text;
412
+ if ((ref === "Pick" || ref === "Omit" || ref === "Readonly") && type.typeArguments?.[0] !== undefined) {
413
+ const inner = membersOf(mod, type.typeArguments[0]);
414
+ return inner === undefined ? undefined : { ...inner, pick: ref === "Pick" ? { mod, keys: type.typeArguments[1] } : inner.pick };
415
+ }
416
+ const declared = typeDeclaration(mod, ref, "propsTypes");
417
+ if (declared === undefined) return undefined;
418
+ const owner = declared.mod.propsTypes.get(declared.local);
419
+ return { mod: declared.mod, owner, members: owner.members };
420
+ };
421
+ /** A single slot's type — `SlotBinding<typeof Hero.schema.slots.street>` — read in `mod`. */
422
+ const slotPropOf = (mod, type) => {
423
+ const bindingName = mod.name("SlotBinding");
424
+ if (bindingName === undefined || type === undefined || !ts.isTypeReferenceNode(type) || !ts.isIdentifier(type.typeName) || type.typeName.text !== bindingName) return undefined;
425
+ const query = schemaQuery(mod, type.typeArguments?.[0]);
426
+ return query !== undefined && query.path.length === 2 && query.path[0] === "slots" ? { section: query.section, key: query.path[1], ident: query.ident } : undefined;
427
+ };
428
+ for (const mod of modules.values()) {
429
+ mod.mapOf = (type) => mapOf(mod, type);
430
+ mod.mapNames = new Map();
431
+ mod.slotProps = new Map();
432
+ const record = (local, typeMod, type) => {
433
+ const section = mapOf(typeMod, type);
434
+ if (section !== undefined) {
435
+ const prior = mod.mapNames.get(local);
436
+ mod.mapNames.set(local, prior === undefined || prior === section ? section : null);
437
+ }
438
+ const prop = slotPropOf(typeMod, type);
439
+ if (prop !== undefined) mod.slotProps.set(local, prop);
440
+ };
441
+ const visit = (node) => {
442
+ if ((ts.isParameter(node) || ts.isPropertySignature(node) || ts.isVariableDeclaration(node)) && node.type !== undefined) {
443
+ if (ts.isIdentifier(node.name)) record(node.name.text, mod, node.type);
444
+ else if (ts.isParameter(node) && ts.isObjectBindingPattern(node.name)) {
445
+ const props = membersOf(mod, node.type);
446
+ for (const element of node.name.elements) {
447
+ const key = element.propertyName ?? element.name;
448
+ if (props === undefined || !ts.isIdentifier(key) || !ts.isIdentifier(element.name)) continue;
449
+ const member = props.members.find((m) => ts.isPropertySignature(m) && m.name !== undefined && ts.isIdentifier(m.name) && m.name.text === key.text);
450
+ if (member?.type !== undefined) record(element.name.text, props.mod, member.type);
451
+ }
452
+ }
453
+ }
454
+ ts.forEachChild(node, visit);
455
+ };
456
+ visit(mod.sf);
457
+ }
458
+
459
+ /** The section whose render (in this module) holds `node`, or `undefined`. */
460
+ const renderOf = (mod, node) => {
461
+ for (const section of mod.sections.values()) {
462
+ const render = section.render;
463
+ if (render !== undefined && node.getStart(mod.sf) >= render.getStart(mod.sf) && node.getEnd() <= render.getEnd()) return section;
464
+ }
465
+ return undefined;
466
+ };
467
+
468
+ /**
469
+ * The slots an expression marks — `slot.k` in a render, `bindings.k` off a binding map,
470
+ * a single-slot prop, or a `.map` parameter over a local array of those — each with
471
+ * the route it came by; `undefined` when it marks nothing this codemod can trace.
472
+ */
473
+ const slotRefs = (mod, expr) => {
474
+ if (expr === undefined) return undefined;
475
+ if (ts.isPropertyAccessExpression(expr) && ts.isIdentifier(expr.expression)) {
476
+ const base = expr.expression.text;
477
+ const key = expr.name.text;
478
+ const render = renderOf(mod, expr);
479
+ if (render !== undefined && render.binding === base && render.declaredSlots.has(key)) return [{ mod, section: render, key, via: "render", name: base }];
480
+ const mapped = mod.mapNames.get(base);
481
+ if (mapped && mapped.declaredSlots.has(key)) return [{ mod, section: mapped, key, via: "map", name: base }];
482
+ return undefined;
483
+ }
484
+ if (!ts.isIdentifier(expr)) return undefined;
485
+ const prop = mod.slotProps.get(expr.text);
486
+ if (prop !== undefined && prop.section.declaredSlots.has(prop.key)) return [{ mod, section: prop.section, key: prop.key, via: "prop", name: expr.text, ident: prop.ident }];
487
+ // A `.map` callback's parameter over a local array of slots — `phases.map((body) =>
488
+ // <Slot.Text slot={body} />)` over `const phases = [slot.a, slot.b]` — or of objects
489
+ // carrying one — `socials.map(({ slot }) => …)` over `[{ slot: xSlot, … }]`.
490
+ const fn = enclosingFunction(expr);
491
+ const param = fn?.parameters[0];
492
+ const call = fn?.parent;
493
+ if (param === undefined || call === undefined || !ts.isCallExpression(call) || !ts.isPropertyAccessExpression(call.expression) || call.expression.name.text !== "map") {
494
+ return undefined;
495
+ }
496
+ let member;
497
+ if (ts.isIdentifier(param.name)) {
498
+ if (param.name.text !== expr.text) return undefined;
499
+ } else if (ts.isObjectBindingPattern(param.name)) {
500
+ const element = param.name.elements.find((e) => ts.isIdentifier(e.name) && e.name.text === expr.text);
501
+ const key = element?.propertyName ?? element?.name;
502
+ if (key === undefined || !ts.isIdentifier(key)) return undefined;
503
+ member = key.text;
504
+ } else {
505
+ return undefined;
506
+ }
507
+ let mapped = call.expression.expression;
508
+ while (ts.isAsExpression(mapped) || ts.isSatisfiesExpression(mapped) || ts.isParenthesizedExpression(mapped)) mapped = mapped.expression;
509
+ const array = ts.isArrayLiteralExpression(mapped) ? mapped : ts.isIdentifier(mapped) ? localArray(mod, mapped) : undefined;
510
+ if (array === undefined) return undefined;
511
+ const refs = [];
512
+ for (const element of array.elements) {
513
+ let value = element;
514
+ if (member !== undefined) {
515
+ const property = ts.isObjectLiteralExpression(element) ? element.properties.find((p) => propertyKey(p) === member) : undefined;
516
+ if (property === undefined) return undefined;
517
+ value = ts.isShorthandPropertyAssignment(property) ? property.name : property.initializer;
518
+ }
519
+ const found = slotRefs(mod, value);
520
+ if (found === undefined) return undefined;
521
+ refs.push(...found);
522
+ }
523
+ return refs.length > 0 ? refs : undefined;
524
+ };
525
+
526
+ /** The array literal a local `const` names (`as const` allowed), or `undefined`. */
527
+ const localArray = (mod, ident) => {
528
+ let found;
529
+ const visit = (node) => {
530
+ if (found !== undefined) return;
531
+ if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.name.text === ident.text && node.initializer !== undefined) {
532
+ let init = node.initializer;
533
+ while (ts.isAsExpression(init) || ts.isSatisfiesExpression(init) || ts.isParenthesizedExpression(init)) init = init.expression;
534
+ if (ts.isArrayLiteralExpression(init)) found = init;
535
+ return;
536
+ }
537
+ ts.forEachChild(node, visit);
538
+ };
539
+ visit(enclosingFunction(ident) ?? mod.sf);
540
+ return found;
541
+ };
542
+
543
+ /** A component a JSX tag renders: the function and its module, or `undefined`. */
544
+ const componentOf = (mod, tag) => {
545
+ if (!ts.isIdentifier(tag) || !/^[A-Z]/.test(tag.text)) return undefined;
546
+ const local = functionNamed(mod, tag.text);
547
+ if (local !== undefined) return { mod, fn: local };
548
+ const imported = mod.localImports.get(tag.text);
549
+ const target = imported === undefined ? undefined : modules.get(imported.file);
550
+ if (target === undefined) return undefined;
551
+ const declared = target.exports.get(imported.name);
552
+ const fn = declared === undefined ? undefined : functionNamed(target, declared);
553
+ return fn === undefined ? undefined : { mod: target, fn };
554
+ };
555
+
556
+ const functionNamed = (mod, ident) => {
557
+ for (const statement of mod.sf.statements) {
558
+ if (ts.isFunctionDeclaration(statement) && statement.name?.text === ident) return statement;
559
+ if (ts.isVariableStatement(statement)) {
560
+ for (const declaration of statement.declarationList.declarations) {
561
+ const init = declaration.initializer;
562
+ if (ts.isIdentifier(declaration.name) && declaration.name.text === ident && init !== undefined && (ts.isArrowFunction(init) || ts.isFunctionExpression(init))) return init;
563
+ }
564
+ }
565
+ }
566
+ return undefined;
567
+ };
568
+
569
+ /**
570
+ * What one card holds: the slots marked under it but under no card nested in it,
571
+ * followed into the components it renders, and whether anything under it marks a slot
572
+ * this codemod cannot trace (a `.map` item, a context value, `{children}`).
573
+ */
574
+ const holdings = (mod, node, depth, seen) => {
575
+ const refs = [];
576
+ let untraced = false;
577
+ const walk = (at, top) => {
578
+ if (!top && isCard(mod, at)) return;
579
+ if (ts.isJsxAttribute(at) && ts.isIdentifier(at.name) && at.name.text === "slot") {
580
+ const found = slotRefs(mod, expressionValue(at));
581
+ if (found === undefined) untraced = true;
582
+ else refs.push(...found);
583
+ }
584
+ if (ts.isJsxExpression(at) && at.parent !== undefined && (ts.isJsxElement(at.parent) || ts.isJsxFragment(at.parent)) && at.expression !== undefined) {
585
+ let child = at.expression;
586
+ while (ts.isAsExpression(child) || ts.isParenthesizedExpression(child) || ts.isNonNullExpression(child)) child = child.expression;
587
+ if ((ts.isIdentifier(child) && child.text === "children") || (ts.isPropertyAccessExpression(child) && child.name.text === "children")) untraced = true;
588
+ }
589
+ if (ts.isJsxElement(at) || ts.isJsxSelfClosingElement(at)) {
590
+ const component = componentOf(mod, openingOf(at).tagName);
591
+ if (component !== undefined && depth < TRACE_DEPTH && !seen.has(component.fn)) {
592
+ const inner = holdings(component.mod, component.fn, depth + 1, new Set([...seen, component.fn]));
593
+ refs.push(...inner.refs);
594
+ untraced ||= inner.untraced;
595
+ } else if (component === undefined && handsBindings(mod, openingOf(at))) {
596
+ untraced = true;
597
+ }
598
+ }
599
+ ts.forEachChild(at, (child) => walk(child, false));
600
+ };
601
+ walk(node, true);
602
+ return { refs, untraced };
603
+ };
604
+
605
+ /** Whether a JSX element this codemod cannot follow is handed bindings it may mark: a spread, the map, a slot. */
606
+ const handsBindings = (mod, opening) =>
607
+ opening.attributes.properties.some((attr) => {
608
+ if (ts.isJsxSpreadAttribute(attr)) return true;
609
+ let expr = expressionValue(attr);
610
+ while (expr !== undefined && ts.isPropertyAccessExpression(expr)) expr = expr.expression;
611
+ if (expr === undefined || !ts.isIdentifier(expr)) return false;
612
+ const render = renderOf(mod, opening);
613
+ return render?.binding === expr.text || Boolean(mod.mapNames.get(expr.text)) || mod.slotProps.has(expr.text);
614
+ });
615
+
616
+ // ---- Cards ------------------------------------------------------------------------
617
+
618
+ const groups = new Map(); // section → Map(id → group)
619
+ const deletions = [];
620
+ for (const mod of modules.values()) {
621
+ const cards = [];
622
+ const find = (node) => {
623
+ if (isCard(mod, node)) cards.push(node);
624
+ ts.forEachChild(node, find);
625
+ };
626
+ find(mod.sf);
627
+ for (const card of cards) {
628
+ if (attributeOf(card, "slot") !== undefined) {
629
+ renameTags(mod, card);
630
+ continue;
631
+ }
632
+ const line = lineOf(mod.sf, card);
633
+ const idAttr = attributeOf(card, "id");
634
+ const labelAttr = attributeOf(card, "label");
635
+ const id = idAttr === undefined ? undefined : stringValue(idAttr);
636
+ const label = labelAttr === undefined ? undefined : stringValue(labelAttr);
637
+ if (id === undefined || label === undefined) {
638
+ mod.refused.push(`a card at line ${String(line)} whose \`id\` or \`label\` is not a plain string — declare the object by hand`);
639
+ mod.groupLeft = true;
640
+ continue;
641
+ }
642
+ const { refs, untraced } = holdings(mod, card, 0, new Set());
643
+ const entry = { mod, card, line, id, label, refs };
644
+ if (refs.length === 0) {
645
+ if (untraced) {
646
+ mod.refused.push(`the card "${id}" at line ${String(line)} marks no slot this codemod can trace to a section — declare its slots as \`${id}: object({ label: compute(() => ${JSON.stringify(label)}), fields })\` in the section and pass the object down to a \`<Slot.Object>\``);
647
+ mod.groupLeft = true;
648
+ } else {
649
+ deletions.push(entry);
650
+ }
651
+ continue;
652
+ }
653
+ const sections = new Set(refs.map((ref) => ref.section));
654
+ if (sections.size > 1) {
655
+ mod.refused.push(`the card "${id}" at line ${String(line)} marks slots of ${String(sections.size)} sections — split it, one object per section`);
656
+ mod.groupLeft = true;
657
+ continue;
658
+ }
659
+ const [section] = sections;
660
+ const perSection = groups.get(section) ?? new Map();
661
+ groups.set(section, perSection);
662
+ const group = perSection.get(id) ?? { id, label, cards: [], problems: [] };
663
+ if (group.label !== label) group.problems.push(`the card "${id}" at line ${String(line)} is labeled both "${group.label}" and "${label}" — pick one label for the object`);
664
+ group.cards.push(entry);
665
+ perSection.set(id, group);
666
+ }
667
+ }
668
+
669
+ // A deleted card keeps the element it rendered, without the card's own props.
670
+ function plainElement(entry) {
671
+ const { mod, card } = entry;
672
+ const opening = openingOf(card);
673
+ const asAttr = attributeOf(card, "as");
674
+ let tag = "div";
675
+ if (asAttr !== undefined) {
676
+ const literal = stringValue(asAttr);
677
+ const expr = expressionValue(asAttr);
678
+ if (literal !== undefined) tag = literal;
679
+ else if (expr !== undefined && (ts.isIdentifier(expr) || ts.isPropertyAccessExpression(expr))) tag = expr.getText(mod.sf);
680
+ else return false;
681
+ }
682
+ mod.edits.add(opening.tagName.getStart(mod.sf), opening.tagName.getEnd(), tag);
683
+ if (ts.isJsxElement(card)) mod.edits.add(card.closingElement.tagName.getStart(mod.sf), card.closingElement.tagName.getEnd(), tag);
684
+ for (const own of ["id", "label", "as", "selectable"]) {
685
+ const attr = attributeOf(card, own);
686
+ if (attr !== undefined) removeAttribute(mod, opening, attr);
687
+ }
688
+ return true;
689
+ }
690
+ for (const entry of deletions) {
691
+ if (!plainElement(entry)) {
692
+ entry.mod.refused.push(`the card "${entry.id}" at line ${String(entry.line)} renders no slot, and its \`as\` is not an element this codemod can keep — delete the card by hand`);
693
+ entry.mod.groupLeft = true;
694
+ }
695
+ }
696
+
697
+ function removeAttribute(mod, opening, attr) {
698
+ let from = attr.getStart(mod.sf);
699
+ while (from > opening.getStart(mod.sf) && /\s/.test(mod.text[from - 1])) from -= 1;
700
+ mod.edits.add(from, attr.getEnd(), "");
701
+ }
702
+
703
+ function renameTags(mod, card) {
704
+ const opening = openingOf(card);
705
+ const tag = mod.name("Slot") ?? "Slot";
706
+ mod.edits.add(opening.tagName.getStart(mod.sf), opening.tagName.getEnd(), `${tag}.Object`);
707
+ if (ts.isJsxElement(card)) mod.edits.add(card.closingElement.tagName.getStart(mod.sf), card.closingElement.tagName.getEnd(), `${tag}.Object`);
708
+ mod.uses.add("Slot");
709
+ }
710
+
711
+ // ---- Passing an object down to a component handed single slots ----------------------
712
+
713
+ /** Props types already given an object's member, so two components sharing one extend it once. */
714
+ const extendedTypes = new Set();
715
+
716
+ /**
717
+ * Plan the edits that hand `fn` (in `mod`) a prop `id` carrying the object, and every
718
+ * place that renders `fn` the value for it. Returns the plan's edit list, or a reason
719
+ * it cannot be planned. `ident` is how `mod` names the section in a type.
720
+ */
721
+ function planProp(mod, fn, section, ident, id, planned) {
722
+ const key = `${mod.file}:${String(fn.pos)}:${id}`;
723
+ if (planned.has(key)) return [];
724
+ planned.add(key);
725
+ const param = fn.parameters[0];
726
+ if (param === undefined || !ts.isObjectBindingPattern(param.name)) return `its component takes no destructured props`;
727
+ const names = new Set();
728
+ const collect = (node) => {
729
+ if ((ts.isBindingElement(node) || ts.isVariableDeclaration(node) || ts.isParameter(node)) && ts.isIdentifier(node.name)) names.add(node.name.text);
730
+ ts.forEachChild(node, collect);
731
+ };
732
+ collect(fn);
733
+ if (names.has(id)) return `its component already names something \`${id}\``;
734
+ const ops = [];
735
+ const elements = param.name.elements;
736
+ const lastElement = [...elements].reverse().find((element) => element.dotDotDotToken === undefined);
737
+ if (lastElement === undefined) return `its component takes no named props`;
738
+ const element = appendEntry(mod.text, lastElement, id, ",");
739
+ ops.push(() => mod.edits.insert(element.at, element.text));
740
+ // The prop's type: an inline literal, or an interface or type literal it names (under
741
+ // a `Pick`, which then picks the new prop too).
742
+ const props = membersOf(mod, param.type);
743
+ if (props === undefined || props.members.length === 0) return `its component's props type is not a type literal this codemod can extend`;
744
+ const typeMod = props.mod;
745
+ const memberKey = `${typeMod.file}:${String(props.owner.pos)}:${id}`;
746
+ const member = appendEntry(typeMod.text, props.members[props.members.length - 1], `${id}: ObjectBinding<typeof ${ident}.schema.slots.${id}>`, ";");
747
+ ops.push(() => {
748
+ if (extendedTypes.has(memberKey)) return;
749
+ extendedTypes.add(memberKey);
750
+ typeMod.edits.insert(member.at, member.text);
751
+ typeMod.uses.add("ObjectBinding");
752
+ });
753
+ if (props.pick !== undefined) {
754
+ const keys = props.pick.keys;
755
+ if (keys === undefined) return `its component's props pick from a type this codemod cannot extend`;
756
+ const pickMod = props.pick.mod;
757
+ ops.push(() => pickMod.edits.insert(keys.getEnd(), ` | ${JSON.stringify(id)}`));
758
+ }
759
+ // Every place that renders it.
760
+ const fnName = functionName(fn);
761
+ if (fnName === undefined) return `its component has no name to find it by`;
762
+ const exportedAs = [...mod.exports].filter(([, local]) => local === fnName).map(([exported]) => exported);
763
+ const usages = [];
764
+ for (const other of modules.values()) {
765
+ const locals = new Set(other === mod ? [fnName] : []);
766
+ for (const [local, imported] of other.localImports) if (imported.file === mod.file && exportedAs.includes(imported.name)) locals.add(local);
767
+ if (locals.size === 0) continue;
768
+ const visit = (node) => {
769
+ if ((ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) && ts.isIdentifier(node.tagName) && locals.has(node.tagName.text)) usages.push({ mod: other, opening: node });
770
+ ts.forEachChild(node, visit);
771
+ };
772
+ visit(other.sf);
773
+ }
774
+ if (usages.length === 0) return `nothing in the workspace renders its component`;
775
+ for (const usage of usages) {
776
+ const attrs = usage.opening.attributes.properties;
777
+ if (attrs.some((a) => ts.isJsxSpreadAttribute(a))) return `a place that renders its component spreads its props (line ${String(lineOf(usage.mod.sf, usage.opening))})`;
778
+ if (attrs.some((a) => ts.isJsxAttribute(a) && ts.isIdentifier(a.name) && a.name.text === id)) continue;
779
+ let value;
780
+ const render = renderOf(usage.mod, usage.opening);
781
+ if (render === section) value = `${render.binding}.${id}`;
782
+ else {
783
+ for (const attr of attrs) {
784
+ const refs = slotRefs(usage.mod, expressionValue(attr));
785
+ const ref = refs?.find((r) => r.section === section);
786
+ if (ref === undefined) continue;
787
+ if (ref.via === "map") value = `${ref.name}.${id}`;
788
+ else if (ref.via === "prop") {
789
+ const outer = enclosingFunctionWithProps(usage.opening, [ref.name]);
790
+ if (outer === undefined) break;
791
+ const nested = planProp(usage.mod, outer, section, ref.ident, id, planned);
792
+ if (typeof nested === "string") return nested;
793
+ ops.push(...nested);
794
+ value = id;
795
+ }
796
+ break;
797
+ }
798
+ }
799
+ if (value === undefined) return `a place that renders its component (line ${String(lineOf(usage.mod.sf, usage.opening))}) holds no binding of the section to pass down`;
800
+ const last = attrs[attrs.length - 1];
801
+ const at = last === undefined ? usage.opening.tagName.getEnd() : last.getEnd();
802
+ const multiline = last !== undefined && attrs.length > 1 && lineOf(usage.mod.sf, attrs[0]) !== lineOf(usage.mod.sf, last);
803
+ const indent = last === undefined ? "" : lineIndent(usage.mod.text, last.getStart(usage.mod.sf));
804
+ const attrText = multiline ? `\n${indent}${id}={${value}}` : ` ${id}={${value}}`;
805
+ const target = usage.mod;
806
+ ops.push(() => target.edits.insert(at, attrText));
807
+ }
808
+ return ops;
809
+ }
810
+
811
+ // ---- Objects ------------------------------------------------------------------------
812
+
813
+ const moved = [];
814
+ for (const [section, perSection] of groups) {
815
+ const { mod, slots, binding, declaredSlots } = section;
816
+ const sf = mod.sf;
817
+ const order = [...declaredSlots.keys()];
818
+ const plans = [];
819
+ const ownerOf = new Map();
820
+ for (const group of perSection.values()) {
821
+ const members = new Set(group.cards.flatMap((entry) => entry.refs.map((ref) => ref.key)));
822
+ const fields = order.filter((key) => members.has(key));
823
+ // Reported at the first copy's file; a copy elsewhere is named by its own.
824
+ const lines = group.cards.map((entry) => `${entry.mod === group.cards[0].mod ? "" : `${name(entry.mod.file)} `}line ${String(entry.line)}`).join(", ");
825
+ const at = `the card "${group.id}" at ${lines}`;
826
+ const problems = [...group.problems];
827
+ const sole = fields.length === 1 ? declaredSlots.get(fields[0]) : undefined;
828
+ const unwrap = sole !== undefined && (fields[0] === group.id || (ts.isPropertyAssignment(sole) && NOT_A_FIELD.test(sole.initializer.getText(sf))));
829
+ if (slots === undefined || binding === undefined) problems.push(`${at} is in a section whose \`slots\` or \`({ slot })\` this codemod cannot read — declare the object by hand`);
830
+ if (!unwrap && declaredSlots.has(group.id) && !members.has(group.id)) problems.push(`${at} shares its id with a slot — rename the object`);
831
+ for (const field of fields) {
832
+ const owner = ownerOf.get(field);
833
+ if (owner !== undefined) {
834
+ const message = `the slot "${field}" is marked in two cards, "${owner.group.id}" (${owner.lines}) and "${group.id}" (${lines}), neither holding the other — keep it in one object`;
835
+ problems.push(message);
836
+ owner.problems.push(message);
837
+ }
838
+ ownerOf.set(field, { group, lines, problems });
839
+ const property = declaredSlots.get(field);
840
+ if (!unwrap && ts.isPropertyAssignment(property) && NOT_A_FIELD.test(property.initializer.getText(sf))) {
841
+ problems.push(`the slot "${field}" in ${at} cannot be an object's field (an object, or a list of objects or rows) — keep it at the top level`);
842
+ }
843
+ }
844
+ plans.push({ group, fields, problems, unwrap, at });
845
+ }
846
+
847
+ const fieldOf = new Map();
848
+ for (const plan of plans) {
849
+ const { group, fields, problems, unwrap, at } = plan;
850
+ if (problems.length === 0 && unwrap) {
851
+ for (const entry of group.cards) if (!plainElement(entry)) problems.push(`${at} holds only its own slot, and its \`as\` is not an element this codemod can keep — delete the card by hand`);
852
+ } else if (problems.length === 0) {
853
+ // How each copy of the card reaches the object.
854
+ const planned = new Set();
855
+ const ops = [];
856
+ for (const entry of group.cards) {
857
+ // Only what the card's own module marks says how that module reaches the section.
858
+ const direct = entry.refs.filter((ref) => ref.mod === entry.mod);
859
+ const routes = new Set(direct.map((ref) => `${ref.via}:${ref.via === "prop" ? "" : ref.name}`));
860
+ let expr;
861
+ if (renderOf(entry.mod, entry.card) === section) expr = `${binding}.${group.id}`;
862
+ else if (routes.size === 1 && direct[0].via === "map") expr = `${direct[0].name}.${group.id}`;
863
+ else if (routes.size === 1 && direct[0].via === "prop") {
864
+ const fn = enclosingFunctionWithProps(entry.card, direct.map((ref) => ref.name));
865
+ const planOps = fn === undefined ? "its component takes none of those slots as props" : planProp(entry.mod, fn, section, direct[0].ident, group.id, planned);
866
+ if (typeof planOps === "string") {
867
+ problems.push(`${at} cannot have the object passed down — ${planOps}; declare \`${group.id}: object({ label: compute(() => ${JSON.stringify(group.label)}), fields })\` in the section and pass it down by hand`);
868
+ break;
869
+ }
870
+ ops.push(...planOps);
871
+ expr = group.id;
872
+ } else {
873
+ problems.push(`${at} marks its slots by more than one route, or only inside the components it renders — pass the object down by hand`);
874
+ break;
875
+ }
876
+ entry.expr = expr;
877
+ }
878
+ if (problems.length === 0) {
879
+ for (const op of ops) op();
880
+ declareObject(section, group, fields);
881
+ for (const entry of group.cards) markObject(entry);
882
+ for (const field of fields) fieldOf.set(field, group.id);
883
+ for (const field of fields) moved.push({ section: section.name, field, object: group.id });
884
+ }
885
+ }
886
+ if (problems.length > 0) {
887
+ for (const entry of group.cards) entry.mod.groupLeft = true;
888
+ for (const problem of problems) {
889
+ const where = group.cards[0].mod;
890
+ where.refused.push(problem);
891
+ }
892
+ }
893
+ }
894
+ if (fieldOf.size > 0) rewriteReads(section, fieldOf);
895
+ }
896
+
897
+ /** The component around `node` that destructures any of the single-slot props `names`. */
898
+ function enclosingFunctionWithProps(node, names) {
899
+ const props = new Set(names);
900
+ for (let fn = enclosingFunction(node); fn !== undefined; fn = enclosingFunction(fn)) {
901
+ const param = fn.parameters[0];
902
+ if (param !== undefined && ts.isObjectBindingPattern(param.name) && param.name.elements.some((element) => ts.isIdentifier(element.name) && props.has(element.name.text))) return fn;
903
+ }
904
+ return undefined;
905
+ }
906
+
907
+ /**
908
+ * The comments written above `node`, one entry per line, each line's own indent dropped
909
+ * (a block comment's ` *` lines keep their one-space lead).
910
+ */
911
+ function leadingCommentLines(sf, node) {
912
+ const ranges = ts.getLeadingCommentRanges(sf.text, node.getFullStart()) ?? [];
913
+ return ranges.flatMap((range) =>
914
+ sf.text
915
+ .slice(range.pos, range.end)
916
+ .split("\n")
917
+ .map((line) => line.trim().replace(/^\*/, " *")),
918
+ );
919
+ }
920
+
921
+ function declareObject(section, group, fields) {
922
+ const { mod, declaredSlots } = section;
923
+ const { sf, text } = mod;
924
+ // The schema: the object where its first field was declared, the rest removed.
925
+ const [first, ...rest] = fields.map((key) => declaredSlots.get(key));
926
+ const indent = lineIndent(text, first.getStart(sf));
927
+ // The first slot's comments stay above the object it becomes; each other slot's
928
+ // move with it, since removing the slot removes the lines above it too.
929
+ const fieldTexts = fields.map((key, i) => {
930
+ const property = declaredSlots.get(key);
931
+ const own = indentTail(property.getText(sf), " ");
932
+ return i === 0 ? own : [...leadingCommentLines(sf, property), own].join(`\n${indent} `);
933
+ });
934
+ const block = [
935
+ `${group.id}: object({`,
936
+ `${indent} label: ${mod.imported.get("compute")?.name.text ?? "compute"}(() => ${JSON.stringify(group.label)}),`,
937
+ `${indent} fields: {`,
938
+ ...fieldTexts.map((field) => `${indent} ${field},`),
939
+ `${indent} },`,
940
+ `${indent}})`,
941
+ ].join("\n");
942
+ mod.edits.add(first.getStart(sf), first.getEnd(), block);
943
+ for (const property of rest) {
944
+ const range = propertyRemoval(text, property, sf);
945
+ mod.edits.add(range.start, range.end, "");
946
+ }
947
+ mod.uses.add("object");
948
+ mod.uses.add("compute");
949
+ }
950
+
951
+ function markObject(entry) {
952
+ const { mod, card, expr } = entry;
953
+ const opening = openingOf(card);
954
+ renameTags(mod, card);
955
+ const idAttr = attributeOf(card, "id");
956
+ mod.edits.add(idAttr.getStart(mod.sf), idAttr.getEnd(), `slot={${expr}}`);
957
+ removeAttribute(mod, opening, attributeOf(card, "label"));
958
+ }
959
+
960
+ /** Every read of a moved slot, in every module, reads the object's field. */
961
+ function rewriteReads(section, fieldOf) {
962
+ for (const mod of modules.values()) {
963
+ const bindingName = mod.name("SlotBinding");
964
+ const visit = (node) => {
965
+ if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.expression) && fieldOf.has(node.name.text)) {
966
+ const base = node.expression.text;
967
+ const render = renderOf(mod, node);
968
+ if ((render === section && render.binding === base) || mod.mapNames.get(base) === section) {
969
+ mod.edits.add(node.getStart(mod.sf), node.getEnd(), `${base}.${fieldOf.get(node.name.text)}.fields.${node.name.text}`);
970
+ return;
971
+ }
972
+ }
973
+ // `Pick<Bindings, "street" | …>` picks the object instead.
974
+ if (ts.isTypeReferenceNode(node) && ts.isIdentifier(node.typeName) && node.typeName.text === "Pick" && node.typeArguments?.length === 2 && mod.mapOf(node.typeArguments[0]) === section) {
975
+ const keys = node.typeArguments[1];
976
+ const literals = ts.isUnionTypeNode(keys) ? keys.types : [keys];
977
+ if (literals.every((t) => ts.isLiteralTypeNode(t) && ts.isStringLiteral(t.literal)) && literals.some((t) => fieldOf.has(t.literal.text))) {
978
+ const picked = [...new Set(literals.map((t) => fieldOf.get(t.literal.text) ?? t.literal.text))];
979
+ mod.edits.add(keys.getStart(mod.sf), keys.getEnd(), picked.map((key) => JSON.stringify(key)).join(" | "));
980
+ return;
981
+ }
982
+ }
983
+ // `Hero.schema.slots.street`, read as a value, is the object's field definition.
984
+ if (
985
+ ts.isPropertyAccessExpression(node) &&
986
+ fieldOf.has(node.name.text) &&
987
+ ts.isPropertyAccessExpression(node.expression) &&
988
+ node.expression.name.text === "slots" &&
989
+ ts.isPropertyAccessExpression(node.expression.expression) &&
990
+ node.expression.expression.name.text === "schema" &&
991
+ ts.isIdentifier(node.expression.expression.expression) &&
992
+ sectionNamed(mod, node.expression.expression.expression.text) === section
993
+ ) {
994
+ mod.edits.add(node.name.getStart(mod.sf), node.name.getEnd(), `${fieldOf.get(node.name.text)}.fields.${node.name.text}`);
995
+ return;
996
+ }
997
+ // `Bindings["street"]` is the object's field binding.
998
+ if (ts.isIndexedAccessTypeNode(node) && mod.mapOf(node.objectType) === section && ts.isLiteralTypeNode(node.indexType) && ts.isStringLiteral(node.indexType.literal) && fieldOf.has(node.indexType.literal.text)) {
999
+ const key = node.indexType.literal.text;
1000
+ mod.edits.add(node.indexType.getStart(mod.sf), node.indexType.getEnd(), `${JSON.stringify(fieldOf.get(key))}]["fields"][${JSON.stringify(key)}`);
1001
+ return;
1002
+ }
1003
+ // `SlotBinding<typeof Hero.schema.slots.street>` types the field now.
1004
+ if (ts.isTypeQueryNode(node)) {
1005
+ const query = schemaQuery(mod, node);
1006
+ if (query?.section === section && query.path.length === 2 && query.path[0] === "slots" && fieldOf.has(query.path[1])) {
1007
+ mod.edits.add(node.exprName.getStart(mod.sf), node.exprName.getEnd(), `${query.ident}.schema.slots.${fieldOf.get(query.path[1])}.fields.${query.path[1]}`);
1008
+ const parent = node.parent;
1009
+ if (bindingName !== undefined && ts.isTypeReferenceNode(parent) && ts.isIdentifier(parent.typeName) && parent.typeName.text === bindingName) {
1010
+ mod.edits.add(parent.typeName.getStart(mod.sf), parent.typeName.getEnd(), "FieldBinding");
1011
+ mod.uses.add("FieldBinding");
1012
+ mod.retyped.add(parent.typeName.getStart(mod.sf));
1013
+ }
1014
+ return;
1015
+ }
1016
+ }
1017
+ ts.forEachChild(node, visit);
1018
+ };
1019
+ visit(mod.sf);
1020
+ }
1021
+ }
1022
+
1023
+ // ---- Imports, writes, report ----------------------------------------------------------
1024
+
1025
+ const rewritten = [];
1026
+ const notRewritten = [];
1027
+ for (const mod of modules.values()) {
1028
+ if (mod.edits.size > 0) {
1029
+ fixImports(mod);
1030
+ const out = mod.edits.apply(mod.text);
1031
+ if (out !== mod.text) {
1032
+ await writeFile(mod.file, out, "utf8");
1033
+ rewritten.push(name(mod.file));
1034
+ }
1035
+ }
1036
+ for (const reason of new Set(mod.manual)) notRewritten.push({ file: name(mod.file), reason: `Manual: ${reason}` });
1037
+ for (const reason of new Set(mod.refused)) notRewritten.push({ file: name(mod.file), reason });
1038
+ }
1039
+
1040
+ function fixImports(mod) {
1041
+ const { sf, text } = mod;
1042
+ const host = mod.hosts.find((statement) => !statement.importClause.isTypeOnly) ?? mod.hosts[0];
1043
+ if (host === undefined) return;
1044
+ const typeOnly = host.importClause.isTypeOnly;
1045
+ const bindings = host.importClause.namedBindings;
1046
+ // `SlotBinding` goes too once every type that named it reads `FieldBinding`.
1047
+ const bindingName = mod.name("SlotBinding");
1048
+ let bindingLeft = bindingName === undefined;
1049
+ const count = (node) => {
1050
+ if (bindingLeft || ts.isImportDeclaration(node)) return;
1051
+ if (ts.isIdentifier(node) && node.text === bindingName && !mod.retyped.has(node.getStart(sf))) bindingLeft = true;
1052
+ ts.forEachChild(node, count);
1053
+ };
1054
+ if (mod.retyped.size > 0) count(sf);
1055
+ else bindingLeft = true;
1056
+ const kept = bindings.elements
1057
+ .filter((element) => {
1058
+ const exported = (element.propertyName ?? element.name).text;
1059
+ return (mod.groupLeft || exported !== "SlotGroup") && (bindingLeft || exported !== "SlotBinding");
1060
+ })
1061
+ .map((element) => element.getText(sf));
1062
+ const wanted = [
1063
+ ["compute", false],
1064
+ ["object", false],
1065
+ ["Slot", false],
1066
+ ["ObjectBinding", true],
1067
+ ["FieldBinding", true],
1068
+ ];
1069
+ const added = wanted.filter(([exported]) => mod.uses.has(exported) && !mod.imported.has(exported)).map(([exported, isType]) => (isType && !typeOnly ? `type ${exported}` : exported));
1070
+ const all = [...kept, ...added].sort((a, b) => a.replace(/^type\s+/, "").localeCompare(b.replace(/^type\s+/, ""), "en", { sensitivity: "base" }));
1071
+ if (all.join() !== bindings.elements.map((element) => element.getText(sf)).join()) {
1072
+ const multiline = bindings.getText(sf).includes("\n");
1073
+ const indent = lineIndent(text, bindings.elements[0]?.getStart(sf) ?? bindings.getStart(sf));
1074
+ mod.edits.add(bindings.getStart(sf), bindings.getEnd(), multiline ? `{\n${all.map((entry) => `${indent}${entry},`).join("\n")}\n}` : `{ ${all.join(", ")} }`);
1075
+ }
1076
+ }
1077
+
1078
+ // Another section reading a slot that moved into an object reads something gone.
1079
+ for (const file of files) {
1080
+ const text = await readFile(file, "utf8");
1081
+ for (const { section, field, object } of moved) {
1082
+ if (new RegExp(`\\b${section}\\.slots\\.${field}\\b`).test(text)) {
1083
+ notRewritten.push({
1084
+ file: name(file),
1085
+ reason: `Manual: \`${section}.slots.${field}\` reads a slot that is now the field "${field}" of the object "${object}" — read what that field reads instead, or keep "${field}" out of the object`,
1086
+ });
1087
+ }
1088
+ }
1089
+ }
1090
+
1091
+ // Fixture content states a moved slot under its object.
1092
+ const content = await contentObjectFields(root);
1093
+ for (const file of content.rewritten) if (!rewritten.includes(file)) rewritten.push(file);
1094
+ notRewritten.push(...content.notRewritten);
1095
+ rewritten.sort();
1096
+ return { rewritten, notRewritten };
1097
+ }