@homepages/concrete 8.0.0 → 9.1.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/CHANGELOG.md +1005 -0
- package/README.md +20 -9
- package/codemods/list-grammar.mjs +923 -0
- package/codemods/manifest.json +64 -0
- package/codemods/process-builders.mjs +198 -0
- package/codemods/slot-object.mjs +1097 -0
- package/codemods/steps/content-object-fields.mjs +324 -0
- package/codemods/steps/list-object-form.mjs +6 -7
- package/codemods/table-rows.mjs +568 -0
- package/codemods/unwrap-object-fields.mjs +310 -0
- package/codemods/viewer-slot-document.mjs +131 -0
- package/codemods/write-property-as-facts-document.mjs +716 -0
- package/codemods/write-the-address-line.mjs +255 -0
- package/codemods/write-the-registry-members.mjs +653 -0
- package/codemods/write-the-select-from.mjs +141 -0
- package/codemods/write-the-sole-from.mjs +369 -0
- package/codemods/write-the-table-from.mjs +267 -0
- package/codemods/write-the-whole-item.mjs +512 -0
- package/codemods/writeback-drop.mjs +168 -0
- package/codemods/written-contact-address.mjs +162 -0
- package/codemods/written-media-category.mjs +227 -0
- package/codemods/written-ref-kinds-name-the-layer.mjs +172 -0
- package/codemods/written-uploads-replace-pool-reads.mjs +454 -0
- package/codemods/written-uploads-sort-into-lists.mjs +405 -0
- package/codemods/written-uploads-stated-in-sorted-lists.mjs +295 -0
- package/codemods/written-video-sources-drop.mjs +144 -0
- package/codemods/written-without-pick-none.mjs +146 -0
- package/codemods/written-without-retired-knowledge.mjs +223 -0
- package/codemods/written-without-the-media-key.mjs +202 -0
- package/codemods/written-without-the-property-type.mjs +249 -0
- package/dist/artifact/playground/client/assets/index-BOYbgBnP.js +1259 -0
- package/dist/artifact/playground/client/assets/index-DDOcM0zA.css +1 -0
- package/dist/artifact/playground/client/index.html +2 -2
- package/dist/artifact/playground/client/island-canvas-host.mjs +1 -1
- package/dist/artifact/playground/host.mjs +61 -27
- package/dist/base.css +58 -1
- package/dist/cli/build/artifact.js +6 -9
- package/dist/cli/build/captures.js +2 -12
- package/dist/cli/build/esbuild.js +1 -25
- package/dist/cli/build/fixtures.js +2 -1
- package/dist/cli/build/icons.js +1 -1
- package/dist/cli/build/static.js +2 -2
- package/dist/cli/capture/capture.js +8 -9
- package/dist/cli/check/analyze.js +8 -1
- package/dist/cli/check/load-outdir.js +47 -0
- package/dist/cli/check/load-workspace.js +4 -4
- package/dist/cli/check/render/html.js +1 -2
- package/dist/cli/check/render/inputs.js +10 -3
- package/dist/cli/check/render/island-frames.js +2 -1
- package/dist/cli/check/render/marker-coverage.js +23 -15
- package/dist/cli/check/report.js +5 -0
- package/dist/cli/check/stages/bundles.js +1 -1
- package/dist/cli/check/stages/relations.js +1 -1
- package/dist/cli/check/stages/render.js +48 -33
- package/dist/cli/check/stages/sections.js +83 -31
- package/dist/cli/check/stages/workspace.js +2 -0
- package/dist/cli/check/template-key.js +16 -0
- package/dist/cli/commands.js +1 -1
- package/dist/cli/dev/crop.js +1 -1
- package/dist/cli/dev/fixture-ladders.js +96 -0
- package/dist/cli/dev/islands.js +12 -123
- package/dist/cli/dev/listen.js +20 -1
- package/dist/cli/dev/load-template.js +1 -1
- package/dist/cli/dev/server.js +35 -10
- package/dist/cli/dev/vite.js +9 -7
- package/dist/cli/dev/workspace-reader.js +16 -16
- package/dist/cli/media/derive.js +43 -57
- package/dist/cli/media/frame-rung-floor.js +0 -1
- package/dist/cli/property/index.js +1 -6
- package/dist/css/inline.js +1 -22
- package/dist/eslint/rules/no-marker-literal.js +1 -1
- package/dist/fixtures/load.js +16 -7
- package/dist/fixtures/media-plugin.js +17 -12
- package/dist/fixtures/media-store.js +115 -0
- package/dist/fixtures/property.js +345 -126
- package/dist/fixtures/resolve.js +260 -251
- package/dist/fixtures/rows.js +4 -11
- package/dist/fixtures.d.ts +234 -109
- package/dist/index.d.ts +25 -16
- package/dist/index.js +10 -6
- package/dist/islands/marker.js +2 -1
- package/dist/packages/external/concrete/package.js +1 -1
- package/dist/packages/internal/canvas-core/dist/island-host.js +62 -0
- package/dist/packages/internal/canvas-core/dist/island-shields.js +30 -0
- package/dist/packages/internal/capture-settle/dist/index.js +3 -0
- package/dist/packages/internal/capture-settle/dist/island-markers.d.ts +13 -0
- package/dist/packages/internal/capture-settle/dist/island-markers.js +6 -0
- package/dist/packages/internal/capture-settle/dist/settle.js +150 -0
- package/dist/packages/internal/{image-ladder/dist/crop.js → crop-region/dist/index.js} +21 -20
- package/dist/packages/internal/fill-bake/dist/bake-property-ref.js +250 -46
- package/dist/packages/internal/fill-bake/dist/compute-reads.js +361 -4
- package/dist/packages/internal/fill-bake/dist/derive-source.js +25 -18
- package/dist/packages/internal/fill-bake/dist/index.js +9 -3
- package/dist/packages/internal/fill-bake/dist/item-merge.js +113 -0
- package/dist/packages/internal/fill-bake/dist/item-overrides.js +1 -0
- package/dist/packages/internal/fill-bake/dist/linked-merge.js +38 -0
- package/dist/packages/internal/fill-bake/dist/list-grammar.js +174 -0
- package/dist/packages/internal/fill-bake/dist/stored-row-reader.js +1 -0
- package/dist/packages/internal/fill-vocabulary/dist/holder.js +51 -0
- package/dist/packages/internal/fill-vocabulary/dist/resolved-decision.js +363 -0
- package/dist/packages/internal/fill-vocabulary/dist/row-planners.js +1 -23
- package/dist/packages/internal/fill-vocabulary/dist/verbs.js +60 -0
- package/dist/packages/internal/format-geometry/dist/capture.js +11 -147
- package/dist/packages/internal/format-geometry/dist/format.d.ts +8 -2
- package/dist/packages/internal/format-geometry/dist/geometry.js +13 -2
- package/dist/packages/internal/image-ladder/dist/encode.js +27 -14
- package/dist/packages/internal/image-ladder/dist/index.js +3 -3
- package/dist/packages/internal/image-ladder/dist/policy.js +28 -53
- package/dist/packages/internal/island-runtime/dist/capture-holds.js +1 -1
- package/dist/packages/internal/island-runtime/dist/contract.d.ts +3 -12
- package/dist/packages/internal/island-runtime/dist/contract.js +2 -2
- package/dist/packages/internal/island-runtime/dist/index.d.ts +3 -7
- package/dist/packages/internal/island-runtime/dist/index.js +2 -1
- package/dist/packages/internal/island-runtime/dist/viewport-schedule.js +1 -1
- package/dist/packages/internal/media-resolution/dist/crop-key.js +7 -0
- package/dist/packages/internal/media-resolution/dist/document.js +63 -0
- package/dist/packages/internal/media-resolution/dist/image.js +226 -0
- package/dist/packages/internal/media-resolution/dist/index.js +11 -0
- package/dist/packages/internal/media-resolution/dist/list.js +112 -0
- package/dist/packages/internal/media-resolution/dist/media.js +59 -0
- package/dist/packages/internal/media-resolution/dist/records.js +7 -0
- package/dist/packages/internal/media-resolution/dist/slot.js +37 -0
- package/dist/packages/internal/media-resolution/dist/store.js +19 -0
- package/dist/packages/internal/media-resolution/dist/tour.js +59 -0
- package/dist/packages/internal/media-resolution/dist/video.js +93 -0
- package/dist/packages/internal/property-facts/dist/registry-facts.js +245 -0
- package/dist/packages/internal/section-contracts/dist/cap-contract.js +8 -17
- package/dist/packages/internal/section-contracts/dist/catalog.js +34 -14
- package/dist/packages/internal/section-contracts/dist/count-bounds-contract.js +11 -2
- package/dist/packages/internal/section-contracts/dist/directly-linked-list-contract.js +21 -0
- package/dist/packages/internal/section-contracts/dist/fill-options.js +12 -0
- package/dist/packages/internal/section-contracts/dist/fill-spec-view.js +1 -5
- package/dist/packages/internal/section-contracts/dist/fill-verb-contract.js +95 -0
- package/dist/packages/internal/section-contracts/dist/grammar-scope.js +154 -0
- package/dist/packages/internal/section-contracts/dist/index.js +7 -2
- package/dist/packages/internal/section-contracts/dist/list-fill-contract.js +2 -36
- package/dist/packages/internal/section-contracts/dist/list-scalar-contract.js +12 -21
- package/dist/packages/internal/section-contracts/dist/list-source-contract.js +46 -0
- package/dist/packages/internal/section-contracts/dist/nested-slot-contract.js +11 -2
- package/dist/packages/internal/section-contracts/dist/object-contract.js +92 -0
- package/dist/packages/internal/section-contracts/dist/poi-curation-contract.js +9 -5
- package/dist/packages/internal/section-contracts/dist/reads-acyclic-contract.js +63 -0
- package/dist/packages/internal/section-contracts/dist/row-fill-contract.js +6 -17
- package/dist/packages/internal/section-contracts/dist/row-rule-contract.js +80 -0
- package/dist/packages/internal/section-contracts/dist/row-source-contract.js +67 -0
- package/dist/packages/internal/section-contracts/dist/runners.js +77 -18
- package/dist/packages/internal/section-contracts/dist/select-contract.js +0 -8
- package/dist/packages/internal/section-contracts/dist/source-contract.js +89 -95
- package/dist/packages/internal/section-contracts/dist/sources-contract.js +47 -17
- package/dist/packages/internal/section-contracts/dist/table-member.js +16 -0
- package/dist/packages/internal/section-contracts/dist/writer-reads-fact-contract.js +39 -23
- package/dist/packages/internal/source-resolution/dist/index.js +113 -73
- package/dist/packages/internal/static-assets-binding/dist/section-asset-url.js +16 -0
- package/dist/packages/internal/street-address/dist/index.js +72 -0
- package/dist/packages/internal/template-artifact/dist/asset-mime.js +30 -0
- package/dist/packages/internal/template-artifact/dist/layout.js +1 -2
- package/dist/packages/internal/template-artifact/dist/manifest.js +1 -1
- package/dist/packages/internal/template-artifact/dist/verify.js +12 -29
- package/dist/packages/internal/template-contracts/dist/address-value.d.ts +52 -0
- package/dist/packages/internal/template-contracts/dist/address-value.js +90 -0
- package/dist/packages/internal/template-contracts/dist/comparable-address.js +19 -0
- package/dist/{schema → packages/internal/template-contracts/dist}/compute.d.ts +29 -6
- package/dist/{schema → packages/internal/template-contracts/dist}/compute.js +11 -13
- package/dist/packages/internal/template-contracts/dist/content-pick.js +51 -0
- package/dist/packages/internal/template-contracts/dist/decision-fills.js +81 -0
- package/dist/packages/internal/template-contracts/dist/display-overrides.js +7 -0
- package/dist/packages/internal/template-contracts/dist/document-value.d.ts +16 -5
- package/dist/packages/internal/template-contracts/dist/fact-compute.d.ts +24 -0
- package/dist/packages/internal/template-contracts/dist/fact-compute.js +30 -0
- package/dist/packages/internal/template-contracts/dist/fact-keys.d.ts +44 -0
- package/dist/packages/internal/template-contracts/dist/fact-keys.js +88 -0
- package/dist/packages/internal/template-contracts/dist/fact-registry.d.ts +290 -0
- package/dist/packages/internal/template-contracts/dist/fact-registry.js +598 -0
- package/dist/packages/internal/template-contracts/dist/facts-view.d.ts +10 -24
- package/dist/packages/internal/template-contracts/dist/fill-builders.d.ts +157 -0
- package/dist/packages/internal/template-contracts/dist/fill-builders.js +79 -0
- package/dist/packages/internal/template-contracts/dist/fill-declaration.d.ts +14 -0
- package/dist/{schema → packages/internal/template-contracts/dist}/fill-declaration.js +6 -5
- package/dist/packages/internal/template-contracts/dist/fill-spec-schema.js +93 -0
- package/dist/packages/internal/template-contracts/dist/fill-spec.js +291 -0
- package/dist/packages/internal/template-contracts/dist/findable.js +8 -0
- package/dist/packages/internal/template-contracts/dist/floor-presets.js +13 -0
- package/dist/packages/internal/template-contracts/dist/icon-names.d.ts +5 -0
- package/dist/packages/internal/template-contracts/dist/icon-names.js +70 -0
- package/dist/packages/internal/template-contracts/dist/legacy-facts.js +167 -0
- package/dist/packages/internal/template-contracts/dist/list-editing.js +93 -0
- package/dist/packages/internal/template-contracts/dist/list.d.ts +215 -0
- package/dist/packages/internal/template-contracts/dist/list.js +258 -0
- package/dist/packages/internal/template-contracts/dist/markers.d.ts +2 -2
- package/dist/packages/internal/template-contracts/dist/media-candidates.js +38 -0
- package/dist/packages/internal/template-contracts/dist/mirror-kind.js +48 -0
- package/dist/packages/internal/template-contracts/dist/number-display.js +10 -0
- package/dist/packages/internal/template-contracts/dist/object.d.ts +89 -0
- package/dist/packages/internal/template-contracts/dist/object.js +131 -0
- package/dist/packages/internal/template-contracts/dist/poi-row.d.ts +1 -1
- package/dist/packages/internal/template-contracts/dist/primitive-descriptors.d.ts +1 -0
- package/dist/packages/internal/template-contracts/dist/primitive-descriptors.js +162 -0
- package/dist/packages/internal/template-contracts/dist/process-builders.d.ts +77 -0
- package/dist/packages/internal/template-contracts/dist/process-builders.js +73 -0
- package/dist/packages/internal/template-contracts/dist/read-graph.js +240 -0
- package/dist/packages/internal/template-contracts/dist/ref-locate.js +176 -0
- package/dist/packages/internal/template-contracts/dist/registry-aliases.js +77 -0
- package/dist/packages/internal/template-contracts/dist/registry-facts.d.ts +270 -0
- package/dist/packages/internal/template-contracts/dist/registry-facts.js +202 -0
- package/dist/packages/internal/template-contracts/dist/registry-shape.d.ts +41 -0
- package/dist/packages/internal/template-contracts/dist/registry-shape.js +135 -0
- package/dist/packages/internal/template-contracts/dist/resolve-scope.js +103 -0
- package/dist/packages/internal/{image-descriptor/dist/index.d.ts → template-contracts/dist/responsive-image.d.ts} +1 -1
- package/dist/packages/internal/template-contracts/dist/row-planners.js +29 -0
- package/dist/packages/internal/template-contracts/dist/row-ref.d.ts +127 -0
- package/dist/packages/internal/template-contracts/dist/row-ref.js +101 -0
- package/dist/packages/internal/template-contracts/dist/row-sources.js +41 -5
- package/dist/packages/internal/template-contracts/dist/row.d.ts +155 -0
- package/dist/packages/internal/template-contracts/dist/row.js +84 -0
- package/dist/packages/internal/template-contracts/dist/runtime-values.d.ts +97 -0
- package/dist/packages/internal/template-contracts/dist/slot-builders.d.ts +782 -0
- package/dist/packages/internal/template-contracts/dist/slot-builders.js +233 -0
- package/dist/packages/internal/template-contracts/dist/slot-catalog.d.ts +233 -405
- package/dist/packages/internal/template-contracts/dist/slot-catalog.js +331 -227
- package/dist/packages/internal/template-contracts/dist/slot-overrides.js +21 -0
- package/dist/packages/internal/template-contracts/dist/slot-ref.d.ts +155 -0
- package/dist/packages/internal/template-contracts/dist/slot-ref.js +76 -0
- package/dist/packages/internal/template-contracts/dist/slot-types.d.ts +163 -0
- package/dist/packages/internal/template-contracts/dist/slot-types.js +139 -0
- package/dist/packages/internal/template-contracts/dist/slot-value.d.ts +95 -0
- package/dist/packages/internal/template-contracts/dist/table-members.d.ts +25 -0
- package/dist/packages/internal/template-contracts/dist/table-members.js +90 -0
- package/dist/packages/internal/template-contracts/dist/tour-value.d.ts +5 -5
- package/dist/packages/internal/template-contracts/dist/value-contracts.js +15 -0
- package/dist/packages/internal/template-contracts/dist/video-value.d.ts +2 -2
- package/dist/platform/fs.js +5 -2
- package/dist/playground/canvas-island-host.d.ts +11 -0
- package/dist/playground/canvas-island-host.js +29 -0
- package/dist/playground/canvas-markers.js +14 -0
- package/dist/playground-island-runtime.d.ts +3 -2
- package/dist/playground-island-runtime.js +3 -2
- package/dist/primitives/FrameShell.js +0 -1
- package/dist/primitives/Image.d.ts +5 -4
- package/dist/primitives/Tour.d.ts +1 -1
- package/dist/primitives/Video.d.ts +2 -2
- package/dist/primitives/index.d.ts +2 -3
- package/dist/primitives/picture-sources.js +2 -2
- package/dist/primitives/probe-context.js +2 -12
- package/dist/primitives/slot-address.d.ts +44 -0
- package/dist/primitives/slot-address.js +19 -0
- package/dist/primitives/slot-bindings.d.ts +65 -26
- package/dist/primitives/slot-bindings.js +104 -46
- package/dist/primitives/slot-document.d.ts +23 -18
- package/dist/primitives/slot-document.js +39 -14
- package/dist/primitives/slot-download.d.ts +39 -0
- package/dist/primitives/slot-download.js +39 -0
- package/dist/primitives/slot-list.d.ts +3 -3
- package/dist/primitives/slot-list.js +0 -2
- package/dist/primitives/slot-media.d.ts +24 -5
- package/dist/primitives/slot-media.js +6 -9
- package/dist/primitives/slot-object.d.ts +49 -0
- package/dist/primitives/slot-object.js +20 -0
- package/dist/primitives/slot-scalar.d.ts +27 -2
- package/dist/primitives/slot-scalar.js +50 -7
- package/dist/primitives/slot.d.ts +13 -6
- package/dist/primitives/slot.js +10 -3
- package/dist/rules/registry.js +25 -30
- package/dist/schema/define-schema.d.ts +4 -8
- package/dist/schema/define-schema.js +178 -79
- package/dist/schema/extract-fill-spec.js +2 -153
- package/dist/schema/fact.d.ts +57 -15
- package/dist/schema/fact.js +42 -7
- package/dist/schema/facts-view.d.ts +14 -0
- package/dist/schema/format.d.ts +1 -0
- package/dist/schema/format.js +2 -1
- package/dist/schema/manifest.js +24 -0
- package/dist/schema/property-facts.d.ts +8 -12
- package/dist/schema/section-props.d.ts +2 -66
- package/dist/schema/section-schema.d.ts +8 -11
- package/dist/schema/section-schema.js +27 -10
- package/dist/section/declare.d.ts +18 -7
- package/dist/section/declare.js +18 -5
- package/dist/section/materialize.js +3 -8
- package/dist/section/section.d.ts +12 -8
- package/dist/section/section.js +1 -1
- package/dist/template/logic-shim.d.ts +14 -7
- package/dist/template/logic-shim.js +14 -7
- package/dist/template/materialize.js +17 -18
- package/dist/template/mirror-types.js +30 -31
- package/dist/template/section-reads.js +85 -9
- package/guide/reference/cli/INDEX.md +2 -2
- package/guide/reference/cli/build.md +1 -1
- package/guide/reference/cli/check.md +1 -1
- package/guide/reference/cli/dev.md +1 -1
- package/guide/reference/cli/property-add.md +1 -1
- package/guide/reference/cli/property-list.md +2 -2
- package/guide/reference/cli/publish.md +1 -1
- package/guide/reference/cli/upgrade.md +1 -1
- package/guide/reference/rules/INDEX.md +29 -25
- package/package.json +9 -8
- package/dist/artifact/playground/client/assets/index-CckS2ONm.css +0 -1
- package/dist/artifact/playground/client/assets/index-Dn_xT16-.js +0 -1101
- package/dist/fixtures/canonical.d.ts +0 -166
- package/dist/fixtures/media.js +0 -163
- package/dist/packages/internal/fill-vocabulary/dist/origination.js +0 -51
- package/dist/packages/internal/fill-vocabulary/dist/row-reads.js +0 -15
- package/dist/packages/internal/format-geometry/dist/index.js +0 -4
- package/dist/packages/internal/property-facts/dist/derive-facts-view.js +0 -10
- package/dist/packages/internal/property-facts/dist/index.js +0 -3
- package/dist/packages/internal/property-facts/dist/property-facts-Cy7-O4p0.js +0 -189
- package/dist/packages/internal/section-contracts/dist/group-contract.js +0 -19
- package/dist/packages/internal/section-contracts/dist/record-collection-contract.js +0 -24
- package/dist/packages/internal/template-contracts/dist/collection-editing.js +0 -22
- package/dist/packages/internal/template-contracts/dist/facts-view.js +0 -104
- package/dist/packages/internal/template-contracts/dist/select-key.js +0 -7
- package/dist/primitives/slot-group.d.ts +0 -40
- package/dist/primitives/slot-group.js +0 -36
- package/dist/schema/extract-used-facts.js +0 -34
- package/dist/schema/fill-builders.d.ts +0 -115
- package/dist/schema/fill-builders.js +0 -25
- package/dist/schema/fill-declaration.d.ts +0 -14
- package/dist/schema/fill-spec.js +0 -98
- package/dist/schema/layout.d.ts +0 -30
- package/dist/schema/layout.js +0 -14
- package/dist/schema/row-fields.js +0 -88
- package/dist/schema/row-ref.d.ts +0 -59
- package/dist/schema/row-ref.js +0 -28
- package/dist/schema/runtime-values.d.ts +0 -71
- package/dist/schema/section-nav.js +0 -14
- package/dist/schema/slot-builders.d.ts +0 -633
- package/dist/schema/slot-builders.js +0 -177
- package/dist/schema/slot-ref.d.ts +0 -79
- package/dist/schema/slot-ref.js +0 -41
- package/dist/schema/slot-types.d.ts +0 -111
- package/dist/schema/slot-types.js +0 -214
- package/dist/section/groups.js +0 -68
|
@@ -0,0 +1,923 @@
|
|
|
1
|
+
// 9.0.0 — one list grammar: a list is a table whose `source` is any list and whose
|
|
2
|
+
// `item` is a function of each element; `object()` groups, `row()` reads, and every fill
|
|
3
|
+
// declares what it does.
|
|
4
|
+
//
|
|
5
|
+
// What this codemod does, in every `.ts` and `.tsx` under the workspace:
|
|
6
|
+
//
|
|
7
|
+
// - Every `fill({…})` becomes the verb its slot implies, and a pick names its
|
|
8
|
+
// candidates in `of` wherever it sits: `fill.write` on a text or a number,
|
|
9
|
+
// `fill.pick({ of: fact.photos })` on an image (`fact.videos`, `fact.tours`,
|
|
10
|
+
// `fact.documents` on a video, a tour, a document — the pool the platform picked
|
|
11
|
+
// from), `fill.pick({ of: <its options> })` on a select; on a list the verb its item
|
|
12
|
+
// implies — `fill.write` for a list of text (the item stays, the kind every written
|
|
13
|
+
// element is), `fill.pick({ of: fact.photos })` for a list of images (videos, tours,
|
|
14
|
+
// documents likewise), `fill.pick({ of: [<options>] })` for a list of selects,
|
|
15
|
+
// `fill.find` for a list of places — which reads `fact.document_notes` too, since the
|
|
16
|
+
// documents name places only to a find that reads them.
|
|
17
|
+
// - A list of places keeps its `none`: a find that admits nothing says so.
|
|
18
|
+
// - A fill's `count` is dropped; its ceiling becomes the list's `max` where the list
|
|
19
|
+
// declared none.
|
|
20
|
+
// - A list's and an item's `unique` are dropped — a pick is unique by default — and so
|
|
21
|
+
// is a list's `writeback`, which a list sourced by a table no longer declares.
|
|
22
|
+
// - A `fields` list becomes an item function returning an `object()`:
|
|
23
|
+
// · sourced by a table, a compute or a slot, the callback's row parameter becomes a
|
|
24
|
+
// row declared from the element — `const unit = row({ label: "Unit", source: el })`
|
|
25
|
+
// — kept as a field of the object, so every `unit.<member>` read stays as written;
|
|
26
|
+
// · filled, its FIRST field becomes the element the list's fill writes or picks —
|
|
27
|
+
// the list's `fill.write`, the field's kind being what it writes, or its
|
|
28
|
+
// `fill.pick({ of: <its options> })` — and the field binds the element
|
|
29
|
+
// (`source: name`), so a later field reading it
|
|
30
|
+
// by its const reads the element — a const the callback declares, returned by
|
|
31
|
+
// shorthand (`photo`) or under a key (`tour: tourField`), names the element;
|
|
32
|
+
// · `title: "<field>"` makes that text field the object's `label`: the field stays a
|
|
33
|
+
// field, declared as a const in the item function, and the label names it by
|
|
34
|
+
// that const — `const name = text.short(…); return object({ label: name, fields:
|
|
35
|
+
// { name, … } })` — so the card's header follows the field the user edits. With
|
|
36
|
+
// no `title`, a text field keyed `label` is the title, under the key `title`; any
|
|
37
|
+
// other field keyed `label` (a select, or a field beside the title) takes the key
|
|
38
|
+
// `title` (or the next free one) too, and no label names it.
|
|
39
|
+
// - A section's `layout` is dropped — an object lists its fields in the order it
|
|
40
|
+
// declares them — and `group` leaves the import once nothing calls it.
|
|
41
|
+
// - An item's `label` is dropped, the item being a value slot (`item: image()`) or the
|
|
42
|
+
// kind a filled list writes (`item: text.short({ cap: 60 })`); a `select` keeps its,
|
|
43
|
+
// which is what makes it a slot rather than a display entry.
|
|
44
|
+
// - Imports `object` and `row` (and `fact`, for a pool `of`) where a rewrite uses them.
|
|
45
|
+
//
|
|
46
|
+
// What it leaves to you, reported per file as `Manual:` lines:
|
|
47
|
+
//
|
|
48
|
+
// - A fill's own `cap`, which a verb no longer takes: lower the kind's `cap`, or say it
|
|
49
|
+
// in the brief.
|
|
50
|
+
// - A filled list with no `max` whose fill set no count ceiling: a filled list owes
|
|
51
|
+
// one.
|
|
52
|
+
// - The first field's own brief on a filled `fields` list: the list's fill now writes
|
|
53
|
+
// or picks that field, so fold what it asked into the list's brief.
|
|
54
|
+
// - A field reading an earlier field other than the first by its const: an item field
|
|
55
|
+
// reads the element or a row's member — read what that field read instead.
|
|
56
|
+
// - A `group(…)` card the dropped `layout` held: move its slots into an `object()`.
|
|
57
|
+
// - A field keyed `label`, now keyed `title` (the key is the object's own): a render
|
|
58
|
+
// reading the item's `.label` reads `.title`.
|
|
59
|
+
// - A list of places whose `reads` the codemod cannot see into: make sure they hold
|
|
60
|
+
// `fact.document_notes`.
|
|
61
|
+
//
|
|
62
|
+
// A table read by position (`fact.property.contacts[0].phone`) is the `table-rows` step's.
|
|
63
|
+
|
|
64
|
+
import { readdir, readFile, writeFile } from "node:fs/promises";
|
|
65
|
+
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
66
|
+
|
|
67
|
+
import ts from "typescript";
|
|
68
|
+
|
|
69
|
+
const CONCRETE = "@homepages/concrete";
|
|
70
|
+
|
|
71
|
+
const EXTENSIONS = [".ts", ".tsx"];
|
|
72
|
+
const SKIP_DIRS = new Set(["node_modules", ".concrete", ".git", "dist", "build"]);
|
|
73
|
+
|
|
74
|
+
/** The value builders whose fill is a write. */
|
|
75
|
+
const WRITTEN = new Set(["text.short", "text.medium", "text.long", "number"]);
|
|
76
|
+
/** The value builders whose fill is a pick. */
|
|
77
|
+
const PICKED = new Set(["select", "image", "video", "tour", "document"]);
|
|
78
|
+
/** The pool an asset builder's elements come from. */
|
|
79
|
+
const POOL = { image: "photos", video: "videos", tour: "tours", document: "documents" };
|
|
80
|
+
/** Every value builder. */
|
|
81
|
+
const VALUE_BUILDERS = new Set([...WRITTEN, ...PICKED, "url", "poi"]);
|
|
82
|
+
|
|
83
|
+
/** `text` — one builder call — binding `param`: its options gain `source: <param>`. */
|
|
84
|
+
function withSource(text, param) {
|
|
85
|
+
if (/\(\s*\)\s*$/.test(text)) return text.replace(/\(\s*\)\s*$/, `({ source: ${param} })`);
|
|
86
|
+
return text.replace(/,?\s*\}\)\s*$/, `, source: ${param} })`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function sourceFiles(root) {
|
|
90
|
+
const found = [];
|
|
91
|
+
async function walk(dir) {
|
|
92
|
+
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
93
|
+
if (entry.isDirectory()) {
|
|
94
|
+
if (!SKIP_DIRS.has(entry.name)) await walk(join(dir, entry.name));
|
|
95
|
+
} else if (EXTENSIONS.some((ext) => entry.name.endsWith(ext)) && !entry.name.endsWith(".d.ts")) {
|
|
96
|
+
found.push(join(dir, entry.name));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
await walk(root);
|
|
101
|
+
return found.sort();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function calleeName(call) {
|
|
105
|
+
const callee = call.expression;
|
|
106
|
+
if (ts.isIdentifier(callee)) return callee.text;
|
|
107
|
+
if (ts.isPropertyAccessExpression(callee) && ts.isIdentifier(callee.expression)) return `${callee.expression.text}.${callee.name.text}`;
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function lineIndent(sf, pos) {
|
|
112
|
+
const { line } = sf.getLineAndCharacterOfPosition(pos);
|
|
113
|
+
const start = sf.getPositionOfLineAndCharacter(line, 0);
|
|
114
|
+
return /^[ \t]*/.exec(sf.text.slice(start))[0];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Moves every continuation line of `text` from `from` indentation to `to`. */
|
|
118
|
+
function reindent(text, from, to) {
|
|
119
|
+
return text
|
|
120
|
+
.split("\n")
|
|
121
|
+
.map((line, i) => (i > 0 && line.startsWith(from) ? to + line.slice(from.length) : line))
|
|
122
|
+
.join("\n");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The fact a find must read for the documents to name its places. */
|
|
126
|
+
const NOTES = "fact.document_notes";
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* The text of the array a `const <name> = [ … ]` in `text` declares, or `undefined` — how
|
|
130
|
+
* a find's `reads` held in a const (`reads: PROPERTY`) is seen into.
|
|
131
|
+
*/
|
|
132
|
+
function declaredArray(text, name) {
|
|
133
|
+
const match = new RegExp(`(?:const|let)\\s+${name}\\b[^=]*=\\s*(\\[[^\\]]*\\])`).exec(text);
|
|
134
|
+
return match?.[1];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** The object literal's properties by name — `undefined` when any member is not a plain one. */
|
|
138
|
+
function membersOf(node) {
|
|
139
|
+
if (node === undefined || !ts.isObjectLiteralExpression(node)) return undefined;
|
|
140
|
+
const members = new Map();
|
|
141
|
+
for (const property of node.properties) {
|
|
142
|
+
if (ts.isPropertyAssignment(property) && (ts.isIdentifier(property.name) || ts.isStringLiteral(property.name))) {
|
|
143
|
+
members.set(property.name.text, property);
|
|
144
|
+
} else if (ts.isShorthandPropertyAssignment(property)) {
|
|
145
|
+
members.set(property.name.text, property);
|
|
146
|
+
} else {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return members;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const valueOf = (property) => (property === undefined ? undefined : ts.isShorthandPropertyAssignment(property) ? property.name : property.initializer);
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The edits one file collects, innermost first. An outer rewrite TAKES the text of the
|
|
157
|
+
* span it replaces with the inner edits already applied, so a fill inside a `fields`
|
|
158
|
+
* list is rewritten once and carried into the item function.
|
|
159
|
+
*/
|
|
160
|
+
class Edits {
|
|
161
|
+
constructor(text) {
|
|
162
|
+
this.text = text;
|
|
163
|
+
this.list = [];
|
|
164
|
+
}
|
|
165
|
+
add(start, end, text) {
|
|
166
|
+
this.list.push({ start, end, text });
|
|
167
|
+
}
|
|
168
|
+
/** The text of [start, end) with every edit inside it applied — those edits absorbed. */
|
|
169
|
+
take(start, end) {
|
|
170
|
+
const inside = this.list.filter((e) => e.start >= start && e.end <= end && !(e.start === e.end && (e.start === start || e.start === end)));
|
|
171
|
+
this.list = this.list.filter((e) => !inside.includes(e));
|
|
172
|
+
inside.sort((a, b) => b.start - a.start || b.end - a.end);
|
|
173
|
+
let out = this.text.slice(start, end);
|
|
174
|
+
for (const e of inside) out = out.slice(0, e.start - start) + e.text + out.slice(e.end - start);
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Drops every edit inside [start, end) — the span is about to be replaced whole. An
|
|
179
|
+
* insertion AT either edge is a neighbour's (`, max: 40` after the `label` a removed
|
|
180
|
+
* `title` follows), so it stays, as `take` leaves it.
|
|
181
|
+
*/
|
|
182
|
+
clear(start, end) {
|
|
183
|
+
this.list = this.list.filter((e) => !(e.start >= start && e.end <= end) || (e.start === e.end && (e.start === start || e.start === end)));
|
|
184
|
+
}
|
|
185
|
+
apply() {
|
|
186
|
+
const sorted = [...this.list].sort((a, b) => b.start - a.start || b.end - a.end);
|
|
187
|
+
let out = this.text;
|
|
188
|
+
for (const e of sorted) out = out.slice(0, e.start) + e.text + out.slice(e.end);
|
|
189
|
+
return out;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The comments written above `node`, one entry per line, each line's own indent dropped
|
|
195
|
+
* (a block comment's ` *` lines keep their one-space lead) — what a field carries when
|
|
196
|
+
* the rewrite moves it, or none.
|
|
197
|
+
*/
|
|
198
|
+
function leadingCommentLines(sf, node) {
|
|
199
|
+
const ranges = ts.getLeadingCommentRanges(sf.text, node.getFullStart()) ?? [];
|
|
200
|
+
return ranges.flatMap((range) =>
|
|
201
|
+
sf.text
|
|
202
|
+
.slice(range.pos, range.end)
|
|
203
|
+
.split("\n")
|
|
204
|
+
.map((line) => line.trim().replace(/^\*/, " *")),
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** `text` with the comment lines above it, each on its own line at `indent`. */
|
|
209
|
+
const withComments = (lines, text, indent) => [...lines, text].join(`\n${indent}`);
|
|
210
|
+
|
|
211
|
+
/** Removes `property` from its object literal, its separating comma and leading space with it. */
|
|
212
|
+
function removeProperty(edits, object, property, sf) {
|
|
213
|
+
const props = object.properties;
|
|
214
|
+
const i = props.indexOf(property);
|
|
215
|
+
const start = i > 0 ? props[i - 1].getEnd() : property.getStart(sf);
|
|
216
|
+
let end = property.getEnd();
|
|
217
|
+
if (i === 0) {
|
|
218
|
+
// The first of several: remove through the comma and the space before the next.
|
|
219
|
+
if (props.length > 1) end = props[1].getStart(sf);
|
|
220
|
+
else if (sf.text[end] === ",") end += 1;
|
|
221
|
+
}
|
|
222
|
+
edits.clear(start, end);
|
|
223
|
+
edits.add(start, end, "");
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const parse = (text, fileName) =>
|
|
227
|
+
ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, fileName.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS);
|
|
228
|
+
|
|
229
|
+
/** `key`, a list's slot key, as the name of one of its rows: `cards` → `card`. */
|
|
230
|
+
function singular(key) {
|
|
231
|
+
if (/ies$/.test(key)) return key.replace(/ies$/, "y");
|
|
232
|
+
if (/[^s]s$/.test(key)) return key.slice(0, -1);
|
|
233
|
+
return `${key}Row`;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* A `fields` callback whose parameter is named `row` would become `const row = row(…)`,
|
|
238
|
+
* shadowing the builder it calls. Before the rewrite, such a parameter and every read of
|
|
239
|
+
* it take the list's row name instead — `cards: list({ fields: (row) => … })` reads
|
|
240
|
+
* `card` — or, where the module or the callback already names it, that name plus `Row`.
|
|
241
|
+
*/
|
|
242
|
+
function renameRowParameters(text, fileName) {
|
|
243
|
+
const sf = parse(text, fileName);
|
|
244
|
+
// Module-scope names: imports and top-level declarations. A name another function
|
|
245
|
+
// binds (a render callback's `(card) =>`) is out of the callback's scope.
|
|
246
|
+
const moduleNames = new Set();
|
|
247
|
+
const collectIdentifiers = (node, into) => {
|
|
248
|
+
if (ts.isIdentifier(node)) into.add(node.text);
|
|
249
|
+
ts.forEachChild(node, (child) => collectIdentifiers(child, into));
|
|
250
|
+
};
|
|
251
|
+
for (const statement of sf.statements) {
|
|
252
|
+
if (ts.isImportDeclaration(statement)) collectIdentifiers(statement, moduleNames);
|
|
253
|
+
else if (ts.isVariableStatement(statement)) for (const d of statement.declarationList.declarations) collectIdentifiers(d.name, moduleNames);
|
|
254
|
+
else if ((ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement)) && statement.name !== undefined) moduleNames.add(statement.name.text);
|
|
255
|
+
}
|
|
256
|
+
const edits = new Edits(text);
|
|
257
|
+
const visit = (node) => {
|
|
258
|
+
ts.forEachChild(node, visit);
|
|
259
|
+
if (!ts.isPropertyAssignment(node) || !ts.isIdentifier(node.name) || node.name.text !== "fields") return;
|
|
260
|
+
const fn = node.initializer;
|
|
261
|
+
if (!ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn)) return;
|
|
262
|
+
const parameter = fn.parameters[0];
|
|
263
|
+
if (parameter === undefined || !ts.isIdentifier(parameter.name) || parameter.name.text !== "row") return;
|
|
264
|
+
const list = node.parent?.parent;
|
|
265
|
+
const slot = list !== undefined && ts.isCallExpression(list) ? list.parent : undefined;
|
|
266
|
+
const key = slot !== undefined && ts.isPropertyAssignment(slot) && ts.isIdentifier(slot.name) ? slot.name.text : "item";
|
|
267
|
+
const taken = new Set(moduleNames);
|
|
268
|
+
const collectReads = (node) => {
|
|
269
|
+
if (ts.isIdentifier(node)) {
|
|
270
|
+
const parent = node.parent;
|
|
271
|
+
const isName =
|
|
272
|
+
(ts.isPropertyAccessExpression(parent) && parent.name === node) ||
|
|
273
|
+
((ts.isPropertyAssignment(parent) || ts.isPropertySignature(parent)) && parent.name === node);
|
|
274
|
+
if (!isName) taken.add(node.text);
|
|
275
|
+
}
|
|
276
|
+
// The keys of the fields the callback returns: the row becomes one of them.
|
|
277
|
+
if (ts.isObjectLiteralExpression(node) && [ts.isArrowFunction, ts.isParenthesizedExpression, ts.isReturnStatement].some((is) => is(node.parent))) {
|
|
278
|
+
for (const property of node.properties) if (property.name !== undefined && ts.isIdentifier(property.name)) taken.add(property.name.text);
|
|
279
|
+
}
|
|
280
|
+
ts.forEachChild(node, collectReads);
|
|
281
|
+
};
|
|
282
|
+
collectReads(fn.body);
|
|
283
|
+
let name = singular(key);
|
|
284
|
+
if (taken.has(name)) name = `${name}Row`;
|
|
285
|
+
edits.add(parameter.name.getStart(sf), parameter.name.getEnd(), name);
|
|
286
|
+
const reads = (inner) => {
|
|
287
|
+
if (ts.isIdentifier(inner) && inner.text === "row") {
|
|
288
|
+
const parent = inner.parent;
|
|
289
|
+
const isName =
|
|
290
|
+
(ts.isPropertyAccessExpression(parent) && parent.name === inner) ||
|
|
291
|
+
((ts.isPropertyAssignment(parent) || ts.isPropertySignature(parent)) && parent.name === inner);
|
|
292
|
+
if (ts.isShorthandPropertyAssignment(parent)) edits.add(inner.getStart(sf), inner.getEnd(), `row: ${name}`);
|
|
293
|
+
else if (!isName) edits.add(inner.getStart(sf), inner.getEnd(), name);
|
|
294
|
+
}
|
|
295
|
+
ts.forEachChild(inner, reads);
|
|
296
|
+
};
|
|
297
|
+
reads(fn.body);
|
|
298
|
+
};
|
|
299
|
+
visit(sf);
|
|
300
|
+
return edits.list.length === 0 ? text : edits.apply();
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** Rewrites one file's text, or returns `null` when nothing in it is this codemod's. */
|
|
304
|
+
// `sources` holds every source file of the workspace by absolute path: a find's reads
|
|
305
|
+
// held in a const another module exports are seen into through it.
|
|
306
|
+
function rewrite(original, fileName, sources = new Map()) {
|
|
307
|
+
const text = renameRowParameters(original, fileName);
|
|
308
|
+
const sf = parse(text, fileName);
|
|
309
|
+
const imported = new Map();
|
|
310
|
+
const importDecls = [];
|
|
311
|
+
for (const statement of sf.statements) {
|
|
312
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier)) continue;
|
|
313
|
+
if (statement.moduleSpecifier.text !== CONCRETE) continue;
|
|
314
|
+
const bindings = statement.importClause?.namedBindings;
|
|
315
|
+
if (bindings === undefined || !ts.isNamedImports(bindings)) continue;
|
|
316
|
+
importDecls.push(statement);
|
|
317
|
+
for (const element of bindings.elements) imported.set(element.name.text, (element.propertyName ?? element.name).text);
|
|
318
|
+
}
|
|
319
|
+
if (importDecls.length === 0) return null;
|
|
320
|
+
|
|
321
|
+
// A builder by its framework name, or `undefined` for a call that is not one.
|
|
322
|
+
const builderOf = (call) => {
|
|
323
|
+
const name = calleeName(call);
|
|
324
|
+
if (name === undefined) return undefined;
|
|
325
|
+
const [head, tail] = name.split(".");
|
|
326
|
+
const original = imported.get(head);
|
|
327
|
+
if (original === undefined) return undefined;
|
|
328
|
+
return tail === undefined ? original : `${original}.${tail}`;
|
|
329
|
+
};
|
|
330
|
+
const isFillCall = (node) => node !== undefined && ts.isCallExpression(node) && builderOf(node) === "fill";
|
|
331
|
+
|
|
332
|
+
const edits = new Edits(text);
|
|
333
|
+
const manual = [];
|
|
334
|
+
const refused = [];
|
|
335
|
+
const uses = new Set();
|
|
336
|
+
const layouts = [];
|
|
337
|
+
// The fills a rewrite removed whole — a first field's own, which the list's fill replaced.
|
|
338
|
+
const droppedFills = new Set();
|
|
339
|
+
|
|
340
|
+
// A fill's callee becomes its verb; its `count` and `cap` leave. Returns the count's
|
|
341
|
+
// ceiling as source text, where it declared one.
|
|
342
|
+
function rewriteFill(call, verb) {
|
|
343
|
+
edits.add(call.expression.getStart(sf), call.expression.getEnd(), `fill.${verb}`);
|
|
344
|
+
const options = call.arguments[0];
|
|
345
|
+
const members = membersOf(options);
|
|
346
|
+
if (members === undefined) {
|
|
347
|
+
refused.push("a fill whose options are not an object literal — rename it to its verb by hand");
|
|
348
|
+
return undefined;
|
|
349
|
+
}
|
|
350
|
+
let ceiling;
|
|
351
|
+
const count = members.get("count");
|
|
352
|
+
if (count !== undefined) {
|
|
353
|
+
const value = valueOf(count);
|
|
354
|
+
if (ts.isNumericLiteral(value)) ceiling = value.getText(sf);
|
|
355
|
+
else if (ts.isObjectLiteralExpression(value)) ceiling = valueOf(membersOf(value)?.get("max"))?.getText(sf);
|
|
356
|
+
removeProperty(edits, options, count, sf);
|
|
357
|
+
}
|
|
358
|
+
const cap = members.get("cap");
|
|
359
|
+
if (cap !== undefined) {
|
|
360
|
+
manual.push(`a fill's \`cap: ${valueOf(cap).getText(sf)}\` was dropped — a verb writes to its kind's own \`cap\`: lower it, or say it in the brief`);
|
|
361
|
+
removeProperty(edits, options, cap, sf);
|
|
362
|
+
}
|
|
363
|
+
return ceiling;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Whether a `reads` expression holds `fact.document_notes`: `true`, `false`, or
|
|
368
|
+
* `undefined` where it cannot be seen into. A const is read where this file declares
|
|
369
|
+
* it, or where the relative module it is imported from does.
|
|
370
|
+
*/
|
|
371
|
+
function readsNotes(expr) {
|
|
372
|
+
while (expr !== undefined && (ts.isAsExpression(expr) || ts.isParenthesizedExpression(expr) || ts.isSatisfiesExpression(expr))) expr = expr.expression;
|
|
373
|
+
if (expr === undefined) return false;
|
|
374
|
+
if (ts.isArrayLiteralExpression(expr)) {
|
|
375
|
+
let unknown = false;
|
|
376
|
+
for (const element of expr.elements) {
|
|
377
|
+
const held = ts.isSpreadElement(element) ? readsNotes(element.expression) : element.getText(sf) === NOTES ? true : ts.isIdentifier(element) ? readsNotes(element) : false;
|
|
378
|
+
if (held === true) return true;
|
|
379
|
+
if (held === undefined) unknown = true;
|
|
380
|
+
}
|
|
381
|
+
return unknown ? undefined : false;
|
|
382
|
+
}
|
|
383
|
+
if (!ts.isIdentifier(expr)) return undefined;
|
|
384
|
+
const local = declaredArray(text, expr.text);
|
|
385
|
+
if (local !== undefined) return local.includes(NOTES);
|
|
386
|
+
for (const decl of sf.statements) {
|
|
387
|
+
if (!ts.isImportDeclaration(decl) || !ts.isStringLiteral(decl.moduleSpecifier) || !decl.moduleSpecifier.text.startsWith(".")) continue;
|
|
388
|
+
const bindings = decl.importClause?.namedBindings;
|
|
389
|
+
if (bindings === undefined || !ts.isNamedImports(bindings)) continue;
|
|
390
|
+
const element = bindings.elements.find((e) => e.name.text === expr.text);
|
|
391
|
+
if (element === undefined) continue;
|
|
392
|
+
const base = resolve(dirname(fileName), decl.moduleSpecifier.text.replace(/\.js$/, ""));
|
|
393
|
+
for (const candidate of [base, `${base}.ts`, `${base}.tsx`, join(base, "index.ts")]) {
|
|
394
|
+
const module = sources.get(candidate);
|
|
395
|
+
const declared = module === undefined ? undefined : declaredArray(module, (element.propertyName ?? element.name).text);
|
|
396
|
+
if (declared !== undefined) return declared.includes(NOTES);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
return undefined;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* A find reads what the model is shown, `fact.document_notes` included: before it the
|
|
404
|
+
* place search read the documents whether or not its `reads` named them, so a migrated
|
|
405
|
+
* list reads them by name.
|
|
406
|
+
*/
|
|
407
|
+
function findReadsNotes(call) {
|
|
408
|
+
const options = call.arguments[0];
|
|
409
|
+
const reads = membersOf(options)?.get("reads");
|
|
410
|
+
const value = valueOf(reads);
|
|
411
|
+
const held = value === undefined ? false : readsNotes(value);
|
|
412
|
+
if (held === true) return;
|
|
413
|
+
if (held === undefined) {
|
|
414
|
+
manual.push(`a list of places reads \`${value.getText(sf)}\` — a find shows the model the uploaded documents only through \`${NOTES}\`: make sure its \`reads\` hold it`);
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
uses.add("fact");
|
|
418
|
+
if (value === undefined) {
|
|
419
|
+
const first = options.properties[0];
|
|
420
|
+
edits.add(first.getStart(sf), first.getStart(sf), `reads: [${NOTES}], `);
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
let inner = value;
|
|
424
|
+
while (ts.isAsExpression(inner) || ts.isParenthesizedExpression(inner) || ts.isSatisfiesExpression(inner)) inner = inner.expression;
|
|
425
|
+
if (ts.isArrayLiteralExpression(inner)) {
|
|
426
|
+
const last = inner.elements[inner.elements.length - 1];
|
|
427
|
+
if (last === undefined) edits.add(inner.getStart(sf), inner.getEnd(), `[${NOTES}]`);
|
|
428
|
+
else edits.add(last.getEnd(), last.getEnd(), `, ${NOTES}`);
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
edits.add(value.getStart(sf), value.getEnd(), `[...${value.getText(sf)}, ${NOTES}]`);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/** Inserts `of: <text>` as the fill's first option. */
|
|
435
|
+
function insertOf(call, ofText) {
|
|
436
|
+
const options = call.arguments[0];
|
|
437
|
+
const first = options.properties[0];
|
|
438
|
+
edits.add(first.getStart(sf), first.getStart(sf), `of: ${ofText}, `);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** Sets a list's `max` from a fill's count ceiling, where it declares none — a filled list owes one. */
|
|
442
|
+
function ensureMax(options, members, ceiling) {
|
|
443
|
+
if (members.has("max")) return;
|
|
444
|
+
if (ceiling === undefined) {
|
|
445
|
+
const label = valueOf(members.get("label"))?.getText(sf) ?? "list";
|
|
446
|
+
manual.push(`the filled list ${label} declares no ceiling, and a filled list owes one — give it a \`max\``);
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
const label = members.get("label");
|
|
450
|
+
const at = label !== undefined ? label.getEnd() : options.properties[0].getStart(sf);
|
|
451
|
+
edits.add(at, at, label !== undefined ? `, max: ${ceiling}` : `max: ${ceiling}, `);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function valueBuilder(call, name) {
|
|
455
|
+
const options = call.arguments[0];
|
|
456
|
+
const members = membersOf(options);
|
|
457
|
+
if (members === undefined) return;
|
|
458
|
+
const source = valueOf(members.get("source"));
|
|
459
|
+
if (isFillCall(source)) {
|
|
460
|
+
const verb = WRITTEN.has(name) ? "write" : PICKED.has(name) ? "pick" : undefined;
|
|
461
|
+
if (verb === undefined) refused.push(`a \`${name}\` declares a fill, which no fill produces`);
|
|
462
|
+
else {
|
|
463
|
+
rewriteFill(source, verb);
|
|
464
|
+
if (verb === "pick") pickOf(source, name, members);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
const unique = members.get("unique");
|
|
468
|
+
if (unique !== undefined) removeProperty(edits, options, unique, sf);
|
|
469
|
+
// An item's label names nothing the editor shows; a select's makes it a slot.
|
|
470
|
+
const label = members.get("label");
|
|
471
|
+
if (label !== undefined && name !== "select" && isItem(call)) {
|
|
472
|
+
if (members.size === 1) edits.add(options.getStart(sf), options.getEnd(), "");
|
|
473
|
+
else removeProperty(edits, options, label, sf);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* A single value's pick names its candidates: the pool the platform picked an asset
|
|
479
|
+
* from, or a select's own options — what the model was shown before `of` was declared.
|
|
480
|
+
*/
|
|
481
|
+
function pickOf(fill, name, members) {
|
|
482
|
+
if (POOL[name] !== undefined) {
|
|
483
|
+
uses.add("fact");
|
|
484
|
+
insertOf(fill, `fact.${POOL[name]}`);
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
const options = valueOf(members.get("options"));
|
|
488
|
+
if (options === undefined) {
|
|
489
|
+
manual.push(`a \`${name}\`'s pick names no candidates — give it \`of\`: the keys the model may choose`);
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
insertOf(fill, options.getText(sf));
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/** Whether `call` is a list's item: `item: image(…)`, or what an item function returns. */
|
|
496
|
+
function isItem(call) {
|
|
497
|
+
let at = call.parent;
|
|
498
|
+
while (at !== undefined && ts.isParenthesizedExpression(at)) at = at.parent;
|
|
499
|
+
if (at !== undefined && ts.isArrowFunction(at) && at.body !== undefined && !ts.isBlock(at.body)) at = at.parent;
|
|
500
|
+
if (at === undefined || !ts.isPropertyAssignment(at) || !ts.isIdentifier(at.name) || at.name.text !== "item") return false;
|
|
501
|
+
const host = at.parent?.parent;
|
|
502
|
+
return host !== undefined && ts.isCallExpression(host) && builderOf(host) === "list";
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// The verb and `of` a filled list takes from the item it holds.
|
|
506
|
+
function listFillOf(item) {
|
|
507
|
+
const name = item !== undefined && ts.isCallExpression(item) ? builderOf(item) : undefined;
|
|
508
|
+
if (name === undefined) return undefined;
|
|
509
|
+
if (WRITTEN.has(name)) return { verb: "write" };
|
|
510
|
+
if (name === "select") {
|
|
511
|
+
const options = valueOf(membersOf(item.arguments[0])?.get("options"));
|
|
512
|
+
return options === undefined ? undefined : { verb: "pick", of: options.getText(sf) };
|
|
513
|
+
}
|
|
514
|
+
if (POOL[name] !== undefined) {
|
|
515
|
+
uses.add("fact");
|
|
516
|
+
return { verb: "pick", of: `fact.${POOL[name]}` };
|
|
517
|
+
}
|
|
518
|
+
if (name === "poi") return { verb: "find" };
|
|
519
|
+
return undefined;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function list(call) {
|
|
523
|
+
const options = call.arguments[0];
|
|
524
|
+
const members = membersOf(options);
|
|
525
|
+
if (members === undefined) {
|
|
526
|
+
refused.push("a `list(…)` whose options are not an object literal");
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
for (const key of ["writeback", "unique"]) {
|
|
530
|
+
const property = members.get(key);
|
|
531
|
+
if (property !== undefined) removeProperty(edits, options, property, sf);
|
|
532
|
+
}
|
|
533
|
+
if (members.has("fields")) {
|
|
534
|
+
fieldsList(options, members);
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
const source = valueOf(members.get("source"));
|
|
538
|
+
if (!isFillCall(source)) return;
|
|
539
|
+
const item = valueOf(members.get("item"));
|
|
540
|
+
const fill = listFillOf(item);
|
|
541
|
+
if (fill === undefined) {
|
|
542
|
+
refused.push("a filled list whose item is no value builder — name its verb and `of` by hand");
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
const ceiling = rewriteFill(source, fill.verb);
|
|
546
|
+
// A written item IS the kind every element is, and stays the list's `item`; a pick
|
|
547
|
+
// names its candidates; a find reads the documents.
|
|
548
|
+
if (fill.of !== undefined) insertOf(source, fill.of);
|
|
549
|
+
if (fill.verb === "find") findReadsNotes(source);
|
|
550
|
+
ensureMax(options, members, ceiling);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
// A `fields` list → an item function returning an object.
|
|
554
|
+
function fieldsList(options, members) {
|
|
555
|
+
const fieldsProperty = members.get("fields");
|
|
556
|
+
const fn = valueOf(fieldsProperty);
|
|
557
|
+
if (!ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn)) {
|
|
558
|
+
refused.push("a `fields` list whose fields are not written inline as a function");
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
let body = fn.body;
|
|
562
|
+
while (ts.isParenthesizedExpression(body)) body = body.expression;
|
|
563
|
+
const statements = ts.isBlock(body) ? [...body.statements] : [];
|
|
564
|
+
const returned = ts.isBlock(body) ? statements.pop() : undefined;
|
|
565
|
+
let fieldsObject = body;
|
|
566
|
+
if (returned !== undefined) {
|
|
567
|
+
fieldsObject = returned.expression;
|
|
568
|
+
while (fieldsObject !== undefined && ts.isParenthesizedExpression(fieldsObject)) fieldsObject = fieldsObject.expression;
|
|
569
|
+
}
|
|
570
|
+
if (fieldsObject === undefined || !ts.isObjectLiteralExpression(fieldsObject) || (returned !== undefined && !ts.isReturnStatement(returned))) {
|
|
571
|
+
refused.push("a `fields` function that does not return its fields as an object literal");
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
const entries = membersOf(fieldsObject);
|
|
575
|
+
if (entries === undefined || entries.size === 0) {
|
|
576
|
+
refused.push("a `fields` function whose fields are not plain properties");
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
// The statement declaring a const, among the function's own statements.
|
|
580
|
+
const declarationOf = (name) =>
|
|
581
|
+
statements.find(
|
|
582
|
+
(s) => ts.isVariableStatement(s) && s.declarationList.declarations.some((d) => ts.isIdentifier(d.name) && d.name.text === name),
|
|
583
|
+
);
|
|
584
|
+
// The const a field names — `photo` shorthand, or `tour: tourField` — or none.
|
|
585
|
+
const constOf = (property) => {
|
|
586
|
+
if (ts.isShorthandPropertyAssignment(property)) return property.name.text;
|
|
587
|
+
return ts.isIdentifier(property.initializer) && declarationOf(property.initializer.text) !== undefined ? property.initializer.text : undefined;
|
|
588
|
+
};
|
|
589
|
+
const initializerOf = (property) => {
|
|
590
|
+
const name = constOf(property);
|
|
591
|
+
if (name === undefined) return property.initializer;
|
|
592
|
+
return declarationOf(name)?.declarationList.declarations.find((d) => ts.isIdentifier(d.name) && d.name.text === name)?.initializer;
|
|
593
|
+
};
|
|
594
|
+
|
|
595
|
+
const titleProperty = members.get("title");
|
|
596
|
+
const titleValue = valueOf(titleProperty);
|
|
597
|
+
const titleKey = titleValue !== undefined && ts.isStringLiteral(titleValue) ? titleValue.text : undefined;
|
|
598
|
+
const titleField = titleKey === undefined ? undefined : entries.get(titleKey);
|
|
599
|
+
const titleInit = titleField === undefined ? undefined : initializerOf(titleField);
|
|
600
|
+
const titleBuilder = titleInit !== undefined && ts.isCallExpression(titleInit) ? builderOf(titleInit) : undefined;
|
|
601
|
+
// An object's label names a text field by its const: a title of any other kind stays
|
|
602
|
+
// a field, and the object takes no label. With no title, a text field keyed "label"
|
|
603
|
+
// is the title: the key is the object's own, so the field could be nothing else.
|
|
604
|
+
const labelInit = titleKey === undefined && entries.has("label") ? initializerOf(entries.get("label")) : undefined;
|
|
605
|
+
const labelBuilder = labelInit !== undefined && ts.isCallExpression(labelInit) ? builderOf(labelInit) : undefined;
|
|
606
|
+
const titled = titleBuilder?.startsWith("text.") === true ? titleKey : labelBuilder?.startsWith("text.") === true ? "label" : undefined;
|
|
607
|
+
|
|
608
|
+
// The names a const the item function declares may not take: every name the file
|
|
609
|
+
// reads or binds.
|
|
610
|
+
const taken = new Set();
|
|
611
|
+
const isNameOnly = (node) => {
|
|
612
|
+
const parent = node.parent;
|
|
613
|
+
return (ts.isPropertyAccessExpression(parent) && parent.name === node) || ((ts.isPropertyAssignment(parent) || ts.isPropertySignature(parent)) && parent.name === node);
|
|
614
|
+
};
|
|
615
|
+
const collectNames = (node) => {
|
|
616
|
+
if (ts.isIdentifier(node) && !isNameOnly(node)) taken.add(node.text);
|
|
617
|
+
ts.forEachChild(node, collectNames);
|
|
618
|
+
};
|
|
619
|
+
collectNames(sf);
|
|
620
|
+
const freeName = (base) => {
|
|
621
|
+
let name = base;
|
|
622
|
+
for (let n = 2; taken.has(name); n += 1) name = `${base}${String(n)}`;
|
|
623
|
+
taken.add(name);
|
|
624
|
+
return name;
|
|
625
|
+
};
|
|
626
|
+
const isPlainName = (name) => ts.isIdentifierText(name, ts.ScriptTarget.Latest) && ts.stringToToken(name) === undefined;
|
|
627
|
+
|
|
628
|
+
const fieldsIndent = lineIndent(sf, fieldsProperty.getStart(sf));
|
|
629
|
+
const inner = `${fieldsIndent} `;
|
|
630
|
+
const objectIndent = `${inner} `;
|
|
631
|
+
const fieldIndent = `${objectIndent} `;
|
|
632
|
+
const kept = [];
|
|
633
|
+
const keptStatements = [];
|
|
634
|
+
// What the object's label names: the title field's const, and the declaration that
|
|
635
|
+
// hoists it where the field was written in place.
|
|
636
|
+
let title;
|
|
637
|
+
let param;
|
|
638
|
+
let rowConst;
|
|
639
|
+
// The name the element went by before it gave it up to the title field, if it did.
|
|
640
|
+
let renamedFrom;
|
|
641
|
+
// The key a field is stored under: its own, but for "label" — the object's own key —
|
|
642
|
+
// which takes "title" (or the next free key), the markup following by hand. `named`
|
|
643
|
+
// says whether the object's label names the field (a text title) or not.
|
|
644
|
+
let rowKey;
|
|
645
|
+
const fieldKeyOf = (key, named) => {
|
|
646
|
+
if (key !== "label") return key;
|
|
647
|
+
let fieldKey = "title";
|
|
648
|
+
for (let n = 2; entries.has(fieldKey) || fieldKey === rowKey; n += 1) fieldKey = `title${String(n)}`;
|
|
649
|
+
const why = named ? "the key \"label\" is the object's own, and the object's label names the field" : "the key \"label\" is the object's own";
|
|
650
|
+
manual.push(`the field "label" is now the field "${fieldKey}" — ${why} — so a render reading the item's \`.label\` reads \`.${fieldKey}\``);
|
|
651
|
+
return fieldKey;
|
|
652
|
+
};
|
|
653
|
+
const titleFieldKey = (key) => fieldKeyOf(key, true);
|
|
654
|
+
// A field no label names, as `key: text` — under another key where it was keyed "label".
|
|
655
|
+
const untitledField = (key, text) => {
|
|
656
|
+
const fieldKey = fieldKeyOf(key, false);
|
|
657
|
+
return fieldKey === key ? text : `${fieldKey}: ${text.startsWith(`${key}: `) ? text.slice(key.length + 2) : text}`;
|
|
658
|
+
};
|
|
659
|
+
// Declares the title field as a const of the item function; returns the field naming it.
|
|
660
|
+
const declareTitle = (key, property, initText, comments) => {
|
|
661
|
+
const fieldKey = titleFieldKey(key);
|
|
662
|
+
const name =
|
|
663
|
+
isPlainName(fieldKey) && (!taken.has(fieldKey) || fieldKey === renamedFrom) ? fieldKey : freeName(isPlainName(fieldKey) ? `${fieldKey}Field` : "titleField");
|
|
664
|
+
taken.add(name);
|
|
665
|
+
title = { name, declaration: withComments(comments, `const ${name} = ${initText};`, inner) };
|
|
666
|
+
const keyText = fieldKey === key ? property.name.getText(sf) : fieldKey;
|
|
667
|
+
return name === fieldKey ? name : `${keyText}: ${name}`;
|
|
668
|
+
};
|
|
669
|
+
|
|
670
|
+
const source = valueOf(members.get("source"));
|
|
671
|
+
const filled = isFillCall(source);
|
|
672
|
+
const entryList = [...entries.entries()];
|
|
673
|
+
|
|
674
|
+
if (filled) {
|
|
675
|
+
// The first field is the element the list's fill writes or picks.
|
|
676
|
+
const [firstKey, firstProperty] = entryList[0];
|
|
677
|
+
const first = initializerOf(firstProperty);
|
|
678
|
+
const name = first !== undefined && ts.isCallExpression(first) ? builderOf(first) : undefined;
|
|
679
|
+
const firstMembers = name === undefined ? undefined : membersOf(first.arguments[0]);
|
|
680
|
+
if (firstMembers === undefined) {
|
|
681
|
+
refused.push("a filled `fields` list whose first field is no value builder");
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
param = constOf(firstProperty) ?? firstKey;
|
|
685
|
+
const firstConst = param;
|
|
686
|
+
// A title that is the element stays a field under its own name, so the element takes
|
|
687
|
+
// another, and every read of it in the other fields follows.
|
|
688
|
+
if (titled === firstKey && param === firstKey && isPlainName(firstKey)) {
|
|
689
|
+
renamedFrom = param;
|
|
690
|
+
param = freeName("el");
|
|
691
|
+
const skip = new Set([firstProperty, declarationOf(renamedFrom)]);
|
|
692
|
+
const renameReads = (node) => {
|
|
693
|
+
if (skip.has(node)) return;
|
|
694
|
+
if (ts.isIdentifier(node) && node.text === renamedFrom && !isNameOnly(node)) {
|
|
695
|
+
const start = node.getStart(sf);
|
|
696
|
+
const end = node.getEnd();
|
|
697
|
+
if (edits.list.some((e) => e.start < e.end && e.start <= start && e.end >= end)) {
|
|
698
|
+
manual.push(`a read of "${renamedFrom}" at line ${String(sf.getLineAndCharacterOfPosition(start).line + 1)} reads the element, now \`${param}\` — rename it by hand`);
|
|
699
|
+
} else {
|
|
700
|
+
edits.add(start, end, ts.isShorthandPropertyAssignment(node.parent) ? `${renamedFrom}: ${param}` : param);
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
ts.forEachChild(node, renameReads);
|
|
704
|
+
};
|
|
705
|
+
renameReads(fn.body);
|
|
706
|
+
}
|
|
707
|
+
const firstSource = firstMembers.get("source");
|
|
708
|
+
let verb;
|
|
709
|
+
let ofText;
|
|
710
|
+
let fieldBase;
|
|
711
|
+
if (WRITTEN.has(name)) {
|
|
712
|
+
verb = "write";
|
|
713
|
+
// The kind it writes is the field itself, its own fill gone: the field binds the
|
|
714
|
+
// element, so a write names no `of`.
|
|
715
|
+
if (firstSource !== undefined) {
|
|
716
|
+
removeProperty(edits, first.arguments[0], firstSource, sf);
|
|
717
|
+
droppedFills.add(valueOf(firstSource));
|
|
718
|
+
}
|
|
719
|
+
fieldBase = edits.take(first.getStart(sf), first.getEnd());
|
|
720
|
+
} else if (name === "select") {
|
|
721
|
+
verb = "pick";
|
|
722
|
+
ofText = valueOf(firstMembers.get("options"))?.getText(sf);
|
|
723
|
+
} else if (POOL[name] !== undefined) {
|
|
724
|
+
verb = "pick";
|
|
725
|
+
uses.add("fact");
|
|
726
|
+
ofText = `fact.${POOL[name]}`;
|
|
727
|
+
}
|
|
728
|
+
if (verb === undefined || (verb === "pick" && ofText === undefined)) {
|
|
729
|
+
refused.push(`a filled \`fields\` list whose first field is a \`${name}\`, which no fill writes or picks`);
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
const firstFill = valueOf(firstSource);
|
|
733
|
+
if (isFillCall(firstFill)) {
|
|
734
|
+
const brief = valueOf(membersOf(firstFill.arguments[0])?.get("brief"));
|
|
735
|
+
manual.push(
|
|
736
|
+
`the field "${firstKey}" is now the element the list's \`fill.${verb}\` produces, so its own brief (${brief?.getText(sf).slice(0, 60) ?? "—"}) is gone — fold it into the list's brief`,
|
|
737
|
+
);
|
|
738
|
+
}
|
|
739
|
+
// The field binds the element: its source is the item function's parameter.
|
|
740
|
+
let fieldText;
|
|
741
|
+
if (verb === "write") {
|
|
742
|
+
fieldText = withSource(fieldBase, param);
|
|
743
|
+
} else if (firstSource !== undefined) {
|
|
744
|
+
edits.clear(valueOf(firstSource).getStart(sf), valueOf(firstSource).getEnd());
|
|
745
|
+
droppedFills.add(valueOf(firstSource));
|
|
746
|
+
edits.add(valueOf(firstSource).getStart(sf), valueOf(firstSource).getEnd(), param);
|
|
747
|
+
fieldText = edits.take(first.getStart(sf), first.getEnd());
|
|
748
|
+
} else {
|
|
749
|
+
fieldText = withSource(edits.take(first.getStart(sf), first.getEnd()), param);
|
|
750
|
+
}
|
|
751
|
+
const firstComments = leadingCommentLines(sf, firstProperty);
|
|
752
|
+
if (titled === firstKey) {
|
|
753
|
+
// Declared in place of the const it was, or of the field: its comments with it.
|
|
754
|
+
const declared = declarationOf(firstConst);
|
|
755
|
+
const comments = declared === undefined ? firstComments : leadingCommentLines(sf, declared);
|
|
756
|
+
kept.push(declareTitle(firstKey, firstProperty, reindent(fieldText, lineIndent(sf, first.getStart(sf)), inner), comments));
|
|
757
|
+
} else kept.push(withComments(firstComments, untitledField(firstKey, `${firstKey}: ${fieldText}`), fieldIndent));
|
|
758
|
+
const ceiling = rewriteFill(source, verb);
|
|
759
|
+
if (ofText !== undefined) insertOf(source, ofText);
|
|
760
|
+
ensureMax(options, members, ceiling);
|
|
761
|
+
for (const statement of statements) if (statement !== declarationOf(firstConst)) keptStatements.push(statement);
|
|
762
|
+
} else {
|
|
763
|
+
keptStatements.push(...statements);
|
|
764
|
+
const parameter = fn.parameters[0];
|
|
765
|
+
if (parameter !== undefined && ts.isIdentifier(parameter.name)) {
|
|
766
|
+
param = "el";
|
|
767
|
+
const rowName = parameter.name.text;
|
|
768
|
+
rowKey = rowName;
|
|
769
|
+
const rowLabel = rowName.charAt(0).toUpperCase() + rowName.slice(1);
|
|
770
|
+
const locked = source !== undefined && !/^fact\./.test(source.getText(sf)) ? ", locked: true" : "";
|
|
771
|
+
rowConst = `const ${rowName} = row({ label: ${JSON.stringify(rowLabel)}, source: el${locked} });`;
|
|
772
|
+
kept.push(rowName);
|
|
773
|
+
uses.add("row");
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
// Every other field, carried with its rewrites; a read of an earlier field's const
|
|
778
|
+
// other than the element's has no item form.
|
|
779
|
+
const earlier = new Set(entryList.map(([key, property]) => constOf(property) ?? key));
|
|
780
|
+
for (const [index, [key, property]] of entryList.entries()) {
|
|
781
|
+
if (filled && index === 0) continue;
|
|
782
|
+
const value = ts.isShorthandPropertyAssignment(property) ? undefined : property.initializer;
|
|
783
|
+
const fieldText =
|
|
784
|
+
value === undefined ? property.name.text : reindent(edits.take(value.getStart(sf), value.getEnd()), lineIndent(sf, property.getStart(sf)), fieldIndent);
|
|
785
|
+
for (const other of earlier) {
|
|
786
|
+
if (other !== param && other !== key && new RegExp(`\\breads:\\s*\\[[^\\]]*(?<![.\\w$])${other}\\b`).test(fieldText)) {
|
|
787
|
+
manual.push(`the field "${key}" reads the field "${other}" by its const — an item field reads the element or a row's member; read what "${other}" reads instead`);
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
const comments = leadingCommentLines(sf, property);
|
|
791
|
+
if (titled === key && (value === undefined || ts.isIdentifier(value))) {
|
|
792
|
+
// A const already: the label names it, and the field stays as written but for its key.
|
|
793
|
+
const constName = value === undefined ? property.name.text : value.text;
|
|
794
|
+
const fieldKey = titleFieldKey(key);
|
|
795
|
+
title = { name: constName };
|
|
796
|
+
const field = fieldKey === key ? (value === undefined ? constName : `${key}: ${fieldText}`) : `${fieldKey}: ${constName}`;
|
|
797
|
+
kept.push(withComments(comments, field, fieldIndent));
|
|
798
|
+
} else if (titled === key) {
|
|
799
|
+
kept.push(declareTitle(key, property, reindent(fieldText, fieldIndent, inner), comments));
|
|
800
|
+
} else kept.push(withComments(comments, untitledField(key, value === undefined ? property.name.text : `${key}: ${fieldText}`), fieldIndent));
|
|
801
|
+
}
|
|
802
|
+
uses.add("object");
|
|
803
|
+
|
|
804
|
+
const objectText = [
|
|
805
|
+
`object({`,
|
|
806
|
+
...(title === undefined ? [] : [`${objectIndent}label: ${title.name},`]),
|
|
807
|
+
`${objectIndent}fields: {`,
|
|
808
|
+
...kept.map((field) => `${fieldIndent}${field},`),
|
|
809
|
+
`${objectIndent}},`,
|
|
810
|
+
`${inner}})`,
|
|
811
|
+
].join("\n");
|
|
812
|
+
const statementTexts = keptStatements.map((s) => `${inner}${reindent(edits.take(s.getStart(sf), s.getEnd()), lineIndent(sf, s.getStart(sf)), inner)}`);
|
|
813
|
+
if (title?.declaration !== undefined) statementTexts.push(`${inner}${title.declaration}`);
|
|
814
|
+
const head = param === undefined ? "()" : `(${param})`;
|
|
815
|
+
const itemText =
|
|
816
|
+
rowConst === undefined && statementTexts.length === 0
|
|
817
|
+
? `${head} =>\n${inner}${objectText}`
|
|
818
|
+
: [`${head} => {`, ...(rowConst === undefined ? [] : [`${inner}${rowConst}`]), ...statementTexts, `${inner}return ${objectText};`, `${fieldsIndent}}`].join("\n");
|
|
819
|
+
|
|
820
|
+
edits.clear(fieldsProperty.getStart(sf), fieldsProperty.getEnd());
|
|
821
|
+
edits.add(fieldsProperty.getStart(sf), fieldsProperty.getEnd(), `item: ${itemText}`);
|
|
822
|
+
if (titleProperty !== undefined) removeProperty(edits, options, titleProperty, sf);
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
// A section's `layout` goes: an object lists its fields in the order it declares them.
|
|
826
|
+
// A `group(…)` card in it was a card the author drew, which an object now is.
|
|
827
|
+
function sectionLayout(call) {
|
|
828
|
+
const options = call.arguments[0];
|
|
829
|
+
if (options === undefined || !ts.isObjectLiteralExpression(options)) return;
|
|
830
|
+
const layout = options.properties.find((p) => ts.isPropertyAssignment(p) && ts.isIdentifier(p.name) && p.name.text === "layout");
|
|
831
|
+
if (layout === undefined) return;
|
|
832
|
+
const cards = ts.isArrayLiteralExpression(layout.initializer)
|
|
833
|
+
? layout.initializer.elements.filter((e) => ts.isCallExpression(e) && builderOf(e) === "group")
|
|
834
|
+
: [];
|
|
835
|
+
for (const card of cards) {
|
|
836
|
+
const [id, label, fields] = card.arguments;
|
|
837
|
+
manual.push(
|
|
838
|
+
`the layout card ${id?.getText(sf) ?? "?"} (${label?.getText(sf) ?? "?"}) is gone with \`layout\` — if its slots ${fields?.getText(sf) ?? ""} are one card, move them into \`${id !== undefined && ts.isStringLiteral(id) ? id.text : "<id>"}: object({ label: compute(() => ${label?.getText(sf) ?? '"…"'}), fields: { … } })\` and render it in a \`<Slot.Object>\``,
|
|
839
|
+
);
|
|
840
|
+
}
|
|
841
|
+
removeProperty(edits, options, layout, sf);
|
|
842
|
+
layouts.push(layout);
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
// Innermost first, so an outer rewrite takes its inner ones' text.
|
|
846
|
+
function visit(node) {
|
|
847
|
+
ts.forEachChild(node, visit);
|
|
848
|
+
if (!ts.isCallExpression(node)) return;
|
|
849
|
+
const name = builderOf(node);
|
|
850
|
+
if (name === undefined) return;
|
|
851
|
+
if (name === "section") sectionLayout(node);
|
|
852
|
+
else if (name === "list") list(node);
|
|
853
|
+
else if (VALUE_BUILDERS.has(name)) valueBuilder(node, name);
|
|
854
|
+
}
|
|
855
|
+
visit(sf);
|
|
856
|
+
|
|
857
|
+
// A `fill(…)` no builder carried: hoisted, or under a key this codemod cannot read.
|
|
858
|
+
const fillCalls = [];
|
|
859
|
+
const findFills = (node) => {
|
|
860
|
+
if (isFillCall(node)) fillCalls.push(node);
|
|
861
|
+
ts.forEachChild(node, findFills);
|
|
862
|
+
};
|
|
863
|
+
findFills(sf);
|
|
864
|
+
for (const call of fillCalls) {
|
|
865
|
+
const renamed = edits.list.some((e) => e.start === call.expression.getStart(sf) && e.end === call.expression.getEnd());
|
|
866
|
+
const absorbed = !renamed && edits.list.some((e) => e.start <= call.getStart(sf) && e.end >= call.getEnd() && e.text.includes("fill."));
|
|
867
|
+
if (!renamed && !absorbed && !droppedFills.has(call)) refused.push(`a \`fill(…)\` outside a slot's \`source:\` at line ${String(sf.getLineAndCharacterOfPosition(call.getStart(sf)).line + 1)} — rename it to its verb by hand`);
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
// `group` leaves the import once the layouts that called it are gone.
|
|
871
|
+
const dropped = new Set();
|
|
872
|
+
const groupName = [...imported].find(([, original]) => original === "group")?.[0];
|
|
873
|
+
if (groupName !== undefined && layouts.length > 0) {
|
|
874
|
+
let called = false;
|
|
875
|
+
const findCalls = (node) => {
|
|
876
|
+
if (ts.isImportDeclaration(node) || layouts.includes(node)) return;
|
|
877
|
+
if (ts.isIdentifier(node) && node.text === groupName) called = true;
|
|
878
|
+
ts.forEachChild(node, findCalls);
|
|
879
|
+
};
|
|
880
|
+
findCalls(sf);
|
|
881
|
+
if (!called) dropped.add(groupName);
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
if (edits.list.length === 0) return { text: null, manual, refused };
|
|
885
|
+
|
|
886
|
+
// The imports the rewrites use, into the first import that brings in values.
|
|
887
|
+
const host = importDecls.find((decl) => !decl.importClause.isTypeOnly);
|
|
888
|
+
if (host !== undefined) {
|
|
889
|
+
const bindings = host.importClause.namedBindings;
|
|
890
|
+
const names = new Set([...imported.keys()]);
|
|
891
|
+
const added = [...uses].filter((name) => !names.has(name));
|
|
892
|
+
if (added.length > 0 || dropped.size > 0) {
|
|
893
|
+
const kept = bindings.elements.filter((element) => !dropped.has(element.name.text)).map((element) => element.getText(sf));
|
|
894
|
+
const all = [...kept, ...added].sort((a, b) => a.replace(/^type\s+/, "").localeCompare(b.replace(/^type\s+/, ""), "en", { sensitivity: "base" }));
|
|
895
|
+
const multiline = bindings.getText(sf).includes("\n");
|
|
896
|
+
const indent = lineIndent(sf, bindings.elements[0]?.getStart(sf) ?? bindings.getStart(sf));
|
|
897
|
+
const clause = multiline ? `{\n${all.map((k) => `${indent}${k},`).join("\n")}\n}` : `{ ${all.join(", ")} }`;
|
|
898
|
+
edits.add(bindings.getStart(sf), bindings.getEnd(), clause);
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
return { text: edits.apply(), manual, refused };
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
/** @param {string} root @returns {Promise<{rewritten: string[], notRewritten: {file: string, reason: string}[]}>} */
|
|
905
|
+
export default async function listGrammar(root) {
|
|
906
|
+
const rewritten = [];
|
|
907
|
+
const notRewritten = [];
|
|
908
|
+
const name = (file) => relative(root, file).split(sep).join("/");
|
|
909
|
+
const sources = new Map();
|
|
910
|
+
for (const file of await sourceFiles(root)) sources.set(file, await readFile(file, "utf8"));
|
|
911
|
+
for (const [file, text] of sources) {
|
|
912
|
+
if (!text.includes(CONCRETE)) continue;
|
|
913
|
+
const result = rewrite(text, file, sources);
|
|
914
|
+
if (result === null) continue;
|
|
915
|
+
if (result.text !== null && result.text !== text) {
|
|
916
|
+
await writeFile(file, result.text, "utf8");
|
|
917
|
+
rewritten.push(name(file));
|
|
918
|
+
}
|
|
919
|
+
for (const reason of new Set(result.manual)) notRewritten.push({ file: name(file), reason: `Manual: ${reason}` });
|
|
920
|
+
for (const reason of new Set(result.refused)) notRewritten.push({ file: name(file), reason });
|
|
921
|
+
}
|
|
922
|
+
return { rewritten, notRewritten };
|
|
923
|
+
}
|