@homepages/concrete 10.0.0 → 12.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/codemods/manifest.json +7 -154
  3. package/dist/artifact/playground/client/assets/index-BcLGpWLR.js +1266 -0
  4. package/dist/artifact/playground/client/assets/index-CxWPYOH2.css +1 -0
  5. package/dist/artifact/playground/client/index.html +2 -2
  6. package/dist/artifact/playground/client/island-canvas-host.mjs +1 -1
  7. package/dist/artifact/playground/host.mjs +39 -40
  8. package/dist/base.css +18 -65
  9. package/dist/cli/build/artifact.js +1 -1
  10. package/dist/cli/build/fixtures.js +3 -2
  11. package/dist/cli/capture/capture.js +8 -6
  12. package/dist/cli/check/stages/sections.js +1 -1
  13. package/dist/cli/dev/workspace-reader.js +4 -2
  14. package/dist/css/compile.js +3 -6
  15. package/dist/css/rules.js +1 -51
  16. package/dist/design-system/format-variants.js +1 -1
  17. package/dist/fixtures/resolve.js +5 -1
  18. package/dist/fixtures/rows.js +2 -2
  19. package/dist/index.d.ts +5 -5
  20. package/dist/packages/external/concrete/package.js +1 -1
  21. package/dist/packages/internal/fill-bake/dist/compute-reads.js +1 -1
  22. package/dist/packages/internal/fill-bake/dist/held-rows.js +2 -0
  23. package/dist/packages/internal/fill-bake/dist/index.js +1 -0
  24. package/dist/packages/internal/fill-vocabulary/dist/holder.js +3 -14
  25. package/dist/packages/internal/format-geometry/dist/capture.js +3 -11
  26. package/dist/packages/internal/format-geometry/dist/format.d.ts +2 -8
  27. package/dist/packages/internal/format-geometry/dist/geometry.js +2 -13
  28. package/dist/packages/internal/media-resolution/dist/document.js +3 -3
  29. package/dist/packages/internal/media-resolution/dist/image.js +3 -3
  30. package/dist/packages/internal/media-resolution/dist/tour.js +6 -6
  31. package/dist/packages/internal/media-resolution/dist/video.js +9 -9
  32. package/dist/packages/internal/section-contracts/dist/fill-verb-contract.js +1 -0
  33. package/dist/packages/internal/section-contracts/dist/runners.js +1 -3
  34. package/dist/packages/internal/section-contracts/dist/source-contract.js +3 -3
  35. package/dist/packages/internal/section-contracts/dist/sources-contract.js +0 -2
  36. package/dist/packages/internal/template-artifact/dist/index.js +2 -2
  37. package/dist/packages/internal/template-artifact/dist/layout.js +11 -11
  38. package/dist/packages/internal/template-artifact/dist/manifest.js +1 -1
  39. package/dist/packages/internal/template-artifact/dist/verify.js +13 -17
  40. package/dist/packages/internal/template-contracts/dist/document-value.d.ts +3 -5
  41. package/dist/packages/internal/template-contracts/dist/fact-registry.js +1 -1
  42. package/dist/packages/internal/template-contracts/dist/fill-builders.js +2 -6
  43. package/dist/packages/internal/template-contracts/dist/fill-spec-schema.js +4 -1
  44. package/dist/packages/internal/template-contracts/dist/list.js +0 -14
  45. package/dist/packages/internal/template-contracts/dist/primitive-descriptors.js +1 -1
  46. package/dist/packages/internal/template-contracts/dist/runtime-values.d.ts +6 -7
  47. package/dist/packages/internal/template-contracts/dist/slot-builders.js +0 -2
  48. package/dist/packages/internal/template-contracts/dist/slot-ref.d.ts +6 -1
  49. package/dist/packages/internal/template-contracts/dist/tour-value.d.ts +2 -4
  50. package/dist/packages/internal/template-contracts/dist/video-value.d.ts +3 -4
  51. package/dist/primitives/FrameShell.d.ts +5 -2
  52. package/dist/primitives/FrameShell.js +2 -1
  53. package/dist/primitives/Image.d.ts +2 -2
  54. package/dist/primitives/Tour.d.ts +2 -2
  55. package/dist/primitives/Video.d.ts +2 -2
  56. package/dist/primitives/slot-bindings.js +6 -13
  57. package/dist/primitives/slot-document.d.ts +1 -1
  58. package/dist/primitives/slot-download.d.ts +3 -1
  59. package/dist/primitives/slot-media.d.ts +5 -2
  60. package/dist/primitives/slot-media.js +1 -1
  61. package/dist/rules/registry.js +1 -7
  62. package/dist/schema/format-presets.d.ts +1 -1
  63. package/dist/schema/format.d.ts +0 -1
  64. package/dist/schema/format.js +1 -2
  65. package/dist/schema/manifest.js +18 -24
  66. package/dist/schema/section-schema.d.ts +5 -5
  67. package/dist/site-analytics.js +6 -1
  68. package/dist/site-runtime/window-height.js +19 -0
  69. package/dist/styles.css +4 -4
  70. package/dist/template/define-template.d.ts +11 -0
  71. package/dist/template/define-template.js +1 -1
  72. package/dist/template/materialize.js +2 -0
  73. package/guide/reference/cli/INDEX.md +2 -2
  74. package/guide/reference/cli/build.md +1 -1
  75. package/guide/reference/cli/check.md +1 -1
  76. package/guide/reference/cli/dev.md +1 -1
  77. package/guide/reference/cli/property-add.md +1 -1
  78. package/guide/reference/cli/property-list.md +1 -1
  79. package/guide/reference/cli/publish.md +1 -1
  80. package/guide/reference/cli/upgrade.md +1 -1
  81. package/guide/reference/rules/INDEX.md +2 -3
  82. package/package.json +2 -2
  83. package/codemods/fill-is-a-source.mjs +0 -533
  84. package/codemods/framework-namespace.mjs +0 -107
  85. package/codemods/list-grammar.mjs +0 -923
  86. package/codemods/object-form-only.mjs +0 -39
  87. package/codemods/platform-namespace.mjs +0 -83
  88. package/codemods/process-builders.mjs +0 -198
  89. package/codemods/reads-replace-reconcile.mjs +0 -285
  90. package/codemods/registry-keys-only.mjs +0 -113
  91. package/codemods/slot-object.mjs +0 -1097
  92. package/codemods/steps/content-object-fields.mjs +0 -324
  93. package/codemods/steps/list-object-form.mjs +0 -686
  94. package/codemods/steps/registry-key-reads.mjs +0 -276
  95. package/codemods/steps/select-object-form.mjs +0 -210
  96. package/codemods/table-rows.mjs +0 -568
  97. package/codemods/theme-collapse.mjs +0 -396
  98. package/codemods/unlayer-author-css.mjs +0 -194
  99. package/codemods/unwrap-object-fields.mjs +0 -310
  100. package/codemods/viewer-slot-document.mjs +0 -131
  101. package/codemods/write-property-as-facts-document.mjs +0 -716
  102. package/codemods/write-the-address-line.mjs +0 -255
  103. package/codemods/write-the-registry-members.mjs +0 -653
  104. package/codemods/write-the-select-from.mjs +0 -141
  105. package/codemods/write-the-sole-from.mjs +0 -369
  106. package/codemods/write-the-table-from.mjs +0 -267
  107. package/codemods/write-the-whole-item.mjs +0 -512
  108. package/codemods/writeback-drop.mjs +0 -168
  109. package/codemods/written-contact-address.mjs +0 -162
  110. package/codemods/written-media-category.mjs +0 -227
  111. package/codemods/written-ref-kinds-name-the-layer.mjs +0 -172
  112. package/codemods/written-uploads-replace-pool-reads.mjs +0 -454
  113. package/codemods/written-uploads-sort-into-lists.mjs +0 -405
  114. package/codemods/written-uploads-stated-in-sorted-lists.mjs +0 -295
  115. package/codemods/written-video-sources-drop.mjs +0 -144
  116. package/codemods/written-without-pick-none.mjs +0 -146
  117. package/codemods/written-without-retired-knowledge.mjs +0 -223
  118. package/codemods/written-without-the-media-key.mjs +0 -202
  119. package/codemods/written-without-the-property-type.mjs +0 -249
  120. package/dist/artifact/playground/client/assets/index-BFNHB7VM.css +0 -1
  121. package/dist/artifact/playground/client/assets/index-Div1yo6s.js +0 -1259
@@ -1,653 +0,0 @@
1
- // `fact.property.units`, `.contacts` and `.comparables` read the facts document's rows:
2
- // each member under the registry's name, an unanswered member `null`, a comparable's
3
- // address one address value whose parts are its members, an owned image `{ asset_id }`
4
- // in a `compute()`, and no `primary` or `position`.
5
- //
6
- // What this codemod does, in every `.ts` and `.tsx` under the workspace, for a row slot
7
- // it can see is a table's — `row({ source })` of a table, of a pick of one, of a list
8
- // sourced by one, or of a table list's item element:
9
- //
10
- // - a unit's `unit_label` reads `label`;
11
- // - a comparable's `address_city`, `address_state`, `address_postal` read
12
- // `address.city`, `.state`, `.postal`, and `lat`, `lng` read `address.latitude`,
13
- // `address.longitude`;
14
- // - a comparable's `address` read as text and its `address_street` become the inline
15
- // join of the parts the legacy line held — as a slot's `source:` or an object's
16
- // `label`, a `compute()` over `comp.address` (the slot's `writeback` goes, since
17
- // nothing writes back through a compute); in a `compute()`'s `reads`, the read is
18
- // `comp.address` and its parameter joins the parts where the body used the text; in a
19
- // fill's `reads`, the model reads the address value.
20
- //
21
- // And in a `compute()` reading a table — the rows its parameter holds, and each element
22
- // it takes off them (`units.map((unit) => …)`, `units[0]`, `for (const unit of units)`):
23
- // `unit_label` → `label`, `unit_id` → `id`, a floor plan's `floor_label` → `floor`, a
24
- // comparable's address members as above (through `?.`: the address may be `null`), and
25
- // an owned image (a contact's `headshot`, `logo_dark`, `logo_light`, a comparable's
26
- // `photo`) → its `asset_id`. A `compute()` reading one image member reads its `asset_id`
27
- // likewise.
28
- //
29
- // What it hands back, as `Manual:` lines naming the rule: a read of a contact's `primary`
30
- // or a unit's `position` (the first row is the primary; a row's place is its index), a
31
- // list drawing a unit's floor plans as bare images (each plan is `{ id, image, floor }`
32
- // now — declare a row from the element and read its `image` and `floor`), rows or a row
33
- // handed whole to a helper this codemod cannot see into, a compute whose function is a
34
- // name rather than inline (`compute({ reads: [fact.property.comparables] }, saleRows)`),
35
- // a destructured parameter, and a comparable's address read as text in a place it cannot
36
- // rewrite.
37
-
38
- import { readdir, readFile, writeFile } from "node:fs/promises";
39
- import { join, relative, sep } from "node:path";
40
-
41
- import ts from "typescript";
42
-
43
- const EXTENSIONS = [".ts", ".tsx"];
44
- const SKIP_DIRS = new Set(["node_modules", ".concrete", ".git", "dist", "build"]);
45
- const CONCRETE = "@homepages/concrete";
46
- const TABLES = new Set(["units", "contacts", "comparables"]);
47
- const FILL_VERBS = new Set(["write", "pick", "find"]);
48
-
49
- // A row member renamed to a registry member path.
50
- const RENAMED = {
51
- units: { unit_label: "label" },
52
- contacts: {},
53
- comparables: {
54
- address_city: "address.city",
55
- address_state: "address.state",
56
- address_postal: "address.postal",
57
- lat: "address.latitude",
58
- lng: "address.longitude",
59
- },
60
- };
61
-
62
- // A legacy member no registry row offers.
63
- const GONE = {
64
- units: { position: "a row's place is its index in the list — read it where the list is rendered" },
65
- contacts: { primary: "the first row is the primary — read it as the table's first row" },
66
- comparables: {},
67
- };
68
-
69
- // An owned image a `compute()` receives as its asset's id under the legacy reading.
70
- const IMAGES = { units: new Set(), contacts: new Set(["headshot", "logo_dark", "logo_light"]), comparables: new Set(["photo"]) };
71
-
72
- // A compute body's own renames, per table — what an element of the rows reads.
73
- const ELEMENT_RENAMED = {
74
- units: { unit_label: "label", unit_id: "id" },
75
- contacts: {},
76
- comparables: {
77
- address_city: "address?.city",
78
- address_state: "address?.state",
79
- address_postal: "address?.postal",
80
- lat: "address?.latitude",
81
- lng: "address?.longitude",
82
- },
83
- };
84
-
85
- const ADDRESS_PARTS = new Set(["street", "unit", "city", "state", "postal", "latitude", "longitude", "source"]);
86
- const TEXT_SIZES = new Set(["short", "medium", "long"]);
87
-
88
- // The array methods whose result is still the rows, and those whose callback's first
89
- // parameter is a row.
90
- const KEEPS_ROWS = new Set(["filter", "slice", "sort", "toSorted", "toReversed", "reverse", "concat"]);
91
- const PASSES_ROW = new Set(["map", "filter", "find", "findLast", "some", "every", "forEach", "flatMap", "sort", "toSorted", "findIndex", "findLastIndex"]);
92
- const YIELDS_ROW = new Set(["find", "findLast", "at"]);
93
-
94
- async function sourceFiles(root) {
95
- const found = [];
96
- async function walk(dir) {
97
- for (const entry of await readdir(dir, { withFileTypes: true })) {
98
- if (entry.isDirectory()) {
99
- if (!SKIP_DIRS.has(entry.name)) await walk(join(dir, entry.name));
100
- } else if (EXTENSIONS.some((ext) => entry.name.endsWith(ext)) && !entry.name.endsWith(".d.ts")) {
101
- found.push(join(dir, entry.name));
102
- }
103
- }
104
- }
105
- await walk(root);
106
- return found.sort();
107
- }
108
-
109
- /** Non-overlapping text replacements, applied back to front. */
110
- class Edits {
111
- constructor() {
112
- this.list = [];
113
- this.taken = new Set();
114
- }
115
- add(start, end, text) {
116
- const key = `${String(start)}:${String(end)}`;
117
- if (this.taken.has(key)) return;
118
- this.taken.add(key);
119
- this.list.push({ start, end, text });
120
- }
121
- apply(source) {
122
- const sorted = [...this.list].sort((a, b) => b.start - a.start || b.end - a.end);
123
- let out = source;
124
- let floor = Infinity;
125
- for (const edit of sorted) {
126
- if (edit.end > floor) throw new Error(`write-the-registry-members: overlapping rewrites at ${String(edit.start)}`);
127
- out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
128
- floor = edit.start;
129
- }
130
- return out;
131
- }
132
- }
133
-
134
- function lineOf(sf, node) {
135
- return sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1;
136
- }
137
-
138
- /** The local names this file imports from the package, by imported name. */
139
- function concreteImports(sf) {
140
- const names = new Map();
141
- let declaration;
142
- for (const statement of sf.statements) {
143
- if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier)) continue;
144
- if (statement.moduleSpecifier.text !== CONCRETE) continue;
145
- const bindings = statement.importClause?.namedBindings;
146
- if (bindings === undefined || !ts.isNamedImports(bindings)) continue;
147
- if (!ts.isTypeOnlyImportDeclaration(statement.importClause)) declaration ??= bindings;
148
- for (const element of bindings.elements) {
149
- if (element.isTypeOnly) continue;
150
- names.set((element.propertyName ?? element.name).text, element.name.text);
151
- }
152
- }
153
- return { names, declaration };
154
- }
155
-
156
- function propertyNamed(options, name) {
157
- return options.properties.find(
158
- (property) => ts.isPropertyAssignment(property) && (ts.isIdentifier(property.name) || ts.isStringLiteral(property.name)) && property.name.text === name,
159
- );
160
- }
161
-
162
- function optionsOf(call) {
163
- const [options] = call.arguments;
164
- return options !== undefined && ts.isObjectLiteralExpression(options) ? options : undefined;
165
- }
166
-
167
- function isCallTo(node, name) {
168
- return name !== undefined && node !== undefined && ts.isCallExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === name;
169
- }
170
-
171
- // Whether `node` is `<fill>.<verb>(…)` off the package's `fill`.
172
- function fillVerb(node, fill) {
173
- if (fill === undefined || !ts.isCallExpression(node) || !ts.isPropertyAccessExpression(node.expression)) return undefined;
174
- const callee = node.expression;
175
- return ts.isIdentifier(callee.expression) && callee.expression.text === fill && FILL_VERBS.has(callee.name.text) ? callee.name.text : undefined;
176
- }
177
-
178
- // The table `fact.property.<table>` names, or `undefined`.
179
- function factTable(node, fact) {
180
- if (fact === undefined || !ts.isPropertyAccessExpression(node) || !TABLES.has(node.name.text)) return undefined;
181
- const property = node.expression;
182
- return ts.isPropertyAccessExpression(property) && property.name.text === "property" && ts.isIdentifier(property.expression) && property.expression.text === fact
183
- ? node.name.text
184
- : undefined;
185
- }
186
-
187
- // Where `id` is declared: a function's parameter, a `for … of` binding, or a variable,
188
- // in the nearest scope declaring the name.
189
- function declarationOf(id) {
190
- const name = id.text;
191
- for (let node = id.parent; node !== undefined; node = node.parent) {
192
- if (ts.isFunctionLike(node) && node.parameters !== undefined) {
193
- const index = node.parameters.findIndex((parameter) => ts.isIdentifier(parameter.name) && parameter.name.text === name);
194
- if (index !== -1) return { kind: "param", fn: node, index, node: node.parameters[index] };
195
- }
196
- if (ts.isForOfStatement(node) && ts.isVariableDeclarationList(node.initializer)) {
197
- const declared = node.initializer.declarations.find((d) => ts.isIdentifier(d.name) && d.name.text === name);
198
- if (declared !== undefined) return { kind: "of", loop: node, node: declared };
199
- }
200
- if (ts.isBlock(node) || ts.isSourceFile(node) || ts.isModuleBlock(node)) {
201
- for (const statement of node.statements) {
202
- if (!ts.isVariableStatement(statement)) continue;
203
- const declared = statement.declarationList.declarations.find((d) => ts.isIdentifier(d.name) && d.name.text === name);
204
- if (declared !== undefined) return { kind: "var", node: declared };
205
- }
206
- }
207
- }
208
- return undefined;
209
- }
210
-
211
- function sameDeclaration(a, b) {
212
- return a !== undefined && b !== undefined && a.node === b.node;
213
- }
214
-
215
- // The legacy line an address value's parts join to: the comparable's street with its
216
- // unit, then the city, then "STATE POSTAL" — what `address_street` and `address` held.
217
- function streetLine(a) {
218
- return `${a}?.street && ${a}.unit ? [${a}.street, ${a}.unit].join(", ") : ${a}?.street ?? null`;
219
- }
220
- function displayLine(a) {
221
- return `[${a}?.street && ${a}.unit ? [${a}.street, ${a}.unit].join(", ") : ${a}?.street, ${a}?.city, [${a}?.state, ${a}?.postal].filter(Boolean).join(" ")].filter(Boolean).join(", ")`;
222
- }
223
-
224
- // The text that drops `property` from its object literal, with the separator it owns.
225
- function removal(options, property) {
226
- const index = options.properties.indexOf(property);
227
- const next = options.properties[index + 1];
228
- if (next !== undefined) return { start: property.getStart(), end: next.getStart() };
229
- const previous = options.properties[index - 1];
230
- return { start: previous === undefined ? property.getStart() : previous.getEnd(), end: property.getEnd() };
231
- }
232
-
233
- function rewrite(sf) {
234
- const edits = new Edits();
235
- const handBack = [];
236
- const { names, declaration } = concreteImports(sf);
237
- const fact = names.get("fact");
238
- const compute = names.get("compute") ?? "compute";
239
- let needsCompute = false;
240
- if (fact === undefined) return { edits, handBack };
241
- const manual = (node, text) => handBack.push(`line ${String(lineOf(sf, node))}: ${text}`);
242
-
243
- // A compute's inline function, or `undefined`. A function it names (`saleRows`) is code
244
- // this codemod cannot see into: handed back once per compute, whatever it reads.
245
- const opaque = new Set();
246
- const inlineFunction = (call, received) => {
247
- const fn = call.arguments[1];
248
- if (fn === undefined || ts.isArrowFunction(fn) || ts.isFunctionExpression(fn)) return fn;
249
- if (!opaque.has(call)) {
250
- opaque.add(call);
251
- manual(fn, `\`${fn.getText(sf)}\` is a compute's function this codemod cannot see into — it receives ${received}; rewrite it by hand`);
252
- }
253
- return undefined;
254
- };
255
-
256
- // ── Which table a source, a row or an element holds rows of ───────────────────────────
257
- const tableOf = (expr, depth = 0) => {
258
- if (expr === undefined || depth > 8) return undefined;
259
- const direct = factTable(expr, fact);
260
- if (direct !== undefined) return direct;
261
- if (fillVerb(expr, names.get("fill")) === "pick") {
262
- const of = optionsOf(expr) === undefined ? undefined : propertyNamed(optionsOf(expr), "of")?.initializer;
263
- return tableOf(of, depth + 1);
264
- }
265
- if (!ts.isIdentifier(expr)) return undefined;
266
- const declared = declarationOf(expr);
267
- if (declared?.kind === "var" && declared.node.initializer !== undefined) {
268
- const init = declared.node.initializer;
269
- if (isCallTo(init, names.get("row")) || isCallTo(init, names.get("list"))) {
270
- const options = optionsOf(init);
271
- return tableOf(options === undefined ? undefined : propertyNamed(options, "source")?.initializer, depth + 1);
272
- }
273
- return undefined;
274
- }
275
- if (declared?.kind === "param" && declared.index === 0) {
276
- const item = declared.fn.parent;
277
- if (!ts.isPropertyAssignment(item) || !ts.isIdentifier(item.name) || item.name.text !== "item") return undefined;
278
- const list = item.parent?.parent;
279
- if (!isCallTo(list, names.get("list"))) return undefined;
280
- return tableOf(propertyNamed(item.parent, "source")?.initializer, depth + 1);
281
- }
282
- return undefined;
283
- };
284
-
285
- // The table whose row `id` holds — a `row()` const of a table — or `undefined`.
286
- const rowTable = (id) => {
287
- if (!ts.isIdentifier(id)) return undefined;
288
- const declared = declarationOf(id);
289
- if (declared?.kind !== "var" || !isCallTo(declared.node.initializer, names.get("row"))) return undefined;
290
- const options = optionsOf(declared.node.initializer);
291
- return tableOf(options === undefined ? undefined : propertyNamed(options, "source")?.initializer);
292
- };
293
-
294
- // `X.member` off a table's row slot: its table and member, or `undefined`.
295
- const memberRead = (node) => {
296
- if (!ts.isPropertyAccessExpression(node)) return undefined;
297
- // `comp.address.city` is already a part of the address.
298
- if (ts.isPropertyAccessExpression(node.expression)) return undefined;
299
- const table = rowTable(node.expression);
300
- return table === undefined ? undefined : { table, member: node.name.text, row: node.expression.getText(sf) };
301
- };
302
-
303
- // ── Where a member read sits ───────────────────────────────────────────────────────────
304
- const owner = (node) => {
305
- const parent = node.parent;
306
- if (ts.isPropertyAssignment(parent) && parent.initializer === node && ts.isIdentifier(parent.name)) {
307
- const options = parent.parent;
308
- const call = options.parent;
309
- if (ts.isCallExpression(call) && call.arguments[0] === options) return { at: parent.name.text, call, options };
310
- }
311
- if (ts.isArrayLiteralExpression(parent)) {
312
- const reads = parent.parent;
313
- if (ts.isPropertyAssignment(reads) && ts.isIdentifier(reads.name) && reads.name.text === "reads") {
314
- const call = reads.parent.parent;
315
- if (isCallTo(call, compute)) return { at: "compute-reads", call, index: parent.elements.indexOf(node) };
316
- if (fillVerb(call, names.get("fill")) !== undefined) return { at: "fill-reads", call };
317
- }
318
- }
319
- return { at: "other" };
320
- };
321
-
322
- const isTextBuilder = (callee) =>
323
- ts.isPropertyAccessExpression(callee) && TEXT_SIZES.has(callee.name.text) && ts.isIdentifier(callee.expression) && callee.expression.text === names.get("text");
324
-
325
- // A comparable's address read as text where it is declared: a text slot's `source:` or an
326
- // object's `label` becomes a compute joining the parts.
327
- const joinAt = (node, read, line) => {
328
- const place = owner(node);
329
- const wrap = (text) => {
330
- edits.add(node.getStart(sf), node.getEnd(), text);
331
- needsCompute = true;
332
- };
333
- const call = `${compute}({ reads: [${read.row}.address] }, (address) => ${line("address")})`;
334
- if (place.at === "source" && isTextBuilder(place.call.expression)) {
335
- wrap(call);
336
- const writeback = propertyNamed(place.options, "writeback");
337
- if (writeback !== undefined) {
338
- const { start, end } = removal(place.options, writeback);
339
- edits.add(start, end, "");
340
- }
341
- return;
342
- }
343
- if (place.at === "label" && isCallTo(place.call, names.get("object"))) return wrap(call);
344
- if (place.at === "fill-reads") {
345
- edits.add(node.name.getStart(sf), node.name.getEnd(), "address");
346
- return;
347
- }
348
- if (place.at === "compute-reads") return joinParameter(node, place, line);
349
- manual(node, `\`${node.getText(sf)}\` — a comparable's \`address\` is an address value now, its parts \`address.street\`, \`.unit\`, \`.city\`, \`.state\`, \`.postal\`; join them where this reads the line`);
350
- };
351
-
352
- // A compute reading a comparable's address as text: it reads `comp.address`, and its
353
- // parameter joins the parts wherever the body used the text.
354
- const joinParameter = (node, place, line) => {
355
- const fn = inlineFunction(place.call, `\`${node.getText(sf)}\`, a comparable's address value now (its parts \`street\`, \`unit\`, \`city\`, \`state\`, \`postal\`)`);
356
- const parameter = fn?.parameters[place.index];
357
- if (node.name.text !== "address") edits.add(node.name.getStart(sf), node.name.getEnd(), "address");
358
- if (parameter === undefined) return;
359
- if (!ts.isIdentifier(parameter.name)) {
360
- manual(parameter, `\`${parameter.name.getText(sf)}\` destructures \`${node.getText(sf)}\`, which reads a comparable's address value now — take it whole and join its parts`);
361
- return;
362
- }
363
- const uses = usesOf(fn.body, parameter);
364
- // Every use already a part of the address: written for the address value.
365
- if (node.name.text === "address" && uses.length > 0 && uses.every((use) => partRead(use) !== undefined)) return;
366
- for (const use of uses) edits.add(use.getStart(sf), use.getEnd(), `(${line(use.text)})`);
367
- };
368
-
369
- // ── The member reads off a table's row slot ────────────────────────────────────────────
370
- const visitMember = (node, read) => {
371
- const { table, member } = read;
372
- const gone = GONE[table][member];
373
- if (gone !== undefined) {
374
- manual(node, `\`${node.getText(sf)}\` — no ${table === "units" ? "unit" : "contact"} offers \`${member}\` now: ${gone}`);
375
- return;
376
- }
377
- const renamed = RENAMED[table][member];
378
- if (renamed !== undefined) {
379
- edits.add(node.name.getStart(sf), node.name.getEnd(), renamed);
380
- return;
381
- }
382
- if (table === "comparables" && member === "address_street") return joinAt(node, read, streetLine);
383
- if (table === "comparables" && member === "address") {
384
- const place = owner(node);
385
- // A part read off it, or an address-typed reader, is the address value already.
386
- if (ts.isPropertyAccessExpression(node.parent) && node.parent.expression === node) return;
387
- if (place.at === "source" && !isTextBuilder(place.call.expression)) return;
388
- if (place.at === "fill-reads") return;
389
- return joinAt(node, read, displayLine);
390
- }
391
- if (member === "floor_plans") floorPlans(node);
392
- if (IMAGES[table].has(member)) {
393
- const place = owner(node);
394
- if (place.at === "compute-reads") imageParameter(node, place);
395
- }
396
- };
397
-
398
- // A list drawing a unit's floor plans as bare images: each plan is `{ id, image, floor }` now.
399
- const floorPlans = (node) => {
400
- const place = owner(node);
401
- if (place.at !== "source" || !isCallTo(place.call, names.get("list"))) return;
402
- const item = propertyNamed(place.options, "item")?.initializer;
403
- const image = names.get("image");
404
- const body = item !== undefined && (ts.isArrowFunction(item) || ts.isFunctionExpression(item)) ? returned(item) : item;
405
- if (body === undefined || isCallTo(body, image)) {
406
- manual(
407
- node,
408
- "a list of a unit's `floor_plans` draws each plan as a bare image — each plan is `{ id, image, floor }` now (`floor` where it held `floor_label`): " +
409
- "declare a row from the element (`const plan = row({ label: \"Plan\", source: el })`) and read `plan.image` and `plan.floor`",
410
- );
411
- }
412
- };
413
-
414
- // A compute reading one owned image: it receives `{ asset_id }`, where it received the id.
415
- const imageParameter = (node, place) => {
416
- const fn = inlineFunction(place.call, `\`${node.getText(sf)}\`, an owned image \`{ asset_id }\` now`);
417
- const parameter = fn?.parameters[place.index];
418
- if (parameter === undefined || !ts.isIdentifier(parameter.name)) return;
419
- for (const use of usesOf(fn.body, parameter)) {
420
- if (ts.isPropertyAccessExpression(use.parent) && use.parent.expression === use && use.parent.name.text === "asset_id") continue;
421
- edits.add(use.getEnd(), use.getEnd(), "?.asset_id");
422
- }
423
- };
424
-
425
- // ── A compute reading a table's rows ───────────────────────────────────────────────────
426
- const visitRows = (call, index, table) => {
427
- const fn = inlineFunction(call, `the ${table} rows, each the registry's row now (${registryNote(table)})`);
428
- const parameter = fn?.parameters[index];
429
- if (parameter === undefined) return;
430
- if (!ts.isIdentifier(parameter.name)) {
431
- manual(parameter, `\`${parameter.name.getText(sf)}\` destructures \`fact.property.${table}\`, whose rows are the registry's now — take the rows whole and read each by its registry name`);
432
- return;
433
- }
434
- const rows = declarationOf(parameter.name);
435
- const isRows = (expr) => {
436
- const inner = skipParens(expr);
437
- if (ts.isIdentifier(inner)) return sameDeclaration(declarationOf(inner), rows);
438
- if (ts.isCallExpression(inner) && ts.isPropertyAccessExpression(inner.expression) && KEEPS_ROWS.has(inner.expression.name.text)) return isRows(inner.expression.expression);
439
- return false;
440
- };
441
- const elements = new Set();
442
- const isElement = (expr) => {
443
- const inner = skipParens(expr);
444
- if (ts.isIdentifier(inner)) {
445
- const declared = declarationOf(inner);
446
- return declared !== undefined && elements.has(declared.node);
447
- }
448
- if (ts.isElementAccessExpression(inner)) return isRows(inner.expression);
449
- if (ts.isCallExpression(inner) && ts.isPropertyAccessExpression(inner.expression) && YIELDS_ROW.has(inner.expression.name.text)) return isRows(inner.expression.expression);
450
- return false;
451
- };
452
- // The elements taken off the rows, before any read of one is judged.
453
- const collect = (node) => {
454
- if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && PASSES_ROW.has(node.expression.name.text) && isRows(node.expression.expression)) {
455
- const callback = node.arguments[0];
456
- if (callback !== undefined && (ts.isArrowFunction(callback) || ts.isFunctionExpression(callback))) {
457
- const taken = node.expression.name.text === "sort" || node.expression.name.text === "toSorted" ? callback.parameters : callback.parameters.slice(0, 1);
458
- for (const p of taken) if (ts.isIdentifier(p.name)) elements.add(p);
459
- }
460
- }
461
- if (ts.isForOfStatement(node) && isRows(node.expression) && ts.isVariableDeclarationList(node.initializer)) {
462
- for (const d of node.initializer.declarations) if (ts.isIdentifier(d.name)) elements.add(d);
463
- }
464
- if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.initializer !== undefined && isElement(node.initializer)) elements.add(node);
465
- ts.forEachChild(node, collect);
466
- };
467
- collect(fn.body);
468
- collect(fn.body);
469
-
470
- const renamed = ELEMENT_RENAMED[table];
471
- const judge = (node) => {
472
- if (ts.isPropertyAccessExpression(node) && isElement(node.expression)) {
473
- elementRead(node, table, renamed);
474
- // The element itself is not a whole use.
475
- ts.forEachChild(node.expression, judge);
476
- return;
477
- }
478
- if (ts.isIdentifier(node) && !isPropertyName(node)) {
479
- const declared = declarationOf(node);
480
- if (sameDeclaration(declared, rows) && node !== parameter.name && !rowsUse(node)) {
481
- manual(node, `\`${node.text}\` hands the ${table} rows whole to code this codemod cannot see into — each is the registry's row now (${registryNote(table)})`);
482
- } else if (declared !== undefined && elements.has(declared.node) && node !== declared.node.name && !elementUse(node)) {
483
- manual(node, `\`${node.text}\` is one of the ${table} rows, used whole — it is the registry's row now (${registryNote(table)})`);
484
- }
485
- }
486
- ts.forEachChild(node, judge);
487
- };
488
- judge(fn.body);
489
- // A floor plan's label, wherever the body reads it.
490
- if (table === "units") renameEverywhere(fn.body, "floor_label", "floor");
491
- };
492
-
493
- // A read off one element of a table's rows.
494
- const elementRead = (node, table, renamed) => {
495
- const member = node.name.text;
496
- const gone = GONE[table][member];
497
- if (gone !== undefined) return manual(node, `\`${node.getText(sf)}\` — no row offers \`${member}\` now: ${gone}`);
498
- if (Object.hasOwn(renamed, member)) return edits.add(node.name.getStart(sf), node.name.getEnd(), renamed[member]);
499
- if (table === "comparables" && (member === "address_street" || (member === "address" && partRead(node) === undefined))) {
500
- if (!repeatable(skipParens(node.expression))) {
501
- return manual(node, `\`${node.getText(sf)}\` reads a comparable's address as text — it is an address value now; join its parts here`);
502
- }
503
- const address = `${skipParens(node.expression).getText(sf)}${node.questionDotToken === undefined ? "." : "?."}address`;
504
- return edits.add(node.getStart(sf), node.getEnd(), `(${(member === "address" ? displayLine : streetLine)(address)})`);
505
- }
506
- if (IMAGES[table].has(member)) {
507
- if (ts.isPropertyAccessExpression(node.parent) && node.parent.expression === node && node.parent.name.text === "asset_id") return;
508
- edits.add(node.getEnd(), node.getEnd(), "?.asset_id");
509
- }
510
- };
511
-
512
- const renameEverywhere = (body, from, to) => {
513
- const visit = (node) => {
514
- if (ts.isPropertyAccessExpression(node) && node.name.text === from) edits.add(node.name.getStart(sf), node.name.getEnd(), to);
515
- ts.forEachChild(node, visit);
516
- };
517
- visit(body);
518
- };
519
-
520
- // ── The walk ─────────────────────────────────────────────────────────────────────────
521
- const visit = (node) => {
522
- const read = memberRead(node);
523
- if (read !== undefined) {
524
- visitMember(node, read);
525
- return;
526
- }
527
- if (isCallTo(node, compute)) {
528
- const options = optionsOf(node);
529
- const reads = options === undefined ? undefined : propertyNamed(options, "reads")?.initializer;
530
- if (reads !== undefined && ts.isArrayLiteralExpression(reads)) {
531
- reads.elements.forEach((element, index) => {
532
- const table = factTable(element, fact);
533
- if (table !== undefined) visitRows(node, index, table);
534
- const member = memberRead(element);
535
- if (member?.table === "units" && member.member === "floor_plans") {
536
- const fn = inlineFunction(node, "a unit's `floor_plans`, each `{ id, image, floor }` now (`floor` where it held `floor_label`)");
537
- if (fn !== undefined) renameEverywhere(fn, "floor_label", "floor");
538
- }
539
- });
540
- }
541
- }
542
- ts.forEachChild(node, visit);
543
- };
544
- visit(sf);
545
-
546
- if (needsCompute && !names.has("compute") && declaration !== undefined) {
547
- // Joins the import beside the names it holds, in its order where it is sorted.
548
- const elements = declaration.elements;
549
- const after = elements.find((element) => element.name.text.localeCompare("compute") > 0);
550
- if (after === undefined) edits.add(elements[elements.length - 1].getEnd(), elements[elements.length - 1].getEnd(), ", compute");
551
- else edits.add(after.getStart(sf), after.getStart(sf), "compute, ");
552
- }
553
- return { edits, handBack };
554
- }
555
-
556
- // An element the join may name once per part: a binding, or the rows at a literal index.
557
- function repeatable(node) {
558
- if (ts.isIdentifier(node)) return true;
559
- return ts.isElementAccessExpression(node) && ts.isIdentifier(node.expression) && ts.isNumericLiteral(node.argumentExpression);
560
- }
561
-
562
- function skipParens(node) {
563
- let inner = node;
564
- while (ts.isParenthesizedExpression(inner) || ts.isNonNullExpression(inner)) inner = inner.expression;
565
- return inner;
566
- }
567
-
568
- // `x.name` or `{ name: … }` names a property, not the binding `name`.
569
- function isPropertyName(id) {
570
- const parent = id.parent;
571
- return (
572
- (ts.isPropertyAccessExpression(parent) && parent.name === id) ||
573
- (ts.isPropertyAssignment(parent) && parent.name === id) ||
574
- (ts.isBindingElement(parent) && parent.propertyName === id)
575
- );
576
- }
577
-
578
- // The identifiers in `body` naming `parameter`.
579
- function usesOf(body, parameter) {
580
- const found = [];
581
- const visit = (node) => {
582
- if (ts.isIdentifier(node) && node.text === parameter.name.text && !isPropertyName(node)) {
583
- const declared = declarationOf(node);
584
- if (declared?.node === parameter) found.push(node);
585
- }
586
- ts.forEachChild(node, visit);
587
- };
588
- visit(body);
589
- return found;
590
- }
591
-
592
- // The address part `use` reads (`address.city`, `address?.city`), or `undefined`.
593
- function partRead(use) {
594
- const parent = use.parent;
595
- return ts.isPropertyAccessExpression(parent) && parent.expression === use && ADDRESS_PARTS.has(parent.name.text) ? parent.name.text : undefined;
596
- }
597
-
598
- // A use of the rows that keeps to them: a method on them, an index, their length.
599
- function rowsUse(id) {
600
- let node = id;
601
- while (ts.isParenthesizedExpression(node.parent) || ts.isNonNullExpression(node.parent)) node = node.parent;
602
- const parent = node.parent;
603
- if (ts.isPropertyAccessExpression(parent) && parent.expression === node) return true;
604
- if (ts.isElementAccessExpression(parent) && parent.expression === node) return true;
605
- if (ts.isForOfStatement(parent) && parent.expression === node) return true;
606
- return false;
607
- }
608
-
609
- // A use of an element that reads it: a member off it, or a comparison of it.
610
- function elementUse(id) {
611
- let node = id;
612
- while (ts.isParenthesizedExpression(node.parent) || ts.isNonNullExpression(node.parent)) node = node.parent;
613
- const parent = node.parent;
614
- if (ts.isPropertyAccessExpression(parent) && parent.expression === node) return true;
615
- if (ts.isElementAccessExpression(parent) && parent.expression === node) return true;
616
- if (ts.isBinaryExpression(parent) && [ts.SyntaxKind.EqualsEqualsEqualsToken, ts.SyntaxKind.ExclamationEqualsEqualsToken, ts.SyntaxKind.AmpersandAmpersandToken, ts.SyntaxKind.BarBarToken, ts.SyntaxKind.QuestionQuestionToken].includes(parent.operatorToken.kind)) return true;
617
- if (ts.isPrefixUnaryExpression(parent) && parent.operator === ts.SyntaxKind.ExclamationToken) return true;
618
- if (ts.isReturnStatement(parent) || ts.isArrowFunction(parent)) return true;
619
- return false;
620
- }
621
-
622
- function registryNote(table) {
623
- if (table === "units") return "`label` for `unit_label`, `id` for `unit_id`, floor plans as `{ id, image, floor }`, `null` for an unanswered member";
624
- if (table === "contacts") return "no `primary`, `null` for a blank member or a missing logo, an image as `{ asset_id }`";
625
- return "`address` an address value with no `address_street`, `lat` or `lng`, `photo` as `{ asset_id }`";
626
- }
627
-
628
- // The expression an item function returns, where it returns one.
629
- function returned(fn) {
630
- if (!ts.isBlock(fn.body)) return skipParens(fn.body);
631
- const last = fn.body.statements.findLast((statement) => ts.isReturnStatement(statement));
632
- return last?.expression === undefined ? undefined : skipParens(last.expression);
633
- }
634
-
635
- /** @param {string} root @returns {Promise<{rewritten: string[], notRewritten: {file: string, reason: string}[]}>} */
636
- export default async function writeTheRegistryMembers(root) {
637
- const rewritten = [];
638
- const notRewritten = [];
639
- const name = (file) => relative(root, file).split(sep).join("/");
640
- for (const file of await sourceFiles(root)) {
641
- const text = await readFile(file, "utf8");
642
- if (!/\bproperty\s*\.\s*(units|contacts|comparables)\b/.test(text)) continue;
643
- const kind = file.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
644
- const sf = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, kind);
645
- const { edits, handBack } = rewrite(sf);
646
- if (edits.list.length > 0) {
647
- await writeFile(file, edits.apply(text), "utf8");
648
- rewritten.push(name(file));
649
- }
650
- for (const reason of new Set(handBack)) notRewritten.push({ file: name(file), reason: `Manual: ${reason}` });
651
- }
652
- return { rewritten, notRewritten };
653
- }