@antheo/morph 0.0.0-stage → 0.3.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.
- package/LICENSE +21 -0
- package/README.md +266 -2
- package/SKILL.md +61 -0
- package/dist/ai/cli-provider.d.ts +80 -0
- package/dist/ai/cli-provider.js +250 -0
- package/dist/ai/gate.d.ts +39 -0
- package/dist/ai/gate.js +124 -0
- package/dist/ai/provider.d.ts +36 -0
- package/dist/ai/provider.js +168 -0
- package/dist/ai/repair.d.ts +90 -0
- package/dist/ai/repair.js +172 -0
- package/dist/analyzer/deps.d.ts +30 -0
- package/dist/analyzer/deps.js +265 -0
- package/dist/analyzer/manifest.d.ts +5 -0
- package/dist/analyzer/manifest.js +119 -0
- package/dist/analyzer/parse.d.ts +21 -0
- package/dist/analyzer/parse.js +29 -0
- package/dist/batch-worker.d.ts +16 -0
- package/dist/batch-worker.js +10 -0
- package/dist/batch.d.ts +26 -0
- package/dist/batch.js +227 -0
- package/dist/bridge/index.d.ts +26 -0
- package/dist/bridge/index.js +138 -0
- package/dist/bridge/props.d.ts +56 -0
- package/dist/bridge/props.js +191 -0
- package/dist/bridge/wrappers.d.ts +24 -0
- package/dist/bridge/wrappers.js +276 -0
- package/dist/cli/format.d.ts +7 -0
- package/dist/cli/format.js +30 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +492 -0
- package/dist/fonts/carry.d.ts +34 -0
- package/dist/fonts/carry.js +113 -0
- package/dist/fonts/fallback.d.ts +11 -0
- package/dist/fonts/fallback.js +21 -0
- package/dist/fonts/google.d.ts +65 -0
- package/dist/fonts/google.js +343 -0
- package/dist/fonts/index.d.ts +27 -0
- package/dist/fonts/index.js +48 -0
- package/dist/fonts/local.d.ts +46 -0
- package/dist/fonts/local.js +150 -0
- package/dist/fonts/sfnt.d.ts +19 -0
- package/dist/fonts/sfnt.js +174 -0
- package/dist/fonts/shim.d.ts +14 -0
- package/dist/fonts/shim.js +96 -0
- package/dist/gate.d.ts +105 -0
- package/dist/gate.js +157 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.js +74 -0
- package/dist/ir/annotate.d.ts +16 -0
- package/dist/ir/annotate.js +145 -0
- package/dist/ir/as-child.d.ts +18 -0
- package/dist/ir/as-child.js +239 -0
- package/dist/ir/compounds.d.ts +27 -0
- package/dist/ir/compounds.js +183 -0
- package/dist/ir/contexts.d.ts +26 -0
- package/dist/ir/contexts.js +185 -0
- package/dist/ir/dom.d.ts +11 -0
- package/dist/ir/dom.js +60 -0
- package/dist/ir/effect-returns.d.ts +10 -0
- package/dist/ir/effect-returns.js +25 -0
- package/dist/ir/extract-ui-types.d.ts +33 -0
- package/dist/ir/extract-ui-types.js +224 -0
- package/dist/ir/extract-ui.d.ts +34 -0
- package/dist/ir/extract-ui.js +567 -0
- package/dist/ir/falsy.d.ts +13 -0
- package/dist/ir/falsy.js +66 -0
- package/dist/ir/forward-refs.d.ts +11 -0
- package/dist/ir/forward-refs.js +155 -0
- package/dist/ir/fragment-source.d.ts +4 -0
- package/dist/ir/fragment-source.js +13 -0
- package/dist/ir/fragments.d.ts +32 -0
- package/dist/ir/fragments.js +192 -0
- package/dist/ir/helper-snippets.d.ts +30 -0
- package/dist/ir/helper-snippets.js +550 -0
- package/dist/ir/latest-refs.d.ts +11 -0
- package/dist/ir/latest-refs.js +187 -0
- package/dist/ir/live-args.d.ts +26 -0
- package/dist/ir/live-args.js +335 -0
- package/dist/ir/lower.d.ts +83 -0
- package/dist/ir/lower.js +6016 -0
- package/dist/ir/markup-values.d.ts +16 -0
- package/dist/ir/markup-values.js +188 -0
- package/dist/ir/model.d.ts +605 -0
- package/dist/ir/model.js +1 -0
- package/dist/ir/oracle.d.ts +91 -0
- package/dist/ir/oracle.js +610 -0
- package/dist/ir/radix.d.ts +122 -0
- package/dist/ir/radix.js +344 -0
- package/dist/ir/rename.d.ts +15 -0
- package/dist/ir/rename.js +151 -0
- package/dist/ir/render-props.d.ts +31 -0
- package/dist/ir/render-props.js +67 -0
- package/dist/ir/rewrite.d.ts +44 -0
- package/dist/ir/rewrite.js +381 -0
- package/dist/ir/running-index.d.ts +11 -0
- package/dist/ir/running-index.js +217 -0
- package/dist/ir/snapshots.d.ts +12 -0
- package/dist/ir/snapshots.js +140 -0
- package/dist/ir/style.d.ts +19 -0
- package/dist/ir/style.js +76 -0
- package/dist/ir/types.d.ts +42 -0
- package/dist/ir/types.js +271 -0
- package/dist/ir/walk.d.ts +11 -0
- package/dist/ir/walk.js +18 -0
- package/dist/mcp/server.d.ts +81 -0
- package/dist/mcp/server.js +518 -0
- package/dist/pipeline.d.ts +26 -0
- package/dist/pipeline.js +750 -0
- package/dist/registry/add.d.ts +91 -0
- package/dist/registry/add.js +273 -0
- package/dist/registry/fetch.d.ts +40 -0
- package/dist/registry/fetch.js +186 -0
- package/dist/registry/ip.d.ts +18 -0
- package/dist/registry/ip.js +153 -0
- package/dist/registry/item.d.ts +39 -0
- package/dist/registry/item.js +131 -0
- package/dist/registry/load.d.ts +35 -0
- package/dist/registry/load.js +106 -0
- package/dist/registry/paths.d.ts +8 -0
- package/dist/registry/paths.js +43 -0
- package/dist/registry/publish.d.ts +119 -0
- package/dist/registry/publish.js +439 -0
- package/dist/targets/angular/bank/bank.d.ts +39 -0
- package/dist/targets/angular/bank/bank.js +61 -0
- package/dist/targets/angular/bank/helpers.d.ts +64 -0
- package/dist/targets/angular/bank/helpers.js +246 -0
- package/dist/targets/angular/bank/kept.d.ts +98 -0
- package/dist/targets/angular/bank/kept.js +791 -0
- package/dist/targets/angular/bank/plan-helpers.d.ts +17 -0
- package/dist/targets/angular/bank/plan-helpers.js +286 -0
- package/dist/targets/angular/bank/plan.d.ts +22 -0
- package/dist/targets/angular/bank/plan.js +152 -0
- package/dist/targets/angular/bank/receivers.d.ts +66 -0
- package/dist/targets/angular/bank/receivers.js +293 -0
- package/dist/targets/angular/bank/runtime.d.ts +14 -0
- package/dist/targets/angular/bank/runtime.js +152 -0
- package/dist/targets/angular/controlled.d.ts +7 -0
- package/dist/targets/angular/controlled.js +37 -0
- package/dist/targets/angular/dynamic-tags.d.ts +34 -0
- package/dist/targets/angular/dynamic-tags.js +192 -0
- package/dist/targets/angular/emit.d.ts +41 -0
- package/dist/targets/angular/emit.js +5970 -0
- package/dist/targets/angular/exports.d.ts +48 -0
- package/dist/targets/angular/exports.js +243 -0
- package/dist/targets/angular/helpers.d.ts +15 -0
- package/dist/targets/angular/helpers.js +33 -0
- package/dist/targets/angular/host-boundary.d.ts +5 -0
- package/dist/targets/angular/host-boundary.js +166 -0
- package/dist/targets/angular/host-form.d.ts +150 -0
- package/dist/targets/angular/host-form.js +727 -0
- package/dist/targets/angular/markup-values.d.ts +58 -0
- package/dist/targets/angular/markup-values.js +661 -0
- package/dist/targets/angular/motion-directive.d.ts +12 -0
- package/dist/targets/angular/motion-directive.js +257 -0
- package/dist/targets/angular/motion-hooks.d.ts +73 -0
- package/dist/targets/angular/motion-hooks.js +293 -0
- package/dist/targets/angular/motion-layout.d.ts +14 -0
- package/dist/targets/angular/motion-layout.js +74 -0
- package/dist/targets/angular/motion-templates.d.ts +10 -0
- package/dist/targets/angular/motion-templates.js +65 -0
- package/dist/targets/angular/motion.d.ts +57 -0
- package/dist/targets/angular/motion.js +202 -0
- package/dist/targets/angular/narrowing.d.ts +24 -0
- package/dist/targets/angular/narrowing.js +205 -0
- package/dist/targets/angular/ngc.d.ts +23 -0
- package/dist/targets/angular/ngc.js +134 -0
- package/dist/targets/angular/passed-handle.d.ts +39 -0
- package/dist/targets/angular/passed-handle.js +154 -0
- package/dist/targets/angular/projected.d.ts +9 -0
- package/dist/targets/angular/projected.js +63 -0
- package/dist/targets/angular/radix/cores/alert-dialog.d.ts +13 -0
- package/dist/targets/angular/radix/cores/alert-dialog.js +388 -0
- package/dist/targets/angular/radix/cores/index.d.ts +17 -0
- package/dist/targets/angular/radix/cores/index.js +50 -0
- package/dist/targets/angular/radix/cores/slider.d.ts +8 -0
- package/dist/targets/angular/radix/cores/slider.js +202 -0
- package/dist/targets/angular/radix/index.d.ts +21 -0
- package/dist/targets/angular/radix/index.js +35 -0
- package/dist/targets/angular/radix/packages.d.ts +3 -0
- package/dist/targets/angular/radix/packages.js +558 -0
- package/dist/targets/angular/radix/part.d.ts +236 -0
- package/dist/targets/angular/radix/part.js +71 -0
- package/dist/targets/angular/radix/prepare.d.ts +66 -0
- package/dist/targets/angular/radix/prepare.js +548 -0
- package/dist/targets/angular/radix/template.d.ts +43 -0
- package/dist/targets/angular/radix/template.js +172 -0
- package/dist/targets/angular/refs.d.ts +68 -0
- package/dist/targets/angular/refs.js +443 -0
- package/dist/targets/angular/slots.d.ts +22 -0
- package/dist/targets/angular/slots.js +60 -0
- package/dist/targets/angular/source-types.d.ts +10 -0
- package/dist/targets/angular/source-types.js +45 -0
- package/dist/targets/angular/spread-forward.d.ts +24 -0
- package/dist/targets/angular/spread-forward.js +72 -0
- package/dist/targets/angular/spread.d.ts +32 -0
- package/dist/targets/angular/spread.js +182 -0
- package/dist/targets/angular/svg-hosts.d.ts +25 -0
- package/dist/targets/angular/svg-hosts.js +107 -0
- package/dist/targets/angular/template-expr.d.ts +36 -0
- package/dist/targets/angular/template-expr.js +287 -0
- package/dist/targets/angular/types.d.ts +9 -0
- package/dist/targets/angular/types.js +63 -0
- package/dist/targets/angular/walk.d.ts +11 -0
- package/dist/targets/angular/walk.js +26 -0
- package/dist/targets/react-native/emit.d.ts +14 -0
- package/dist/targets/react-native/emit.js +585 -0
- package/dist/targets/react-native/fidelity.d.ts +5 -0
- package/dist/targets/react-native/fidelity.js +43 -0
- package/dist/targets/react-native/motion.d.ts +10 -0
- package/dist/targets/react-native/motion.js +290 -0
- package/dist/targets/react-native/skia.d.ts +30 -0
- package/dist/targets/react-native/skia.js +358 -0
- package/dist/targets/shared/context.d.ts +47 -0
- package/dist/targets/shared/context.js +195 -0
- package/dist/targets/shared/foreign.d.ts +10 -0
- package/dist/targets/shared/foreign.js +72 -0
- package/dist/targets/shared/fragments.d.ts +21 -0
- package/dist/targets/shared/fragments.js +309 -0
- package/dist/targets/shared/handles.d.ts +65 -0
- package/dist/targets/shared/handles.js +299 -0
- package/dist/targets/shared/inline.d.ts +53 -0
- package/dist/targets/shared/inline.js +524 -0
- package/dist/targets/shared/layout.d.ts +70 -0
- package/dist/targets/shared/layout.js +224 -0
- package/dist/targets/shared/markup.d.ts +26 -0
- package/dist/targets/shared/markup.js +65 -0
- package/dist/targets/shared/module.d.ts +84 -0
- package/dist/targets/shared/module.js +468 -0
- package/dist/targets/shared/motion-hooks.d.ts +37 -0
- package/dist/targets/shared/motion-hooks.js +87 -0
- package/dist/targets/shared/motion-layout.d.ts +70 -0
- package/dist/targets/shared/motion-layout.js +392 -0
- package/dist/targets/shared/portal.d.ts +10 -0
- package/dist/targets/shared/portal.js +23 -0
- package/dist/targets/shared/prop-writes.d.ts +11 -0
- package/dist/targets/shared/prop-writes.js +32 -0
- package/dist/targets/shared/prune.d.ts +54 -0
- package/dist/targets/shared/prune.js +405 -0
- package/dist/targets/shared/react-libs.d.ts +25 -0
- package/dist/targets/shared/react-libs.js +142 -0
- package/dist/targets/shared/react-types.d.ts +16 -0
- package/dist/targets/shared/react-types.js +374 -0
- package/dist/targets/shared/react-values.d.ts +20 -0
- package/dist/targets/shared/react-values.js +145 -0
- package/dist/targets/shared/ref-callback.d.ts +16 -0
- package/dist/targets/shared/ref-callback.js +114 -0
- package/dist/targets/shared/script.d.ts +65 -0
- package/dist/targets/shared/script.js +406 -0
- package/dist/targets/shared/unused.d.ts +12 -0
- package/dist/targets/shared/unused.js +146 -0
- package/dist/targets/svelte/dom-spread.d.ts +27 -0
- package/dist/targets/svelte/dom-spread.js +146 -0
- package/dist/targets/svelte/emit.d.ts +15 -0
- package/dist/targets/svelte/emit.js +3784 -0
- package/dist/targets/svelte/handle-branches.d.ts +22 -0
- package/dist/targets/svelte/handle-branches.js +146 -0
- package/dist/targets/svelte/html-keys.d.ts +9 -0
- package/dist/targets/svelte/html-keys.js +40 -0
- package/dist/targets/svelte/markup-data/data.d.ts +51 -0
- package/dist/targets/svelte/markup-data/data.js +305 -0
- package/dist/targets/svelte/markup-data/helper.d.ts +12 -0
- package/dist/targets/svelte/markup-data/helper.js +81 -0
- package/dist/targets/svelte/markup-data/module-data.d.ts +21 -0
- package/dist/targets/svelte/markup-data/module-data.js +473 -0
- package/dist/targets/svelte/markup-data/sites.d.ts +52 -0
- package/dist/targets/svelte/markup-data/sites.js +303 -0
- package/dist/targets/svelte/motion-layout.d.ts +13 -0
- package/dist/targets/svelte/motion-layout.js +70 -0
- package/dist/targets/svelte/motion-table.d.ts +14 -0
- package/dist/targets/svelte/motion-table.js +308 -0
- package/dist/targets/svelte/motion.d.ts +46 -0
- package/dist/targets/svelte/motion.js +405 -0
- package/dist/targets/svelte/presence.d.ts +71 -0
- package/dist/targets/svelte/presence.js +318 -0
- package/dist/targets/svelte/radix/accordion.d.ts +9 -0
- package/dist/targets/svelte/radix/accordion.js +24 -0
- package/dist/targets/svelte/radix/aspect-ratio.d.ts +6 -0
- package/dist/targets/svelte/radix/aspect-ratio.js +8 -0
- package/dist/targets/svelte/radix/avatar.d.ts +6 -0
- package/dist/targets/svelte/radix/avatar.js +10 -0
- package/dist/targets/svelte/radix/checkbox.d.ts +10 -0
- package/dist/targets/svelte/radix/checkbox.js +24 -0
- package/dist/targets/svelte/radix/collapsible.d.ts +7 -0
- package/dist/targets/svelte/radix/collapsible.js +16 -0
- package/dist/targets/svelte/radix/context-menu.d.ts +8 -0
- package/dist/targets/svelte/radix/context-menu.js +49 -0
- package/dist/targets/svelte/radix/cores/alert-dialog.d.ts +17 -0
- package/dist/targets/svelte/radix/cores/alert-dialog.js +449 -0
- package/dist/targets/svelte/radix/cores/collapsible.d.ts +12 -0
- package/dist/targets/svelte/radix/cores/collapsible.js +214 -0
- package/dist/targets/svelte/radix/cores/core.d.ts +32 -0
- package/dist/targets/svelte/radix/cores/core.js +80 -0
- package/dist/targets/svelte/radix/cores/direction.d.ts +10 -0
- package/dist/targets/svelte/radix/cores/direction.js +64 -0
- package/dist/targets/svelte/radix/cores/index.d.ts +19 -0
- package/dist/targets/svelte/radix/cores/index.js +85 -0
- package/dist/targets/svelte/radix/cores/modal-layer.d.ts +11 -0
- package/dist/targets/svelte/radix/cores/modal-layer.js +244 -0
- package/dist/targets/svelte/radix/cores/presence.d.ts +12 -0
- package/dist/targets/svelte/radix/cores/presence.js +129 -0
- package/dist/targets/svelte/radix/cores/slider-logic.d.ts +10 -0
- package/dist/targets/svelte/radix/cores/slider-logic.js +287 -0
- package/dist/targets/svelte/radix/cores/slider.d.ts +13 -0
- package/dist/targets/svelte/radix/cores/slider.js +267 -0
- package/dist/targets/svelte/radix/dialog.d.ts +8 -0
- package/dist/targets/svelte/radix/dialog.js +20 -0
- package/dist/targets/svelte/radix/dropdown-menu.d.ts +9 -0
- package/dist/targets/svelte/radix/dropdown-menu.js +38 -0
- package/dist/targets/svelte/radix/emit.d.ts +12 -0
- package/dist/targets/svelte/radix/emit.js +1339 -0
- package/dist/targets/svelte/radix/hover-card.d.ts +7 -0
- package/dist/targets/svelte/radix/hover-card.js +22 -0
- package/dist/targets/svelte/radix/index.d.ts +12 -0
- package/dist/targets/svelte/radix/index.js +28 -0
- package/dist/targets/svelte/radix/label.d.ts +3 -0
- package/dist/targets/svelte/radix/label.js +5 -0
- package/dist/targets/svelte/radix/menubar.d.ts +7 -0
- package/dist/targets/svelte/radix/menubar.js +34 -0
- package/dist/targets/svelte/radix/navigation-menu.d.ts +8 -0
- package/dist/targets/svelte/radix/navigation-menu.js +25 -0
- package/dist/targets/svelte/radix/packages.d.ts +34 -0
- package/dist/targets/svelte/radix/packages.js +100 -0
- package/dist/targets/svelte/radix/part.d.ts +161 -0
- package/dist/targets/svelte/radix/part.js +85 -0
- package/dist/targets/svelte/radix/popover.d.ts +7 -0
- package/dist/targets/svelte/radix/popover.js +15 -0
- package/dist/targets/svelte/radix/progress.d.ts +11 -0
- package/dist/targets/svelte/radix/progress.js +27 -0
- package/dist/targets/svelte/radix/radio-group.d.ts +9 -0
- package/dist/targets/svelte/radix/radio-group.js +26 -0
- package/dist/targets/svelte/radix/scroll-area.d.ts +7 -0
- package/dist/targets/svelte/radix/scroll-area.js +19 -0
- package/dist/targets/svelte/radix/select.d.ts +13 -0
- package/dist/targets/svelte/radix/select.js +81 -0
- package/dist/targets/svelte/radix/separator.d.ts +3 -0
- package/dist/targets/svelte/radix/separator.js +5 -0
- package/dist/targets/svelte/radix/slider.d.ts +6 -0
- package/dist/targets/svelte/radix/slider.js +8 -0
- package/dist/targets/svelte/radix/switch.d.ts +6 -0
- package/dist/targets/svelte/radix/switch.js +10 -0
- package/dist/targets/svelte/radix/tabs.d.ts +8 -0
- package/dist/targets/svelte/radix/tabs.js +15 -0
- package/dist/targets/svelte/radix/toggle-group.d.ts +9 -0
- package/dist/targets/svelte/radix/toggle-group.js +17 -0
- package/dist/targets/svelte/radix/toggle.d.ts +6 -0
- package/dist/targets/svelte/radix/toggle.js +9 -0
- package/dist/targets/svelte/radix/tooltip.d.ts +6 -0
- package/dist/targets/svelte/radix/tooltip.js +24 -0
- package/dist/targets/svelte/strip.d.ts +2 -0
- package/dist/targets/svelte/strip.js +5 -0
- package/dist/targets/vue/emit.d.ts +15 -0
- package/dist/targets/vue/emit.js +4092 -0
- package/dist/targets/vue/events.d.ts +11 -0
- package/dist/targets/vue/events.js +13 -0
- package/dist/targets/vue/module-builders.d.ts +28 -0
- package/dist/targets/vue/module-builders.js +258 -0
- package/dist/targets/vue/motion-layout.d.ts +19 -0
- package/dist/targets/vue/motion-layout.js +79 -0
- package/dist/targets/vue/motion.d.ts +12 -0
- package/dist/targets/vue/motion.js +46 -0
- package/dist/targets/vue/radix/accordion.d.ts +3 -0
- package/dist/targets/vue/radix/accordion.js +12 -0
- package/dist/targets/vue/radix/alert-dialog.d.ts +6 -0
- package/dist/targets/vue/radix/alert-dialog.js +18 -0
- package/dist/targets/vue/radix/aspect-ratio.d.ts +6 -0
- package/dist/targets/vue/radix/aspect-ratio.js +8 -0
- package/dist/targets/vue/radix/avatar.d.ts +3 -0
- package/dist/targets/vue/radix/avatar.js +7 -0
- package/dist/targets/vue/radix/checkbox.d.ts +3 -0
- package/dist/targets/vue/radix/checkbox.js +9 -0
- package/dist/targets/vue/radix/collapsible.d.ts +7 -0
- package/dist/targets/vue/radix/collapsible.js +11 -0
- package/dist/targets/vue/radix/context-menu.d.ts +3 -0
- package/dist/targets/vue/radix/context-menu.js +30 -0
- package/dist/targets/vue/radix/dialog.d.ts +3 -0
- package/dist/targets/vue/radix/dialog.js +12 -0
- package/dist/targets/vue/radix/direction.d.ts +7 -0
- package/dist/targets/vue/radix/direction.js +9 -0
- package/dist/targets/vue/radix/dropdown-menu.d.ts +3 -0
- package/dist/targets/vue/radix/dropdown-menu.js +29 -0
- package/dist/targets/vue/radix/hover-card.d.ts +3 -0
- package/dist/targets/vue/radix/hover-card.js +11 -0
- package/dist/targets/vue/radix/index.d.ts +65 -0
- package/dist/targets/vue/radix/index.js +225 -0
- package/dist/targets/vue/radix/label.d.ts +3 -0
- package/dist/targets/vue/radix/label.js +5 -0
- package/dist/targets/vue/radix/menubar.d.ts +6 -0
- package/dist/targets/vue/radix/menubar.js +29 -0
- package/dist/targets/vue/radix/navigation-menu.d.ts +6 -0
- package/dist/targets/vue/radix/navigation-menu.js +21 -0
- package/dist/targets/vue/radix/part.d.ts +41 -0
- package/dist/targets/vue/radix/part.js +46 -0
- package/dist/targets/vue/radix/popover.d.ts +3 -0
- package/dist/targets/vue/radix/popover.js +11 -0
- package/dist/targets/vue/radix/progress.d.ts +3 -0
- package/dist/targets/vue/radix/progress.js +6 -0
- package/dist/targets/vue/radix/radio-group.d.ts +3 -0
- package/dist/targets/vue/radix/radio-group.js +10 -0
- package/dist/targets/vue/radix/scroll-area.d.ts +3 -0
- package/dist/targets/vue/radix/scroll-area.js +11 -0
- package/dist/targets/vue/radix/select.d.ts +3 -0
- package/dist/targets/vue/radix/select.js +29 -0
- package/dist/targets/vue/radix/separator.d.ts +3 -0
- package/dist/targets/vue/radix/separator.js +5 -0
- package/dist/targets/vue/radix/slider.d.ts +3 -0
- package/dist/targets/vue/radix/slider.js +13 -0
- package/dist/targets/vue/radix/switch.d.ts +3 -0
- package/dist/targets/vue/radix/switch.js +9 -0
- package/dist/targets/vue/radix/tabs.d.ts +3 -0
- package/dist/targets/vue/radix/tabs.js +12 -0
- package/dist/targets/vue/radix/toggle-group.d.ts +3 -0
- package/dist/targets/vue/radix/toggle-group.js +10 -0
- package/dist/targets/vue/radix/toggle.d.ts +3 -0
- package/dist/targets/vue/radix/toggle.js +8 -0
- package/dist/targets/vue/radix/tooltip.d.ts +3 -0
- package/dist/targets/vue/radix/tooltip.js +14 -0
- package/dist/targets/vue/render-fn/h.d.ts +52 -0
- package/dist/targets/vue/render-fn/h.js +442 -0
- package/dist/targets/vue/render-fn.d.ts +21 -0
- package/dist/targets/vue/render-fn.js +357 -0
- package/dist/targets/vue/slot.d.ts +9 -0
- package/dist/targets/vue/slot.js +57 -0
- package/dist/typecheck/host.d.ts +45 -0
- package/dist/typecheck/host.js +309 -0
- package/dist/typecheck/index.d.ts +16 -0
- package/dist/typecheck/index.js +17 -0
- package/dist/typecheck/lucide.d.ts +8 -0
- package/dist/typecheck/lucide.js +45 -0
- package/dist/typecheck/sourcemap.d.ts +9 -0
- package/dist/typecheck/sourcemap.js +54 -0
- package/dist/typecheck/svelte.d.ts +9 -0
- package/dist/typecheck/svelte.js +116 -0
- package/dist/typecheck/vue.d.ts +8 -0
- package/dist/typecheck/vue.js +78 -0
- package/dist/types.d.ts +446 -0
- package/dist/types.js +11 -0
- package/dist/utils/cleanup.d.ts +32 -0
- package/dist/utils/cleanup.js +119 -0
- package/dist/utils/errors.d.ts +7 -0
- package/dist/utils/errors.js +15 -0
- package/dist/utils/fs-safe.d.ts +14 -0
- package/dist/utils/fs-safe.js +64 -0
- package/dist/validate.d.ts +16 -0
- package/dist/validate.js +204 -0
- package/dist/verify/auto-steps.d.ts +107 -0
- package/dist/verify/auto-steps.js +471 -0
- package/dist/verify/bundle.d.ts +68 -0
- package/dist/verify/bundle.js +548 -0
- package/dist/verify/diff.d.ts +31 -0
- package/dist/verify/diff.js +133 -0
- package/dist/verify/leaks.d.ts +25 -0
- package/dist/verify/leaks.js +261 -0
- package/dist/verify/link-worker.d.ts +1 -0
- package/dist/verify/link-worker.js +27 -0
- package/dist/verify/runner.d.ts +57 -0
- package/dist/verify/runner.js +867 -0
- package/dist/verify/sandbox.d.ts +40 -0
- package/dist/verify/sandbox.js +496 -0
- package/dist/verify/steps.d.ts +42 -0
- package/dist/verify/steps.js +475 -0
- package/dist/verify/verdict.d.ts +15 -0
- package/dist/verify/verdict.js +19 -0
- package/package.json +118 -4
|
@@ -0,0 +1,1339 @@
|
|
|
1
|
+
import * as t from '@babel/types';
|
|
2
|
+
import { parseExpression } from '@babel/parser';
|
|
3
|
+
import { forEachElement } from '../../../ir/radix.js';
|
|
4
|
+
import { unwrap } from '../../../ir/lower.js';
|
|
5
|
+
import { domAttributeName } from '../../../ir/dom.js';
|
|
6
|
+
import { BITS_GATED, BITS_REFUSED, SVELTE_PACKAGES as BITS_PACKAGES, bitsPartName } from './packages.js';
|
|
7
|
+
// M3: Radix packages morph writes itself (headless cores), imported from the conversion's barrel
|
|
8
|
+
import { DIRECTION_ROOTS, READS_DIRECTION, SVELTE_CORES, SVELTE_CORES_FILE, useSvelteCorePackage } from './cores/index.js';
|
|
9
|
+
import { OUTPUT_IMPORTS } from '../../shared/module.js';
|
|
10
|
+
import { resolveLocal } from '../../../analyzer/deps.js';
|
|
11
|
+
/**
|
|
12
|
+
* Prints claimed Radix parts (ir/radix) as bits-ui's, before the Svelte emitter runs: each element's tag becomes the
|
|
13
|
+
* bits-ui part (`<SwitchPrimitive.Root>` stays `<SwitchPrimitive.Root>`, its namespace now imported from bits-ui), its
|
|
14
|
+
* props and callbacks take bits-ui's names, Radix's controlled state its controlled form, `asChild` the `child` snippet,
|
|
15
|
+
* a ref `bind:ref`; the module imports the namespaces it uses from bits-ui, and props typed as a Radix part's props are
|
|
16
|
+
* typed as bits-ui's. Anything the map cannot say exactly is a located diagnostic.
|
|
17
|
+
*/
|
|
18
|
+
export function prepareSvelteRadix(mod, ctx) {
|
|
19
|
+
const info = mod.radix;
|
|
20
|
+
if (!info)
|
|
21
|
+
return;
|
|
22
|
+
refuse(mod, ctx);
|
|
23
|
+
const remaining = new Set(mod.imports.flatMap((d) => d.specifiers.map((s) => s.local.name)));
|
|
24
|
+
// a binding claimRadix kept in the imports was refused, and the React-library guard has since replaced it with a
|
|
25
|
+
// placeholder the module body declares: its elements stay as they are (stubbed, reported)
|
|
26
|
+
const declared = new Set(mod.moduleBody.flatMap((s) => Object.keys(t.getOuterBindingIdentifiers(s))));
|
|
27
|
+
const claimed = new Map([...info.bindings].filter(([local]) => !remaining.has(local) && !declared.has(local)));
|
|
28
|
+
if (!claimed.size)
|
|
29
|
+
return;
|
|
30
|
+
const taken = new Set([...mod.moduleJsx.moduleNames, ...remaining, ...mod.components.map((c) => c.name), ...mod.hooks.map((h) => h.name), ...mod.components.flatMap((c) => [...c.bindings.keys()])]);
|
|
31
|
+
const run = new RadixRun(mod, ctx, claimed, taken);
|
|
32
|
+
for (const comp of mod.components)
|
|
33
|
+
run.component(comp);
|
|
34
|
+
for (const comp of mod.components)
|
|
35
|
+
run.callers(comp);
|
|
36
|
+
// (M3: a core part rendered as the one child of a component's Slot takes the Slot's props)
|
|
37
|
+
for (const comp of mod.components)
|
|
38
|
+
slotOverCore(mod, comp, ctx);
|
|
39
|
+
run.finishTypes();
|
|
40
|
+
run.addImports();
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* M3: `<Button asChild><AlertDialogPrimitive.Cancel {...props} /></Button>`, where Button renders Radix's Slot (its
|
|
44
|
+
* asChild prop, ir/as-child) and its one child is a part of a core that takes a Slot's props (SvelteCore.slot): the
|
|
45
|
+
* part becomes Button's `child` snippet and is given the props Button passes it as `slotProps`, which the core merges
|
|
46
|
+
* under its own as Radix's Slot does (its own win, class names and styles joined, both handlers run). The component may
|
|
47
|
+
* be of this file or imported by name from another file of the conversion (shadcn's Button).
|
|
48
|
+
*/
|
|
49
|
+
/** The component a local name imports by name from another file of the conversion, when it is one. */
|
|
50
|
+
function importedComponent(mod, local, ctx) {
|
|
51
|
+
for (const decl of mod.imports) {
|
|
52
|
+
for (const s of decl.specifiers) {
|
|
53
|
+
if (!t.isImportSpecifier(s) || s.local.name !== local)
|
|
54
|
+
continue;
|
|
55
|
+
const imported = t.isIdentifier(s.imported) ? s.imported.name : s.imported.value;
|
|
56
|
+
const file = resolveLocal(ctx.sourceFile, decl.source.value, ctx.project);
|
|
57
|
+
return file ? ctx.moduleOf?.(file)?.components.find((c) => c.name === imported) : undefined;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
function slotOverCore(mod, comp, ctx) {
|
|
63
|
+
forEachElement(comp, (el) => {
|
|
64
|
+
if (!el.component || el.radix)
|
|
65
|
+
return;
|
|
66
|
+
const receiver = mod.components.find((c) => c.name === el.tag) ?? importedComponent(mod, el.tag, ctx);
|
|
67
|
+
const key = receiver?.asChildProp;
|
|
68
|
+
if (!key || el.attrs.some((a) => a.kind === 'slotProp' && a.name === 'child'))
|
|
69
|
+
return;
|
|
70
|
+
const flag = el.attrs.find((a) => (a.kind === 'bool' || a.kind === 'expr') && a.name === key);
|
|
71
|
+
if (!flag || (flag.kind === 'expr' && !t.isBooleanLiteral(unwrap(flag.expr), { value: true })))
|
|
72
|
+
return;
|
|
73
|
+
const content = el.children.filter((c) => !(c.type === 'text' && !c.value.trim()));
|
|
74
|
+
const only = content[0];
|
|
75
|
+
if (content.length !== 1 || only?.type !== 'element' || !only.radix)
|
|
76
|
+
return;
|
|
77
|
+
const core = SVELTE_CORES[only.radix.pkg];
|
|
78
|
+
const name = bitsPartName(only.radix.pkg, only.radix.part);
|
|
79
|
+
const part = name ? core?.parts[name] : undefined;
|
|
80
|
+
if (!core?.core.slot || !part?.asChild)
|
|
81
|
+
return;
|
|
82
|
+
let local = 'slotProps';
|
|
83
|
+
for (let i = 2; comp.bindings.has(local) || comp.props.some((p) => p.local === local); i++)
|
|
84
|
+
local = `slotProps${i}`;
|
|
85
|
+
only.attrs.unshift({ kind: 'expr', name: 'slotProps', expr: t.identifier(local) });
|
|
86
|
+
const param = t.objectPattern([t.objectProperty(t.identifier('props'), t.identifier(local), false, false)]);
|
|
87
|
+
param.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral([t.tsPropertySignature(t.identifier('props'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([t.tsStringKeyword(), t.tsUnknownKeyword()]))))]));
|
|
88
|
+
el.attrs.push({ kind: 'slotProp', name: 'child', children: [only], render: t.arrowFunctionExpression([param], t.nullLiteral()) });
|
|
89
|
+
el.children = [];
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* A package bits-ui's parts cannot convert exactly (BITS_REFUSED): claimRadix has refused each of its elements, and this
|
|
94
|
+
* says why, once per package, at its first element.
|
|
95
|
+
*/
|
|
96
|
+
function refuse(mod, ctx) {
|
|
97
|
+
const said = new Set();
|
|
98
|
+
for (const comp of mod.components) {
|
|
99
|
+
forEachElement(comp, (el) => {
|
|
100
|
+
const pkg = el.radix?.pkg;
|
|
101
|
+
if (!pkg || said.has(pkg))
|
|
102
|
+
return;
|
|
103
|
+
const why = BITS_REFUSED[pkg] ?? BITS_GATED[pkg];
|
|
104
|
+
if (!why)
|
|
105
|
+
return;
|
|
106
|
+
said.add(pkg);
|
|
107
|
+
const line = lineOf(el) ?? mod.radix?.bindings.get(el.tag.split('.')[0] ?? el.tag)?.line;
|
|
108
|
+
ctx.diagnostics.push({ code: 'react-library', severity: 'error', message: `<${el.tag}> (Radix ${pkg}): ${why}. Not converted.`, file: ctx.sourceRel, target: 'svelte', ...(line ? { line } : {}) });
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const LAYER = 'bits-ui';
|
|
113
|
+
/** Components that spread their rest props onto a bits-ui part taking the `child` snippet: a caller's `child` reaches it. */
|
|
114
|
+
const childForwarders = new WeakSet();
|
|
115
|
+
/** Whether a component passes a `child` snippet its caller gives on to a bits-ui part (see RadixRun.callers). */
|
|
116
|
+
export function passesBitsChild(comp) {
|
|
117
|
+
return childForwarders.has(comp);
|
|
118
|
+
}
|
|
119
|
+
class RadixRun {
|
|
120
|
+
mod;
|
|
121
|
+
ctx;
|
|
122
|
+
claimed;
|
|
123
|
+
taken;
|
|
124
|
+
/** bits-ui namespace → the local name the module imports it under */
|
|
125
|
+
locals = new Map();
|
|
126
|
+
/** namespace bindings (`import * as SwitchPrimitive`) keep their name: bits-ui's namespace is imported under it */
|
|
127
|
+
aliases = new Map();
|
|
128
|
+
constructor(mod, ctx, claimed, taken) {
|
|
129
|
+
this.mod = mod;
|
|
130
|
+
this.ctx = ctx;
|
|
131
|
+
this.claimed = claimed;
|
|
132
|
+
this.taken = taken;
|
|
133
|
+
}
|
|
134
|
+
warn(code, message, severity, line) {
|
|
135
|
+
this.ctx.diagnostics.push({ code, severity, message, file: this.ctx.sourceRel, target: 'svelte', ...(line ? { line } : {}) });
|
|
136
|
+
}
|
|
137
|
+
/** The local name bits-ui's namespace `ns` is read by, for a binding (a namespace binding keeps its own name). */
|
|
138
|
+
nsLocal(ns, binding) {
|
|
139
|
+
if (binding.part === null) {
|
|
140
|
+
this.aliases.set(binding.local, ns);
|
|
141
|
+
return binding.local;
|
|
142
|
+
}
|
|
143
|
+
const known = this.locals.get(ns);
|
|
144
|
+
if (known)
|
|
145
|
+
return known;
|
|
146
|
+
let local = ns;
|
|
147
|
+
for (let i = 2; this.taken.has(local); i++)
|
|
148
|
+
local = i === 2 ? `Bits${ns}` : `Bits${ns}${i}`;
|
|
149
|
+
this.taken.add(local);
|
|
150
|
+
this.locals.set(ns, local);
|
|
151
|
+
return local;
|
|
152
|
+
}
|
|
153
|
+
addImports() {
|
|
154
|
+
const pairs = [...this.aliases, ...[...this.locals].map(([ns, local]) => [local, ns])];
|
|
155
|
+
// M3: a namespace of a core comes from the conversion's barrel (cores/core.ts), written because it is used here
|
|
156
|
+
const cores = new Map(Object.entries(SVELTE_CORES).map(([pkg, c]) => [c.core.ns, { pkg, core: c.core }]));
|
|
157
|
+
const specifiers = pairs.filter(([, ns]) => !cores.has(ns)).map(([local, ns]) => t.importSpecifier(t.identifier(local), t.identifier(ns)));
|
|
158
|
+
if (specifiers.length)
|
|
159
|
+
this.mod.imports.push(t.importDeclaration(specifiers, t.stringLiteral(LAYER)));
|
|
160
|
+
const own = pairs.filter(([, ns]) => cores.has(ns));
|
|
161
|
+
if (!own.length)
|
|
162
|
+
return;
|
|
163
|
+
for (const [, ns] of own) {
|
|
164
|
+
const c = cores.get(ns);
|
|
165
|
+
if (c)
|
|
166
|
+
useSvelteCorePackage(this.ctx.diagnostics, c.pkg);
|
|
167
|
+
}
|
|
168
|
+
const barrel = this.ctx.helperPath?.(SVELTE_CORES_FILE) ?? SVELTE_CORES_FILE;
|
|
169
|
+
const decl = t.importDeclaration(own.map(([local, ns]) => t.importSpecifier(t.identifier(local), t.identifier(ns))), t.stringLiteral(barrel));
|
|
170
|
+
// each component file imports the barrel by its own path (shared/module: OUTPUT_IMPORTS)
|
|
171
|
+
OUTPUT_IMPORTS.set(decl, barrel);
|
|
172
|
+
this.mod.imports.push(decl);
|
|
173
|
+
}
|
|
174
|
+
locate(el) {
|
|
175
|
+
const r = el.radix;
|
|
176
|
+
if (!r)
|
|
177
|
+
return null;
|
|
178
|
+
const local = el.tag.split('.')[0] ?? el.tag;
|
|
179
|
+
const binding = this.claimed.get(local);
|
|
180
|
+
const part = bitsPartName(r.pkg, r.part);
|
|
181
|
+
const map = part ? BITS_PACKAGES[r.pkg]?.parts[part] : undefined;
|
|
182
|
+
if (!binding || !part || !map)
|
|
183
|
+
return null;
|
|
184
|
+
return { el, pkg: r.pkg, part, map, binding };
|
|
185
|
+
}
|
|
186
|
+
component(comp) {
|
|
187
|
+
const found = [];
|
|
188
|
+
forEachElement(comp, (el) => {
|
|
189
|
+
const at = this.locate(el);
|
|
190
|
+
if (at)
|
|
191
|
+
found.push(at);
|
|
192
|
+
});
|
|
193
|
+
if (!found.length)
|
|
194
|
+
return;
|
|
195
|
+
this.types(comp);
|
|
196
|
+
// synthesised parts first: their parents give them their snippet's values
|
|
197
|
+
const synth = found.filter((f) => f.map.synth || f.map.synthIn);
|
|
198
|
+
for (const f of found)
|
|
199
|
+
if (!f.map.synth && !f.map.synthIn)
|
|
200
|
+
this.element(comp, f, synth);
|
|
201
|
+
for (const s of synth) {
|
|
202
|
+
if (this.placed.has(s.el))
|
|
203
|
+
continue;
|
|
204
|
+
const parents = s.map.synth ? [s.map.synth.parent] : Object.keys(s.map.synthIn ?? {});
|
|
205
|
+
this.warn('react-library', `<${s.el.tag}> (Radix ${s.pkg} ${s.part}) shows its parent's state, which bits-ui gives only inside the parent's own markup: it must sit inside its ${parents.join(' or ')} in the same component. Not converted.`, 'error', lineOf(s.el) ?? s.binding.line);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
placed = new Set();
|
|
209
|
+
/** A Radix part's element, rewritten in place as bits-ui's. */
|
|
210
|
+
element(comp, at, synth) {
|
|
211
|
+
const { el, map } = at;
|
|
212
|
+
if (map.plain) {
|
|
213
|
+
this.plain(at, map.plain);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
const ns = BITS_PACKAGES[at.pkg]?.ns ?? '';
|
|
217
|
+
const member = map.name.split('.')[1] ?? 'Root';
|
|
218
|
+
el.tag = `${this.nsLocal(ns, at.binding)}.${member}`;
|
|
219
|
+
const where = `<${el.tag}> (Radix ${at.pkg} ${at.part})`;
|
|
220
|
+
const line = lineOf(el) ?? at.binding.line;
|
|
221
|
+
if (map.radixArrow) {
|
|
222
|
+
this.arrow(el, where, line);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
const stateNames = statePairs(map);
|
|
226
|
+
const values = new Map();
|
|
227
|
+
const changes = new Map();
|
|
228
|
+
const attrs = [];
|
|
229
|
+
let asChild = false;
|
|
230
|
+
for (const a of el.attrs) {
|
|
231
|
+
if (a.kind === 'static' || a.kind === 'expr' || a.kind === 'bool') {
|
|
232
|
+
if (a.name === 'asChild') {
|
|
233
|
+
const literal = a.kind === 'bool' || (a.kind === 'expr' && t.isBooleanLiteral(unwrap(a.expr)));
|
|
234
|
+
const on = a.kind === 'bool' || (a.kind === 'expr' && t.isBooleanLiteral(unwrap(a.expr), { value: true }));
|
|
235
|
+
if (!literal)
|
|
236
|
+
this.warn('radix-prop', `${where}: asChild is decided at run time (${a.kind === 'expr' ? 'an expression' : 'a string'}); bits-ui's form is a \`child\` snippet written in the markup. Not converted.`, 'error', line);
|
|
237
|
+
asChild = on;
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
const to = map.props[a.name];
|
|
241
|
+
// a part with no element of its own (Root, Portal): Radix reads its own props only, so a data-* or aria-*
|
|
242
|
+
// attribute given to it reaches nothing in React either (shadcn's `data-slot="dialog"`)
|
|
243
|
+
if (to === undefined && !map.element && /^(data|aria)-/.test(a.name))
|
|
244
|
+
continue;
|
|
245
|
+
if (to === undefined) {
|
|
246
|
+
// an attribute the part passes to its element: the DOM's name for React's (`htmlFor` → `for`)
|
|
247
|
+
attrs.push(map.element ? { ...a, name: domName(a.name) } : a);
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (typeof to !== 'string') {
|
|
251
|
+
this.dropped(at, a, to.why, where, line);
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
if (stateNames.has(a.name)) {
|
|
255
|
+
values.set(a.name, a);
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
attrs.push({ ...a, name: to });
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
if (a.kind === 'event') {
|
|
262
|
+
const to = map.events[a.react];
|
|
263
|
+
if (to === undefined) {
|
|
264
|
+
attrs.push(a);
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
if (stateNames.has(a.react))
|
|
268
|
+
changes.set(a.react, a.handler);
|
|
269
|
+
// a bits-ui callback prop, by its name as written (`onSelect` would otherwise print as the DOM's `onselect`)
|
|
270
|
+
else
|
|
271
|
+
attrs.push({ kind: 'expr', name: to, expr: a.handler });
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
if (a.kind === 'ref') {
|
|
275
|
+
const ref = this.ref(comp, a.expr);
|
|
276
|
+
if (ref)
|
|
277
|
+
attrs.push(ref);
|
|
278
|
+
else
|
|
279
|
+
this.warn('radix-prop', `${where}: this ref is not converted (bits-ui hands a part's element to \`bind:ref\`, which takes an element ref of the component).`, 'error', line);
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
attrs.push(a);
|
|
283
|
+
}
|
|
284
|
+
attrs.push(...this.state(comp, at, values, changes, attrs));
|
|
285
|
+
this.radixDefaults(comp, at, attrs, where, line);
|
|
286
|
+
if (map.pressFocus)
|
|
287
|
+
pressFocus(attrs);
|
|
288
|
+
if (map.noFocus)
|
|
289
|
+
this.noFocus(attrs, where, line);
|
|
290
|
+
if (map.radixStyle)
|
|
291
|
+
this.radixStyle(attrs, map.radixStyle, where, line);
|
|
292
|
+
if (restSpread(comp, attrs) && !this.forwarders.has(comp.name)) {
|
|
293
|
+
this.forwarders.set(comp.name, { comp, at });
|
|
294
|
+
if (map.asChild)
|
|
295
|
+
childForwarders.add(comp);
|
|
296
|
+
}
|
|
297
|
+
if (map.cssVars) {
|
|
298
|
+
const style = Object.entries(map.cssVars).map(([radix, bits]) => `${radix}: var(${bits})`).join('; ');
|
|
299
|
+
if (attrs.some((a) => a.kind === 'style' || ((a.kind === 'static' || a.kind === 'expr') && a.name === 'style')))
|
|
300
|
+
this.warn('radix-data', `${where}: Radix sets ${Object.keys(map.cssVars).join(', ')} on this part, bits-ui ${Object.values(map.cssVars).join(', ')}; the part has a style of its own, so the Radix names are not given (a class reading them reads nothing).`, 'warning', line);
|
|
301
|
+
else
|
|
302
|
+
attrs.push({ kind: 'static', name: 'style', value: style });
|
|
303
|
+
}
|
|
304
|
+
this.dataDiffers(at, attrs, where, line);
|
|
305
|
+
this.collapsible(at, el.attrs, where, line);
|
|
306
|
+
if (map.differs)
|
|
307
|
+
this.warn('radix-behaviour', `${where}: ${map.differs}.`, 'warning', line);
|
|
308
|
+
el.attrs = attrs;
|
|
309
|
+
// (M3: a part that writes the DirectionProvider's direction as its root's dir)
|
|
310
|
+
this.direction(comp, at, where, line);
|
|
311
|
+
if (asChild)
|
|
312
|
+
this.asChild(el, where, line);
|
|
313
|
+
// a synthesised child part (Checkbox.Indicator) under this one: the children become bits-ui's `children` snippet
|
|
314
|
+
const mine = synth.filter((s) => synthSpec(s.map, at.part) && s.pkg === at.pkg && contains(el, s.el));
|
|
315
|
+
const spec = mine[0] ? synthSpec(mine[0].map, at.part) : undefined;
|
|
316
|
+
if (spec?.fromProps)
|
|
317
|
+
this.fromParent(comp, el, mine, attrs);
|
|
318
|
+
else if (spec)
|
|
319
|
+
this.synthesise(comp, el, mine, attrs, spec);
|
|
320
|
+
}
|
|
321
|
+
saidDirection = new Set();
|
|
322
|
+
/**
|
|
323
|
+
* M3: in a file that renders Radix's DirectionProvider (the direction core), a bits-ui part whose Radix counterpart
|
|
324
|
+
* writes the direction as its root's `dir` attribute (DIRECTION_ROOTS) is rendered inside the core's Consumer, which
|
|
325
|
+
* reads the provider's direction where it is rendered and gives it to the part as its `dir` (the part's own dir, else
|
|
326
|
+
* the provider's, else ltr, as Radix's useDirection). Any other bits-ui part whose Radix counterpart reads the
|
|
327
|
+
* direction does not get it: said once per package.
|
|
328
|
+
*/
|
|
329
|
+
direction(comp, at, where, line) {
|
|
330
|
+
const provider = [...this.claimed.values()].find((b) => b.pkg === 'direction');
|
|
331
|
+
if (!provider || at.map.core)
|
|
332
|
+
return;
|
|
333
|
+
if (DIRECTION_ROOTS[at.pkg] !== at.part) {
|
|
334
|
+
// (a part of a package whose root takes the direction, as an item does through its root, is not said)
|
|
335
|
+
if (READS_DIRECTION.includes(at.pkg) && !Object.hasOwn(DIRECTION_ROOTS, at.pkg) && !this.saidDirection.has(at.pkg)) {
|
|
336
|
+
this.saidDirection.add(at.pkg);
|
|
337
|
+
this.warn('radix-behaviour', `${where}: Radix's ${at.pkg} reads the DirectionProvider's dir; bits-ui's part follows the page's direction instead, so the provider's dir does not reach it.`, 'warning', line);
|
|
338
|
+
}
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
const el = at.el;
|
|
342
|
+
const ns = this.nsLocal('Direction', provider);
|
|
343
|
+
let local = 'radixDir';
|
|
344
|
+
for (let i = 2; comp.bindings.has(local) || this.taken.has(local); i++)
|
|
345
|
+
local = `radixDir${i}`;
|
|
346
|
+
this.taken.add(local);
|
|
347
|
+
const own = el.attrs.filter((a) => (a.kind === 'static' || a.kind === 'expr') && a.name === 'dir');
|
|
348
|
+
el.attrs = [{ kind: 'expr', name: 'dir', expr: t.identifier(local) }, ...el.attrs.filter((a) => !own.includes(a))];
|
|
349
|
+
const param = t.objectPattern([t.objectProperty(t.identifier('dir'), t.identifier(local), false, false)]);
|
|
350
|
+
const consumer = {
|
|
351
|
+
type: 'element',
|
|
352
|
+
tag: `${ns}.Consumer`,
|
|
353
|
+
component: true,
|
|
354
|
+
motion: null,
|
|
355
|
+
attrs: [...own, { kind: 'slotProp', name: 'children', children: [el], render: t.arrowFunctionExpression([param], t.nullLiteral()) }],
|
|
356
|
+
children: [],
|
|
357
|
+
};
|
|
358
|
+
if (comp.template === el) {
|
|
359
|
+
comp.template = consumer;
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
// (the consumer holds the part: it is put in the part's place once, and not walked into)
|
|
363
|
+
let done = false;
|
|
364
|
+
const walk = (nodes) => {
|
|
365
|
+
const i = nodes.indexOf(el);
|
|
366
|
+
if (i >= 0) {
|
|
367
|
+
nodes[i] = consumer;
|
|
368
|
+
done = true;
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
for (const n of nodes) {
|
|
372
|
+
if (done)
|
|
373
|
+
return;
|
|
374
|
+
if (n.type === 'element') {
|
|
375
|
+
walk(n.children);
|
|
376
|
+
for (const a of n.attrs)
|
|
377
|
+
if (a.kind === 'slotProp' && !done)
|
|
378
|
+
walk(a.children);
|
|
379
|
+
}
|
|
380
|
+
else if (n.type === 'if')
|
|
381
|
+
for (const b of n.branches)
|
|
382
|
+
walk(b.children);
|
|
383
|
+
else if (n.type === 'slot')
|
|
384
|
+
walk(n.fallback);
|
|
385
|
+
else if (n.type === 'for' || n.type === 'fragment' || n.type === 'keyed' || n.type === 'presence')
|
|
386
|
+
walk(n.children);
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
for (const root of [comp.template, ...comp.fragments.map((f) => f.template)])
|
|
390
|
+
if (!done)
|
|
391
|
+
walk([root]);
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Radix's default of a prop where bits-ui's differs (BitsPartMap.defaults): the prop as the source resolves it, given
|
|
395
|
+
* last so it wins, falling back on Radix's default. Where the prop may carry a value, what bits-ui still does
|
|
396
|
+
* differently with it is said (BitsPartMap.differsWith).
|
|
397
|
+
*/
|
|
398
|
+
radixDefaults(comp, at, attrs, where, line) {
|
|
399
|
+
const differs = at.map.differsWith;
|
|
400
|
+
if (differs && differs.props.some((p) => propValue(comp, attrs, p) !== null))
|
|
401
|
+
this.warn('radix-data', `${where}: ${differs.why}.`, 'warning', line);
|
|
402
|
+
for (const [name, def] of Object.entries(at.map.defaults ?? {})) {
|
|
403
|
+
const given = propValue(comp, attrs, name);
|
|
404
|
+
if (given === 'refused') {
|
|
405
|
+
this.warn('radix-prop', `${where}: ${name} may come from a spread morph cannot read the prop from (only an object named in the markup). Not converted.`, 'error', line);
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
for (let i = attrs.length - 1; i >= 0; i--) {
|
|
409
|
+
const a = attrs[i];
|
|
410
|
+
if (a && (a.kind === 'static' || a.kind === 'expr' || a.kind === 'bool') && a.name === name)
|
|
411
|
+
attrs.splice(i, 1);
|
|
412
|
+
}
|
|
413
|
+
attrs.push({ kind: 'expr', name, expr: fold(given ? t.logicalExpression('??', given, parseExpression(def)) : parseExpression(def)) });
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Synthesised parts whose values are their parent's props, not a snippet's (SynthPart.fromProps: Progress.Indicator):
|
|
418
|
+
* each is written in place, reading the parent's props as the source gives them.
|
|
419
|
+
*/
|
|
420
|
+
fromParent(comp, el, mine, parentAttrs) {
|
|
421
|
+
for (const s of mine) {
|
|
422
|
+
const spec = s.map.synth;
|
|
423
|
+
if (!spec?.fromProps)
|
|
424
|
+
continue;
|
|
425
|
+
const values = new Map();
|
|
426
|
+
let refused = false;
|
|
427
|
+
for (const [p, def] of Object.entries(spec.fromProps)) {
|
|
428
|
+
const given = propValue(comp, parentAttrs, p);
|
|
429
|
+
if (given === 'refused')
|
|
430
|
+
refused = true;
|
|
431
|
+
else
|
|
432
|
+
values.set(p, given ? fold(t.logicalExpression('??', given, parseExpression(def))) : parseExpression(def));
|
|
433
|
+
}
|
|
434
|
+
if (refused) {
|
|
435
|
+
this.warn('radix-prop', `<${s.el.tag}> (Radix ${s.pkg} ${s.part}): its parent's props may come from a spread morph cannot read them from (only an object named in the markup). Not converted.`, 'error', lineOf(s.el) ?? s.binding.line);
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
const replaced = this.synthElement(s, spec, values, undefined);
|
|
439
|
+
if (replaced) {
|
|
440
|
+
replaceNode(el, s.el, replaced);
|
|
441
|
+
this.placed.add(s.el);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* A part whose Radix element takes no focus (BitsPartMap.noFocus): given no tabindex of its own, it gets the one a
|
|
447
|
+
* spread of the source carries, else `null`, written last, so bits-ui's `tabindex="-1"` is not rendered.
|
|
448
|
+
*/
|
|
449
|
+
noFocus(attrs, where, line) {
|
|
450
|
+
if (attrs.some((a) => (a.kind === 'static' || a.kind === 'expr' || a.kind === 'bool') && /^tab-?index$/i.test(a.name)))
|
|
451
|
+
return;
|
|
452
|
+
let value = t.nullLiteral();
|
|
453
|
+
for (const a of attrs) {
|
|
454
|
+
if (a.kind !== 'spread')
|
|
455
|
+
continue;
|
|
456
|
+
if (!t.isIdentifier(a.expr)) {
|
|
457
|
+
this.warn('radix-behaviour', `${where}: a tabindex may come from a spread morph cannot read it from (only a named object), and Radix's part takes no focus where bits-ui's has tabindex="-1"; the part is given none, so a tabindex in that spread is not rendered.`, 'warning', line);
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
// a later spread wins, as in React: `rest.tabindex ?? earlier ?? null`
|
|
461
|
+
value = t.logicalExpression('??', t.memberExpression(t.identifier(a.expr.name), t.identifier('tabindex')), value);
|
|
462
|
+
}
|
|
463
|
+
attrs.push({ kind: 'expr', name: 'tabindex', expr: value });
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Radix's own inline style under the part's (BitsPartMap.radixStyle): joined before a static style, which then wins
|
|
467
|
+
* where both set a property, as Radix's object spread does; else written first, so a style through a spread replaces
|
|
468
|
+
* it (said) and a style given as an expression is refused, since joining it needs its form at run time.
|
|
469
|
+
*/
|
|
470
|
+
radixStyle(attrs, style, where, line) {
|
|
471
|
+
const i = attrs.findIndex((a) => (a.kind === 'static' || a.kind === 'expr') && a.name === 'style');
|
|
472
|
+
const given = i >= 0 ? attrs[i] : undefined;
|
|
473
|
+
if (given?.kind === 'static') {
|
|
474
|
+
attrs[i] = { ...given, value: `${style}; ${given.value}` };
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
if (given || attrs.some((a) => a.kind === 'style')) {
|
|
478
|
+
this.warn('radix-behaviour', `${where}: Radix sets ${style} on this part under the style it is given, which bits-ui does not; the style here is not a fixed string, so Radix's is not joined to it and the browser's focus outline may show.`, 'warning', line);
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
attrs.unshift({ kind: 'static', name: 'style', value: style });
|
|
482
|
+
if (attrs.some((a) => a.kind === 'spread'))
|
|
483
|
+
this.warn('radix-behaviour', `${where}: Radix sets ${style} on this part under the style it is given, which bits-ui does not; it is written here, and a style given through the spread replaces it (Radix would keep ${style} unless that style sets it).`, 'warning', line);
|
|
484
|
+
}
|
|
485
|
+
/** A single accordion given no `collapsible` keeps its open item open in Radix; bits-ui's always closes it on a click. */
|
|
486
|
+
collapsible(at, given, where, line) {
|
|
487
|
+
if (at.pkg !== 'accordion' || at.part !== 'Root')
|
|
488
|
+
return;
|
|
489
|
+
const type = given.find((a) => (a.kind === 'static' || a.kind === 'expr') && a.name === 'type');
|
|
490
|
+
const single = type?.kind === 'static' ? type.value === 'single' : type?.kind === 'expr' ? !t.isStringLiteral(unwrap(type.expr), { value: 'multiple' }) : false;
|
|
491
|
+
if (!single || given.some((a) => (a.kind === 'static' || a.kind === 'expr' || a.kind === 'bool') && a.name === 'collapsible'))
|
|
492
|
+
return;
|
|
493
|
+
this.warn('radix-behaviour', `${where}: a single accordion without collapsible keeps its open item open in Radix; bits-ui's single accordion closes it on a click, so Svelte lets it close.`, 'warning', line);
|
|
494
|
+
}
|
|
495
|
+
/** A part Radix renders as a plain element with its props (BitsPartMap.plain): that element, its events by DOM names. */
|
|
496
|
+
plain(at, tag) {
|
|
497
|
+
const { el } = at;
|
|
498
|
+
const where = `<${el.tag}> (Radix ${at.pkg} ${at.part})`;
|
|
499
|
+
const line = lineOf(el) ?? at.binding.line;
|
|
500
|
+
const attrs = [];
|
|
501
|
+
let asChild = false;
|
|
502
|
+
for (const a of el.attrs) {
|
|
503
|
+
if ((a.kind === 'static' || a.kind === 'expr' || a.kind === 'bool') && a.name === 'asChild') {
|
|
504
|
+
const literal = a.kind === 'bool' || (a.kind === 'expr' && t.isBooleanLiteral(unwrap(a.expr)));
|
|
505
|
+
if (!literal)
|
|
506
|
+
this.warn('radix-prop', `${where}: asChild is decided at run time on a part written as a plain <${tag}>. Not converted.`, 'error', line);
|
|
507
|
+
asChild = a.kind === 'bool' || (a.kind === 'expr' && t.isBooleanLiteral(unwrap(a.expr), { value: true }));
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
if (a.kind === 'static' || a.kind === 'expr' || a.kind === 'bool') {
|
|
511
|
+
attrs.push({ ...a, name: domName(a.name) });
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
attrs.push(a.kind === 'event' ? { ...a, name: a.react.slice(2).toLowerCase() } : a);
|
|
515
|
+
}
|
|
516
|
+
delete el.radix;
|
|
517
|
+
if (asChild) {
|
|
518
|
+
this.plainAsChild(el, attrs, where, line);
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
el.tag = tag;
|
|
522
|
+
el.component = false;
|
|
523
|
+
el.attrs = attrs;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* asChild on a part written as a plain element: Radix's Slot puts the part's attributes on its one child element, the
|
|
527
|
+
* child's own winning and class names joined. Attributes both set otherwise (a handler, a style) are refused.
|
|
528
|
+
*/
|
|
529
|
+
plainAsChild(el, partAttrs, where, line) {
|
|
530
|
+
const content = el.children.filter((c) => !(c.type === 'text' && !c.value.trim()));
|
|
531
|
+
const only = content[0];
|
|
532
|
+
if (content.length !== 1 || only?.type !== 'element' || only.portal) {
|
|
533
|
+
this.warn('radix-prop', `${where}: asChild merges the part onto its one child element; it has ${content.length === 1 ? 'a child that is not an element' : `${content.length} children`}. Not converted.`, 'error', line);
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
const named = (a) => (a.kind === 'static' || a.kind === 'expr' || a.kind === 'bool' ? a.name : null);
|
|
537
|
+
const own = new Set(only.attrs.map(named).filter((n) => n !== null));
|
|
538
|
+
const merged = [];
|
|
539
|
+
for (const a of partAttrs) {
|
|
540
|
+
const name = named(a);
|
|
541
|
+
if (name === null || (name === 'class' && own.has('class')) || only.attrs.some((b) => b.kind === 'spread')) {
|
|
542
|
+
this.warn('radix-prop', `${where}: asChild merges the part's ${name ?? a.kind} onto <${only.tag}>, which needs merging at run time. Not converted.`, 'error', line);
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
if (!own.has(name))
|
|
546
|
+
merged.push(a);
|
|
547
|
+
}
|
|
548
|
+
el.tag = only.tag;
|
|
549
|
+
el.component = only.component;
|
|
550
|
+
el.motion = only.motion;
|
|
551
|
+
el.children = only.children;
|
|
552
|
+
el.attrs = [...merged, ...only.attrs];
|
|
553
|
+
}
|
|
554
|
+
/** Radix's arrow svg, with the part's props, as bits-ui's Arrow's children (BitsPartMap.radixArrow). */
|
|
555
|
+
arrow(el, where, line) {
|
|
556
|
+
const svg = [];
|
|
557
|
+
const size = { width: { kind: 'static', name: 'width', value: '10' }, height: { kind: 'static', name: 'height', value: '5' } };
|
|
558
|
+
for (const a of el.attrs) {
|
|
559
|
+
const name = a.kind === 'static' || a.kind === 'expr' || a.kind === 'bool' ? a.name : null;
|
|
560
|
+
if (name === 'asChild' || a.kind === 'ref' || a.kind === 'style' || a.kind === 'spread' || a.kind === 'html' || name === 'style') {
|
|
561
|
+
this.warn('radix-prop', `${where}: ${name ?? a.kind} on the arrow is not converted (morph writes Radix's own svg inside bits-ui's arrow, without merging it at run time).`, 'error', line);
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
if (name === 'width' || name === 'height')
|
|
565
|
+
size[name] = a;
|
|
566
|
+
else if (name !== null)
|
|
567
|
+
svg.push({ ...a, name: domName(name) });
|
|
568
|
+
else
|
|
569
|
+
svg.push(a.kind === 'event' ? { ...a, name: a.react.slice(2).toLowerCase() } : a);
|
|
570
|
+
}
|
|
571
|
+
const polygon = { type: 'element', tag: 'polygon', component: false, motion: null, attrs: [{ kind: 'static', name: 'points', value: '0,0 30,0 15,10' }], children: [] };
|
|
572
|
+
const attrs = [...svg, ...Object.values(size), { kind: 'static', name: 'viewBox', value: '0 0 30 10' }, { kind: 'static', name: 'preserveAspectRatio', value: 'none' }, { kind: 'static', name: 'style', value: 'display: block' }];
|
|
573
|
+
// Radix renders the polygon, not the arrow's children, unless asChild (refused above)
|
|
574
|
+
el.children = [{ type: 'element', tag: 'svg', component: false, motion: null, attrs, children: [polygon] }];
|
|
575
|
+
el.attrs = [];
|
|
576
|
+
}
|
|
577
|
+
/** A harmless drop is checked against the value it was given (Accordion's `collapsible`). */
|
|
578
|
+
dropped(at, a, why, where, line) {
|
|
579
|
+
if (at.pkg === 'accordion' && a.name === 'collapsible') {
|
|
580
|
+
const yes = a.kind === 'bool' || (a.kind === 'expr' && t.isBooleanLiteral(unwrap(a.expr), { value: true }));
|
|
581
|
+
if (!yes)
|
|
582
|
+
this.warn('radix-behaviour', `${where}: collapsible is ${a.kind === 'expr' ? 'decided at run time' : 'false'}; ${why}, so a click on the open item closes it in Svelte where Radix may keep it open.`, 'warning', line);
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
this.warn('radix-prop', `${where}: ${a.name} is left out: ${why}.`, 'info', line);
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* Radix's state props. Controlled (`open={o}`): a function binding, which bits-ui reads and writes through
|
|
589
|
+
* (`bind:open={() => o, onOpenChange}`), so the value stays the parent's as in React. A default only
|
|
590
|
+
* (`defaultOpen`): bits-ui's prop given once, which bits-ui then changes itself. The change callback keeps its name
|
|
591
|
+
* otherwise. A component that spreads its own rest props onto the part (shadcn's wrappers) takes the three props
|
|
592
|
+
* itself and keeps the state as Radix's useControllableState does.
|
|
593
|
+
*/
|
|
594
|
+
state(comp, at, values, changes, attrs) {
|
|
595
|
+
const out = [];
|
|
596
|
+
const where = `<${at.el.tag}> (Radix ${at.pkg} ${at.part})`;
|
|
597
|
+
const line = lineOf(at.el) ?? at.binding.line;
|
|
598
|
+
const third = at.map.indeterminate;
|
|
599
|
+
for (const pair of stateTriples(at.map)) {
|
|
600
|
+
const value = values.get(pair.prop);
|
|
601
|
+
const def = values.get(pair.def);
|
|
602
|
+
const change = changes.get(pair.change) ?? null;
|
|
603
|
+
const rest = restSpread(comp, attrs);
|
|
604
|
+
if (value) {
|
|
605
|
+
const v = attrValue(value);
|
|
606
|
+
if (!third) {
|
|
607
|
+
out.push(binding(pair.to, v, change, comp));
|
|
608
|
+
continue;
|
|
609
|
+
}
|
|
610
|
+
// Radix's CheckedState: bits-ui's value is it being true, its `indeterminate` it being the string
|
|
611
|
+
out.push(binding(pair.to, isTrue(v), change, comp), binding(third, isString(t.cloneNode(v, true)), null));
|
|
612
|
+
continue;
|
|
613
|
+
}
|
|
614
|
+
if (rest && !def && !at.map.discriminated && pair.hasDefault) {
|
|
615
|
+
const kept = this.controllable(comp, at.map, pair, change, where, line);
|
|
616
|
+
if (kept) {
|
|
617
|
+
out.push(...kept);
|
|
618
|
+
continue;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
if (rest && at.map.discriminated)
|
|
622
|
+
this.forwarded.set(comp, at);
|
|
623
|
+
if (def) {
|
|
624
|
+
const d = attrValue(def);
|
|
625
|
+
if (third && def.kind !== 'bool' && !t.isBooleanLiteral(d))
|
|
626
|
+
out.push({ kind: 'expr', name: pair.to, expr: isTrue(d) }, { kind: 'expr', name: third, expr: isString(t.cloneNode(d, true)) });
|
|
627
|
+
else
|
|
628
|
+
out.push(def.kind === 'static' ? { ...def, name: pair.to } : { kind: 'expr', name: pair.to, expr: d });
|
|
629
|
+
}
|
|
630
|
+
if (change)
|
|
631
|
+
out.push({ kind: 'expr', name: pair.toChange, expr: change });
|
|
632
|
+
}
|
|
633
|
+
return out;
|
|
634
|
+
}
|
|
635
|
+
/** components that spread their rest onto a part whose state their callers give bits-ui themselves (see callers) */
|
|
636
|
+
forwarded = new Map();
|
|
637
|
+
/** components of the module that spread their rest props onto a part (shadcn's wrappers), by name */
|
|
638
|
+
forwarders = new Map();
|
|
639
|
+
/**
|
|
640
|
+
* A component of this module rendering a wrapper that spreads its rest onto a part (`<DialogTrigger asChild>`): what
|
|
641
|
+
* it gives the wrapper reaches the part, so Radix's props take bits-ui's form here: `asChild` the `child` snippet, a
|
|
642
|
+
* renamed prop or callback its bits-ui name, a harmless drop dropped, and, for a part whose state the wrapper cannot
|
|
643
|
+
* keep (see BitsPartMap.discriminated), the default as bits-ui's value.
|
|
644
|
+
*/
|
|
645
|
+
callers(comp) {
|
|
646
|
+
forEachElement(comp, (el) => {
|
|
647
|
+
if (!el.component || el.radix)
|
|
648
|
+
return;
|
|
649
|
+
const fwd = this.forwarders.get(el.tag);
|
|
650
|
+
if (!fwd || fwd.comp === comp)
|
|
651
|
+
return;
|
|
652
|
+
const map = fwd.at.map;
|
|
653
|
+
const where = `<${el.tag}> (spreads its props onto Radix ${fwd.at.pkg} ${fwd.at.part})`;
|
|
654
|
+
const line = lineOf(el);
|
|
655
|
+
const kept = this.defaults.get(fwd.comp) ?? [];
|
|
656
|
+
const triples = stateTriples(map);
|
|
657
|
+
const attrs = [];
|
|
658
|
+
let asChild = false;
|
|
659
|
+
for (const a of el.attrs) {
|
|
660
|
+
if ((a.kind === 'static' || a.kind === 'expr' || a.kind === 'bool') && a.name === 'asChild' && map.asChild) {
|
|
661
|
+
const literal = a.kind === 'bool' || (a.kind === 'expr' && t.isBooleanLiteral(unwrap(a.expr)));
|
|
662
|
+
if (!literal) {
|
|
663
|
+
this.warn('radix-prop', `${where}: asChild is decided at run time; bits-ui's form is a \`child\` snippet written in the markup. Not converted.`, 'error', line);
|
|
664
|
+
attrs.push(a);
|
|
665
|
+
continue;
|
|
666
|
+
}
|
|
667
|
+
asChild = a.kind === 'bool' || (a.kind === 'expr' && t.isBooleanLiteral(unwrap(a.expr), { value: true }));
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
670
|
+
const name = a.kind === 'event' ? a.react : a.kind === 'static' || a.kind === 'expr' || a.kind === 'bool' ? a.name : null;
|
|
671
|
+
// a prop the wrapper takes itself (shadcn's DialogContent `showCloseButton`) does not reach the part through its rest
|
|
672
|
+
if (name !== null && fwd.comp.props.some((p) => p.key === name)) {
|
|
673
|
+
attrs.push(a);
|
|
674
|
+
continue;
|
|
675
|
+
}
|
|
676
|
+
const triple = name ? triples.find((p) => p.prop === name || p.def === name || p.change === name) : undefined;
|
|
677
|
+
if (triple && kept.some((p) => p.prop === triple.prop)) {
|
|
678
|
+
// the wrapper keeps this state itself (see controllable)
|
|
679
|
+
attrs.push(a);
|
|
680
|
+
continue;
|
|
681
|
+
}
|
|
682
|
+
if (triple && (a.kind === 'static' || a.kind === 'expr' || a.kind === 'bool')) {
|
|
683
|
+
if (name === triple.def)
|
|
684
|
+
attrs.push({ ...a, name: triple.to });
|
|
685
|
+
else {
|
|
686
|
+
this.warn('radix-state', `${where}: ${name} is given through the wrapper's rest props, so bits-ui takes it as a plain prop: it follows each new value, but a change the parent does not accept still shows (Radix would keep the parent's value).`, 'warning', line);
|
|
687
|
+
attrs.push({ ...a, name: triple.to });
|
|
688
|
+
}
|
|
689
|
+
continue;
|
|
690
|
+
}
|
|
691
|
+
if (a.kind === 'static' || a.kind === 'expr' || a.kind === 'bool') {
|
|
692
|
+
const to = map.props[a.name];
|
|
693
|
+
// the part renders no element (a Portal): a data-* or aria-* attribute reaches nothing in React either
|
|
694
|
+
if (to === undefined && !map.element && /^(data|aria)-/.test(a.name))
|
|
695
|
+
continue;
|
|
696
|
+
if (to !== undefined && typeof to !== 'string') {
|
|
697
|
+
this.dropped(fwd.at, a, to.why, where, line);
|
|
698
|
+
continue;
|
|
699
|
+
}
|
|
700
|
+
attrs.push(typeof to === 'string' && to !== a.name ? { ...a, name: to } : a);
|
|
701
|
+
continue;
|
|
702
|
+
}
|
|
703
|
+
if (a.kind === 'event') {
|
|
704
|
+
const to = map.events[a.react];
|
|
705
|
+
attrs.push(to !== undefined ? { kind: 'expr', name: to, expr: a.handler } : a);
|
|
706
|
+
continue;
|
|
707
|
+
}
|
|
708
|
+
attrs.push(a);
|
|
709
|
+
}
|
|
710
|
+
this.collapsible(fwd.at, el.attrs, where, line);
|
|
711
|
+
el.attrs = attrs;
|
|
712
|
+
if (asChild)
|
|
713
|
+
this.asChild(el, where, line);
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
/**
|
|
717
|
+
* The component's rest props reach the part (`<SwitchPrimitive.Root {...props} />`): its callers set Radix's state
|
|
718
|
+
* props on it. The component takes the value, its default and its callback out of the rest, and binds bits-ui's prop to
|
|
719
|
+
* a state that follows the value while one is given and holds its own otherwise (Radix's useControllableState).
|
|
720
|
+
*/
|
|
721
|
+
controllable(comp, map, pair, change, where, line) {
|
|
722
|
+
if (comp.propsStyle !== 'destructured' || !comp.rest)
|
|
723
|
+
return null;
|
|
724
|
+
if (change || comp.props.some((p) => p.key === pair.prop || p.key === pair.def || p.key === pair.change)) {
|
|
725
|
+
this.warn('radix-prop', `${where}: the component takes ${[pair.prop, pair.def, pair.change].filter((k) => comp.props.some((p) => p.key === k)).join(', ') || pair.change} itself and spreads the rest of its props onto the part; morph keeps Radix's state from the rest only. Not converted.`, 'error', line);
|
|
726
|
+
return null;
|
|
727
|
+
}
|
|
728
|
+
const alloc = (name) => {
|
|
729
|
+
let local = name;
|
|
730
|
+
for (let i = 2; comp.bindings.has(local) || comp.props.some((p) => p.local === local) || this.taken.has(local); i++)
|
|
731
|
+
local = `${name}${i}`;
|
|
732
|
+
this.taken.add(local);
|
|
733
|
+
return local;
|
|
734
|
+
};
|
|
735
|
+
const value = alloc(pair.prop);
|
|
736
|
+
const def = alloc(pair.def);
|
|
737
|
+
const onChange = alloc(pair.change);
|
|
738
|
+
const own = alloc(`${pair.prop}State`);
|
|
739
|
+
const prop = (key, local, kind) => ({ key, local, defaultValue: null, kind, type: null, optional: true, inherited: true, doc: null, templateDefault: null });
|
|
740
|
+
comp.props.push(prop(pair.prop, value, 'value'), prop(pair.def, def, 'value'), prop(pair.change, onChange, 'event'));
|
|
741
|
+
for (const name of [value, def, onChange])
|
|
742
|
+
comp.bindings.set(name, { name, kind: 'prop', reactive: true });
|
|
743
|
+
comp.bindings.set(own, { name: own, kind: 'state', reactive: true });
|
|
744
|
+
// the state holds the default until the component's own changes (Radix reads defaultProp once); with none, bits-ui's
|
|
745
|
+
// own fallback (`false`, or BitsPartMap.fallbacks), as binding undefined to a prop with a fallback throws
|
|
746
|
+
const fallback = map.fallbacks?.[pair.to];
|
|
747
|
+
comp.items.unshift({ kind: 'state', name: own, setter: null, init: t.logicalExpression('??', t.identifier(def), fallback === undefined ? t.booleanLiteral(false) : t.stringLiteral(fallback)), type: null });
|
|
748
|
+
const v = t.identifier('next');
|
|
749
|
+
const current = () => t.conditionalExpression(t.binaryExpression('===', t.identifier(value), t.identifier('undefined')), t.identifier(own), t.identifier(value));
|
|
750
|
+
const third = map.indeterminate;
|
|
751
|
+
const getter = t.arrowFunctionExpression([], third ? isTrue(t.parenthesizedExpression(current())) : current());
|
|
752
|
+
const setter = t.arrowFunctionExpression([v], t.blockStatement([
|
|
753
|
+
t.ifStatement(t.binaryExpression('===', t.identifier(value), t.identifier('undefined')), t.expressionStatement(t.assignmentExpression('=', t.identifier(own), t.identifier('next')))),
|
|
754
|
+
// bits-ui's binding is typed with its optional prop's `undefined`, which it never writes; Radix's callback takes the value
|
|
755
|
+
t.ifStatement(t.binaryExpression('!==', t.identifier('next'), t.identifier('undefined')), t.expressionStatement(t.optionalCallExpression(t.identifier(onChange), [t.identifier('next')], true))),
|
|
756
|
+
]));
|
|
757
|
+
this.defaults.set(comp, [...(this.defaults.get(comp) ?? []), { ...pair, ...(third ? { indeterminate: third } : {}) }]);
|
|
758
|
+
const out = [{ kind: 'expr', name: `bind:${pair.to}`, expr: t.sequenceExpression([getter, setter]) }];
|
|
759
|
+
// bits-ui clears `indeterminate` itself as it sets the value, whose setter above gives Radix's callback the result
|
|
760
|
+
if (third)
|
|
761
|
+
out.push({ kind: 'expr', name: `bind:${third}`, expr: t.sequenceExpression([t.arrowFunctionExpression([], isString(t.parenthesizedExpression(current()))), t.arrowFunctionExpression([], t.blockStatement([]))]) });
|
|
762
|
+
return out;
|
|
763
|
+
}
|
|
764
|
+
/** components whose props take a Radix default (`defaultChecked`): their props type says so (see types) */
|
|
765
|
+
defaults = new Map();
|
|
766
|
+
/** `ref={el}` on a part: bits-ui's bindable `ref`, for an element ref of the component. */
|
|
767
|
+
ref(comp, expr) {
|
|
768
|
+
const inner = unwrap(expr);
|
|
769
|
+
if (!t.isIdentifier(inner))
|
|
770
|
+
return null;
|
|
771
|
+
const b = comp.bindings.get(inner.name);
|
|
772
|
+
if (b?.kind !== 'ref' || b.refKind !== 'dom' || b.escapes || comp.forwardRef === inner.name)
|
|
773
|
+
return null;
|
|
774
|
+
return { kind: 'expr', name: 'bind:ref', expr: t.identifier(inner.name) };
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* `asChild`: the one element the part is given becomes bits-ui's `child` snippet, spreading the props bits-ui gives
|
|
778
|
+
* first so its own win, its class joined after theirs and each of its handlers run before theirs, as Radix's Slot
|
|
779
|
+
* merges them.
|
|
780
|
+
*/
|
|
781
|
+
asChild(el, where, line) {
|
|
782
|
+
const content = el.children.filter((c) => !(c.type === 'text' && !c.value.trim()));
|
|
783
|
+
const only = content[0];
|
|
784
|
+
if (content.length !== 1 || only?.type !== 'element' || only.portal) {
|
|
785
|
+
this.warn('radix-prop', `${where}: asChild merges the part onto its one child element; it has ${content.length === 1 ? 'a child that is not an element' : `${content.length} children`}. Not converted.`, 'error', line);
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
788
|
+
if (only.attrs.some((a) => a.kind === 'style' || a.kind === 'spread' || a.kind === 'html' || a.kind === 'ref')) {
|
|
789
|
+
this.warn('radix-prop', `${where}: asChild merges the part onto <${only.tag}>, whose style, spread or ref would need merging with bits-ui's props at run time, which the \`child\` snippet does not do. Not converted.`, 'error', line);
|
|
790
|
+
return;
|
|
791
|
+
}
|
|
792
|
+
const local = 'props';
|
|
793
|
+
const given = (member) => t.memberExpression(t.identifier(local), t.identifier(member));
|
|
794
|
+
const attrs = [{ kind: 'spread', expr: t.identifier(local) }];
|
|
795
|
+
for (const a of only.attrs) {
|
|
796
|
+
if ((a.kind === 'static' || a.kind === 'expr') && a.name === 'class') {
|
|
797
|
+
const own = a.kind === 'static' ? t.stringLiteral(a.value) : a.expr;
|
|
798
|
+
const joined = t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.arrayExpression([given('class'), own]), t.identifier('filter')), [t.identifier('Boolean')]), t.identifier('join')), [t.stringLiteral(' ')]);
|
|
799
|
+
attrs.push({ kind: 'expr', name: 'class', expr: joined });
|
|
800
|
+
}
|
|
801
|
+
else if (a.kind === 'event' && !only.component) {
|
|
802
|
+
const event = t.identifier('event');
|
|
803
|
+
const theirs = given(`on${a.name}${a.capture ? 'capture' : ''}`);
|
|
804
|
+
// a handler written to take nothing is called with nothing (as TypeScript requires), any other with the event
|
|
805
|
+
const own = unwrap(a.handler);
|
|
806
|
+
const takesNothing = (t.isArrowFunctionExpression(own) || t.isFunctionExpression(own)) && !own.params.length;
|
|
807
|
+
const first = takesNothing && t.isArrowFunctionExpression(own) && t.isExpression(own.body) ? own.body : t.callExpression(a.handler, takesNothing ? [] : [event]);
|
|
808
|
+
const body = t.blockStatement([
|
|
809
|
+
t.expressionStatement(first),
|
|
810
|
+
t.ifStatement(t.binaryExpression('===', t.unaryExpression('typeof', theirs), t.stringLiteral('function')), t.expressionStatement(t.callExpression(t.cloneNode(theirs), [event]))),
|
|
811
|
+
]);
|
|
812
|
+
attrs.push({ ...a, handler: t.arrowFunctionExpression([event], body) });
|
|
813
|
+
}
|
|
814
|
+
else if (a.kind === 'event') {
|
|
815
|
+
this.warn('radix-prop', `${where}: asChild merges the part onto <${only.tag}>, a component whose own ${a.react} would need running before bits-ui's. Not converted.`, 'error', line);
|
|
816
|
+
return;
|
|
817
|
+
}
|
|
818
|
+
else
|
|
819
|
+
attrs.push(a);
|
|
820
|
+
}
|
|
821
|
+
const param = t.objectPattern([t.objectProperty(t.identifier('props'), t.identifier(local), false, true)]);
|
|
822
|
+
param.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral([t.tsPropertySignature(t.identifier('props'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([t.tsStringKeyword(), t.tsUnknownKeyword()]))))]));
|
|
823
|
+
const merged = { ...only, attrs };
|
|
824
|
+
el.attrs.push({ kind: 'slotProp', name: 'child', children: [merged], render: t.arrowFunctionExpression([param], t.nullLiteral()) });
|
|
825
|
+
el.children = [];
|
|
826
|
+
}
|
|
827
|
+
/**
|
|
828
|
+
* The children of a part that holds a synthesised part (Checkbox.Root holding Checkbox.Indicator) become bits-ui's
|
|
829
|
+
* `children` snippet, whose values (`{ checked, indeterminate }`) the synthesised element reads; each such element is
|
|
830
|
+
* written as Radix renders it, under Radix's condition.
|
|
831
|
+
*/
|
|
832
|
+
synthesise(comp, el, mine, parentAttrs, spec) {
|
|
833
|
+
const names = new Map();
|
|
834
|
+
for (const p of spec.params) {
|
|
835
|
+
let local = `${p}Value`;
|
|
836
|
+
for (let i = 2; comp.bindings.has(local) || this.taken.has(local); i++)
|
|
837
|
+
local = `${p}Value${i}`;
|
|
838
|
+
this.taken.add(local);
|
|
839
|
+
names.set(p, local);
|
|
840
|
+
}
|
|
841
|
+
const disabled = parentAttrs.find((a) => (a.kind === 'expr' || a.kind === 'bool' || a.kind === 'static') && a.name === 'disabled');
|
|
842
|
+
for (const s of mine) {
|
|
843
|
+
const replaced = this.synthElement(s, spec, names, disabled);
|
|
844
|
+
if (replaced) {
|
|
845
|
+
replaceNode(el, s.el, replaced);
|
|
846
|
+
this.placed.add(s.el);
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
this.freeChildren(comp, el);
|
|
850
|
+
const pattern = t.objectPattern(spec.params.map((p) => t.objectProperty(t.identifier(p), t.identifier(names.get(p) ?? p), false, false)));
|
|
851
|
+
el.attrs.push({ kind: 'slotProp', name: 'children', children: el.children, render: t.arrowFunctionExpression([pattern], t.nullLiteral()) });
|
|
852
|
+
el.children = [];
|
|
853
|
+
}
|
|
854
|
+
/**
|
|
855
|
+
* The component's own `children` prop rendered inside the part's `children` snippet (a Checkbox wrapper that renders
|
|
856
|
+
* its children beside the Indicator) would be shadowed by the snippet's name: the prop takes another local name. Only a prop
|
|
857
|
+
* read nowhere but as a rendered slot is renamed; any other read keeps the name and is refused.
|
|
858
|
+
*/
|
|
859
|
+
freeChildren(comp, el) {
|
|
860
|
+
const prop = comp.props.find((p) => p.local === 'children');
|
|
861
|
+
if (!prop)
|
|
862
|
+
return;
|
|
863
|
+
const slots = [];
|
|
864
|
+
visitNodes(el.children, (n) => {
|
|
865
|
+
if (n.type === 'slot' && n.prop === prop.local)
|
|
866
|
+
slots.push(n);
|
|
867
|
+
});
|
|
868
|
+
if (!slots.length)
|
|
869
|
+
return;
|
|
870
|
+
const read = (n) => {
|
|
871
|
+
let found = false;
|
|
872
|
+
t.traverseFast(n, (x) => {
|
|
873
|
+
if (t.isIdentifier(x, { name: prop.local }))
|
|
874
|
+
found = true;
|
|
875
|
+
});
|
|
876
|
+
return found;
|
|
877
|
+
};
|
|
878
|
+
const elsewhere = comp.items.some((item) => Object.values(item).some((v) => (Array.isArray(v) ? v : [v]).some((x) => t.isNode(x) && read(x))));
|
|
879
|
+
let exprRead = false;
|
|
880
|
+
forEachElement(comp, (e) => {
|
|
881
|
+
for (const a of e.attrs)
|
|
882
|
+
if (('expr' in a && a.expr && read(a.expr)) || (a.kind === 'event' && read(a.handler)))
|
|
883
|
+
exprRead = true;
|
|
884
|
+
});
|
|
885
|
+
if (elsewhere || exprRead) {
|
|
886
|
+
this.warn('radix-prop', `<${el.tag}>: the component's own children are rendered inside the part's \`children\` snippet and read elsewhere, so they cannot take another name. Not converted.`, 'error', lineOf(el));
|
|
887
|
+
return;
|
|
888
|
+
}
|
|
889
|
+
let local = 'childrenContent';
|
|
890
|
+
for (let i = 2; comp.bindings.has(local) || comp.props.some((p) => p.local === local) || this.taken.has(local); i++)
|
|
891
|
+
local = `childrenContent${i}`;
|
|
892
|
+
this.taken.add(local);
|
|
893
|
+
const binding = comp.bindings.get(prop.local);
|
|
894
|
+
if (binding) {
|
|
895
|
+
comp.bindings.delete(prop.local);
|
|
896
|
+
comp.bindings.set(local, { ...binding, name: local });
|
|
897
|
+
}
|
|
898
|
+
visitNodes([comp.template, ...comp.fragments.map((f) => f.template)], (n) => {
|
|
899
|
+
if (n.type === 'slot' && n.prop === prop.local)
|
|
900
|
+
n.prop = local;
|
|
901
|
+
});
|
|
902
|
+
prop.local = local;
|
|
903
|
+
}
|
|
904
|
+
synthElement(s, spec, names, disabled) {
|
|
905
|
+
const where = `<${s.el.tag}> (Radix ${s.pkg} ${s.part})`;
|
|
906
|
+
const line = lineOf(s.el) ?? s.binding.line;
|
|
907
|
+
const attrs = [];
|
|
908
|
+
let force = null;
|
|
909
|
+
for (const a of s.el.attrs) {
|
|
910
|
+
if ((a.kind === 'bool' || a.kind === 'expr') && a.name === 'forceMount') {
|
|
911
|
+
force = a.kind === 'bool' ? t.booleanLiteral(true) : a.expr;
|
|
912
|
+
continue;
|
|
913
|
+
}
|
|
914
|
+
// a style of its own is kept, unless Radix sets one on the element too (merged at run time, which this does not)
|
|
915
|
+
if ((a.kind === 'style' && spec.style) || a.kind === 'ref' || ((a.kind === 'bool' || a.kind === 'expr' || a.kind === 'static') && a.name === 'asChild')) {
|
|
916
|
+
this.warn('radix-prop', `${where}: ${a.kind === 'style' ? 'a style' : a.kind === 'ref' ? 'a ref' : 'asChild'} on the indicator is not converted (morph writes Radix's own <${spec.tag}> here).`, 'error', line);
|
|
917
|
+
return null;
|
|
918
|
+
}
|
|
919
|
+
// the part is now a plain element: a DOM event keeps its DOM name
|
|
920
|
+
attrs.push(a.kind === 'event' ? { ...a, name: a.react.slice(2).toLowerCase() } : a);
|
|
921
|
+
}
|
|
922
|
+
const read = (src) => readWith(names, src);
|
|
923
|
+
for (const [name, value] of Object.entries(spec.attrs)) {
|
|
924
|
+
const expr = read(value);
|
|
925
|
+
// a value known here (Radix's default) is written as the attribute it is, or left out where it is undefined
|
|
926
|
+
if (t.isIdentifier(expr, { name: 'undefined' }))
|
|
927
|
+
continue;
|
|
928
|
+
attrs.unshift(t.isStringLiteral(expr) ? { kind: 'static', name, value: expr.value } : { kind: 'expr', name, expr });
|
|
929
|
+
}
|
|
930
|
+
if (disabled && !spec.noDisabled) {
|
|
931
|
+
const test = disabled.kind === 'bool' ? t.booleanLiteral(true) : disabled.kind === 'static' ? t.stringLiteral(disabled.value) : t.cloneNode(disabled.expr, true);
|
|
932
|
+
attrs.unshift({ kind: 'expr', name: 'data-disabled', expr: t.conditionalExpression(test, t.stringLiteral(''), t.identifier('undefined')) });
|
|
933
|
+
}
|
|
934
|
+
if (spec.style)
|
|
935
|
+
attrs.push({ kind: 'static', name: 'style', value: Object.entries(spec.style).map(([k, v]) => `${k}: ${v}`).join('; ') });
|
|
936
|
+
const element = { type: 'element', tag: spec.tag, component: false, motion: null, attrs: attrs.map((a) => (a.kind === 'static' || a.kind === 'expr' || a.kind === 'bool' ? { ...a, name: a.name } : a)), children: s.el.children };
|
|
937
|
+
if (spec.when === undefined)
|
|
938
|
+
return element;
|
|
939
|
+
const when = read(spec.when);
|
|
940
|
+
return { type: 'if', branches: [{ test: force ? t.logicalExpression('||', force, when) : when, children: [element] }] };
|
|
941
|
+
}
|
|
942
|
+
/** A class of the part that reads a data attribute whose value differs between Radix and bits-ui. */
|
|
943
|
+
dataDiffers(at, attrs, where, line) {
|
|
944
|
+
const differs = at.map.dataDiffers;
|
|
945
|
+
if (!differs?.length)
|
|
946
|
+
return;
|
|
947
|
+
const texts = [];
|
|
948
|
+
for (const a of attrs) {
|
|
949
|
+
if (a.kind === 'static' && a.name === 'class')
|
|
950
|
+
texts.push(a.value);
|
|
951
|
+
if (a.kind === 'expr' && a.name === 'class')
|
|
952
|
+
t.traverseFast(a.expr, (n) => {
|
|
953
|
+
if (t.isStringLiteral(n))
|
|
954
|
+
texts.push(n.value);
|
|
955
|
+
if (t.isTemplateElement(n))
|
|
956
|
+
texts.push(n.value.raw);
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
for (const attr of differs) {
|
|
960
|
+
const short = attr.replace(/^data-/, '');
|
|
961
|
+
if (!texts.some((x) => x.includes(`data-[${short}`) || x.includes(`[${attr}`)))
|
|
962
|
+
continue;
|
|
963
|
+
this.warn('radix-data', `${where}: its class reads ${attr}, which bits-ui sets on this part and Radix does not: those classes apply in Svelte only.`, 'warning', line);
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
/**
|
|
967
|
+
* Props typed as a Radix part's (`React.ComponentProps<typeof SwitchPrimitive.Root>`): bits-ui's props of the part
|
|
968
|
+
* (`SwitchPrimitive.RootProps`), and Radix's default of its state for a component that keeps it (see controllable).
|
|
969
|
+
*/
|
|
970
|
+
types(comp) {
|
|
971
|
+
const swap = (type) => {
|
|
972
|
+
if (!t.isTSTypeReference(type))
|
|
973
|
+
return null;
|
|
974
|
+
const name = t.isIdentifier(type.typeName) ? type.typeName.name : t.isTSQualifiedName(type.typeName) ? type.typeName.right.name : '';
|
|
975
|
+
const arg = type.typeParameters?.params[0];
|
|
976
|
+
if (!/^ComponentProps(WithoutRef|WithRef)?$/.test(name) || !t.isTSTypeQuery(arg))
|
|
977
|
+
return null;
|
|
978
|
+
const q = arg.exprName;
|
|
979
|
+
const [root, member] = t.isTSQualifiedName(q) && t.isIdentifier(q.left) ? [q.left.name, q.right.name] : t.isIdentifier(q) ? [q.name, null] : [null, null];
|
|
980
|
+
const binding = root ? this.claimed.get(root) : undefined;
|
|
981
|
+
if (!binding || !root)
|
|
982
|
+
return null;
|
|
983
|
+
const radixPart = binding.part ?? member;
|
|
984
|
+
const part = radixPart ? bitsPartName(binding.pkg, radixPart) : null;
|
|
985
|
+
const map = part ? BITS_PACKAGES[binding.pkg]?.parts[part] : undefined;
|
|
986
|
+
const ns = BITS_PACKAGES[binding.pkg]?.ns;
|
|
987
|
+
// a synthesised or plain part keeps the element typing React's ComponentProps gave (ir/lower: `extendsHtml`)
|
|
988
|
+
if (!map || map.synth || map.synthIn || map.plain || !ns)
|
|
989
|
+
return null;
|
|
990
|
+
const local = this.nsLocal(ns, binding);
|
|
991
|
+
const memberName = map.name.split('.')[1] ?? 'Root';
|
|
992
|
+
const ref = t.tsTypeReference(t.tsQualifiedName(t.identifier(local), t.identifier(`${memberName}Props`)));
|
|
993
|
+
if (map.defaults && !this.retype.has(comp))
|
|
994
|
+
this.retype.set(comp, { ref, keys: Object.keys(map.defaults) });
|
|
995
|
+
// (M3: a core's props a component takes itself are typed as the core types them, Radix's own types)
|
|
996
|
+
else if (map.core && Object.keys(map.props).length && !this.retype.has(comp))
|
|
997
|
+
this.retype.set(comp, { ref, keys: Object.keys(map.props) });
|
|
998
|
+
return ref;
|
|
999
|
+
};
|
|
1000
|
+
let found = false;
|
|
1001
|
+
const walk = (type) => {
|
|
1002
|
+
if (!type)
|
|
1003
|
+
return type;
|
|
1004
|
+
const direct = swap(type);
|
|
1005
|
+
if (direct) {
|
|
1006
|
+
found = true;
|
|
1007
|
+
return direct;
|
|
1008
|
+
}
|
|
1009
|
+
if (t.isTSIntersectionType(type) || t.isTSUnionType(type))
|
|
1010
|
+
type.types = type.types.map((m) => walk(m) ?? m);
|
|
1011
|
+
return type;
|
|
1012
|
+
};
|
|
1013
|
+
comp.propsType = walk(comp.propsType);
|
|
1014
|
+
comp.propsHeritage = comp.propsHeritage.map((h) => walk(h) ?? h);
|
|
1015
|
+
if (found && comp.extendsHtml && /^ComponentProps/.test(comp.extendsHtml.react) && comp.propsType) {
|
|
1016
|
+
// the props are bits-ui's part's, not an element's attributes
|
|
1017
|
+
comp.extendsHtml = null;
|
|
1018
|
+
const parts = t.isTSIntersectionType(comp.propsType) ? comp.propsType.types : [comp.propsType];
|
|
1019
|
+
for (const p of parts)
|
|
1020
|
+
if (this.isPartProps(p))
|
|
1021
|
+
comp.propsHeritage.push(p);
|
|
1022
|
+
}
|
|
1023
|
+
this.pending.push(comp);
|
|
1024
|
+
}
|
|
1025
|
+
pending = [];
|
|
1026
|
+
/**
|
|
1027
|
+
* Components typed as a part's props that take a prop with a Radix default themselves (Progress's `value`): the prop
|
|
1028
|
+
* is typed as bits-ui types it on the part, which is Radix's type, not the element attribute's of the same name.
|
|
1029
|
+
*/
|
|
1030
|
+
retype = new Map();
|
|
1031
|
+
/** a bits-ui part's props type, as types() writes it */
|
|
1032
|
+
isPartProps(p) {
|
|
1033
|
+
return t.isTSTypeReference(p) && t.isTSQualifiedName(p.typeName) && /Props$/.test(p.typeName.right.name);
|
|
1034
|
+
}
|
|
1035
|
+
/**
|
|
1036
|
+
* After the elements: a component that keeps Radix's state (see controllable) declares the three props it takes out
|
|
1037
|
+
* of its rest, each typed as bits-ui types the part's value and callback (the default as the value).
|
|
1038
|
+
*/
|
|
1039
|
+
finishTypes() {
|
|
1040
|
+
for (const [comp, { ref, keys }] of this.retype) {
|
|
1041
|
+
const own = [];
|
|
1042
|
+
for (const p of comp.props) {
|
|
1043
|
+
if (!keys.includes(p.key))
|
|
1044
|
+
continue;
|
|
1045
|
+
own.push(p.key);
|
|
1046
|
+
comp.declared.set(p.key, { type: t.tsIndexedAccessType(t.cloneNode(ref, true), t.tsLiteralType(t.stringLiteral(p.key))), optional: true, doc: null });
|
|
1047
|
+
}
|
|
1048
|
+
if (!own.length)
|
|
1049
|
+
continue;
|
|
1050
|
+
// the component gives the part Radix's default itself, so its caller may leave the prop out even where bits-ui
|
|
1051
|
+
// requires it (ScrollArea.Scrollbar's orientation): the part's props type leaves it to the component's declaration
|
|
1052
|
+
const omit = t.tsTypeReference(t.identifier('Omit'), t.tsTypeParameterInstantiation([t.cloneNode(ref, true), t.tsUnionType(own.map((k) => t.tsLiteralType(t.stringLiteral(k))))]));
|
|
1053
|
+
const swapRef = (type) => {
|
|
1054
|
+
if (type === ref)
|
|
1055
|
+
return omit;
|
|
1056
|
+
if (t.isTSIntersectionType(type) || t.isTSUnionType(type))
|
|
1057
|
+
type.types = type.types.map(swapRef);
|
|
1058
|
+
return type;
|
|
1059
|
+
};
|
|
1060
|
+
if (comp.propsType)
|
|
1061
|
+
comp.propsType = swapRef(comp.propsType);
|
|
1062
|
+
comp.propsHeritage = comp.propsHeritage.map(swapRef);
|
|
1063
|
+
}
|
|
1064
|
+
for (const comp of this.pending) {
|
|
1065
|
+
const pairs = this.defaults.get(comp);
|
|
1066
|
+
if (!pairs?.length)
|
|
1067
|
+
continue;
|
|
1068
|
+
const heritage = comp.propsHeritage.find((h) => this.isPartProps(h));
|
|
1069
|
+
// a CheckedState value is not bits-ui's boolean: the part's own value and callback leave its props type
|
|
1070
|
+
const retyped = pairs.filter((p) => p.indeterminate).flatMap((p) => [p.to, p.toChange]);
|
|
1071
|
+
if (heritage && retyped.length) {
|
|
1072
|
+
const omit = t.tsTypeReference(t.identifier('Omit'), t.tsTypeParameterInstantiation([t.cloneNode(heritage, true), t.tsUnionType(retyped.map((k) => t.tsLiteralType(t.stringLiteral(k))))]));
|
|
1073
|
+
comp.propsHeritage = comp.propsHeritage.map((h) => (h === heritage ? omit : h));
|
|
1074
|
+
}
|
|
1075
|
+
for (const p of pairs) {
|
|
1076
|
+
const of = (member) => (heritage ? t.tsIndexedAccessType(t.cloneNode(heritage, true), t.tsLiteralType(t.stringLiteral(member))) : null);
|
|
1077
|
+
// Radix's CheckedState, as Radix types the value and its callback
|
|
1078
|
+
const checked = () => t.tsUnionType([t.tsBooleanKeyword(), t.tsLiteralType(t.stringLiteral('indeterminate'))]);
|
|
1079
|
+
const param = t.identifier(p.prop);
|
|
1080
|
+
param.typeAnnotation = t.tsTypeAnnotation(checked());
|
|
1081
|
+
comp.declared.set(p.prop, { type: p.indeterminate ? checked() : of(p.to), optional: true, doc: null });
|
|
1082
|
+
comp.declared.set(p.def, { type: p.indeterminate ? checked() : of(p.to), optional: true, doc: null });
|
|
1083
|
+
comp.declared.set(p.change, { type: p.indeterminate ? t.tsFunctionType(null, [param], t.tsTypeAnnotation(t.tsVoidKeyword())) : of(p.toChange), optional: true, doc: null });
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
/**
|
|
1089
|
+
* Radix focuses the part on a left press that is not a Ctrl-click (BitsPartMap.pressFocus), after the part's own
|
|
1090
|
+
* mousedown handler unless that prevented it; bits-ui's part does not, so the part is given that handler.
|
|
1091
|
+
*/
|
|
1092
|
+
function pressFocus(attrs) {
|
|
1093
|
+
const i = attrs.findIndex((a) => a.kind === 'event' && a.react === 'onMouseDown' && !a.capture);
|
|
1094
|
+
const own = i >= 0 ? attrs[i] : undefined;
|
|
1095
|
+
const event = t.identifier('event');
|
|
1096
|
+
event.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('MouseEvent')));
|
|
1097
|
+
const target = () => t.memberExpression(t.identifier('event'), t.identifier('currentTarget'));
|
|
1098
|
+
const tests = [
|
|
1099
|
+
t.binaryExpression('===', t.memberExpression(t.identifier('event'), t.identifier('button')), t.numericLiteral(0)),
|
|
1100
|
+
t.unaryExpression('!', t.memberExpression(t.identifier('event'), t.identifier('ctrlKey'))),
|
|
1101
|
+
...(own ? [t.unaryExpression('!', t.memberExpression(t.identifier('event'), t.identifier('defaultPrevented')))] : []),
|
|
1102
|
+
t.binaryExpression('instanceof', target(), t.identifier('HTMLElement')),
|
|
1103
|
+
];
|
|
1104
|
+
const focus = t.ifStatement(tests.reduce((a, b) => t.logicalExpression('&&', a, b)), t.expressionStatement(t.callExpression(t.memberExpression(target(), t.identifier('focus')), [])));
|
|
1105
|
+
const body = own?.kind === 'event' ? [t.expressionStatement(t.callExpression(own.handler, [t.identifier('event')])), focus] : [focus];
|
|
1106
|
+
const handler = { kind: 'expr', name: 'onmousedown', expr: t.arrowFunctionExpression([event], t.blockStatement(body)) };
|
|
1107
|
+
if (i >= 0)
|
|
1108
|
+
attrs.splice(i, 1, handler);
|
|
1109
|
+
else
|
|
1110
|
+
attrs.push(handler);
|
|
1111
|
+
}
|
|
1112
|
+
/**
|
|
1113
|
+
* The DOM's name of a React DOM attribute a part passes to its element (`htmlFor` → `for`, `tabIndex` → `tabindex`).
|
|
1114
|
+
* React's `defaultValue` / `defaultChecked` are state, never renamed to an attribute here.
|
|
1115
|
+
*/
|
|
1116
|
+
function domName(name) {
|
|
1117
|
+
return name === 'defaultValue' || name === 'defaultChecked' ? name : domAttributeName(name);
|
|
1118
|
+
}
|
|
1119
|
+
/**
|
|
1120
|
+
* An expression of a map (`selected ? 'checked' : 'unchecked'`), its parameters read by their local names (a snippet's)
|
|
1121
|
+
* or replaced by the expressions they stand for (a parent's props, SynthPart.fromProps), then folded.
|
|
1122
|
+
*/
|
|
1123
|
+
function readWith(names, src) {
|
|
1124
|
+
const swap = (n) => {
|
|
1125
|
+
if (t.isIdentifier(n)) {
|
|
1126
|
+
const to = names.get(n.name);
|
|
1127
|
+
if (typeof to === 'string')
|
|
1128
|
+
return t.identifier(to);
|
|
1129
|
+
if (to)
|
|
1130
|
+
return t.cloneNode(to, true);
|
|
1131
|
+
return n;
|
|
1132
|
+
}
|
|
1133
|
+
for (const key of t.VISITOR_KEYS[n.type] ?? []) {
|
|
1134
|
+
const value = Reflect.get(n, key);
|
|
1135
|
+
if (Array.isArray(value))
|
|
1136
|
+
Reflect.set(n, key, value.map((v) => (t.isNode(v) ? swap(v) : v)));
|
|
1137
|
+
else if (t.isNode(value))
|
|
1138
|
+
Reflect.set(n, key, swap(value));
|
|
1139
|
+
}
|
|
1140
|
+
return n;
|
|
1141
|
+
};
|
|
1142
|
+
const e = swap(parseExpression(src));
|
|
1143
|
+
return fold(t.isExpression(e) ? e : parseExpression(src));
|
|
1144
|
+
}
|
|
1145
|
+
/**
|
|
1146
|
+
* The value a part is given for a prop, as its attributes give it in order (the last wins, as in React): an attribute
|
|
1147
|
+
* of that name, or a spread of an object named in the markup that may hold it (`'max' in props ? props.max : …`; the
|
|
1148
|
+
* component's rest props hold no prop it takes itself). null: nothing gives it; 'refused': a spread whose keys cannot
|
|
1149
|
+
* be read here (a call, a member).
|
|
1150
|
+
*/
|
|
1151
|
+
function propValue(comp, attrs, name) {
|
|
1152
|
+
let value = null;
|
|
1153
|
+
for (const a of attrs) {
|
|
1154
|
+
if ((a.kind === 'static' || a.kind === 'expr' || a.kind === 'bool') && a.name === name)
|
|
1155
|
+
value = attrValue(a);
|
|
1156
|
+
else if (a.kind === 'spread') {
|
|
1157
|
+
const obj = unwrap(a.expr);
|
|
1158
|
+
if (!t.isIdentifier(obj))
|
|
1159
|
+
return 'refused';
|
|
1160
|
+
if (obj.name === comp.rest && comp.props.some((p) => p.key === name))
|
|
1161
|
+
continue;
|
|
1162
|
+
const read = t.memberExpression(t.identifier(obj.name), t.identifier(name));
|
|
1163
|
+
// a spread that holds the key gives its value, undefined too; one that does not leaves what came before
|
|
1164
|
+
value = value === null ? read : t.conditionalExpression(t.binaryExpression('in', t.stringLiteral(name), t.identifier(obj.name)), read, value);
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
return value;
|
|
1168
|
+
}
|
|
1169
|
+
/** Folds what a known value decides (`null ?? 100` → `100`, `null == null ? 'a' : b` → `'a'`), so the output reads plainly. */
|
|
1170
|
+
function fold(e) {
|
|
1171
|
+
const known = (x) => t.isNullLiteral(x) || t.isIdentifier(x, { name: 'undefined' });
|
|
1172
|
+
const literal = (x) => t.isNumericLiteral(x) || t.isStringLiteral(x) || t.isBooleanLiteral(x);
|
|
1173
|
+
if (t.isLogicalExpression(e) && e.operator === '??') {
|
|
1174
|
+
const left = fold(e.left);
|
|
1175
|
+
const right = fold(e.right);
|
|
1176
|
+
if (known(left))
|
|
1177
|
+
return right;
|
|
1178
|
+
if (literal(left))
|
|
1179
|
+
return left;
|
|
1180
|
+
// `(x ?? null) ?? y`: the inner default is nullish itself, so `x ?? y`
|
|
1181
|
+
if (t.isLogicalExpression(left) && left.operator === '??' && known(left.right))
|
|
1182
|
+
return fold(t.logicalExpression('??', left.left, right));
|
|
1183
|
+
return t.logicalExpression('??', left, right);
|
|
1184
|
+
}
|
|
1185
|
+
if (t.isBinaryExpression(e) && (e.operator === '==' || e.operator === '===') && t.isExpression(e.left)) {
|
|
1186
|
+
const left = fold(e.left);
|
|
1187
|
+
const right = fold(e.right);
|
|
1188
|
+
if (t.isNullLiteral(left) && t.isNullLiteral(right))
|
|
1189
|
+
return t.booleanLiteral(true);
|
|
1190
|
+
if (e.operator === '==' && t.isNullLiteral(right) && literal(left))
|
|
1191
|
+
return t.booleanLiteral(false);
|
|
1192
|
+
// `(x ?? null) == null` is `x == null`; `(x ?? null) === 50` is `x === 50` (neither nullish value is 50)
|
|
1193
|
+
if (t.isLogicalExpression(left) && left.operator === '??' && known(left.right) && ((e.operator === '==' && t.isNullLiteral(right)) || literal(right)))
|
|
1194
|
+
return t.binaryExpression(e.operator, left.left, right);
|
|
1195
|
+
return t.binaryExpression(e.operator, left, right);
|
|
1196
|
+
}
|
|
1197
|
+
if (t.isConditionalExpression(e)) {
|
|
1198
|
+
const test = fold(e.test);
|
|
1199
|
+
if (t.isBooleanLiteral(test))
|
|
1200
|
+
return fold(test.value ? e.consequent : e.alternate);
|
|
1201
|
+
return t.conditionalExpression(test, fold(e.consequent), fold(e.alternate));
|
|
1202
|
+
}
|
|
1203
|
+
return e;
|
|
1204
|
+
}
|
|
1205
|
+
/** How a synthesised part (BitsPartMap.synth / synthIn) is written inside the parent part `parent`, if it can sit there. */
|
|
1206
|
+
function synthSpec(map, parent) {
|
|
1207
|
+
if (map.synth)
|
|
1208
|
+
return map.synth.parent === parent ? map.synth : undefined;
|
|
1209
|
+
return map.synthIn && Object.hasOwn(map.synthIn, parent) ? map.synthIn[parent] : undefined;
|
|
1210
|
+
}
|
|
1211
|
+
/** `v === true`: bits-ui's boolean of a Radix CheckedState */
|
|
1212
|
+
function isTrue(v) {
|
|
1213
|
+
return t.binaryExpression('===', v, t.booleanLiteral(true));
|
|
1214
|
+
}
|
|
1215
|
+
/** `typeof v === 'string'`: a Radix CheckedState is 'indeterminate' (a test TypeScript allows on a plain boolean too) */
|
|
1216
|
+
function isString(v) {
|
|
1217
|
+
return t.binaryExpression('===', t.unaryExpression('typeof', v), t.stringLiteral('string'));
|
|
1218
|
+
}
|
|
1219
|
+
/** The state props of a part: a Radix prop `x` and `defaultX` mapped to the same bits-ui prop, with `onXChange`. */
|
|
1220
|
+
function stateTriples(map) {
|
|
1221
|
+
const out = [];
|
|
1222
|
+
// (M3: a core takes Radix's state props as they are: nothing is bound)
|
|
1223
|
+
if (map.core)
|
|
1224
|
+
return out;
|
|
1225
|
+
for (const [prop, to] of Object.entries(map.props)) {
|
|
1226
|
+
const cap = prop.charAt(0).toUpperCase() + prop.slice(1);
|
|
1227
|
+
const def = `default${cap}`;
|
|
1228
|
+
const change = `on${cap}Change`;
|
|
1229
|
+
// `defaultX` absent: Radix has no default for this value (DropdownMenu.RadioGroup), so it is controlled or fixed
|
|
1230
|
+
if (typeof to !== 'string' || (map.props[def] !== undefined && map.props[def] !== to) || /^default[A-Z]/.test(prop))
|
|
1231
|
+
continue;
|
|
1232
|
+
const toChange = map.events[change];
|
|
1233
|
+
if (toChange)
|
|
1234
|
+
out.push({ prop, def, change, to, toChange, ...(map.props[def] === to ? { hasDefault: true } : {}) });
|
|
1235
|
+
}
|
|
1236
|
+
return out;
|
|
1237
|
+
}
|
|
1238
|
+
function statePairs(map) {
|
|
1239
|
+
return new Set(stateTriples(map).flatMap((p) => [p.prop, p.def, p.change]));
|
|
1240
|
+
}
|
|
1241
|
+
function attrValue(a) {
|
|
1242
|
+
return a.kind === 'bool' ? t.booleanLiteral(true) : a.kind === 'static' ? t.stringLiteral(a.value) : a.expr;
|
|
1243
|
+
}
|
|
1244
|
+
/** `bind:open={() => value, change}`: bits-ui reads the value and writes through the callback (none: it stays). */
|
|
1245
|
+
function binding(to, value, change, comp) {
|
|
1246
|
+
const fn = change ? unwrap(change) : null;
|
|
1247
|
+
// a function written here, a React state setter or a function the component declares is the setter; any other name
|
|
1248
|
+
// may hold undefined (an optional callback prop), so it is called if set
|
|
1249
|
+
const kind = fn && t.isIdentifier(fn) ? comp?.bindings.get(fn.name)?.kind : undefined;
|
|
1250
|
+
const direct = t.isArrowFunctionExpression(fn) || t.isFunctionExpression(fn) || kind === 'setter' || kind === 'function' || kind === 'dispatch';
|
|
1251
|
+
const setter = !change ? t.arrowFunctionExpression([], t.blockStatement([])) : direct ? change : t.arrowFunctionExpression([t.identifier('next')], t.optionalCallExpression(change, [t.identifier('next')], true));
|
|
1252
|
+
return { kind: 'expr', name: `bind:${to}`, expr: t.sequenceExpression([t.arrowFunctionExpression([], value), setter]) };
|
|
1253
|
+
}
|
|
1254
|
+
/** The component's own rest props, spread onto the part. */
|
|
1255
|
+
function restSpread(comp, attrs) {
|
|
1256
|
+
return !!comp.rest && attrs.some((a) => a.kind === 'spread' && t.isIdentifier(unwrap(a.expr), { name: comp.rest ?? '' }));
|
|
1257
|
+
}
|
|
1258
|
+
function lineOf(el) {
|
|
1259
|
+
for (const a of el.attrs) {
|
|
1260
|
+
const e = a.kind === 'event' ? a.handler : 'expr' in a ? a.expr : null;
|
|
1261
|
+
const line = e?.loc?.start.line;
|
|
1262
|
+
if (line)
|
|
1263
|
+
return line;
|
|
1264
|
+
}
|
|
1265
|
+
return undefined;
|
|
1266
|
+
}
|
|
1267
|
+
function contains(root, target) {
|
|
1268
|
+
let found = false;
|
|
1269
|
+
visitNodes(root.children, (n) => {
|
|
1270
|
+
if (n === target)
|
|
1271
|
+
found = true;
|
|
1272
|
+
});
|
|
1273
|
+
return found;
|
|
1274
|
+
}
|
|
1275
|
+
function visitNodes(nodes, fn) {
|
|
1276
|
+
for (const n of nodes) {
|
|
1277
|
+
fn(n);
|
|
1278
|
+
switch (n.type) {
|
|
1279
|
+
case 'element':
|
|
1280
|
+
visitNodes(n.children, fn);
|
|
1281
|
+
for (const a of n.attrs)
|
|
1282
|
+
if (a.kind === 'slotProp')
|
|
1283
|
+
visitNodes(a.children, fn);
|
|
1284
|
+
break;
|
|
1285
|
+
case 'if':
|
|
1286
|
+
for (const b of n.branches)
|
|
1287
|
+
visitNodes(b.children, fn);
|
|
1288
|
+
break;
|
|
1289
|
+
case 'slot':
|
|
1290
|
+
visitNodes(n.fallback, fn);
|
|
1291
|
+
break;
|
|
1292
|
+
case 'for':
|
|
1293
|
+
case 'fragment':
|
|
1294
|
+
case 'keyed':
|
|
1295
|
+
case 'presence':
|
|
1296
|
+
visitNodes(n.children, fn);
|
|
1297
|
+
break;
|
|
1298
|
+
default:
|
|
1299
|
+
break;
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
/** Replaces `target` with `replacement` wherever it sits under `root`. */
|
|
1304
|
+
function replaceNode(root, target, replacement) {
|
|
1305
|
+
const swap = (list) => {
|
|
1306
|
+
const i = list.indexOf(target);
|
|
1307
|
+
if (i >= 0)
|
|
1308
|
+
list[i] = replacement;
|
|
1309
|
+
};
|
|
1310
|
+
const walk = (nodes) => {
|
|
1311
|
+
swap(nodes);
|
|
1312
|
+
for (const n of nodes) {
|
|
1313
|
+
switch (n.type) {
|
|
1314
|
+
case 'element':
|
|
1315
|
+
walk(n.children);
|
|
1316
|
+
for (const a of n.attrs)
|
|
1317
|
+
if (a.kind === 'slotProp')
|
|
1318
|
+
walk(a.children);
|
|
1319
|
+
break;
|
|
1320
|
+
case 'if':
|
|
1321
|
+
for (const b of n.branches)
|
|
1322
|
+
walk(b.children);
|
|
1323
|
+
break;
|
|
1324
|
+
case 'slot':
|
|
1325
|
+
walk(n.fallback);
|
|
1326
|
+
break;
|
|
1327
|
+
case 'for':
|
|
1328
|
+
case 'fragment':
|
|
1329
|
+
case 'keyed':
|
|
1330
|
+
case 'presence':
|
|
1331
|
+
walk(n.children);
|
|
1332
|
+
break;
|
|
1333
|
+
default:
|
|
1334
|
+
break;
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
};
|
|
1338
|
+
walk(root.children);
|
|
1339
|
+
}
|