@homepages/concrete 8.0.0 → 9.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +988 -0
- package/README.md +20 -9
- package/codemods/list-grammar.mjs +923 -0
- package/codemods/manifest.json +60 -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-BUmdDDX5.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/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 +263 -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 +251 -33
- package/dist/packages/internal/fill-bake/dist/compute-reads.js +365 -4
- package/dist/packages/internal/fill-bake/dist/derive-source.js +35 -21
- package/dist/packages/internal/fill-bake/dist/index.js +8 -3
- package/dist/packages/internal/fill-bake/dist/item-merge.js +119 -0
- package/dist/packages/internal/fill-bake/dist/item-overrides.js +28 -0
- package/dist/packages/internal/fill-bake/dist/linked-merge.js +40 -0
- package/dist/packages/internal/fill-bake/dist/list-grammar.js +175 -0
- package/dist/packages/internal/fill-vocabulary/dist/holder.js +51 -0
- package/dist/packages/internal/fill-vocabulary/dist/resolved-decision.js +390 -0
- package/dist/packages/internal/fill-vocabulary/dist/row-planners.js +1 -23
- package/dist/packages/internal/fill-vocabulary/dist/row-reads.js +1 -1
- package/dist/packages/internal/fill-vocabulary/dist/verbs.js +75 -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 +228 -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 +34 -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 +244 -0
- package/dist/packages/internal/section-contracts/dist/cap-contract.js +8 -17
- package/dist/packages/internal/section-contracts/dist/catalog.js +34 -10
- 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 +27 -0
- package/dist/packages/internal/section-contracts/dist/fill-options.js +12 -0
- 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 -1
- package/dist/packages/internal/section-contracts/dist/list-fill-contract.js +15 -20
- package/dist/packages/internal/section-contracts/dist/list-scalar-contract.js +12 -7
- 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 +5 -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 +7 -4
- package/dist/packages/internal/section-contracts/dist/row-rule-contract.js +83 -0
- package/dist/packages/internal/section-contracts/dist/row-source-contract.js +67 -0
- package/dist/packages/internal/section-contracts/dist/runners.js +74 -10
- package/dist/packages/internal/section-contracts/dist/select-contract.js +0 -8
- package/dist/packages/internal/section-contracts/dist/source-contract.js +103 -46
- 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 +42 -21
- package/dist/packages/internal/source-resolution/dist/index.js +113 -72
- 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 +85 -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 +158 -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 +94 -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 +250 -0
- package/dist/packages/internal/template-contracts/dist/ref-locate.js +192 -0
- package/dist/packages/internal/template-contracts/dist/registry-aliases.js +77 -0
- package/dist/packages/internal/template-contracts/dist/registry-facts.d.ts +268 -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 +133 -0
- package/dist/packages/internal/template-contracts/dist/row-ref.js +101 -0
- package/dist/packages/internal/template-contracts/dist/row-sources.js +49 -3
- 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 +243 -384
- package/dist/packages/internal/template-contracts/dist/slot-catalog.js +335 -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 +140 -0
- package/dist/packages/internal/template-contracts/dist/slot-value.d.ts +97 -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 +19 -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 +28 -24
- 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/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/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,162 @@
|
|
|
1
|
+
// 9.0.0 — a contact's address is an address: its parts and its pin, never one line of text.
|
|
2
|
+
//
|
|
3
|
+
// A fixture contact states its address as a property's does — `{ street, unit?, city,
|
|
4
|
+
// state, postal, latitude, longitude }`, or `null` when the card names none — and a
|
|
5
|
+
// string no longer compiles.
|
|
6
|
+
//
|
|
7
|
+
// What this codemod does, in every `.ts` and `.tsx` under the workspace: in each object
|
|
8
|
+
// literal that states a contact (it has an `org_name` and an `address`), an `address`
|
|
9
|
+
// written as one line of text in the shape "street, [line 2,] city, ST 12345" becomes
|
|
10
|
+
// those parts with no pin (`latitude: null, longitude: null`); an empty one becomes
|
|
11
|
+
// `null`. A `const` the contact's `address` names is rewritten where it is declared, so
|
|
12
|
+
// two contacts sharing an office keep sharing it.
|
|
13
|
+
//
|
|
14
|
+
// What it hands back, by line, as `Manual:`: a contact address in any other shape —
|
|
15
|
+
// it is left as it was, and no longer compiles until you state its parts.
|
|
16
|
+
|
|
17
|
+
import { readdir, readFile, writeFile } from "node:fs/promises";
|
|
18
|
+
import { join, relative, sep } from "node:path";
|
|
19
|
+
|
|
20
|
+
import ts from "typescript";
|
|
21
|
+
|
|
22
|
+
const EXTENSIONS = [".ts", ".tsx"];
|
|
23
|
+
const SKIP_DIRS = new Set(["node_modules", ".concrete", ".git", "dist", "build"]);
|
|
24
|
+
|
|
25
|
+
async function sourceFiles(root) {
|
|
26
|
+
const found = [];
|
|
27
|
+
async function walk(dir) {
|
|
28
|
+
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
29
|
+
if (entry.isDirectory()) {
|
|
30
|
+
if (!SKIP_DIRS.has(entry.name)) await walk(join(dir, entry.name));
|
|
31
|
+
} else if (EXTENSIONS.some((ext) => entry.name.endsWith(ext)) && !entry.name.endsWith(".d.ts")) {
|
|
32
|
+
found.push(join(dir, entry.name));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
await walk(root);
|
|
37
|
+
return found.sort();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function nameOf(property) {
|
|
41
|
+
const name = property.name;
|
|
42
|
+
return name !== undefined && (ts.isIdentifier(name) || ts.isStringLiteral(name)) ? name.text : undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function lineOf(sf, position) {
|
|
46
|
+
return sf.getLineAndCharacterOfPosition(position).line + 1;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const REGION = /^([A-Z]{2})\s+(\d{5}(?:-\d{4})?)$/;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The parts one line of text states, or `null` when it is not "street, [line 2,] city,
|
|
53
|
+
* ST 12345". Nothing is guessed: every part is a whole segment of the text.
|
|
54
|
+
*/
|
|
55
|
+
export function partsOf(line) {
|
|
56
|
+
const segments = line.split(",").map((segment) => segment.trim());
|
|
57
|
+
if (segments.length !== 3 && segments.length !== 4) return null;
|
|
58
|
+
if (segments.some((segment) => segment === "")) return null;
|
|
59
|
+
const region = REGION.exec(segments[segments.length - 1]);
|
|
60
|
+
if (region === null) return null;
|
|
61
|
+
return {
|
|
62
|
+
street: segments[0],
|
|
63
|
+
unit: segments.length === 4 ? segments[1] : null,
|
|
64
|
+
city: segments[segments.length - 2],
|
|
65
|
+
state: region[1],
|
|
66
|
+
postal: region[2],
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function literalOf(parts) {
|
|
71
|
+
const entries = [`street: ${JSON.stringify(parts.street)}`];
|
|
72
|
+
if (parts.unit !== null) entries.push(`unit: ${JSON.stringify(parts.unit)}`);
|
|
73
|
+
entries.push(`city: ${JSON.stringify(parts.city)}`, `state: ${JSON.stringify(parts.state)}`, `postal: ${JSON.stringify(parts.postal)}`);
|
|
74
|
+
entries.push("latitude: null", "longitude: null");
|
|
75
|
+
return `{ ${entries.join(", ")} }`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function isText(node) {
|
|
79
|
+
return ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The `const` declaration in `sf` an identifier names, when its initializer is text. */
|
|
83
|
+
function textConst(sf, name) {
|
|
84
|
+
let found;
|
|
85
|
+
const visit = (node) => {
|
|
86
|
+
if (found !== undefined) return;
|
|
87
|
+
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.name.text === name && node.initializer !== undefined && isText(node.initializer)) {
|
|
88
|
+
found = node;
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
ts.forEachChild(node, visit);
|
|
92
|
+
};
|
|
93
|
+
visit(sf);
|
|
94
|
+
return found;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* `text` with every contact's text address stated as parts, and the lines it hands back;
|
|
99
|
+
* `undefined` when there is nothing to rewrite.
|
|
100
|
+
*/
|
|
101
|
+
export function rewriteSource(fileName, text) {
|
|
102
|
+
const kind = fileName.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
103
|
+
const sf = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, kind);
|
|
104
|
+
const edits = new Map();
|
|
105
|
+
const reasons = [];
|
|
106
|
+
const rewrite = (literal) => {
|
|
107
|
+
const at = literal.getStart(sf);
|
|
108
|
+
if (edits.has(at)) return;
|
|
109
|
+
const line = literal.text.trim();
|
|
110
|
+
if (line === "") {
|
|
111
|
+
edits.set(at, { end: literal.getEnd(), text: "null" });
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const parts = partsOf(line);
|
|
115
|
+
if (parts === null) {
|
|
116
|
+
reasons.push(
|
|
117
|
+
`Manual: line ${String(lineOf(sf, at))}: state this contact's address as its parts — ` +
|
|
118
|
+
`\`{ street, unit, city, state, postal, latitude, longitude }\` — in place of ${JSON.stringify(line)}`,
|
|
119
|
+
);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
edits.set(at, { end: literal.getEnd(), text: literalOf(parts) });
|
|
123
|
+
};
|
|
124
|
+
const visit = (node) => {
|
|
125
|
+
if (ts.isObjectLiteralExpression(node) && node.properties.some((property) => nameOf(property) === "org_name")) {
|
|
126
|
+
for (const property of node.properties) {
|
|
127
|
+
if (nameOf(property) !== "address") continue;
|
|
128
|
+
if (ts.isPropertyAssignment(property) && isText(property.initializer)) rewrite(property.initializer);
|
|
129
|
+
const named = ts.isPropertyAssignment(property) && ts.isIdentifier(property.initializer)
|
|
130
|
+
? property.initializer.text
|
|
131
|
+
: ts.isShorthandPropertyAssignment(property) ? "address" : undefined;
|
|
132
|
+
const declared = named === undefined ? undefined : textConst(sf, named);
|
|
133
|
+
if (declared !== undefined) rewrite(declared.initializer);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
ts.forEachChild(node, visit);
|
|
137
|
+
};
|
|
138
|
+
visit(sf);
|
|
139
|
+
if (edits.size === 0 && reasons.length === 0) return undefined;
|
|
140
|
+
let out = text;
|
|
141
|
+
for (const [start, edit] of [...edits.entries()].sort((a, b) => b[0] - a[0])) out = out.slice(0, start) + edit.text + out.slice(edit.end);
|
|
142
|
+
return { text: out, changed: edits.size > 0, reasons };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** @param {string} root @returns {Promise<{rewritten: string[], notRewritten: {file: string, reason: string}[]}>} */
|
|
146
|
+
export default async function writtenContactAddress(root) {
|
|
147
|
+
const rewritten = [];
|
|
148
|
+
const notRewritten = [];
|
|
149
|
+
for (const file of await sourceFiles(root)) {
|
|
150
|
+
const before = await readFile(file, "utf8");
|
|
151
|
+
if (!before.includes("org_name")) continue;
|
|
152
|
+
const result = rewriteSource(file, before);
|
|
153
|
+
if (result === undefined) continue;
|
|
154
|
+
const rel = relative(root, file).split(sep).join("/");
|
|
155
|
+
if (result.changed) {
|
|
156
|
+
await writeFile(file, result.text, "utf8");
|
|
157
|
+
rewritten.push(rel);
|
|
158
|
+
}
|
|
159
|
+
for (const reason of result.reasons) notRewritten.push({ file: rel, reason });
|
|
160
|
+
}
|
|
161
|
+
return { rewritten, notRewritten };
|
|
162
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
// A media slot is picked by its kind and `category`; `pool.*` is gone.
|
|
2
|
+
//
|
|
3
|
+
// A section's media is always picked from the property's uploads, so a media slot names
|
|
4
|
+
// no pool: its candidates are the uploads of its kind, narrowed by an optional
|
|
5
|
+
// `category` — `"property"` for the property's photos, videos and tours,
|
|
6
|
+
// `"floor_plan"` for its plans. `pool` is no longer exported, and `from` on a media slot
|
|
7
|
+
// no longer compiles.
|
|
8
|
+
//
|
|
9
|
+
// What this codemod does, in every `.ts` and `.tsx` under the workspace that imports this
|
|
10
|
+
// package: in the options object of an `image`, `video`, `tour` or `document` call, a
|
|
11
|
+
// `from` naming pools is rewritten by what the pools held —
|
|
12
|
+
//
|
|
13
|
+
// image `from: pool.photos` → `category: "property"`
|
|
14
|
+
// `from: pool.floor_plans` → `category: "floor_plan"`
|
|
15
|
+
// `from: [pool.photos, pool.floor_plans]` → removed: any of the property's images
|
|
16
|
+
// video `from: pool.videos` → removed: any of its videos
|
|
17
|
+
// tour `from: pool.tours` → removed: any of its tours
|
|
18
|
+
// document `from: pool.documents` → removed: any of its documents
|
|
19
|
+
//
|
|
20
|
+
// — and `pool` leaves the import once nothing reads it. A pool named in a one-element
|
|
21
|
+
// list reads as the pool alone.
|
|
22
|
+
//
|
|
23
|
+
// What it hands back, by line: a `from` naming pools another way (a variable, a pool of
|
|
24
|
+
// another kind) and any other read of `pool`, each of which the author settles by hand.
|
|
25
|
+
|
|
26
|
+
import { readdir, readFile, writeFile } from "node:fs/promises";
|
|
27
|
+
import { join, relative, sep } from "node:path";
|
|
28
|
+
|
|
29
|
+
import ts from "typescript";
|
|
30
|
+
|
|
31
|
+
const CONCRETE = "@homepages/concrete";
|
|
32
|
+
const EXTENSIONS = [".ts", ".tsx"];
|
|
33
|
+
const SKIP_DIRS = new Set(["node_modules", ".concrete", ".git", "dist", "build"]);
|
|
34
|
+
|
|
35
|
+
// The media builders, by the name a call reaches them under (`image`, `c.image`).
|
|
36
|
+
const MEDIA_CALL = /(^|\.)(image|video|tour|document)$/;
|
|
37
|
+
|
|
38
|
+
// Each kind's pools, as a sorted key, to what the rewritten slot states: a category, or
|
|
39
|
+
// nothing — every one of its kind.
|
|
40
|
+
const REWRITES = {
|
|
41
|
+
image: { photos: "property", floor_plans: "floor_plan", "floor_plans,photos": null },
|
|
42
|
+
video: { videos: null },
|
|
43
|
+
tour: { tours: null },
|
|
44
|
+
document: { documents: null },
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
async function sourceFiles(root) {
|
|
48
|
+
const found = [];
|
|
49
|
+
async function walk(dir) {
|
|
50
|
+
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
51
|
+
if (entry.isDirectory()) {
|
|
52
|
+
if (!SKIP_DIRS.has(entry.name)) await walk(join(dir, entry.name));
|
|
53
|
+
} else if (EXTENSIONS.some((ext) => entry.name.endsWith(ext)) && !entry.name.endsWith(".d.ts")) {
|
|
54
|
+
found.push(join(dir, entry.name));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
await walk(root);
|
|
59
|
+
return found.sort();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function lineOf(sf, pos) {
|
|
63
|
+
return sf.getLineAndCharacterOfPosition(pos).line + 1;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function nameOf(property) {
|
|
67
|
+
const name = property.name;
|
|
68
|
+
return name !== undefined && (ts.isIdentifier(name) || ts.isStringLiteral(name)) ? name.text : undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// The import of `pool` from the framework: its specifier node and its declaration.
|
|
72
|
+
function poolImport(sf, isFramework) {
|
|
73
|
+
for (const statement of sf.statements) {
|
|
74
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier) || !isFramework(statement.moduleSpecifier.text)) continue;
|
|
75
|
+
const bindings = statement.importClause?.namedBindings;
|
|
76
|
+
if (bindings === undefined || !ts.isNamedImports(bindings)) continue;
|
|
77
|
+
const element = bindings.elements.find((e) => (e.propertyName ?? e.name).text === "pool");
|
|
78
|
+
if (element !== undefined) return { statement, bindings, element, local: element.name.text };
|
|
79
|
+
}
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// The receiver a namespace import reaches `pool` through (`c` in `c.pool.photos`).
|
|
84
|
+
function namespaceImports(sf, isFramework) {
|
|
85
|
+
const names = new Set();
|
|
86
|
+
for (const statement of sf.statements) {
|
|
87
|
+
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier) || !isFramework(statement.moduleSpecifier.text)) continue;
|
|
88
|
+
const bindings = statement.importClause?.namedBindings;
|
|
89
|
+
if (bindings !== undefined && ts.isNamespaceImport(bindings)) names.add(bindings.name.text);
|
|
90
|
+
}
|
|
91
|
+
return names;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// The pool a `pool.x` (or `c.pool.x`) expression names, or `undefined`.
|
|
95
|
+
function poolNamed(node, local, namespaces) {
|
|
96
|
+
if (!ts.isPropertyAccessExpression(node)) return undefined;
|
|
97
|
+
const target = node.expression;
|
|
98
|
+
if (local !== undefined && ts.isIdentifier(target) && target.text === local) return node.name.text;
|
|
99
|
+
if (
|
|
100
|
+
ts.isPropertyAccessExpression(target) &&
|
|
101
|
+
target.name.text === "pool" &&
|
|
102
|
+
ts.isIdentifier(target.expression) &&
|
|
103
|
+
namespaces.has(target.expression.text)
|
|
104
|
+
) {
|
|
105
|
+
return node.name.text;
|
|
106
|
+
}
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// The pools a `from` initializer names, or `undefined` when it is not spelled by pools.
|
|
111
|
+
function poolsOf(initializer, local, namespaces) {
|
|
112
|
+
const one = poolNamed(initializer, local, namespaces);
|
|
113
|
+
if (one !== undefined) return [one];
|
|
114
|
+
if (!ts.isArrayLiteralExpression(initializer) || initializer.elements.length === 0) return undefined;
|
|
115
|
+
const all = initializer.elements.map((element) => poolNamed(element, local, namespaces));
|
|
116
|
+
return all.every((name) => name !== undefined) ? all : undefined;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// The span a removed property occupies: its own line when nothing else shares it, else
|
|
120
|
+
// the property and the comma (and space) that joined it to its neighbour.
|
|
121
|
+
function removalSpan(sf, text, literal, index) {
|
|
122
|
+
const property = literal.properties[index];
|
|
123
|
+
const start = property.getStart(sf);
|
|
124
|
+
const end = property.getEnd();
|
|
125
|
+
const lineStart = text.lastIndexOf("\n", start - 1) + 1;
|
|
126
|
+
if (text.slice(lineStart, start).trim() === "") {
|
|
127
|
+
const after = text.slice(end).match(/^,?[ \t]*\n/);
|
|
128
|
+
if (after !== null) return { start: lineStart, end: end + after[0].length, insert: "" };
|
|
129
|
+
}
|
|
130
|
+
const trailing = text.slice(end).match(/^\s*,[ \t]*/);
|
|
131
|
+
if (trailing !== null) return { start, end: end + trailing[0].length, insert: "" };
|
|
132
|
+
const previous = literal.properties[index - 1];
|
|
133
|
+
return { start: previous === undefined ? start : previous.getEnd(), end, insert: "" };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// The edit dropping `pool` from its import: the whole declaration when it named nothing
|
|
137
|
+
// else, else the specifier and its comma.
|
|
138
|
+
function importRemoval(sf, text, found) {
|
|
139
|
+
const { statement, bindings, element } = found;
|
|
140
|
+
if (bindings.elements.length === 1 && statement.importClause.name === undefined) {
|
|
141
|
+
const end = statement.getEnd();
|
|
142
|
+
const after = text.slice(end).match(/^[ \t]*\n/);
|
|
143
|
+
return { start: statement.getStart(sf), end: end + (after === null ? 0 : after[0].length), insert: "" };
|
|
144
|
+
}
|
|
145
|
+
const index = bindings.elements.indexOf(element);
|
|
146
|
+
const start = element.getStart(sf);
|
|
147
|
+
const next = bindings.elements[index + 1];
|
|
148
|
+
if (next !== undefined) return { start, end: next.getStart(sf), insert: "" };
|
|
149
|
+
return { start: bindings.elements[index - 1].getEnd(), end: element.getEnd(), insert: "" };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Rewrites one file's text; `undefined` when it names no pool this package exports.
|
|
154
|
+
* `isFramework` decides which import specifiers are the framework.
|
|
155
|
+
*/
|
|
156
|
+
export function rewriteSource(fileName, text, isFramework = (specifier) => specifier === CONCRETE) {
|
|
157
|
+
if (!text.includes("pool")) return undefined;
|
|
158
|
+
const kind = fileName.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
159
|
+
const sf = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, kind);
|
|
160
|
+
const imported = poolImport(sf, isFramework);
|
|
161
|
+
const namespaces = namespaceImports(sf, isFramework);
|
|
162
|
+
if (imported === undefined && namespaces.size === 0) return undefined;
|
|
163
|
+
const local = imported?.local;
|
|
164
|
+
|
|
165
|
+
const edits = [];
|
|
166
|
+
const reasons = [];
|
|
167
|
+
const handled = new Set();
|
|
168
|
+
const visit = (node) => {
|
|
169
|
+
if (ts.isObjectLiteralExpression(node) && ts.isCallExpression(node.parent) && node.parent.arguments[0] === node) {
|
|
170
|
+
const callee = node.parent.expression.getText(sf);
|
|
171
|
+
const media = MEDIA_CALL.exec(callee)?.[2];
|
|
172
|
+
node.properties.forEach((property, index) => {
|
|
173
|
+
if (media === undefined || !ts.isPropertyAssignment(property) || nameOf(property) !== "from") return;
|
|
174
|
+
const pools = poolsOf(property.initializer, local, namespaces);
|
|
175
|
+
if (pools === undefined) return;
|
|
176
|
+
const key = [...new Set(pools)].sort().join(",");
|
|
177
|
+
const table = REWRITES[media];
|
|
178
|
+
handled.add(property.initializer);
|
|
179
|
+
if (!Object.hasOwn(table, key)) {
|
|
180
|
+
reasons.push(
|
|
181
|
+
`line ${String(lineOf(sf, property.getStart(sf)))}: ${/^[aeiou]/.test(media) ? "an" : "a"} ${media} picked from ${pools.map((p) => `pool.${p}`).join(", ")} — ` +
|
|
182
|
+
`${/^[aeiou]/.test(media) ? "an" : "a"} ${media} is picked from the property's ${media}s, narrowed by \`category\`; state the category it holds, or none`,
|
|
183
|
+
);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const category = table[key];
|
|
187
|
+
edits.push(
|
|
188
|
+
category === null
|
|
189
|
+
? removalSpan(sf, text, node, index)
|
|
190
|
+
: { start: property.getStart(sf), end: property.getEnd(), insert: `category: "${category}"` },
|
|
191
|
+
);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
// Any other read of `pool` is the author's.
|
|
195
|
+
const named = poolNamed(node, local, namespaces);
|
|
196
|
+
if (named !== undefined && ![...handled].some((h) => h.pos <= node.pos && node.end <= h.end)) {
|
|
197
|
+
reasons.push(`line ${String(lineOf(sf, node.getStart(sf)))}: \`${node.getText(sf)}\` — there are no pools; a media slot is picked by its kind and \`category\``);
|
|
198
|
+
}
|
|
199
|
+
ts.forEachChild(node, visit);
|
|
200
|
+
};
|
|
201
|
+
visit(sf);
|
|
202
|
+
if (edits.length === 0) return reasons.length > 0 ? { text, changed: false, reasons } : undefined;
|
|
203
|
+
|
|
204
|
+
// Drop `pool` from the import once nothing left reads it.
|
|
205
|
+
if (imported !== undefined && reasons.length === 0) edits.push(importRemoval(sf, text, imported));
|
|
206
|
+
let out = text;
|
|
207
|
+
for (const edit of edits.sort((a, b) => b.start - a.start)) out = out.slice(0, edit.start) + edit.insert + out.slice(edit.end);
|
|
208
|
+
return { text: out, changed: true, reasons };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** @param {string} root @returns {Promise<{rewritten: string[], notRewritten: {file: string, reason: string}[]}>} */
|
|
212
|
+
export default async function poolToCategory(root) {
|
|
213
|
+
const rewritten = [];
|
|
214
|
+
const notRewritten = [];
|
|
215
|
+
for (const file of await sourceFiles(root)) {
|
|
216
|
+
const before = await readFile(file, "utf8");
|
|
217
|
+
const result = rewriteSource(file, before);
|
|
218
|
+
if (result === undefined) continue;
|
|
219
|
+
const rel = relative(root, file).split(sep).join("/");
|
|
220
|
+
if (result.changed) {
|
|
221
|
+
await writeFile(file, result.text, "utf8");
|
|
222
|
+
rewritten.push(rel);
|
|
223
|
+
}
|
|
224
|
+
for (const reason of result.reasons) notRewritten.push({ file: rel, reason });
|
|
225
|
+
}
|
|
226
|
+
return { rewritten, notRewritten };
|
|
227
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// 9.0.0 — a ref's wire kind names the layer it reads: `home` or `section`.
|
|
2
|
+
//
|
|
3
|
+
// A slot is the generic: a home slot holds a fact, a section slot holds content. Every
|
|
4
|
+
// ref is one of five kind-tagged forms, one vocabulary for a `source:` and a `reads`
|
|
5
|
+
// entry alike — `{ kind: "home", key }`, `{ kind: "section", section, slot }`,
|
|
6
|
+
// `{ kind: "row", slot, field }`, `{ kind: "item", field }` and `{ kind: "element" }`.
|
|
7
|
+
// A fact ref has one spelling: `fact.property.beds` IS `{ kind: "home", key:
|
|
8
|
+
// "property.beds" }`, where it was `{ kind: "direct", field }` as a source and
|
|
9
|
+
// `{ kind: "fact", key }` as a read. An entry without `kind` no longer reads as a slot.
|
|
10
|
+
//
|
|
11
|
+
// What this codemod does, in every `.ts` and `.tsx` under the workspace — the wire
|
|
12
|
+
// literals a section's own test or fixture spells:
|
|
13
|
+
//
|
|
14
|
+
// - `{ kind: "direct", field: X }` becomes `{ kind: "home", key: X }`.
|
|
15
|
+
// - `{ kind: "fact", key: X }` becomes `{ kind: "home", key: X }`.
|
|
16
|
+
// - `{ kind: "slot", section: S, slot: T }` becomes `{ kind: "section", section: S, slot: T }`.
|
|
17
|
+
// - in a file importing this package, `.field` read off a `fact.*` entry
|
|
18
|
+
// (`fact.property.units.field`) becomes `.key`.
|
|
19
|
+
//
|
|
20
|
+
// An object literal is rewritten only when it holds exactly those members, so an
|
|
21
|
+
// editor selection (`{ kind: "slot", instanceId, slotId }`) is left as it is.
|
|
22
|
+
//
|
|
23
|
+
// What it hands back, by line, as `Manual:`: a comparison of a `kind` against
|
|
24
|
+
// `"direct"`, `"fact"` or `"slot"` — code branching on a ref's wire kind, which reads
|
|
25
|
+
// `"home"` or `"section"` now — and an object literal tagged `"direct"`, `"fact"` or
|
|
26
|
+
// `"slot"` that holds other members than those, whose meaning the file decides.
|
|
27
|
+
|
|
28
|
+
import { readdir, readFile, writeFile } from "node:fs/promises";
|
|
29
|
+
import { join, relative, sep } from "node:path";
|
|
30
|
+
|
|
31
|
+
import ts from "typescript";
|
|
32
|
+
|
|
33
|
+
const CONCRETE = "@homepages/concrete";
|
|
34
|
+
const EXTENSIONS = [".ts", ".tsx"];
|
|
35
|
+
const SKIP_DIRS = new Set(["node_modules", ".concrete", ".git", "dist", "build"]);
|
|
36
|
+
|
|
37
|
+
// Each retired kind: the members its literal holds, and the members it holds after.
|
|
38
|
+
const RENAMES = {
|
|
39
|
+
direct: { members: ["field"], kind: "home", rename: { field: "key" } },
|
|
40
|
+
fact: { members: ["key"], kind: "home", rename: {} },
|
|
41
|
+
slot: { members: ["section", "slot"], kind: "section", rename: {} },
|
|
42
|
+
};
|
|
43
|
+
const RETIRED = new Set(Object.keys(RENAMES));
|
|
44
|
+
|
|
45
|
+
async function sourceFiles(root) {
|
|
46
|
+
const found = [];
|
|
47
|
+
async function walk(dir) {
|
|
48
|
+
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
49
|
+
if (entry.isDirectory()) {
|
|
50
|
+
if (!SKIP_DIRS.has(entry.name)) await walk(join(dir, entry.name));
|
|
51
|
+
} else if (EXTENSIONS.some((ext) => entry.name.endsWith(ext)) && !entry.name.endsWith(".d.ts")) {
|
|
52
|
+
found.push(join(dir, entry.name));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
await walk(root);
|
|
57
|
+
return found.sort();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function lineOf(sf, pos) {
|
|
61
|
+
return sf.getLineAndCharacterOfPosition(pos).line + 1;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function importsFramework(sf, isFramework) {
|
|
65
|
+
return sf.statements.some((s) => ts.isImportDeclaration(s) && ts.isStringLiteral(s.moduleSpecifier) && isFramework(s.moduleSpecifier.text));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function nameOf(property) {
|
|
69
|
+
const name = property.name;
|
|
70
|
+
return name !== undefined && (ts.isIdentifier(name) || ts.isStringLiteral(name)) ? name.text : undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function stringValue(node) {
|
|
74
|
+
return ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node) ? node.text : undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// The retired kind an object literal is tagged with, and its `kind` property — or undefined.
|
|
78
|
+
function retiredTag(literal) {
|
|
79
|
+
const kind = literal.properties.find((p) => ts.isPropertyAssignment(p) && nameOf(p) === "kind");
|
|
80
|
+
const value = kind === undefined ? undefined : stringValue(kind.initializer);
|
|
81
|
+
return value !== undefined && RETIRED.has(value) ? { kind, value } : undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Whether a `.field` access reads a `fact.*` entry: its object is a property-access chain rooted at `fact`.
|
|
85
|
+
function readsFactEntry(access) {
|
|
86
|
+
let at = access.expression;
|
|
87
|
+
while (ts.isPropertyAccessExpression(at)) at = at.expression;
|
|
88
|
+
return ts.isIdentifier(at) && at.text === "fact" && access.expression !== at;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Rewrites one file's text; `undefined` when it holds nothing this codemod rewrites or
|
|
93
|
+
* names. `isFramework` decides which import specifiers are the framework.
|
|
94
|
+
*/
|
|
95
|
+
export function rewriteSource(fileName, text, isFramework = (specifier) => specifier === CONCRETE) {
|
|
96
|
+
const kind = fileName.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
97
|
+
const sf = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, kind);
|
|
98
|
+
const framework = importsFramework(sf, isFramework);
|
|
99
|
+
|
|
100
|
+
const edits = [];
|
|
101
|
+
const reasons = [];
|
|
102
|
+
const visit = (node) => {
|
|
103
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
104
|
+
const tag = retiredTag(node);
|
|
105
|
+
if (tag !== undefined) {
|
|
106
|
+
const rule = RENAMES[tag.value];
|
|
107
|
+
const others = node.properties.filter((p) => p !== tag.kind);
|
|
108
|
+
const names = others.map((p) => (ts.isPropertyAssignment(p) || ts.isShorthandPropertyAssignment(p) ? nameOf(p) : undefined));
|
|
109
|
+
const exact = names.length === rule.members.length && rule.members.every((m) => names.includes(m));
|
|
110
|
+
if (exact) {
|
|
111
|
+
const quote = text[tag.kind.initializer.getStart(sf)];
|
|
112
|
+
edits.push({ start: tag.kind.initializer.getStart(sf), end: tag.kind.initializer.getEnd(), text: `${quote}${rule.kind}${quote}` });
|
|
113
|
+
for (const property of others) {
|
|
114
|
+
const to = rule.rename[nameOf(property)];
|
|
115
|
+
if (to === undefined) continue;
|
|
116
|
+
if (ts.isShorthandPropertyAssignment(property)) {
|
|
117
|
+
edits.push({ start: property.getStart(sf), end: property.getEnd(), text: `${to}: ${property.name.text}` });
|
|
118
|
+
} else {
|
|
119
|
+
edits.push({ start: property.name.getStart(sf), end: property.name.getEnd(), text: to });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
} else {
|
|
123
|
+
reasons.push(
|
|
124
|
+
`Manual: line ${String(lineOf(sf, node.getStart(sf)))}: an object tagged \`kind: "${tag.value}"\` holding ${names.map((n) => `\`${String(n)}\``).join(", ") || "nothing else"} — ` +
|
|
125
|
+
"if it is a ref, spell it `{ kind: \"home\", key }` or `{ kind: \"section\", section, slot }`",
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (
|
|
131
|
+
ts.isBinaryExpression(node) &&
|
|
132
|
+
[ts.SyntaxKind.EqualsEqualsEqualsToken, ts.SyntaxKind.ExclamationEqualsEqualsToken, ts.SyntaxKind.EqualsEqualsToken, ts.SyntaxKind.ExclamationEqualsToken].includes(node.operatorToken.kind)
|
|
133
|
+
) {
|
|
134
|
+
const sides = [node.left, node.right];
|
|
135
|
+
const literal = sides.map(stringValue).find((v) => v !== undefined && RETIRED.has(v));
|
|
136
|
+
const readsKind = sides.some((side) => (ts.isPropertyAccessExpression(side) && side.name.text === "kind") || (ts.isElementAccessExpression(side) && stringValue(side.argumentExpression) === "kind"));
|
|
137
|
+
if (literal !== undefined && readsKind) {
|
|
138
|
+
reasons.push(
|
|
139
|
+
`Manual: line ${String(lineOf(sf, node.getStart(sf)))}: compares a ref's \`kind\` to "${literal}" — a fact ref's kind is "home" and a slot ref's "section"`,
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (framework && ts.isPropertyAccessExpression(node) && node.name.text === "field" && readsFactEntry(node)) {
|
|
144
|
+
edits.push({ start: node.name.getStart(sf), end: node.name.getEnd(), text: "key" });
|
|
145
|
+
}
|
|
146
|
+
ts.forEachChild(node, visit);
|
|
147
|
+
};
|
|
148
|
+
visit(sf);
|
|
149
|
+
if (edits.length === 0 && reasons.length === 0) return undefined;
|
|
150
|
+
let out = text;
|
|
151
|
+
for (const edit of edits.sort((a, b) => b.start - a.start)) out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
|
|
152
|
+
return { text: out, changed: edits.length > 0, reasons };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** @param {string} root @returns {Promise<{rewritten: string[], notRewritten: {file: string, reason: string}[]}>} */
|
|
156
|
+
export default async function writtenRefKindsNameTheLayer(root) {
|
|
157
|
+
const rewritten = [];
|
|
158
|
+
const notRewritten = [];
|
|
159
|
+
for (const file of await sourceFiles(root)) {
|
|
160
|
+
const before = await readFile(file, "utf8");
|
|
161
|
+
if (!/\b(direct|fact|slot)\b/.test(before)) continue;
|
|
162
|
+
const result = rewriteSource(file, before);
|
|
163
|
+
if (result === undefined) continue;
|
|
164
|
+
const rel = relative(root, file).split(sep).join("/");
|
|
165
|
+
if (result.changed) {
|
|
166
|
+
await writeFile(file, result.text, "utf8");
|
|
167
|
+
rewritten.push(rel);
|
|
168
|
+
}
|
|
169
|
+
for (const reason of result.reasons) notRewritten.push({ file: rel, reason });
|
|
170
|
+
}
|
|
171
|
+
return { rewritten, notRewritten };
|
|
172
|
+
}
|