@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.
- package/CHANGELOG.md +48 -0
- package/codemods/manifest.json +7 -154
- package/dist/artifact/playground/client/assets/index-BcLGpWLR.js +1266 -0
- package/dist/artifact/playground/client/assets/index-CxWPYOH2.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 +39 -40
- package/dist/base.css +18 -65
- package/dist/cli/build/artifact.js +1 -1
- package/dist/cli/build/fixtures.js +3 -2
- package/dist/cli/capture/capture.js +8 -6
- package/dist/cli/check/stages/sections.js +1 -1
- package/dist/cli/dev/workspace-reader.js +4 -2
- package/dist/css/compile.js +3 -6
- package/dist/css/rules.js +1 -51
- package/dist/design-system/format-variants.js +1 -1
- package/dist/fixtures/resolve.js +5 -1
- package/dist/fixtures/rows.js +2 -2
- package/dist/index.d.ts +5 -5
- package/dist/packages/external/concrete/package.js +1 -1
- package/dist/packages/internal/fill-bake/dist/compute-reads.js +1 -1
- package/dist/packages/internal/fill-bake/dist/held-rows.js +2 -0
- package/dist/packages/internal/fill-bake/dist/index.js +1 -0
- package/dist/packages/internal/fill-vocabulary/dist/holder.js +3 -14
- package/dist/packages/internal/format-geometry/dist/capture.js +3 -11
- package/dist/packages/internal/format-geometry/dist/format.d.ts +2 -8
- package/dist/packages/internal/format-geometry/dist/geometry.js +2 -13
- package/dist/packages/internal/media-resolution/dist/document.js +3 -3
- package/dist/packages/internal/media-resolution/dist/image.js +3 -3
- package/dist/packages/internal/media-resolution/dist/tour.js +6 -6
- package/dist/packages/internal/media-resolution/dist/video.js +9 -9
- package/dist/packages/internal/section-contracts/dist/fill-verb-contract.js +1 -0
- package/dist/packages/internal/section-contracts/dist/runners.js +1 -3
- package/dist/packages/internal/section-contracts/dist/source-contract.js +3 -3
- package/dist/packages/internal/section-contracts/dist/sources-contract.js +0 -2
- package/dist/packages/internal/template-artifact/dist/index.js +2 -2
- package/dist/packages/internal/template-artifact/dist/layout.js +11 -11
- package/dist/packages/internal/template-artifact/dist/manifest.js +1 -1
- package/dist/packages/internal/template-artifact/dist/verify.js +13 -17
- package/dist/packages/internal/template-contracts/dist/document-value.d.ts +3 -5
- package/dist/packages/internal/template-contracts/dist/fact-registry.js +1 -1
- package/dist/packages/internal/template-contracts/dist/fill-builders.js +2 -6
- package/dist/packages/internal/template-contracts/dist/fill-spec-schema.js +4 -1
- package/dist/packages/internal/template-contracts/dist/list.js +0 -14
- package/dist/packages/internal/template-contracts/dist/primitive-descriptors.js +1 -1
- package/dist/packages/internal/template-contracts/dist/runtime-values.d.ts +6 -7
- package/dist/packages/internal/template-contracts/dist/slot-builders.js +0 -2
- package/dist/packages/internal/template-contracts/dist/slot-ref.d.ts +6 -1
- package/dist/packages/internal/template-contracts/dist/tour-value.d.ts +2 -4
- package/dist/packages/internal/template-contracts/dist/video-value.d.ts +3 -4
- package/dist/primitives/FrameShell.d.ts +5 -2
- package/dist/primitives/FrameShell.js +2 -1
- package/dist/primitives/Image.d.ts +2 -2
- package/dist/primitives/Tour.d.ts +2 -2
- package/dist/primitives/Video.d.ts +2 -2
- package/dist/primitives/slot-bindings.js +6 -13
- package/dist/primitives/slot-document.d.ts +1 -1
- package/dist/primitives/slot-download.d.ts +3 -1
- package/dist/primitives/slot-media.d.ts +5 -2
- package/dist/primitives/slot-media.js +1 -1
- package/dist/rules/registry.js +1 -7
- package/dist/schema/format-presets.d.ts +1 -1
- package/dist/schema/format.d.ts +0 -1
- package/dist/schema/format.js +1 -2
- package/dist/schema/manifest.js +18 -24
- package/dist/schema/section-schema.d.ts +5 -5
- package/dist/site-analytics.js +6 -1
- package/dist/site-runtime/window-height.js +19 -0
- package/dist/styles.css +4 -4
- package/dist/template/define-template.d.ts +11 -0
- package/dist/template/define-template.js +1 -1
- package/dist/template/materialize.js +2 -0
- 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 +1 -1
- package/guide/reference/cli/publish.md +1 -1
- package/guide/reference/cli/upgrade.md +1 -1
- package/guide/reference/rules/INDEX.md +2 -3
- package/package.json +2 -2
- package/codemods/fill-is-a-source.mjs +0 -533
- package/codemods/framework-namespace.mjs +0 -107
- package/codemods/list-grammar.mjs +0 -923
- package/codemods/object-form-only.mjs +0 -39
- package/codemods/platform-namespace.mjs +0 -83
- package/codemods/process-builders.mjs +0 -198
- package/codemods/reads-replace-reconcile.mjs +0 -285
- package/codemods/registry-keys-only.mjs +0 -113
- package/codemods/slot-object.mjs +0 -1097
- package/codemods/steps/content-object-fields.mjs +0 -324
- package/codemods/steps/list-object-form.mjs +0 -686
- package/codemods/steps/registry-key-reads.mjs +0 -276
- package/codemods/steps/select-object-form.mjs +0 -210
- package/codemods/table-rows.mjs +0 -568
- package/codemods/theme-collapse.mjs +0 -396
- package/codemods/unlayer-author-css.mjs +0 -194
- package/codemods/unwrap-object-fields.mjs +0 -310
- package/codemods/viewer-slot-document.mjs +0 -131
- package/codemods/write-property-as-facts-document.mjs +0 -716
- package/codemods/write-the-address-line.mjs +0 -255
- package/codemods/write-the-registry-members.mjs +0 -653
- package/codemods/write-the-select-from.mjs +0 -141
- package/codemods/write-the-sole-from.mjs +0 -369
- package/codemods/write-the-table-from.mjs +0 -267
- package/codemods/write-the-whole-item.mjs +0 -512
- package/codemods/writeback-drop.mjs +0 -168
- package/codemods/written-contact-address.mjs +0 -162
- package/codemods/written-media-category.mjs +0 -227
- package/codemods/written-ref-kinds-name-the-layer.mjs +0 -172
- package/codemods/written-uploads-replace-pool-reads.mjs +0 -454
- package/codemods/written-uploads-sort-into-lists.mjs +0 -405
- package/codemods/written-uploads-stated-in-sorted-lists.mjs +0 -295
- package/codemods/written-video-sources-drop.mjs +0 -144
- package/codemods/written-without-pick-none.mjs +0 -146
- package/codemods/written-without-retired-knowledge.mjs +0 -223
- package/codemods/written-without-the-media-key.mjs +0 -202
- package/codemods/written-without-the-property-type.mjs +0 -249
- package/dist/artifact/playground/client/assets/index-BFNHB7VM.css +0 -1
- package/dist/artifact/playground/client/assets/index-Div1yo6s.js +0 -1259
package/codemods/table-rows.mjs
DELETED
|
@@ -1,568 +0,0 @@
|
|
|
1
|
-
// 9.0.0 — a table is read through a row: `fact.property.contacts[0].phone`, a read by
|
|
2
|
-
// position, is gone, and a section reads one row of a table through a `row()` slot the
|
|
3
|
-
// user can see and re-pick.
|
|
4
|
-
//
|
|
5
|
-
// What this codemod does, in every `.ts` and `.tsx` under the workspace, for each table a
|
|
6
|
-
// section reads by position (`fact.property.<table>[0].<member>`):
|
|
7
|
-
//
|
|
8
|
-
// - Declares the row once, before the section: `const contact = row({ label: "Contact",
|
|
9
|
-
// source: fact.property.contacts })` — the table's first row, re-pickable — and reads
|
|
10
|
-
// every member through it: `contact.phone`, in a `source:`, a `compute()`'s `reads` and
|
|
11
|
-
// a fill's `reads` alike.
|
|
12
|
-
// - Groups the row with the slots that read it, where it can:
|
|
13
|
-
// · every reader a field of one object, or a top-level slot beside it → the row joins
|
|
14
|
-
// that object's fields, and a top-level reader reads it there;
|
|
15
|
-
// · every reader a top-level slot → the readers move into a new object of the row's
|
|
16
|
-
// name, `contact: object({ label: compute(() => "Contact"), fields: { contact, name,
|
|
17
|
-
// phone } })`,
|
|
18
|
-
// their keys losing the prefix they share (`agent_name` → `name`); the render
|
|
19
|
-
// reads `slot.contact.fields.name`, and the element holding every reader becomes
|
|
20
|
-
// `<Slot.Object slot={slot.contact}>`.
|
|
21
|
-
// - Otherwise declares the row as a top-level slot (`contact,`), which every slot of the
|
|
22
|
-
// section — a display entry, a field, an item — reads in scope, so no markup moves.
|
|
23
|
-
// It is `locked: true`: a top-level row has no card to show its bar in and nothing on
|
|
24
|
-
// the canvas selects it, so it keeps reading the table's first row.
|
|
25
|
-
// - Imports `row` (and `object`, `compute`, `Slot`) where a rewrite uses them.
|
|
26
|
-
//
|
|
27
|
-
// What it leaves to you, reported per file as `Manual:` lines:
|
|
28
|
-
//
|
|
29
|
-
// - Top-level readers it could not group — rendered outside the render function, read
|
|
30
|
-
// from another file or section, or with no one element holding them all — keep their
|
|
31
|
-
// place beside a locked top-level row: group them with the row into an object by hand
|
|
32
|
-
// if they are one card, and drop `locked` to let the user re-pick it.
|
|
33
|
-
//
|
|
34
|
-
// Refused, by line: a read by a position other than the first, a whole row read by
|
|
35
|
-
// position, and a read outside any section in a file declaring several.
|
|
36
|
-
|
|
37
|
-
import { readdir, readFile, writeFile } from "node:fs/promises";
|
|
38
|
-
import { basename, dirname, join, relative, sep } from "node:path";
|
|
39
|
-
|
|
40
|
-
import ts from "typescript";
|
|
41
|
-
|
|
42
|
-
const CONCRETE = "@homepages/concrete";
|
|
43
|
-
|
|
44
|
-
const EXTENSIONS = [".ts", ".tsx"];
|
|
45
|
-
const SKIP_DIRS = new Set(["node_modules", ".concrete", ".git", "dist", "build"]);
|
|
46
|
-
|
|
47
|
-
/** Each table's row, by the singular a section names it with. */
|
|
48
|
-
const SINGULAR = { units: "unit", contacts: "contact", comparables: "comparable" };
|
|
49
|
-
|
|
50
|
-
/** A slot whose declaration cannot sit in an object's `fields`. */
|
|
51
|
-
const NOT_A_FIELD = /^\s*(object\(|list\([\s\S]*(\bobject\(|\brow\())/;
|
|
52
|
-
|
|
53
|
-
/** A binding read for its value, which renders nothing and so needs no container. */
|
|
54
|
-
const VALUE_READS = new Set(["present", "value"]);
|
|
55
|
-
|
|
56
|
-
async function sourceFiles(root) {
|
|
57
|
-
const found = [];
|
|
58
|
-
async function walk(dir) {
|
|
59
|
-
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
60
|
-
if (entry.isDirectory()) {
|
|
61
|
-
if (!SKIP_DIRS.has(entry.name)) await walk(join(dir, entry.name));
|
|
62
|
-
} else if (EXTENSIONS.some((ext) => entry.name.endsWith(ext)) && !entry.name.endsWith(".d.ts")) {
|
|
63
|
-
found.push(join(dir, entry.name));
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
await walk(root);
|
|
68
|
-
return found.sort();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Text replacements, innermost first. An outer rewrite TAKES the text of the span it
|
|
73
|
-
* moves with the inner edits already applied, so a member read inside a moved slot is
|
|
74
|
-
* rewritten once and carried along.
|
|
75
|
-
*/
|
|
76
|
-
class Edits {
|
|
77
|
-
constructor(text) {
|
|
78
|
-
this.text = text;
|
|
79
|
-
this.list = [];
|
|
80
|
-
}
|
|
81
|
-
add(start, end, text) {
|
|
82
|
-
this.list.push({ start, end, text });
|
|
83
|
-
}
|
|
84
|
-
/** The text of [start, end) with every edit inside it applied — those edits absorbed. */
|
|
85
|
-
take(start, end) {
|
|
86
|
-
const inside = this.list.filter((e) => e.start >= start && e.end <= end && !(e.start === e.end && (e.start === start || e.start === end)));
|
|
87
|
-
this.list = this.list.filter((e) => !inside.includes(e));
|
|
88
|
-
inside.sort((a, b) => b.start - a.start || b.end - a.end);
|
|
89
|
-
let out = this.text.slice(start, end);
|
|
90
|
-
for (const e of inside) out = out.slice(0, e.start - start) + e.text + out.slice(e.end - start);
|
|
91
|
-
return out;
|
|
92
|
-
}
|
|
93
|
-
apply() {
|
|
94
|
-
const sorted = [...this.list].sort((a, b) => b.start - a.start || b.end - a.end);
|
|
95
|
-
let out = this.text;
|
|
96
|
-
let floor = Infinity;
|
|
97
|
-
for (const e of sorted) {
|
|
98
|
-
if (e.end > floor) throw new Error(`table-rows: overlapping rewrites at ${String(e.start)}`);
|
|
99
|
-
out = out.slice(0, e.start) + e.text + out.slice(e.end);
|
|
100
|
-
floor = e.start;
|
|
101
|
-
}
|
|
102
|
-
return out;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function lineIndent(text, pos) {
|
|
107
|
-
const start = text.lastIndexOf("\n", pos - 1) + 1;
|
|
108
|
-
return /^[ \t]*/.exec(text.slice(start, pos))[0];
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/** Moves every continuation line of `text` from `from` indentation to `to`. */
|
|
112
|
-
function reindent(text, from, to) {
|
|
113
|
-
return text
|
|
114
|
-
.split("\n")
|
|
115
|
-
.map((line, i) => (i > 0 && line.startsWith(from) ? to + line.slice(from.length) : line))
|
|
116
|
-
.join("\n");
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function lineOf(sf, node) {
|
|
120
|
-
return sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function propertyKey(property) {
|
|
124
|
-
if (ts.isShorthandPropertyAssignment(property)) return property.name.text;
|
|
125
|
-
if (!ts.isPropertyAssignment(property)) return undefined;
|
|
126
|
-
const { name } = property;
|
|
127
|
-
return ts.isIdentifier(name) || ts.isStringLiteral(name) ? name.text : undefined;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function objectMember(object, key) {
|
|
131
|
-
return object.properties.find((p) => propertyKey(p) === key);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/** The removal range of one property of an object literal: itself, its leading trivia and its comma. */
|
|
135
|
-
function propertyRemoval(text, property, sf) {
|
|
136
|
-
const end = property.getEnd();
|
|
137
|
-
let at = end;
|
|
138
|
-
while (at < text.length && /\s/.test(text[at])) at += 1;
|
|
139
|
-
if (text[at] === ",") return { start: property.getFullStart(), end: at + 1 };
|
|
140
|
-
let before = property.getFullStart() - 1;
|
|
141
|
-
while (before >= 0 && /\s/.test(text[before])) before -= 1;
|
|
142
|
-
return text[before] === "," ? { start: before, end } : { start: property.getStart(sf), end };
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/** The comments above a property, as written, or "" — carried with it when it moves. */
|
|
146
|
-
function leadingComments(text, property) {
|
|
147
|
-
const ranges = ts.getLeadingCommentRanges(text, property.getFullStart()) ?? [];
|
|
148
|
-
if (ranges.length === 0) return "";
|
|
149
|
-
return text.slice(ranges[0].pos, property.getStart());
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/** Every name the file declares or imports, in any scope — a new const must shadow none. */
|
|
153
|
-
function declaredNames(sf) {
|
|
154
|
-
const names = new Set();
|
|
155
|
-
const visit = (node) => {
|
|
156
|
-
if ((ts.isVariableDeclaration(node) || ts.isParameter(node) || ts.isBindingElement(node)) && ts.isIdentifier(node.name)) names.add(node.name.text);
|
|
157
|
-
if ((ts.isFunctionDeclaration(node) || ts.isClassDeclaration(node)) && node.name !== undefined) names.add(node.name.text);
|
|
158
|
-
if (ts.isImportSpecifier(node) || ts.isImportClause(node) || ts.isNamespaceImport(node)) if (node.name !== undefined) names.add(node.name.text);
|
|
159
|
-
ts.forEachChild(node, visit);
|
|
160
|
-
};
|
|
161
|
-
visit(sf);
|
|
162
|
-
return names;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/** The longest `_`-ended prefix every key shares, or "" — stripped so a field reads as the row's. */
|
|
166
|
-
function sharedPrefix(keys) {
|
|
167
|
-
if (keys.length < 2) return "";
|
|
168
|
-
let prefix = keys[0];
|
|
169
|
-
for (const key of keys) while (!key.startsWith(prefix)) prefix = prefix.slice(0, -1);
|
|
170
|
-
const cut = prefix.lastIndexOf("_");
|
|
171
|
-
return cut <= 0 ? "" : prefix.slice(0, cut + 1);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const capitalized = (word) => word.charAt(0).toUpperCase() + word.slice(1);
|
|
175
|
-
|
|
176
|
-
function rewrite(text, file, otherFiles) {
|
|
177
|
-
if (!text.includes(CONCRETE) || !/\[\s*\d+\s*\]/.test(text)) return null;
|
|
178
|
-
const sf = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, file.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS);
|
|
179
|
-
|
|
180
|
-
let host;
|
|
181
|
-
const imported = new Map();
|
|
182
|
-
for (const statement of sf.statements) {
|
|
183
|
-
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier)) continue;
|
|
184
|
-
if (statement.moduleSpecifier.text !== CONCRETE) continue;
|
|
185
|
-
const bindings = statement.importClause?.namedBindings;
|
|
186
|
-
if (bindings === undefined || !ts.isNamedImports(bindings)) continue;
|
|
187
|
-
if (!statement.importClause.isTypeOnly && host === undefined) host = statement;
|
|
188
|
-
for (const element of bindings.elements) imported.set((element.propertyName ?? element.name).text, element.name.text);
|
|
189
|
-
}
|
|
190
|
-
const factName = imported.get("fact");
|
|
191
|
-
if (factName === undefined || host === undefined) return null;
|
|
192
|
-
const sectionName = imported.get("section");
|
|
193
|
-
|
|
194
|
-
// Every read by position: `fact.property.<table>[<n>]`, with the member read off it.
|
|
195
|
-
const reads = [];
|
|
196
|
-
const findReads = (node) => {
|
|
197
|
-
if (
|
|
198
|
-
ts.isElementAccessExpression(node) &&
|
|
199
|
-
ts.isPropertyAccessExpression(node.expression) &&
|
|
200
|
-
SINGULAR[node.expression.name.text] !== undefined &&
|
|
201
|
-
ts.isPropertyAccessExpression(node.expression.expression) &&
|
|
202
|
-
node.expression.expression.name.text === "property" &&
|
|
203
|
-
ts.isIdentifier(node.expression.expression.expression) &&
|
|
204
|
-
node.expression.expression.expression.text === factName
|
|
205
|
-
) {
|
|
206
|
-
reads.push(node);
|
|
207
|
-
}
|
|
208
|
-
ts.forEachChild(node, findReads);
|
|
209
|
-
};
|
|
210
|
-
findReads(sf);
|
|
211
|
-
if (reads.length === 0) return null;
|
|
212
|
-
|
|
213
|
-
const edits = new Edits(text);
|
|
214
|
-
const manual = [];
|
|
215
|
-
const refused = [];
|
|
216
|
-
const uses = new Set();
|
|
217
|
-
const names = declaredNames(sf);
|
|
218
|
-
|
|
219
|
-
const sections = [];
|
|
220
|
-
const findSections = (node) => {
|
|
221
|
-
if (ts.isCallExpression(node) && sectionName !== undefined && ts.isIdentifier(node.expression) && node.expression.text === sectionName && node.arguments[0] !== undefined) {
|
|
222
|
-
sections.push(node);
|
|
223
|
-
}
|
|
224
|
-
ts.forEachChild(node, findSections);
|
|
225
|
-
};
|
|
226
|
-
findSections(sf);
|
|
227
|
-
const within = (node, outer) => node.getStart(sf) >= outer.getStart(sf) && node.getEnd() <= outer.getEnd();
|
|
228
|
-
const topStatement = (node) => {
|
|
229
|
-
let at = node;
|
|
230
|
-
while (at.parent !== sf) at = at.parent;
|
|
231
|
-
return at;
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
// Each usable read by table and section; a module-level read belongs to the file's one section.
|
|
235
|
-
const byTable = new Map();
|
|
236
|
-
for (const read of reads) {
|
|
237
|
-
const table = read.expression.name.text;
|
|
238
|
-
const line = lineOf(sf, read);
|
|
239
|
-
if (!ts.isNumericLiteral(read.argumentExpression) || read.argumentExpression.text !== "0") {
|
|
240
|
-
refused.push(`\`${read.getText(sf)}\` at line ${String(line)} reads a row by a position other than the first — declare a row and re-pick it in the editor, or read it from a list`);
|
|
241
|
-
continue;
|
|
242
|
-
}
|
|
243
|
-
if (!ts.isPropertyAccessExpression(read.parent) || read.parent.expression !== read) {
|
|
244
|
-
refused.push(`\`${read.getText(sf)}\` at line ${String(line)} reads a whole row by position — declare \`const ${SINGULAR[table]} = row({ label, source: fact.property.${table} })\` and read its members`);
|
|
245
|
-
continue;
|
|
246
|
-
}
|
|
247
|
-
let section = sections.find((call) => within(read, call.arguments[0]));
|
|
248
|
-
if (section === undefined) {
|
|
249
|
-
if (sections.length !== 1) {
|
|
250
|
-
refused.push(`\`${read.parent.getText(sf)}\` at line ${String(line)} is read outside a section, in a file declaring ${sections.length === 0 ? "none" : "several"} — declare the row in the section that reads it`);
|
|
251
|
-
continue;
|
|
252
|
-
}
|
|
253
|
-
section = sections[0];
|
|
254
|
-
}
|
|
255
|
-
const key = `${table}`;
|
|
256
|
-
const perTable = byTable.get(key) ?? new Map();
|
|
257
|
-
byTable.set(key, perTable);
|
|
258
|
-
const list = perTable.get(section) ?? [];
|
|
259
|
-
list.push(read);
|
|
260
|
-
perTable.set(section, list);
|
|
261
|
-
}
|
|
262
|
-
if (byTable.size === 0) return { text: null, manual, refused };
|
|
263
|
-
|
|
264
|
-
const rowsBefore = new Map();
|
|
265
|
-
const claimed = new Set();
|
|
266
|
-
|
|
267
|
-
for (const [table, perSection] of byTable) {
|
|
268
|
-
const singular = SINGULAR[table];
|
|
269
|
-
for (const [section, tableReads] of perSection) {
|
|
270
|
-
const options = section.arguments[0];
|
|
271
|
-
const slotsProperty = ts.isObjectLiteralExpression(options) ? objectMember(options, "slots") : undefined;
|
|
272
|
-
const slots = slotsProperty !== undefined && ts.isPropertyAssignment(slotsProperty) && ts.isObjectLiteralExpression(slotsProperty.initializer) ? slotsProperty.initializer : undefined;
|
|
273
|
-
if (slots === undefined) {
|
|
274
|
-
refused.push(`the section at line ${String(lineOf(sf, section))} reads \`fact.property.${table}\` by position, but its \`slots\` is not an object literal this codemod can read — declare the row by hand`);
|
|
275
|
-
continue;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
// The row's const: the table's singular, unless the file already names it.
|
|
279
|
-
let constName = singular;
|
|
280
|
-
if (names.has(constName)) constName = `primary${capitalized(singular)}`;
|
|
281
|
-
for (let n = 2; names.has(constName); n += 1) constName = `primary${capitalized(singular)}${String(n)}`;
|
|
282
|
-
names.add(constName);
|
|
283
|
-
|
|
284
|
-
// Where each read sits: a top-level slot, a field of a top-level object, or elsewhere
|
|
285
|
-
// (a display entry, an item, the module) — only a top-level row is in scope there.
|
|
286
|
-
let forceTop = false;
|
|
287
|
-
const topReaders = new Map();
|
|
288
|
-
const objectReaders = new Map();
|
|
289
|
-
for (const read of tableReads) {
|
|
290
|
-
if (!within(read, slots)) {
|
|
291
|
-
forceTop = true;
|
|
292
|
-
continue;
|
|
293
|
-
}
|
|
294
|
-
const property = slots.properties.find((p) => within(read, p));
|
|
295
|
-
const key = propertyKey(property);
|
|
296
|
-
const init = ts.isPropertyAssignment(property) ? property.initializer : undefined;
|
|
297
|
-
if (init !== undefined && ts.isCallExpression(init) && ts.isIdentifier(init.expression) && init.expression.text === imported.get("object")) {
|
|
298
|
-
objectReaders.set(key, property);
|
|
299
|
-
} else if (init !== undefined && ts.isCallExpression(init) && ts.isIdentifier(init.expression) && init.expression.text === imported.get("list")) {
|
|
300
|
-
forceTop = true;
|
|
301
|
-
} else {
|
|
302
|
-
topReaders.set(key, property);
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
for (const read of tableReads) {
|
|
307
|
-
const access = read.parent;
|
|
308
|
-
edits.add(access.getStart(sf), access.getEnd(), `${constName}.${access.name.text}`);
|
|
309
|
-
}
|
|
310
|
-
const statement = topStatement(tableReads.reduce((first, read) => (read.getStart(sf) < first.getStart(sf) ? read : first), section));
|
|
311
|
-
const earliest = statement.getStart(sf) < topStatement(section).getStart(sf) ? statement : topStatement(section);
|
|
312
|
-
const declarations = rowsBefore.get(earliest) ?? [];
|
|
313
|
-
const rowDeclaration = (locked) =>
|
|
314
|
-
`const ${constName} = row({ label: ${JSON.stringify(capitalized(singular))}, source: ${factName}.property.${table}${locked ? ", locked: true" : ""} });`;
|
|
315
|
-
const declared = declarations.push(rowDeclaration(false)) - 1;
|
|
316
|
-
rowsBefore.set(earliest, declarations);
|
|
317
|
-
uses.add("row");
|
|
318
|
-
|
|
319
|
-
const slotKeys = new Set(slots.properties.map(propertyKey));
|
|
320
|
-
const fieldEntry = (key) => (key === constName ? constName : `${key}: ${constName}`);
|
|
321
|
-
// A top-level row has no card for its bar and no canvas target, so it is locked: the
|
|
322
|
-
// section keeps reading the primary row until an author groups it into an object.
|
|
323
|
-
const topLevel = (reason) => {
|
|
324
|
-
declarations[declared] = rowDeclaration(true);
|
|
325
|
-
let key = singular;
|
|
326
|
-
for (let n = 2; slotKeys.has(key); n += 1) key = `${singular}_row${n === 2 ? "" : String(n)}`;
|
|
327
|
-
slotKeys.add(key);
|
|
328
|
-
const first = slots.properties[0];
|
|
329
|
-
const indent = first === undefined ? "" : lineIndent(text, first.getStart(sf));
|
|
330
|
-
const at = first === undefined ? slots.getStart(sf) + 1 : first.getStart(sf);
|
|
331
|
-
edits.add(at, at, `${fieldEntry(key)},\n${indent}`);
|
|
332
|
-
if (reason !== undefined) {
|
|
333
|
-
manual.push(
|
|
334
|
-
`the slots reading \`fact.property.${table}\` by position now read the top-level row "${key}" (${reason}), locked because a top-level row cannot be re-picked — if they are one card, group them with the row into \`${singular}: object({ label: compute(() => "${capitalized(singular)}"), fields: { ${singular}, … } })\`, render it in a \`<Slot.Object>\` and drop \`locked\``,
|
|
335
|
-
);
|
|
336
|
-
}
|
|
337
|
-
};
|
|
338
|
-
|
|
339
|
-
if (forceTop || objectReaders.size > 1) {
|
|
340
|
-
const split = [...objectReaders.keys()].map((k) => `the object "${k}"`).concat(topReaders.size > 0 ? [`the slots ${[...topReaders.keys()].map((k) => `"${k}"`).join(", ")}`] : []);
|
|
341
|
-
topLevel(forceTop || split.length < 2 ? undefined : `its readers are split across ${split.join(" and ")}`);
|
|
342
|
-
continue;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
if (objectReaders.size === 1) {
|
|
346
|
-
// Every reader is a field of one object, or a top-level slot beside it: the row
|
|
347
|
-
// joins the object, where its bar is, and a top-level reader reads it there.
|
|
348
|
-
const [[objectKey, property]] = [...objectReaders];
|
|
349
|
-
const call = property.initializer;
|
|
350
|
-
const objectOptions = call.arguments[0];
|
|
351
|
-
const fieldsProperty = objectOptions !== undefined && ts.isObjectLiteralExpression(objectOptions) ? objectMember(objectOptions, "fields") : undefined;
|
|
352
|
-
const fields = fieldsProperty !== undefined && ts.isPropertyAssignment(fieldsProperty) && ts.isObjectLiteralExpression(fieldsProperty.initializer) ? fieldsProperty.initializer : undefined;
|
|
353
|
-
if (fields === undefined) {
|
|
354
|
-
topLevel(`the object "${objectKey}" declares its fields in a form this codemod cannot read`);
|
|
355
|
-
continue;
|
|
356
|
-
}
|
|
357
|
-
const fieldKeys = new Set(fields.properties.map(propertyKey));
|
|
358
|
-
let key = singular;
|
|
359
|
-
for (let n = 2; fieldKeys.has(key) || key === "label"; n += 1) key = `${singular}${String(n)}`;
|
|
360
|
-
const first = fields.properties[0];
|
|
361
|
-
const indent = first === undefined ? "" : lineIndent(text, first.getStart(sf));
|
|
362
|
-
const at = first === undefined ? fields.getStart(sf) + 1 : first.getStart(sf);
|
|
363
|
-
edits.add(at, at, `${fieldEntry(key)},\n${indent}`);
|
|
364
|
-
continue;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
// Every reader is a top-level slot: group them with the row into an object.
|
|
368
|
-
const group = groupReaders({ singular, constName, section, slotKeys, readers: [...topReaders.values()] });
|
|
369
|
-
if (typeof group === "string") topLevel(group);
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
// Moves `readers` into `<singular>: object({ … })` and marks the element holding them;
|
|
374
|
-
// returns why it could not, having changed nothing.
|
|
375
|
-
function groupReaders({ singular, constName, section, slotKeys, readers }) {
|
|
376
|
-
const keys = readers.map(propertyKey);
|
|
377
|
-
if (readers.some((p) => claimed.has(p))) return "a reader also reads another table by position";
|
|
378
|
-
if (slotKeys.has(singular)) return `the section already has a slot "${singular}"`;
|
|
379
|
-
for (const property of readers) {
|
|
380
|
-
if (!ts.isPropertyAssignment(property) || NOT_A_FIELD.test(property.initializer.getText(sf))) return `"${propertyKey(property)}" cannot be an object's field`;
|
|
381
|
-
}
|
|
382
|
-
const prefix = sharedPrefix(keys);
|
|
383
|
-
let fieldKeys = keys.map((key) => key.slice(prefix.length));
|
|
384
|
-
if (fieldKeys.some((key) => key === "" || key === "label" || key === singular || /^\d/.test(key)) || new Set(fieldKeys).size !== fieldKeys.length) fieldKeys = keys;
|
|
385
|
-
if (fieldKeys.includes(singular) || fieldKeys.includes("label")) return `a reader's key collides with the row's field "${singular}" or the reserved "label"`;
|
|
386
|
-
const fieldOf = new Map(keys.map((key, i) => [key, fieldKeys[i]]));
|
|
387
|
-
|
|
388
|
-
// The readers must be read nowhere but this section's render.
|
|
389
|
-
const constName_ = ts.isVariableDeclaration(section.parent) && ts.isIdentifier(section.parent.name) ? section.parent.name.text : undefined;
|
|
390
|
-
for (const key of keys) {
|
|
391
|
-
const elsewhere = new RegExp(`\\.${key}\\b`);
|
|
392
|
-
const outside = text.slice(0, section.getStart(sf)) + text.slice(section.getEnd());
|
|
393
|
-
if (elsewhere.test(outside)) return `"${key}" is read outside the section's render`;
|
|
394
|
-
if (constName_ !== undefined && otherFiles.some(({ text: other }) => new RegExp(`\\b${constName_}\\.slots\\.${key}\\b`).test(other))) return `another section reads \`${constName_}.slots.${key}\``;
|
|
395
|
-
// The section's own modules read its slots by key through the schema's types.
|
|
396
|
-
const near = otherFiles.find(({ file: other, text: body }) => other.startsWith(`${dirname(file)}${sep}`) && elsewhere.test(body));
|
|
397
|
-
if (near !== undefined) return `"${key}" is read by ${basename(near.file)}`;
|
|
398
|
-
}
|
|
399
|
-
const render = section.arguments[1];
|
|
400
|
-
if (render === undefined || !(ts.isArrowFunction(render) || ts.isFunctionExpression(render))) return "the section's render is not written inline";
|
|
401
|
-
const param = render.parameters[0];
|
|
402
|
-
let binding;
|
|
403
|
-
if (param !== undefined && ts.isObjectBindingPattern(param.name)) {
|
|
404
|
-
for (const element of param.name.elements) {
|
|
405
|
-
const key = element.propertyName ?? element.name;
|
|
406
|
-
if (ts.isIdentifier(key) && key.text === "slot" && ts.isIdentifier(element.name)) binding = element.name.text;
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
if (binding === undefined) return "the render does not destructure `slot`";
|
|
410
|
-
|
|
411
|
-
// Every read of a reader off the binding; the binding passed whole hides where they render.
|
|
412
|
-
const usages = [];
|
|
413
|
-
let passedWhole = false;
|
|
414
|
-
const scan = (node) => {
|
|
415
|
-
const parent = node.parent;
|
|
416
|
-
const isName =
|
|
417
|
-
parent !== undefined &&
|
|
418
|
-
(ts.isBindingElement(parent) ||
|
|
419
|
-
(ts.isJsxAttribute(parent) && parent.name === node) ||
|
|
420
|
-
(ts.isPropertyAccessExpression(parent) && parent.name === node) ||
|
|
421
|
-
(ts.isPropertyAssignment(parent) && parent.name === node));
|
|
422
|
-
if (ts.isIdentifier(node) && node.text === binding && !isName) {
|
|
423
|
-
if (ts.isPropertyAccessExpression(parent) && parent.expression === node) {
|
|
424
|
-
if (fieldOf.has(parent.name.text)) usages.push(parent);
|
|
425
|
-
} else {
|
|
426
|
-
passedWhole = true;
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
ts.forEachChild(node, scan);
|
|
430
|
-
};
|
|
431
|
-
scan(render.body);
|
|
432
|
-
if (passedWhole) return "the render passes `slot` on whole";
|
|
433
|
-
if (usages.length === 0) return "the render reads none of them";
|
|
434
|
-
|
|
435
|
-
// The element that will mark the object: the deepest host element holding every
|
|
436
|
-
// rendered reader, with no other container between them and it.
|
|
437
|
-
const isElement = (node) => ts.isJsxElement(node) || ts.isJsxSelfClosingElement(node);
|
|
438
|
-
const tagOf = (node) => (ts.isJsxElement(node) ? node.openingElement.tagName : node.tagName);
|
|
439
|
-
const isContainer = (node) => isElement(node) && /^(Slot\.(Object|Group|List)|SlotGroup)$/.test(tagOf(node).getText(sf));
|
|
440
|
-
const chains = [];
|
|
441
|
-
const slotTag = imported.get("Slot") ?? "Slot";
|
|
442
|
-
for (const usage of usages) {
|
|
443
|
-
if (ts.isPropertyAccessExpression(usage.parent) && VALUE_READS.has(usage.parent.name.text)) continue;
|
|
444
|
-
// A field's binding is a primitive's to render; a component typed for a slot's refuses it.
|
|
445
|
-
const attribute = usage.parent?.parent;
|
|
446
|
-
const primitive =
|
|
447
|
-
ts.isJsxExpression(usage.parent) &&
|
|
448
|
-
attribute !== undefined &&
|
|
449
|
-
ts.isJsxAttribute(attribute) &&
|
|
450
|
-
attribute.name.getText(sf) === "slot" &&
|
|
451
|
-
attribute.parent.parent.tagName.getText(sf).startsWith(`${slotTag}.`);
|
|
452
|
-
if (!primitive) return `"${usage.name.text}" is handed to something other than a \`${slotTag}\` primitive`;
|
|
453
|
-
const chain = [];
|
|
454
|
-
for (let at = usage.parent; at !== render; at = at.parent) {
|
|
455
|
-
if (at === undefined) return "a reader renders outside the render";
|
|
456
|
-
if (ts.isArrowFunction(at) || ts.isFunctionExpression(at) || ts.isFunctionDeclaration(at)) return "a reader renders inside a callback";
|
|
457
|
-
if (isElement(at)) chain.unshift(at);
|
|
458
|
-
}
|
|
459
|
-
if (chain.length === 0) return "a reader is read outside the markup";
|
|
460
|
-
chains.push(chain);
|
|
461
|
-
}
|
|
462
|
-
if (chains.length === 0) return "the render reads no reader through a primitive";
|
|
463
|
-
let depth = 0;
|
|
464
|
-
while (chains.every((chain) => chain[depth] !== undefined && chain[depth] === chains[0][depth])) depth += 1;
|
|
465
|
-
let at = depth - 1;
|
|
466
|
-
const isHost = (node) => ts.isJsxElement(node) && ts.isIdentifier(node.openingElement.tagName) && /^[a-z]/.test(node.openingElement.tagName.text);
|
|
467
|
-
while (at >= 0 && !isHost(chains[0][at])) at -= 1;
|
|
468
|
-
if (at <= 0) return "no element below the section's root holds them all";
|
|
469
|
-
const holder = chains[0][at];
|
|
470
|
-
for (const chain of chains) if (chain.slice(at + 1).some(isContainer)) return "a reader renders inside another container";
|
|
471
|
-
const opening = holder.openingElement;
|
|
472
|
-
if (opening.attributes.properties.some((a) => ts.isJsxAttribute(a) && a.name.getText(sf) === "slot")) return "the holding element already carries a `slot` attribute";
|
|
473
|
-
// Another object's field inside the holder must stay inside its own container.
|
|
474
|
-
let stray = false;
|
|
475
|
-
const strays = (node, inside) => {
|
|
476
|
-
if (isElement(node) && isContainer(node)) inside = true;
|
|
477
|
-
if (!inside && ts.isPropertyAccessExpression(node) && node.name.text === "fields" && ts.isPropertyAccessExpression(node.expression) && ts.isIdentifier(node.expression.expression) && node.expression.expression.text === binding) stray = true;
|
|
478
|
-
ts.forEachChild(node, (child) => strays(child, inside));
|
|
479
|
-
};
|
|
480
|
-
for (const child of holder.children) strays(child, false);
|
|
481
|
-
if (stray) return "the holding element renders another object's fields outside their container";
|
|
482
|
-
|
|
483
|
-
// The schema: the object where the first reader was declared, the rest removed.
|
|
484
|
-
const [first, ...rest] = readers;
|
|
485
|
-
const indent = lineIndent(text, first.getStart(sf));
|
|
486
|
-
const fieldIndent = `${indent} `;
|
|
487
|
-
const fieldTexts = readers.map((property) => {
|
|
488
|
-
const comments = leadingComments(text, property);
|
|
489
|
-
const value = edits.take(property.initializer.getStart(sf), property.initializer.getEnd());
|
|
490
|
-
const body = `${fieldOf.get(propertyKey(property))}: ${reindent(value, indent, fieldIndent)}`;
|
|
491
|
-
return comments === "" ? body : `${reindent(comments, indent, fieldIndent)}${body}`;
|
|
492
|
-
});
|
|
493
|
-
const rowField = singular === constName ? constName : `${singular}: ${constName}`;
|
|
494
|
-
const block = [
|
|
495
|
-
`${singular}: object({`,
|
|
496
|
-
`${indent} label: ${imported.get("compute") ?? "compute"}(() => ${JSON.stringify(capitalized(singular))}),`,
|
|
497
|
-
`${indent} fields: {`,
|
|
498
|
-
`${fieldIndent}${rowField},`,
|
|
499
|
-
...fieldTexts.map((field) => `${fieldIndent}${field},`),
|
|
500
|
-
`${indent} },`,
|
|
501
|
-
`${indent}})`,
|
|
502
|
-
].join("\n");
|
|
503
|
-
// The first reader's comments move with it, so the object takes their place.
|
|
504
|
-
const firstComments = ts.getLeadingCommentRanges(text, first.getFullStart()) ?? [];
|
|
505
|
-
edits.add(firstComments.length > 0 ? firstComments[0].pos : first.getStart(sf), first.getEnd(), block);
|
|
506
|
-
for (const property of rest) {
|
|
507
|
-
const range = propertyRemoval(text, property, sf);
|
|
508
|
-
edits.add(range.start, range.end, "");
|
|
509
|
-
}
|
|
510
|
-
for (const property of readers) claimed.add(property);
|
|
511
|
-
slotKeys.add(singular);
|
|
512
|
-
uses.add("object");
|
|
513
|
-
uses.add("compute");
|
|
514
|
-
|
|
515
|
-
// The markup: every read of a reader reads the object's field, and the holder marks it.
|
|
516
|
-
for (const usage of usages) {
|
|
517
|
-
edits.add(usage.getStart(sf), usage.getEnd(), `${binding}.${singular}.fields.${fieldOf.get(usage.name.text)}`);
|
|
518
|
-
}
|
|
519
|
-
if (!imported.has("Slot")) uses.add("Slot");
|
|
520
|
-
const tag = opening.tagName.text;
|
|
521
|
-
edits.add(opening.tagName.getStart(sf), opening.tagName.getEnd(), `${slotTag}.Object slot={${binding}.${singular}}${tag === "div" ? "" : ` as="${tag}"`}`);
|
|
522
|
-
edits.add(holder.closingElement.tagName.getStart(sf), holder.closingElement.tagName.getEnd(), `${slotTag}.Object`);
|
|
523
|
-
return undefined;
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
// The rows, each declared once before the statement that first reads it.
|
|
527
|
-
for (const [statement, declarations] of rowsBefore) {
|
|
528
|
-
const at = statement.getStart(sf);
|
|
529
|
-
const comments = ts.getLeadingCommentRanges(text, statement.getFullStart()) ?? [];
|
|
530
|
-
const start = comments.length > 0 ? comments[0].pos : at;
|
|
531
|
-
edits.add(start, start, `${declarations.join("\n")}\n\n`);
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
// The imports the rewrites use, into the first import that brings in values.
|
|
535
|
-
const bindings = host.importClause.namedBindings;
|
|
536
|
-
const added = [...uses].filter((name) => !imported.has(name));
|
|
537
|
-
if (added.length > 0) {
|
|
538
|
-
const kept = bindings.elements.map((element) => element.getText(sf));
|
|
539
|
-
const all = [...kept, ...added].sort((a, b) => a.replace(/^type\s+/, "").localeCompare(b.replace(/^type\s+/, ""), "en", { sensitivity: "base" }));
|
|
540
|
-
const multiline = bindings.getText(sf).includes("\n");
|
|
541
|
-
const indent = lineIndent(text, bindings.elements[0]?.getStart(sf) ?? bindings.getStart(sf));
|
|
542
|
-
edits.add(bindings.getStart(sf), bindings.getEnd(), multiline ? `{\n${all.map((k) => `${indent}${k},`).join("\n")}\n}` : `{ ${all.join(", ")} }`);
|
|
543
|
-
}
|
|
544
|
-
return { text: edits.apply(), manual, refused };
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
/** @param {string} root @returns {Promise<{rewritten: string[], notRewritten: {file: string, reason: string}[]}>} */
|
|
548
|
-
export default async function tableRows(root) {
|
|
549
|
-
const rewritten = [];
|
|
550
|
-
const notRewritten = [];
|
|
551
|
-
const name = (file) => relative(root, file).split(sep).join("/");
|
|
552
|
-
const files = await sourceFiles(root);
|
|
553
|
-
const texts = new Map();
|
|
554
|
-
for (const file of files) texts.set(file, await readFile(file, "utf8"));
|
|
555
|
-
for (const file of files) {
|
|
556
|
-
const text = texts.get(file);
|
|
557
|
-
const others = files.filter((other) => other !== file).map((other) => ({ file: other, text: texts.get(other) }));
|
|
558
|
-
const result = rewrite(text, file, others);
|
|
559
|
-
if (result === null) continue;
|
|
560
|
-
if (result.text !== null && result.text !== text) {
|
|
561
|
-
await writeFile(file, result.text, "utf8");
|
|
562
|
-
rewritten.push(name(file));
|
|
563
|
-
}
|
|
564
|
-
for (const reason of new Set(result.manual)) notRewritten.push({ file: name(file), reason: `Manual: ${reason}` });
|
|
565
|
-
for (const reason of new Set(result.refused)) notRewritten.push({ file: name(file), reason });
|
|
566
|
-
}
|
|
567
|
-
return { rewritten, notRewritten };
|
|
568
|
-
}
|