@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,223 +0,0 @@
1
- // 9.0.0 — a fixture upload states what the platform learned of it as one `account`.
2
- //
3
- // The asset record's knowledge is one member, `account`: plain statements of what the
4
- // upload is, what it shows, every word printed on it and how it looks. A fixture's photo,
5
- // video and document entries state it in place of `tags`, `caption`, a video's
6
- // `description` and a document's `notes`, none of which compiles or loads any more.
7
- //
8
- // What this codemod does, in every `.ts` and `.tsx` under the workspace: in each object
9
- // literal that states an upload (it has an `id` and a `kind` of "image", "video" or
10
- // "document") —
11
- //
12
- // `caption` (a photo's or a video's) → `account`, a video's `description` joined
13
- // after it on a line of its own
14
- // `notes` (a document's) → `account`
15
- // `tags` → removed
16
- //
17
- // — and a `null` or empty one leaves no `account`, since an entry that states none is an
18
- // upload the platform has not looked at.
19
- //
20
- // What it hands back, by line, as `Manual:`: an upload whose tags were the only statement
21
- // of what it is (no caption or notes beside them), whose account to write is yours; and
22
- // a `caption` and `description` it cannot join because one is not written as text, or an
23
- // entry already stating an `account` beside them — each left as it was.
24
-
25
- import { readdir, readFile, writeFile } from "node:fs/promises";
26
- import { join, relative, sep } from "node:path";
27
-
28
- import ts from "typescript";
29
-
30
- const EXTENSIONS = [".ts", ".tsx"];
31
- const SKIP_DIRS = new Set(["node_modules", ".concrete", ".git", "dist", "build"]);
32
-
33
- const KNOWING_KINDS = new Set(["image", "video", "document"]);
34
- const RETIRED = new Set(["tags", "caption", "description", "notes"]);
35
-
36
- async function sourceFiles(root) {
37
- const found = [];
38
- async function walk(dir) {
39
- for (const entry of await readdir(dir, { withFileTypes: true })) {
40
- if (entry.isDirectory()) {
41
- if (!SKIP_DIRS.has(entry.name)) await walk(join(dir, entry.name));
42
- } else if (EXTENSIONS.some((ext) => entry.name.endsWith(ext)) && !entry.name.endsWith(".d.ts")) {
43
- found.push(join(dir, entry.name));
44
- }
45
- }
46
- }
47
- await walk(root);
48
- return found.sort();
49
- }
50
-
51
- function nameOf(property) {
52
- const name = property.name;
53
- return name !== undefined && (ts.isIdentifier(name) || ts.isStringLiteral(name)) ? name.text : undefined;
54
- }
55
-
56
- function lineOf(sf, position) {
57
- return sf.getLineAndCharacterOfPosition(position).line + 1;
58
- }
59
-
60
- function isText(node) {
61
- return ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node);
62
- }
63
-
64
- /** The upload kind an object literal states, or `undefined` when it is no upload that knows anything. */
65
- function uploadKind(literal) {
66
- let kind;
67
- let hasId = false;
68
- for (const property of literal.properties) {
69
- const name = nameOf(property);
70
- if (name === "id") hasId = true;
71
- if (name === "kind" && ts.isPropertyAssignment(property) && isText(property.initializer)) kind = property.initializer.text;
72
- }
73
- return hasId && KNOWING_KINDS.has(kind) ? kind : undefined;
74
- }
75
-
76
- // A stated value, read as what it says: `null` and empty text say nothing; other text is
77
- // itself; anything else (a variable, a call) is an expression whose words only its
78
- // author knows.
79
- function statedValue(sf, property) {
80
- if (ts.isShorthandPropertyAssignment(property)) return { expression: property.name.text };
81
- const value = property.initializer;
82
- if (value.kind === ts.SyntaxKind.NullKeyword) return { says: null };
83
- if (isText(value)) return { says: value.text.trim() === "" ? null : value.text };
84
- return { expression: value.getText(sf) };
85
- }
86
-
87
- // The spans a literal's removed properties occupy, one per run of neighbours removed
88
- // together, so no two overlap: whole lines when each starts its own line, else the run
89
- // up to the property after it — or, when the run ends the literal, from the property
90
- // before it, so no dangling comma is left.
91
- function removalSpans(sf, text, literal, removed) {
92
- const spans = [];
93
- const { properties } = literal;
94
- for (let i = 0; i < properties.length; i++) {
95
- if (!removed.has(i)) continue;
96
- let j = i;
97
- while (removed.has(j + 1)) j++;
98
- const start = properties[i].getStart(sf);
99
- const end = properties[j].getEnd();
100
- const ownLines = properties.slice(i, j + 1).every((property) => {
101
- const at = property.getStart(sf);
102
- return text.slice(text.lastIndexOf("\n", at - 1) + 1, at).trim() === "";
103
- });
104
- const after = text.slice(end).match(/^,?[ \t]*\n/);
105
- if (ownLines && after !== null) {
106
- spans.push({ start: text.lastIndexOf("\n", start - 1) + 1, end: end + after[0].length });
107
- } else if (j + 1 < properties.length) {
108
- spans.push({ start, end: properties[j + 1].getStart(sf) });
109
- } else if (i > 0) {
110
- spans.push({ start: properties[i - 1].getEnd(), end });
111
- } else {
112
- spans.push({ start, end });
113
- }
114
- i = j;
115
- }
116
- return spans;
117
- }
118
-
119
- function tagsOf(property) {
120
- if (!ts.isPropertyAssignment(property) || !ts.isArrayLiteralExpression(property.initializer)) return undefined;
121
- return property.initializer.elements.filter(isText).map((element) => element.text);
122
- }
123
-
124
- /**
125
- * `text` with every upload entry's knowledge stated as its `account`, and the lines it
126
- * hands back; `undefined` when there is nothing to rewrite.
127
- */
128
- export function rewriteSource(fileName, text) {
129
- const kind = fileName.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
130
- const sf = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, kind);
131
- const edits = [];
132
- const reasons = [];
133
-
134
- const rewriteEntry = (literal, entryKind) => {
135
- const at = (name) => literal.properties.findIndex((property) => nameOf(property) === name && (ts.isPropertyAssignment(property) || ts.isShorthandPropertyAssignment(property)));
136
- const retired = literal.properties.map((property, index) => ({ name: nameOf(property), index })).filter(({ name }) => RETIRED.has(name));
137
- if (retired.length === 0) return;
138
- const line = `line ${String(lineOf(sf, literal.getStart(sf)))}`;
139
- if (at("account") !== -1) {
140
- reasons.push(`Manual: ${line}: this upload states an \`account\` beside ${retired.map(({ name }) => `\`${name}\``).join(", ")}; fold what they say into the account and remove them`);
141
- return;
142
- }
143
-
144
- // What the entry said of the upload, in its order: a caption (with a video's
145
- // description) or a document's notes.
146
- const sayers = entryKind === "document" ? ["notes"] : entryKind === "video" ? ["caption", "description"] : ["caption"];
147
- const said = sayers.map((name) => ({ name, index: at(name) })).filter(({ index }) => index !== -1);
148
- const values = said.map(({ index }) => statedValue(sf, literal.properties[index]));
149
- const expressions = values.filter((value) => value.expression !== undefined);
150
- const words = values.filter((value) => value.says !== undefined && value.says !== null).map((value) => value.says);
151
- if (expressions.length > 0 && expressions.length + words.length > 1) {
152
- reasons.push(
153
- `Manual: ${line}: state this upload's \`account\` from its ${said.map(({ name }) => `\`${name}\``).join(" and ")} — ` +
154
- "one of them is not written as text, so they were not joined — and remove them",
155
- );
156
- return;
157
- }
158
-
159
- // The first sayer becomes the account (or leaves, when it said nothing); every other
160
- // retired member leaves.
161
- const [first] = said;
162
- const account =
163
- expressions.length === 1 ? expressions[0].expression : words.length > 0 ? JSON.stringify(words.join("\n")) : undefined;
164
- // When the first sayer alone said it, as written, only its name changes, so the
165
- // author's layout of the words stays.
166
- const firstSaidAll = first !== undefined && values.filter((value) => value.says !== null).length === 1 && values[0].says !== null;
167
- const removed = new Set();
168
- for (const { name, index } of retired) {
169
- if (first !== undefined && index === first.index && account !== undefined) {
170
- const property = literal.properties[index];
171
- if (firstSaidAll && ts.isPropertyAssignment(property)) {
172
- edits.push({ start: property.name.getStart(sf), end: property.name.getEnd(), text: "account" });
173
- } else {
174
- edits.push({ start: property.getStart(sf), end: property.getEnd(), text: `account: ${account}` });
175
- }
176
- } else {
177
- removed.add(index);
178
- }
179
- if (name === "tags" && account === undefined) {
180
- const tags = tagsOf(literal.properties[index]);
181
- if (tags === undefined || tags.length > 0) {
182
- reasons.push(
183
- `Manual: ${line}: this upload's tags${tags === undefined ? "" : ` (${tags.join(", ")})`} were the only statement of what it is; ` +
184
- "state an `account` — plain statements of what it is, what it shows, every word printed on it and how it looks — or leave it unstated",
185
- );
186
- }
187
- }
188
- }
189
- for (const span of removalSpans(sf, text, literal, removed)) edits.push({ ...span, text: "" });
190
- };
191
-
192
- const visit = (node) => {
193
- if (ts.isObjectLiteralExpression(node)) {
194
- const entryKind = uploadKind(node);
195
- if (entryKind !== undefined) rewriteEntry(node, entryKind);
196
- }
197
- ts.forEachChild(node, visit);
198
- };
199
- visit(sf);
200
- if (edits.length === 0 && reasons.length === 0) return undefined;
201
- let out = text;
202
- for (const edit of edits.sort((a, b) => b.start - a.start)) out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
203
- return { text: out, changed: edits.length > 0, reasons };
204
- }
205
-
206
- /** @param {string} root @returns {Promise<{rewritten: string[], notRewritten: {file: string, reason: string}[]}>} */
207
- export default async function writtenWithoutRetiredKnowledge(root) {
208
- const rewritten = [];
209
- const notRewritten = [];
210
- for (const file of await sourceFiles(root)) {
211
- const before = await readFile(file, "utf8");
212
- if (!/\b(tags|caption|notes)\b/.test(before)) continue;
213
- const result = rewriteSource(file, before);
214
- if (result === undefined) continue;
215
- const rel = relative(root, file).split(sep).join("/");
216
- if (result.changed) {
217
- await writeFile(file, result.text, "utf8");
218
- rewritten.push(rel);
219
- }
220
- for (const reason of result.reasons) notRewritten.push({ file: rel, reason });
221
- }
222
- return { rewritten, notRewritten };
223
- }
@@ -1,202 +0,0 @@
1
- // 9.0.0 — the property's sorted list of what shows it is keyed `property_media`.
2
- //
3
- // `media` names the open-kind slot builder and a candidate pool, so the list of the
4
- // property's photos, videos and tours, sorted out of its uploads, is keyed
5
- // `property_media`: a slot picks `from: fact.property.property_media`, a `compute()` reads
6
- // `fact.property.property_media`, and a fixture's `Property` states `property_media`.
7
- // `floor_plans` and `uploads` keep their names.
8
- //
9
- // What this codemod does, in every `.ts` and `.tsx` under the workspace:
10
- //
11
- // - `fact.property.media` becomes `fact.property.property_media`, in a file importing
12
- // `fact` from this package, under any local name;
13
- // - the string `"property.media"` — the list's registry key, as a wire literal spells it
14
- // (`{ kind: "home", key: "property.media" }`) — becomes `"property.property_media"`;
15
- // - a fixture's property states `property_media` instead of `media`: an object literal
16
- // stating `uploads` or `floor_plans`, spreading a `Property` (a variant), or typed
17
- // `Property` (`: Property`, `satisfies Property`, `as Property`);
18
- // - `.media` read off a name declared with the type `Property` (`(property: Property) =>
19
- // property.media.filter(…)`), here or in the relative module it is imported from by
20
- // name (`sparseListing.media`), reads `.property_media`.
21
- //
22
- // Every other `media` is left as it is: the `media()` builder, a slot or field named
23
- // `media`, a fixture's media catalog.
24
-
25
- import { readFileSync } from "node:fs";
26
- import { readdir, readFile, writeFile } from "node:fs/promises";
27
- import { dirname, join, relative, resolve, 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
- const OLD_KEY = "media";
35
- const NEW_KEY = "property_media";
36
- // The keys a property states beside its sorted list, any one of which marks a literal as one.
37
- const PROPERTY_SIBLINGS = new Set(["uploads", "floor_plans"]);
38
-
39
- async function sourceFiles(root) {
40
- const found = [];
41
- async function walk(dir) {
42
- for (const entry of await readdir(dir, { withFileTypes: true })) {
43
- if (entry.isDirectory()) {
44
- if (!SKIP_DIRS.has(entry.name)) await walk(join(dir, entry.name));
45
- } else if (EXTENSIONS.some((ext) => entry.name.endsWith(ext)) && !entry.name.endsWith(".d.ts")) {
46
- found.push(join(dir, entry.name));
47
- }
48
- }
49
- }
50
- await walk(root);
51
- return found.sort();
52
- }
53
-
54
- function nameOf(property) {
55
- const name = property.name;
56
- return name !== undefined && (ts.isIdentifier(name) || ts.isStringLiteral(name)) ? name.text : undefined;
57
- }
58
-
59
- // The local names `fact` is imported under from the framework — `fact`, or its alias.
60
- function factNames(sf, isFramework) {
61
- const names = new Set();
62
- for (const statement of sf.statements) {
63
- if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier) || !isFramework(statement.moduleSpecifier.text)) continue;
64
- const bindings = statement.importClause?.namedBindings;
65
- if (bindings === undefined || !ts.isNamedImports(bindings)) continue;
66
- for (const element of bindings.elements) {
67
- if ((element.propertyName ?? element.name).text === "fact") names.add(element.name.text);
68
- }
69
- }
70
- return names;
71
- }
72
-
73
- // Whether a type annotation is the fixture's `Property`.
74
- function isPropertyType(type) {
75
- return type !== undefined && ts.isTypeReferenceNode(type) && ts.isIdentifier(type.typeName) && type.typeName.text === "Property";
76
- }
77
-
78
- // Every name a module declares with the type `Property` — a parameter or a variable.
79
- function declaredPropertyNames(sf) {
80
- const names = new Set();
81
- const visit = (node) => {
82
- if ((ts.isParameter(node) || ts.isVariableDeclaration(node)) && ts.isIdentifier(node.name) && isPropertyType(node.type)) names.add(node.name.text);
83
- ts.forEachChild(node, visit);
84
- };
85
- visit(sf);
86
- return names;
87
- }
88
-
89
- // The module a relative specifier names, read as `readModule` answers it — `.ts`, `.tsx`
90
- // or an index — or undefined when none is there.
91
- function relativeModule(fileName, specifier, readModule) {
92
- const base = resolve(dirname(fileName), specifier.replace(/\.(m?js|tsx?)$/, ""));
93
- for (const candidate of [`${base}.ts`, `${base}.tsx`, join(base, "index.ts")]) {
94
- const text = readModule(candidate);
95
- if (text !== undefined) return { path: candidate, text };
96
- }
97
- return undefined;
98
- }
99
-
100
- // Every name the file reads as a `Property`: one it declares so, and one it imports by
101
- // name from a relative module that declares it so (`sparseListing`, from its property file).
102
- function propertyTypedNames(fileName, sf, readModule) {
103
- const names = declaredPropertyNames(sf);
104
- for (const statement of sf.statements) {
105
- if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier) || !statement.moduleSpecifier.text.startsWith(".")) continue;
106
- const bindings = statement.importClause?.namedBindings;
107
- if (bindings === undefined || !ts.isNamedImports(bindings)) continue;
108
- const module = relativeModule(fileName, statement.moduleSpecifier.text, readModule);
109
- if (module === undefined) continue;
110
- const declared = declaredPropertyNames(parse(module.path, module.text));
111
- for (const element of bindings.elements) {
112
- if (declared.has((element.propertyName ?? element.name).text)) names.add(element.name.text);
113
- }
114
- }
115
- return names;
116
- }
117
-
118
- function parse(fileName, text) {
119
- return ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, fileName.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS);
120
- }
121
-
122
- function readIfThere(path) {
123
- try {
124
- return readFileSync(path, "utf8");
125
- } catch {
126
- return undefined;
127
- }
128
- }
129
-
130
- // Whether `access` is `<fact>.property.media` for one of the framework's `fact` names.
131
- function isFactPropertyMedia(access, facts) {
132
- if (access.name.text !== OLD_KEY) return false;
133
- const property = access.expression;
134
- return ts.isPropertyAccessExpression(property) && property.name.text === "property" && ts.isIdentifier(property.expression) && facts.has(property.expression.text);
135
- }
136
-
137
- // Whether an object literal is a property: it states `uploads` or `floor_plans`, spreads a
138
- // name read as a `Property`, or is itself typed `Property` (`: Property`, `satisfies`, `as`).
139
- function isPropertyLiteral(literal, typed) {
140
- for (const member of literal.properties) {
141
- if ((ts.isPropertyAssignment(member) || ts.isShorthandPropertyAssignment(member)) && PROPERTY_SIBLINGS.has(nameOf(member))) return true;
142
- if (ts.isSpreadAssignment(member) && ts.isIdentifier(member.expression) && typed.has(member.expression.text)) return true;
143
- }
144
- const parent = literal.parent;
145
- if (ts.isVariableDeclaration(parent) && parent.initializer === literal) return isPropertyType(parent.type);
146
- if (ts.isSatisfiesExpression(parent) || ts.isAsExpression(parent)) return isPropertyType(parent.type);
147
- return false;
148
- }
149
-
150
- /**
151
- * Rewrites one file's text; `undefined` when it holds nothing this codemod rewrites.
152
- * `isFramework` decides which import specifiers are the framework; `readModule(path)`
153
- * answers a module's text, or undefined when it is not there.
154
- */
155
- export function rewriteSource(fileName, text, isFramework = (specifier) => specifier === CONCRETE, readModule = readIfThere) {
156
- const sf = parse(fileName, text);
157
- const facts = factNames(sf, isFramework);
158
- const typed = propertyTypedNames(fileName, sf, readModule);
159
-
160
- const edits = [];
161
- const visit = (node) => {
162
- if (ts.isPropertyAccessExpression(node) && node.name.text === OLD_KEY) {
163
- const owner = node.expression;
164
- if (isFactPropertyMedia(node, facts) || (ts.isIdentifier(owner) && typed.has(owner.text))) {
165
- edits.push({ start: node.name.getStart(sf), end: node.name.getEnd(), text: NEW_KEY });
166
- }
167
- } else if (ts.isStringLiteral(node) && node.text === `property.${OLD_KEY}`) {
168
- const quote = text[node.getStart(sf)];
169
- edits.push({ start: node.getStart(sf), end: node.getEnd(), text: `${quote}property.${NEW_KEY}${quote}` });
170
- } else if (ts.isObjectLiteralExpression(node)) {
171
- if (isPropertyLiteral(node, typed)) {
172
- for (const property of node.properties) {
173
- if (ts.isPropertyAssignment(property) && nameOf(property) === OLD_KEY) {
174
- edits.push({ start: property.name.getStart(sf), end: property.name.getEnd(), text: NEW_KEY });
175
- } else if (ts.isShorthandPropertyAssignment(property) && property.name.text === OLD_KEY) {
176
- edits.push({ start: property.getStart(sf), end: property.getEnd(), text: `${NEW_KEY}: ${OLD_KEY}` });
177
- }
178
- }
179
- }
180
- }
181
- ts.forEachChild(node, visit);
182
- };
183
- visit(sf);
184
- if (edits.length === 0) return undefined;
185
- let out = text;
186
- for (const edit of edits.sort((a, b) => b.start - a.start)) out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
187
- return { text: out };
188
- }
189
-
190
- /** @param {string} root @returns {Promise<{rewritten: string[], notRewritten: {file: string, reason: string}[]}>} */
191
- export default async function writtenWithoutTheMediaKey(root) {
192
- const rewritten = [];
193
- for (const file of await sourceFiles(root)) {
194
- const before = await readFile(file, "utf8");
195
- if (!before.includes(OLD_KEY)) continue;
196
- const result = rewriteSource(file, before);
197
- if (result === undefined || result.text === before) continue;
198
- await writeFile(file, result.text, "utf8");
199
- rewritten.push(relative(root, file).split(sep).join("/"));
200
- }
201
- return { rewritten, notRewritten: [] };
202
- }
@@ -1,249 +0,0 @@
1
- // 9.0.0 — the property type is gone: `fact.property.property_type` and a comparable's
2
- // `property_type`.
3
- //
4
- // One units table serves every property, so nothing branches on a declared type: a
5
- // template that wants one home versus many counts `fact.property.units` itself. The fact,
6
- // its option vocabulary and the comparable's member leave the platform together.
7
- //
8
- // What this codemod does, in every `.ts` and `.tsx` under the workspace:
9
- //
10
- // - `fact.property.property_type` (in a file importing `fact` from this package, under
11
- // any local name) is removed from every array it is an element of — a fill's `reads`,
12
- // the `PROPERTY` list `fill-reads.ts` hoists — except a `compute()`'s `reads`;
13
- // - a fixture's property drops `property_type`: an object literal stating `uploads`,
14
- // `floor_plans`, `property_media`, `listing_intent` or `comparables`, spreading a
15
- // `Property`, or typed `Property` (`: Property`, `satisfies Property`, `as Property`);
16
- // - a fixture comparable drops `property_type`: an item of a property's `comparables`,
17
- // or an object literal typed `ComparableEntry` (or held in a `ComparableEntry[]`).
18
- //
19
- // What it hands back, by line, as `Manual:` — each left as it was, since only its author
20
- // knows what it should read instead:
21
- //
22
- // - a `compute()` whose `reads` name the type: its callback takes the value by position,
23
- // so dropping the read would hand every later parameter the wrong value;
24
- // - any other `fact.property.property_type` — a slot bound to it, a select's `from`;
25
- // - the registry key `"property.property_type"` written as a string;
26
- // - `.property_type` read off a name declared with the type `Property`.
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
- const REMOVED = "property_type";
37
- // The keys a property states beside its type, any one of which marks a literal as one.
38
- const PROPERTY_SIBLINGS = new Set(["uploads", "floor_plans", "property_media", "listing_intent", "comparables"]);
39
- const COMPARABLE_TYPE = "ComparableEntry";
40
- const COMPUTE_HINT =
41
- "this `compute()` reads `fact.property.property_type`, which is gone; count `fact.property.units` instead (one unit is a single home) and take that value in its parameter's place";
42
-
43
- async function sourceFiles(root) {
44
- const found = [];
45
- async function walk(dir) {
46
- for (const entry of await readdir(dir, { withFileTypes: true })) {
47
- if (entry.isDirectory()) {
48
- if (!SKIP_DIRS.has(entry.name)) await walk(join(dir, entry.name));
49
- } else if (EXTENSIONS.some((ext) => entry.name.endsWith(ext)) && !entry.name.endsWith(".d.ts")) {
50
- found.push(join(dir, entry.name));
51
- }
52
- }
53
- }
54
- await walk(root);
55
- return found.sort();
56
- }
57
-
58
- function nameOf(property) {
59
- const name = property.name;
60
- return name !== undefined && (ts.isIdentifier(name) || ts.isStringLiteral(name)) ? name.text : undefined;
61
- }
62
-
63
- function lineOf(sf, position) {
64
- return sf.getLineAndCharacterOfPosition(position).line + 1;
65
- }
66
-
67
- // The local names `fact` is imported under from the framework — `fact`, or its alias.
68
- function factNames(sf, isFramework) {
69
- const names = new Set();
70
- for (const statement of sf.statements) {
71
- if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier) || !isFramework(statement.moduleSpecifier.text)) continue;
72
- const bindings = statement.importClause?.namedBindings;
73
- if (bindings === undefined || !ts.isNamedImports(bindings)) continue;
74
- for (const element of bindings.elements) {
75
- if ((element.propertyName ?? element.name).text === "fact") names.add(element.name.text);
76
- }
77
- }
78
- return names;
79
- }
80
-
81
- function isTypeNamed(type, name) {
82
- if (type === undefined) return false;
83
- if (ts.isTypeOperatorNode(type)) return isTypeNamed(type.type, name);
84
- return ts.isTypeReferenceNode(type) && ts.isIdentifier(type.typeName) && type.typeName.text === name;
85
- }
86
-
87
- function isArrayOf(type, name) {
88
- if (type === undefined) return false;
89
- if (ts.isTypeOperatorNode(type)) return isArrayOf(type.type, name);
90
- if (ts.isArrayTypeNode(type)) return isTypeNamed(type.elementType, name);
91
- return (
92
- ts.isTypeReferenceNode(type) &&
93
- ts.isIdentifier(type.typeName) &&
94
- (type.typeName.text === "Array" || type.typeName.text === "ReadonlyArray") &&
95
- type.typeArguments?.length === 1 &&
96
- isTypeNamed(type.typeArguments[0], name)
97
- );
98
- }
99
-
100
- // The type a literal is declared or asserted with: `: T`, `satisfies T`, `as T`.
101
- function statedType(literal) {
102
- const parent = literal.parent;
103
- if (ts.isVariableDeclaration(parent) && parent.initializer === literal) return parent.type;
104
- if (ts.isSatisfiesExpression(parent) || ts.isAsExpression(parent)) return parent.type;
105
- return undefined;
106
- }
107
-
108
- // Every name a module declares with the type `Property` — a parameter or a variable.
109
- function propertyTypedNames(sf) {
110
- const names = new Set();
111
- const visit = (node) => {
112
- if ((ts.isParameter(node) || ts.isVariableDeclaration(node)) && ts.isIdentifier(node.name) && isTypeNamed(node.type, "Property")) names.add(node.name.text);
113
- ts.forEachChild(node, visit);
114
- };
115
- visit(sf);
116
- return names;
117
- }
118
-
119
- function isPropertyLiteral(literal, typed) {
120
- for (const member of literal.properties) {
121
- if ((ts.isPropertyAssignment(member) || ts.isShorthandPropertyAssignment(member)) && PROPERTY_SIBLINGS.has(nameOf(member))) return true;
122
- if (ts.isSpreadAssignment(member) && ts.isIdentifier(member.expression) && typed.has(member.expression.text)) return true;
123
- }
124
- return isTypeNamed(statedType(literal), "Property");
125
- }
126
-
127
- function isComparableLiteral(literal, typed) {
128
- if (isTypeNamed(statedType(literal), COMPARABLE_TYPE)) return true;
129
- const array = literal.parent;
130
- if (!ts.isArrayLiteralExpression(array)) return false;
131
- if (isArrayOf(statedType(array), COMPARABLE_TYPE)) return true;
132
- const holder = array.parent;
133
- return ts.isPropertyAssignment(holder) && nameOf(holder) === "comparables" && ts.isObjectLiteralExpression(holder.parent) && isPropertyLiteral(holder.parent, typed);
134
- }
135
-
136
- // Whether `node` is `<fact>.property.property_type` for one of the framework's `fact` names.
137
- function isRemovedFact(node, facts) {
138
- if (!ts.isPropertyAccessExpression(node) || node.name.text !== REMOVED) return false;
139
- const property = node.expression;
140
- return ts.isPropertyAccessExpression(property) && property.name.text === "property" && ts.isIdentifier(property.expression) && facts.has(property.expression.text);
141
- }
142
-
143
- // Whether `array` is the `reads` of a `compute({ reads }, …)` call.
144
- function isComputeReads(array) {
145
- const assignment = array.parent;
146
- if (!ts.isPropertyAssignment(assignment) || nameOf(assignment) !== "reads") return false;
147
- const options = assignment.parent;
148
- const call = options.parent;
149
- if (!ts.isObjectLiteralExpression(options) || call === undefined || !ts.isCallExpression(call) || call.arguments[0] !== options) return false;
150
- const callee = call.expression;
151
- return (ts.isIdentifier(callee) && callee.text === "compute") || (ts.isPropertyAccessExpression(callee) && callee.name.text === "compute");
152
- }
153
-
154
- // The spans a list's removed members occupy, one per run of neighbours removed together,
155
- // so no two overlap: whole lines when each starts its own line, else the run up to the
156
- // member after it — or, when the run ends the list, from the member before it, so no
157
- // dangling comma is left.
158
- function removalSpans(sf, text, members, removed) {
159
- const spans = [];
160
- for (let i = 0; i < members.length; i++) {
161
- if (!removed.has(i)) continue;
162
- let j = i;
163
- while (removed.has(j + 1)) j++;
164
- const start = members[i].getStart(sf);
165
- const end = members[j].getEnd();
166
- const ownLines = members.slice(i, j + 1).every((member) => {
167
- const at = member.getStart(sf);
168
- return text.slice(text.lastIndexOf("\n", at - 1) + 1, at).trim() === "";
169
- });
170
- const after = text.slice(end).match(/^,?[ \t]*\n/);
171
- if (ownLines && after !== null) {
172
- spans.push({ start: text.lastIndexOf("\n", start - 1) + 1, end: end + after[0].length });
173
- } else if (j + 1 < members.length) {
174
- spans.push({ start, end: members[j + 1].getStart(sf) });
175
- } else if (i > 0) {
176
- spans.push({ start: members[i - 1].getEnd(), end });
177
- } else {
178
- spans.push({ start, end });
179
- }
180
- i = j;
181
- }
182
- return spans;
183
- }
184
-
185
- /**
186
- * `text` with the property type removed wherever it is a list member or a fixture's, and
187
- * the lines it hands back; `undefined` when there is nothing to rewrite or report.
188
- * `isFramework` decides which import specifiers are the framework.
189
- */
190
- export function rewriteSource(fileName, text, isFramework = (specifier) => specifier === CONCRETE) {
191
- const kind = fileName.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
192
- const sf = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, kind);
193
- const facts = factNames(sf, isFramework);
194
- const typed = propertyTypedNames(sf);
195
- const edits = [];
196
- const reasons = [];
197
- const handled = new Set();
198
- const manual = (node, reason) => reasons.push(`Manual: line ${String(lineOf(sf, node.getStart(sf)))}: ${reason}`);
199
-
200
- const visit = (node) => {
201
- if (ts.isArrayLiteralExpression(node)) {
202
- const removed = new Set();
203
- node.elements.forEach((element, index) => {
204
- if (!isRemovedFact(element, facts)) return;
205
- handled.add(element);
206
- if (isComputeReads(node)) manual(element, COMPUTE_HINT);
207
- else removed.add(index);
208
- });
209
- for (const span of removalSpans(sf, text, node.elements, removed)) edits.push({ ...span, text: "" });
210
- } else if (ts.isObjectLiteralExpression(node) && (isPropertyLiteral(node, typed) || isComparableLiteral(node, typed))) {
211
- const removed = new Set();
212
- node.properties.forEach((property, index) => {
213
- if ((ts.isPropertyAssignment(property) || ts.isShorthandPropertyAssignment(property)) && nameOf(property) === REMOVED) removed.add(index);
214
- });
215
- for (const span of removalSpans(sf, text, node.properties, removed)) edits.push({ ...span, text: "" });
216
- } else if (isRemovedFact(node, facts) && !handled.has(node)) {
217
- manual(node, "this names `fact.property.property_type`, which is gone; bind or read what the section means by it instead (`fact.property.units` counts the homes)");
218
- } else if (ts.isStringLiteral(node) && node.text === `property.${REMOVED}`) {
219
- manual(node, 'the registry key `"property.property_type"` names a fact that is gone; remove it');
220
- } else if (ts.isPropertyAccessExpression(node) && node.name.text === REMOVED && ts.isIdentifier(node.expression) && typed.has(node.expression.text)) {
221
- manual(node, "a fixture `Property` states no `property_type` any more; count its `units` instead");
222
- }
223
- ts.forEachChild(node, visit);
224
- };
225
- visit(sf);
226
- if (edits.length === 0 && reasons.length === 0) return undefined;
227
- let out = text;
228
- for (const edit of edits.sort((a, b) => b.start - a.start)) out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
229
- return { text: out, changed: edits.length > 0, reasons };
230
- }
231
-
232
- /** @param {string} root @returns {Promise<{rewritten: string[], notRewritten: {file: string, reason: string}[]}>} */
233
- export default async function writtenWithoutThePropertyType(root) {
234
- const rewritten = [];
235
- const notRewritten = [];
236
- for (const file of await sourceFiles(root)) {
237
- const before = await readFile(file, "utf8");
238
- if (!before.includes(REMOVED)) continue;
239
- const result = rewriteSource(file, before);
240
- if (result === undefined) continue;
241
- const rel = relative(root, file).split(sep).join("/");
242
- if (result.changed) {
243
- await writeFile(file, result.text, "utf8");
244
- rewritten.push(rel);
245
- }
246
- for (const reason of result.reasons) notRewritten.push({ file: rel, reason });
247
- }
248
- return { rewritten, notRewritten };
249
- }