@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
|
@@ -1,686 +0,0 @@
|
|
|
1
|
-
// A list takes one options object, and a record list's fields each declare their origin.
|
|
2
|
-
//
|
|
3
|
-
// `list.of(item, opts)` and `list.of({ …fields }, opts)` became
|
|
4
|
-
// `list({ label, item, … })` and `list({ label, source, fields: (row) => ({ … }) })`. A
|
|
5
|
-
// single-item list emits exactly what it did; a record list's row fields now name where
|
|
6
|
-
// each value comes from, instead of the list deriving it from which fields lack a source.
|
|
7
|
-
//
|
|
8
|
-
// This is a codemod STEP, not a release's codemod: it shipped with the minor that
|
|
9
|
-
// deprecated the old forms, which could ship no codemod of its own, and 7.0.0's
|
|
10
|
-
// `object-form-only` codemod — the release that removed them — runs it in its own pass.
|
|
11
|
-
// A workspace can also run it alone: it is not an export, so import this file by path
|
|
12
|
-
// from the installed package
|
|
13
|
-
// (`node_modules/@homepages/concrete/codemods/steps/list-object-form.mjs`) and call its
|
|
14
|
-
// default export with the workspace root. It honors the contract a release codemod
|
|
15
|
-
// does: `(root) => { rewritten, notRewritten }`, where a rewritten file may still carry
|
|
16
|
-
// `Manual:` lines for what only its author can finish.
|
|
17
|
-
//
|
|
18
|
-
// What it rewrites, in every `.ts` and `.tsx` under the workspace that imports `list`
|
|
19
|
-
// from the framework, innermost list first:
|
|
20
|
-
//
|
|
21
|
-
// - `list.of(ITEM, { … })` → `list({ …, item: ITEM })`; a list-level `unique: true`
|
|
22
|
-
// moves onto the item's own options.
|
|
23
|
-
// - `list.of({ …fields }, { source: fact.property.units, … })` → a `fields` list
|
|
24
|
-
// whose `row` is the collection's: a field bound to `fact.unit.<m>` /
|
|
25
|
-
// `fact.contact.<m>` binds `row.<m>`; an unsourced field whose key names a member
|
|
26
|
-
// binds that member, with the member's writeback (a single `image` field on a
|
|
27
|
-
// member holding several images becomes a list of them).
|
|
28
|
-
// - `list.of({ …fields }, { source: compute(…) | <slot>, … })` → a `fields` list whose
|
|
29
|
-
// unsourced fields bind the member of the same name.
|
|
30
|
-
// - `list.of({ …fields }, { source: fill(…), … })` → a `fields` list whose every field
|
|
31
|
-
// fills itself: the field the old list keyed its rows on (its `title:` text field, or
|
|
32
|
-
// its one select) or picked them from (its one unsourced asset field) is declared
|
|
33
|
-
// first as a const and given a `fill()` with a brief made from its label, `unique:
|
|
34
|
-
// true` where the list kept those distinct, and the pool its pick is made from; every
|
|
35
|
-
// other filled field reads it, as it saw it before.
|
|
36
|
-
//
|
|
37
|
-
// What it leaves to you, reported per file: every generated brief (write the real one),
|
|
38
|
-
// every fill in a bound row (it saw the whole row before; now it sees only its `reads`,
|
|
39
|
-
// so add the members it uses), and every call it cannot read — an options argument or a
|
|
40
|
-
// record that is not written in place, a field with no origin to give it, a source it
|
|
41
|
-
// does not recognize.
|
|
42
|
-
|
|
43
|
-
import { readdir, readFile, writeFile } from "node:fs/promises";
|
|
44
|
-
import { join, relative, sep } from "node:path";
|
|
45
|
-
|
|
46
|
-
import ts from "typescript";
|
|
47
|
-
|
|
48
|
-
const CONCRETE = "@homepages/concrete";
|
|
49
|
-
|
|
50
|
-
const EXTENSIONS = [".ts", ".tsx"];
|
|
51
|
-
const SKIP_DIRS = new Set(["node_modules", ".concrete", ".git", "dist", "build"]);
|
|
52
|
-
|
|
53
|
-
// What each collection's row offers, as the catalog's row descriptors declared it when
|
|
54
|
-
// this step was written: the member's author name, the storage key a field may still be
|
|
55
|
-
// keyed by, its value, and whether it writes back. A codemod is a snapshot of one
|
|
56
|
-
// migration, so it carries its own copy — the descriptors it was taken from are gone.
|
|
57
|
-
export const TABLE_ROWS = {
|
|
58
|
-
units: {
|
|
59
|
-
param: "unit",
|
|
60
|
-
primary: "unit",
|
|
61
|
-
members: {
|
|
62
|
-
position: { value: "number" },
|
|
63
|
-
floor_plans: { value: "image_list" },
|
|
64
|
-
unit_label: { value: "text", writable: true },
|
|
65
|
-
beds: { value: "number", writable: true },
|
|
66
|
-
baths: { value: "number", writable: true },
|
|
67
|
-
sqft: { value: "number", writable: true },
|
|
68
|
-
price: { value: "text", writable: true },
|
|
69
|
-
},
|
|
70
|
-
},
|
|
71
|
-
contacts: {
|
|
72
|
-
param: "contact",
|
|
73
|
-
primary: "contact",
|
|
74
|
-
members: {
|
|
75
|
-
primary: { value: "boolean" },
|
|
76
|
-
name: { value: "text", writable: true },
|
|
77
|
-
title: { value: "text", writable: true },
|
|
78
|
-
org_name: { value: "text", writable: true },
|
|
79
|
-
bio: { value: "text", writable: true },
|
|
80
|
-
address: { value: "text" },
|
|
81
|
-
email: { value: "text", writable: true },
|
|
82
|
-
phone: { value: "text", writable: true },
|
|
83
|
-
website: { value: "text", path: "website_url", writable: true },
|
|
84
|
-
instagram: { value: "text", path: "instagram_url", writable: true },
|
|
85
|
-
linkedin: { value: "text", path: "linkedin_url", writable: true },
|
|
86
|
-
x: { value: "text", path: "x_url", writable: true },
|
|
87
|
-
facebook: { value: "text", path: "facebook_url", writable: true },
|
|
88
|
-
headshot: { value: "image", path: "avatar_upload_id" },
|
|
89
|
-
logo_dark: { value: "image", path: "logo_dark_upload_id" },
|
|
90
|
-
logo_light: { value: "image", path: "logo_light_upload_id" },
|
|
91
|
-
},
|
|
92
|
-
},
|
|
93
|
-
comparables: {
|
|
94
|
-
param: "comp",
|
|
95
|
-
primary: undefined,
|
|
96
|
-
members: {
|
|
97
|
-
address_street: { value: "text" },
|
|
98
|
-
address_city: { value: "text" },
|
|
99
|
-
address_state: { value: "text" },
|
|
100
|
-
address_postal: { value: "text" },
|
|
101
|
-
address: { value: "text" },
|
|
102
|
-
lat: { value: "number" },
|
|
103
|
-
lng: { value: "number" },
|
|
104
|
-
distance_m: { value: "number" },
|
|
105
|
-
status: { value: "text" },
|
|
106
|
-
price: { value: "number", writable: true },
|
|
107
|
-
price_period: { value: "text" },
|
|
108
|
-
sale_date: { value: "text", writable: true },
|
|
109
|
-
beds: { value: "number", writable: true },
|
|
110
|
-
baths: { value: "number", writable: true },
|
|
111
|
-
sqft: { value: "number", writable: true },
|
|
112
|
-
photo: { value: "image", path: "photo_upload_id" },
|
|
113
|
-
year_built: { value: "number" },
|
|
114
|
-
lot_sqft: { value: "number" },
|
|
115
|
-
stories: { value: "number" },
|
|
116
|
-
units: { value: "number" },
|
|
117
|
-
hoa_monthly: { value: "number" },
|
|
118
|
-
list_price: { value: "number" },
|
|
119
|
-
listed_date: { value: "text" },
|
|
120
|
-
mls_number: { value: "text" },
|
|
121
|
-
description: { value: "text" },
|
|
122
|
-
},
|
|
123
|
-
},
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
// The builders a member's value may bind, by the member's value.
|
|
127
|
-
const BINDS = {
|
|
128
|
-
text: new Set(["text.short", "text.medium", "text.long", "select", "select.of", "url"]),
|
|
129
|
-
number: new Set(["number"]),
|
|
130
|
-
boolean: new Set(),
|
|
131
|
-
image: new Set(["image"]),
|
|
132
|
-
image_list: new Set(),
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
// The pool an unsourced asset field of an AI-built list picked its rows out of.
|
|
136
|
-
const ASSET_POOLS = { image: "photos", video: "videos", tour: "tours", document: "documents" };
|
|
137
|
-
|
|
138
|
-
async function sourceFiles(root) {
|
|
139
|
-
const found = [];
|
|
140
|
-
async function walk(dir) {
|
|
141
|
-
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
142
|
-
if (entry.isDirectory()) {
|
|
143
|
-
if (!SKIP_DIRS.has(entry.name)) await walk(join(dir, entry.name));
|
|
144
|
-
} else if (EXTENSIONS.some((ext) => entry.name.endsWith(ext)) && !entry.name.endsWith(".d.ts")) {
|
|
145
|
-
found.push(join(dir, entry.name));
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
await walk(root);
|
|
150
|
-
return found.sort();
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function lineOf(sf, pos) {
|
|
154
|
-
return sf.getLineAndCharacterOfPosition(pos).line + 1;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/** The names this file reaches the framework by: named imports (local name by exported name), and namespaces. */
|
|
158
|
-
function frameworkBindings(sf) {
|
|
159
|
-
const named = new Map();
|
|
160
|
-
const namespaces = new Set();
|
|
161
|
-
let importDecl;
|
|
162
|
-
for (const statement of sf.statements) {
|
|
163
|
-
if (!ts.isImportDeclaration(statement) || statement.importClause?.isTypeOnly) continue;
|
|
164
|
-
if (!ts.isStringLiteral(statement.moduleSpecifier) || statement.moduleSpecifier.text !== CONCRETE) continue;
|
|
165
|
-
const bindings = statement.importClause?.namedBindings;
|
|
166
|
-
if (bindings === undefined) continue;
|
|
167
|
-
if (ts.isNamespaceImport(bindings)) {
|
|
168
|
-
namespaces.add(bindings.name.text);
|
|
169
|
-
continue;
|
|
170
|
-
}
|
|
171
|
-
importDecl ??= statement;
|
|
172
|
-
for (const element of bindings.elements) {
|
|
173
|
-
if (element.isTypeOnly) continue;
|
|
174
|
-
named.set((element.propertyName ?? element.name).text, element.name.text);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
return { named, namespaces, importDecl };
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/** How this file spells the framework export `name`, or `undefined` when it cannot reach it. */
|
|
181
|
-
function spelling(bindings, name) {
|
|
182
|
-
const local = bindings.named.get(name);
|
|
183
|
-
if (local !== undefined) return local;
|
|
184
|
-
const [ns] = bindings.namespaces;
|
|
185
|
-
return ns === undefined ? undefined : `${ns}.${name}`;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/** Whether `node` is the framework export `name` (`name`, or `<ns>.name`). */
|
|
189
|
-
function isExport(node, bindings, name) {
|
|
190
|
-
if (ts.isIdentifier(node)) return bindings.named.get(name) === node.text;
|
|
191
|
-
return (
|
|
192
|
-
ts.isPropertyAccessExpression(node) &&
|
|
193
|
-
node.name.text === name &&
|
|
194
|
-
ts.isIdentifier(node.expression) &&
|
|
195
|
-
bindings.namespaces.has(node.expression.text)
|
|
196
|
-
);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/** A call to the framework builder `name` — `name(…)`, or `name.<member>(…)` for a sized text. */
|
|
200
|
-
function builderOf(node, bindings) {
|
|
201
|
-
if (!ts.isCallExpression(node)) return undefined;
|
|
202
|
-
const callee = node.expression;
|
|
203
|
-
for (const name of ["text", "number", "select", "image", "video", "tour", "document", "url", "poi", "list"]) {
|
|
204
|
-
if (isExport(callee, bindings, name)) return name;
|
|
205
|
-
if (ts.isPropertyAccessExpression(callee) && isExport(callee.expression, bindings, name)) return `${name}.${callee.name.text}`;
|
|
206
|
-
}
|
|
207
|
-
return undefined;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
function isListOf(node, bindings) {
|
|
211
|
-
return (
|
|
212
|
-
ts.isCallExpression(node) &&
|
|
213
|
-
ts.isPropertyAccessExpression(node.expression) &&
|
|
214
|
-
node.expression.name.text === "of" &&
|
|
215
|
-
isExport(node.expression.expression, bindings, "list")
|
|
216
|
-
);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/** The `fact.<a>.<b>` path of an expression, when it is one written in place. */
|
|
220
|
-
function factPath(node, bindings) {
|
|
221
|
-
const parts = [];
|
|
222
|
-
let at = node;
|
|
223
|
-
while (ts.isPropertyAccessExpression(at)) {
|
|
224
|
-
parts.unshift(at.name.text);
|
|
225
|
-
at = at.expression;
|
|
226
|
-
}
|
|
227
|
-
return isExport(at, bindings, "fact") || (ts.isIdentifier(at) && at.text === spelling(bindings, "fact")) ? parts : undefined;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
function propertyOf(literal, name) {
|
|
231
|
-
return literal.properties.find(
|
|
232
|
-
(p) => (ts.isPropertyAssignment(p) || ts.isShorthandPropertyAssignment(p)) && p.name.getText() === name,
|
|
233
|
-
);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/** The whitespace a line starts with. */
|
|
237
|
-
function indentAt(text, pos) {
|
|
238
|
-
const lineStart = text.lastIndexOf("\n", pos - 1) + 1;
|
|
239
|
-
return /^[ \t]*/.exec(text.slice(lineStart))[0];
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/** `value`'s text moved to a line indented `to`: every line after its first shifts with it. */
|
|
243
|
-
function reindent(value, from, to) {
|
|
244
|
-
return value
|
|
245
|
-
.split("\n")
|
|
246
|
-
.map((line, i) => (i === 0 ? line : line.startsWith(from) ? to + line.slice(from.length) : line))
|
|
247
|
-
.join("\n");
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
function applyEdits(text, edits, base = 0) {
|
|
251
|
-
const sorted = [...edits].sort((a, b) => b.start - a.start || b.end - a.end);
|
|
252
|
-
let out = text;
|
|
253
|
-
let floor = Infinity;
|
|
254
|
-
for (const edit of sorted) {
|
|
255
|
-
if (edit.end > floor) throw new Error("overlapping list rewrites");
|
|
256
|
-
out = out.slice(0, edit.start - base) + edit.text + out.slice(edit.end - base);
|
|
257
|
-
floor = edit.start;
|
|
258
|
-
}
|
|
259
|
-
return out;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* The edit that appends `name: value` to an object literal — on a line of its own when
|
|
264
|
-
* the literal spans lines (its value reindented to match), inline when it does not.
|
|
265
|
-
*/
|
|
266
|
-
function appendProperty(sf, literal, entry) {
|
|
267
|
-
const text = sf.text;
|
|
268
|
-
const close = literal.getEnd() - 1;
|
|
269
|
-
const last = literal.properties[literal.properties.length - 1];
|
|
270
|
-
const multiline = text.slice(literal.getStart(sf), literal.getEnd()).includes("\n");
|
|
271
|
-
if (last === undefined) return { start: literal.getStart(sf) + 1, end: close, text: ` ${entry} ` };
|
|
272
|
-
const trailing = text.slice(last.getEnd(), close);
|
|
273
|
-
const hasComma = trailing.trimStart().startsWith(",");
|
|
274
|
-
if (!multiline) return { start: last.getEnd(), end: last.getEnd(), text: `, ${entry}` };
|
|
275
|
-
const indent = indentAt(text, last.getStart(sf));
|
|
276
|
-
const at = hasComma ? last.getEnd() + trailing.indexOf(",") + 1 : last.getEnd();
|
|
277
|
-
return { start: at, end: at, text: `${hasComma ? "" : ","}\n${indent}${entry}${hasComma ? "," : ""}` };
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
/** A field call's options object: its last argument, when it is written in place. */
|
|
281
|
-
function optionsOf(call) {
|
|
282
|
-
const last = call.arguments[call.arguments.length - 1];
|
|
283
|
-
return last !== undefined && ts.isObjectLiteralExpression(last) ? last : undefined;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
/** The literal label of a slot call, or `undefined`. */
|
|
287
|
-
function labelOf(call) {
|
|
288
|
-
const opts = optionsOf(call);
|
|
289
|
-
const label = opts === undefined ? undefined : propertyOf(opts, "label");
|
|
290
|
-
return label !== undefined && ts.isPropertyAssignment(label) && ts.isStringLiteralLike(label.initializer) ? label.initializer.text : undefined;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/** The generic one-line brief B4 gives a field: "One question.", from its label. */
|
|
294
|
-
function genericBrief(call) {
|
|
295
|
-
const label = labelOf(call);
|
|
296
|
-
return label === undefined ? "One item." : `One ${label.charAt(0).toLowerCase()}${label.slice(1)}.`;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
/** The `fill({ … })` a field's `source:` holds, written in place — or `undefined`. */
|
|
300
|
-
function fillOf(call, bindings) {
|
|
301
|
-
const opts = optionsOf(call);
|
|
302
|
-
const source = opts === undefined ? undefined : propertyOf(opts, "source");
|
|
303
|
-
if (source === undefined || !ts.isPropertyAssignment(source)) return undefined;
|
|
304
|
-
const init = source.initializer;
|
|
305
|
-
return ts.isCallExpression(init) && isExport(init.expression, bindings, "fill") && init.arguments[0] !== undefined && ts.isObjectLiteralExpression(init.arguments[0])
|
|
306
|
-
? init.arguments[0]
|
|
307
|
-
: undefined;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
function sourceOf(call) {
|
|
311
|
-
const opts = optionsOf(call);
|
|
312
|
-
const source = opts === undefined ? undefined : propertyOf(opts, "source");
|
|
313
|
-
return source !== undefined && ts.isPropertyAssignment(source) ? source.initializer : undefined;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
/** The edit appending `read` to a fill's `reads`, whatever form the reads take. */
|
|
317
|
-
function addRead(sf, fillLiteral, read) {
|
|
318
|
-
const reads = propertyOf(fillLiteral, "reads");
|
|
319
|
-
if (reads === undefined) return appendProperty(sf, fillLiteral, `reads: [${read}]`);
|
|
320
|
-
if (!ts.isPropertyAssignment(reads)) return { start: reads.getStart(sf), end: reads.getEnd(), text: `reads: [...reads, ${read}]` };
|
|
321
|
-
const init = reads.initializer;
|
|
322
|
-
if (ts.isArrayLiteralExpression(init)) {
|
|
323
|
-
const last = init.elements[init.elements.length - 1];
|
|
324
|
-
if (last === undefined) return { start: init.getStart(sf), end: init.getEnd(), text: `[${read}]` };
|
|
325
|
-
return { start: last.getEnd(), end: last.getEnd(), text: `, ${read}` };
|
|
326
|
-
}
|
|
327
|
-
return { start: init.getStart(sf), end: init.getEnd(), text: `[...${init.getText(sf)}, ${read}]` };
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
// ----- the single-item form ------------------------------------------------
|
|
331
|
-
|
|
332
|
-
function rewriteItemList(sf, call, item, options, reasons) {
|
|
333
|
-
const edits = [{ start: call.expression.getStart(sf), end: call.expression.getEnd(), text: call.expression.expression.getText(sf) }];
|
|
334
|
-
edits.push({ start: item.getStart(sf), end: options.getStart(sf), text: "" });
|
|
335
|
-
|
|
336
|
-
let itemText = item.getText(sf);
|
|
337
|
-
const unique = propertyOf(options, "unique");
|
|
338
|
-
if (unique !== undefined) {
|
|
339
|
-
if (ts.isPropertyAssignment(unique) && unique.initializer.kind === ts.SyntaxKind.TrueKeyword) {
|
|
340
|
-
const itemOpts = ts.isCallExpression(item) ? optionsOf(item) : undefined;
|
|
341
|
-
if (itemOpts === undefined) {
|
|
342
|
-
reasons.push(`Manual: line ${lineOf(sf, call.getStart(sf))} — write \`unique: true\` on the list's item, whose options are not written in place`);
|
|
343
|
-
} else if (propertyOf(itemOpts, "unique") === undefined) {
|
|
344
|
-
const edit = appendProperty(sf, itemOpts, "unique: true");
|
|
345
|
-
itemText = applyEdits(itemText, [edit], item.getStart(sf));
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
const entry = `item: ${reindent(itemText, indentAt(sf.text, item.getStart(sf)), indentAt(sf.text, (options.properties[0] ?? options).getStart(sf)))}`;
|
|
350
|
-
// The list's `unique` gives its place to the item that now carries it.
|
|
351
|
-
edits.push(unique === undefined ? appendProperty(sf, options, entry) : { start: unique.getStart(sf), end: unique.getEnd(), text: entry });
|
|
352
|
-
return edits;
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
// ----- the record form -----------------------------------------------------
|
|
356
|
-
|
|
357
|
-
/** Where a record list's rows come from, read off its `source:`. */
|
|
358
|
-
function rowsOf(options, bindings) {
|
|
359
|
-
const source = propertyOf(options, "source");
|
|
360
|
-
if (source === undefined || !ts.isPropertyAssignment(source)) return { kind: "none" };
|
|
361
|
-
const init = source.initializer;
|
|
362
|
-
const path = factPath(init, bindings);
|
|
363
|
-
if (path?.length === 2 && path[0] === "property" && path[1] in TABLE_ROWS) return { kind: "collection", collection: path[1] };
|
|
364
|
-
if (ts.isCallExpression(init) && isExport(init.expression, bindings, "fill")) return { kind: "fill" };
|
|
365
|
-
if (ts.isCallExpression(init) && isExport(init.expression, bindings, "compute")) return { kind: "value" };
|
|
366
|
-
if (ts.isPropertyAccessExpression(init) || ts.isIdentifier(init)) return { kind: "value" };
|
|
367
|
-
return { kind: "unknown" };
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
function fieldEntries(record) {
|
|
371
|
-
return record.properties.map((p) => ({
|
|
372
|
-
property: p,
|
|
373
|
-
key: ts.isPropertyAssignment(p) || ts.isShorthandPropertyAssignment(p) ? p.name.getText() : undefined,
|
|
374
|
-
call: ts.isPropertyAssignment(p) && ts.isCallExpression(p.initializer) ? p.initializer : undefined,
|
|
375
|
-
}));
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
/** A bound row's field edits: member sources for `fact.<primary>.<m>` and for an unsourced field named for a member. */
|
|
379
|
-
function boundFieldEdits(sf, at, rows, param, fields, bindings, reasons) {
|
|
380
|
-
const edits = [];
|
|
381
|
-
const { members, primary } = TABLE_ROWS[rows.collection];
|
|
382
|
-
const byPath = new Map(Object.entries(members).map(([name, m]) => [m.path ?? name, name]));
|
|
383
|
-
for (const { key, call } of fields) {
|
|
384
|
-
if (call === undefined) {
|
|
385
|
-
reasons.push(`Manual: ${at} — field "${key}" is not a builder call written in place; give it a row member, a \`fill()\` or a \`compute()\``);
|
|
386
|
-
continue;
|
|
387
|
-
}
|
|
388
|
-
const builder = builderOf(call, bindings);
|
|
389
|
-
const source = sourceOf(call);
|
|
390
|
-
const opts = optionsOf(call);
|
|
391
|
-
if (source !== undefined) {
|
|
392
|
-
const path = factPath(source, bindings);
|
|
393
|
-
if (path !== undefined && path.length === 2 && path[0] === primary && path[1] in members) {
|
|
394
|
-
edits.push({ start: source.getStart(sf), end: source.getEnd(), text: `${param}.${path[1]}` });
|
|
395
|
-
} else if (path !== undefined) {
|
|
396
|
-
reasons.push(`Manual: ${at} — field "${key}" binds \`fact.${path.join(".")}\`, the same value in every row; read it in the field's \`fill()\` or \`compute()\` instead`);
|
|
397
|
-
} else if (fillOf(call, bindings) !== undefined) {
|
|
398
|
-
reasons.push(`Manual: ${at} — field "${key}"'s fill saw its whole row before and now sees only its \`reads\`: add the members it uses (\`${param}.<member>\`)`);
|
|
399
|
-
}
|
|
400
|
-
continue;
|
|
401
|
-
}
|
|
402
|
-
const member = key in members ? key : byPath.get(key);
|
|
403
|
-
if (member === undefined || opts === undefined) {
|
|
404
|
-
reasons.push(`Manual: ${at} — field "${key}" names no member of the row; bind one (\`${param}.<member>\`), \`fill()\` it or \`compute()\` it`);
|
|
405
|
-
continue;
|
|
406
|
-
}
|
|
407
|
-
const { value, writable } = members[member];
|
|
408
|
-
const bind = `source: ${param}.${member}, writeback: ${writable === true}`;
|
|
409
|
-
if (value === "image_list" && builder === "image") {
|
|
410
|
-
// One image field the bake handed several images: a list of them now.
|
|
411
|
-
const label = propertyOf(opts, "label");
|
|
412
|
-
const labelText = label !== undefined && ts.isPropertyAssignment(label) ? label.initializer.getText(sf) : JSON.stringify(key);
|
|
413
|
-
const listName = spelling(bindings, "list");
|
|
414
|
-
edits.push({ start: call.getStart(sf), end: call.getEnd(), text: `${listName}({ label: ${labelText}, ${bind}, item: ${call.getText(sf)} })` });
|
|
415
|
-
} else if (value === "image_list" && builder === "list") {
|
|
416
|
-
edits.push(appendProperty(sf, opts, bind));
|
|
417
|
-
} else if (BINDS[value]?.has(builder) === true) {
|
|
418
|
-
edits.push(appendProperty(sf, opts, bind));
|
|
419
|
-
} else {
|
|
420
|
-
reasons.push(`Manual: ${at} — field "${key}" is a \`${builder ?? "?"}\`, which cannot hold the member "${member}" (${value}); bind a field that can`);
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
return edits;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
/** Whether a field is a select carrying its own vocabulary — `select.of(…)`, or `select({ options })`. */
|
|
427
|
-
function isVocabularySelect(call, bindings) {
|
|
428
|
-
const builder = builderOf(call, bindings);
|
|
429
|
-
if (builder === "select.of") return true;
|
|
430
|
-
const opts = builder === "select" ? optionsOf(call) : undefined;
|
|
431
|
-
return opts !== undefined && propertyOf(opts, "options") !== undefined;
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
/** The field an AI-built list's rows were keyed on or picked from, as the pre-grammar derivation found it. */
|
|
435
|
-
function originatingField(options, fields, bindings) {
|
|
436
|
-
const selects = fields.filter(({ call }) => call !== undefined && isVocabularySelect(call, bindings));
|
|
437
|
-
const pooled = fields.filter(({ call }) => {
|
|
438
|
-
if (call === undefined || sourceOf(call) !== undefined) return false;
|
|
439
|
-
const builder = builderOf(call, bindings);
|
|
440
|
-
return builder !== undefined && builder in ASSET_POOLS;
|
|
441
|
-
});
|
|
442
|
-
const title = propertyOf(options, "title");
|
|
443
|
-
const titleKey = title !== undefined && ts.isPropertyAssignment(title) && ts.isStringLiteralLike(title.initializer) ? title.initializer.text : undefined;
|
|
444
|
-
let key;
|
|
445
|
-
if (titleKey !== undefined) {
|
|
446
|
-
const field = fields.find((f) => f.key === titleKey);
|
|
447
|
-
const builder = field?.call === undefined ? undefined : builderOf(field.call, bindings);
|
|
448
|
-
if (builder?.startsWith("text") === true) key = { field, picked: false };
|
|
449
|
-
else if (field !== undefined && selects.includes(field)) key = { field, picked: true };
|
|
450
|
-
} else if (selects.length === 1) {
|
|
451
|
-
key = { field: selects[0], picked: true };
|
|
452
|
-
}
|
|
453
|
-
if (pooled.length === 1 && key?.picked !== true) {
|
|
454
|
-
return { field: pooled[0], unique: true, pool: ASSET_POOLS[builderOf(pooled[0].call, bindings)] };
|
|
455
|
-
}
|
|
456
|
-
return key === undefined ? undefined : { field: key.field, unique: key.picked };
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
/**
|
|
460
|
-
* The names an expression reads — every identifier but a property's own name — which a
|
|
461
|
-
* const declared around it must not shadow (a field keyed `video` built by `video(…)`).
|
|
462
|
-
*/
|
|
463
|
-
function namesRead(node) {
|
|
464
|
-
const names = new Set();
|
|
465
|
-
const visit = (n) => {
|
|
466
|
-
if (ts.isIdentifier(n)) {
|
|
467
|
-
const parent = n.parent;
|
|
468
|
-
const isPropertyName = (ts.isPropertyAssignment(parent) && parent.name === n) || (ts.isPropertyAccessExpression(parent) && parent.name === n);
|
|
469
|
-
if (!isPropertyName) names.add(n.text);
|
|
470
|
-
}
|
|
471
|
-
ts.forEachChild(n, visit);
|
|
472
|
-
};
|
|
473
|
-
visit(node);
|
|
474
|
-
return names;
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
/** A const name for field `key` that shadows nothing the list reads. */
|
|
478
|
-
function constNameFor(key, taken) {
|
|
479
|
-
if (!taken.has(key)) return key;
|
|
480
|
-
let name = `${key}Field`;
|
|
481
|
-
for (let i = 2; taken.has(name); i += 1) name = `${key}Field${i}`;
|
|
482
|
-
return name;
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
/**
|
|
486
|
-
* An AI-built row: the originating field hoisted to a const with its own fill, every
|
|
487
|
-
* other filled field reading it. Returns the `fields` value text, or `undefined` with a
|
|
488
|
-
* reason when the list cannot be read.
|
|
489
|
-
*/
|
|
490
|
-
function aiBuiltFields(sf, at, list, record, options, fields, bindings, reasons, needs) {
|
|
491
|
-
const origin = originatingField(options, fields, bindings);
|
|
492
|
-
if (origin === undefined || origin.field.key === undefined || origin.field.call === undefined || !/^[A-Za-z_$][\w$]*$/.test(origin.field.key)) {
|
|
493
|
-
reasons.push(`${at} — no field keys or picks its rows that this step can read; declare a \`fill()\` on every field by hand`);
|
|
494
|
-
return undefined;
|
|
495
|
-
}
|
|
496
|
-
const { key, call, property } = origin.field;
|
|
497
|
-
if (fillOf(call, bindings) !== undefined || sourceOf(call) !== undefined) {
|
|
498
|
-
reasons.push(`${at} — its originating field "${key}" already declares a source; declare the rest by hand`);
|
|
499
|
-
return undefined;
|
|
500
|
-
}
|
|
501
|
-
const originIndex = fields.indexOf(origin.field);
|
|
502
|
-
const name = constNameFor(key, new Set([...namesRead(list), ...bindings.named.values()]));
|
|
503
|
-
const fillName = spelling(bindings, "fill") ?? "fill";
|
|
504
|
-
needs.add("fill");
|
|
505
|
-
const opts = optionsOf(call);
|
|
506
|
-
const fillOpts = [`brief: ${JSON.stringify(genericBrief(call))}`];
|
|
507
|
-
if (origin.pool !== undefined) {
|
|
508
|
-
needs.add("fact");
|
|
509
|
-
fillOpts.unshift(`reads: [${spelling(bindings, "fact") ?? "fact"}.${origin.pool}]`);
|
|
510
|
-
}
|
|
511
|
-
const originEdits = [appendProperty(sf, opts, `source: ${fillName}({ ${fillOpts.join(", ")} })`)];
|
|
512
|
-
if (origin.unique && propertyOf(opts, "unique") === undefined) originEdits.push(appendProperty(sf, opts, "unique: true"));
|
|
513
|
-
const constText = applyEdits(call.getText(sf), originEdits, call.getStart(sf));
|
|
514
|
-
reasons.push(`Manual: ${at} — write a real brief for "${key}"; its fill says only ${JSON.stringify(genericBrief(call))}`);
|
|
515
|
-
|
|
516
|
-
// The field's own comments travel with it to its const; the row keeps the name.
|
|
517
|
-
const leading = sf.text.slice(property.getFullStart(), property.getStart(sf));
|
|
518
|
-
const comments = leading.includes("//") || leading.includes("/*") ? leading.replace(/^\s*\n/, "").replace(/\s+$/, "") : "";
|
|
519
|
-
const shorthand = name === key ? key : `${key}: ${name}`;
|
|
520
|
-
const recordEdits = [
|
|
521
|
-
comments === ""
|
|
522
|
-
? { start: property.getStart(sf), end: property.getEnd(), text: shorthand }
|
|
523
|
-
: { start: property.getFullStart(), end: property.getEnd(), text: `\n${indentAt(sf.text, property.getStart(sf))}${shorthand}` },
|
|
524
|
-
];
|
|
525
|
-
fields.forEach((f, index) => {
|
|
526
|
-
if (f === origin.field || f.call === undefined) return;
|
|
527
|
-
if (builderOf(f.call, bindings) === undefined) {
|
|
528
|
-
reasons.push(`Manual: ${at} — field "${f.key}" is built by \`${f.call.expression.getText(sf)}(…)\`; if it fills itself, add "${key}" to that fill's \`reads\`, which is what it saw before`);
|
|
529
|
-
return;
|
|
530
|
-
}
|
|
531
|
-
const fillLiteral = fillOf(f.call, bindings);
|
|
532
|
-
if (fillLiteral === undefined) {
|
|
533
|
-
const source = sourceOf(f.call);
|
|
534
|
-
if (source === undefined) reasons.push(`Manual: ${at} — field "${f.key}" declares no origin; \`fill()\` it or \`compute()\` it`);
|
|
535
|
-
// A fill built by a helper (`source: marketMetric(…)`) saw the whole row too,
|
|
536
|
-
// and this step cannot reach its `reads` to add the key.
|
|
537
|
-
else {
|
|
538
|
-
const spelled = ts.isCallExpression(source) ? `${source.expression.getText(sf)}(…)` : source.getText(sf);
|
|
539
|
-
reasons.push(`Manual: ${at} — field "${f.key}"'s source \`${spelled}\` is not a \`fill()\` written in place; if it fills itself, add "${key}" to its \`reads\`, which is what it saw before`);
|
|
540
|
-
}
|
|
541
|
-
return;
|
|
542
|
-
}
|
|
543
|
-
if (index < originIndex) {
|
|
544
|
-
reasons.push(`Manual: ${at} — field "${f.key}" comes before "${key}", so it cannot read it; move it after "${key}" and add it to its reads`);
|
|
545
|
-
return;
|
|
546
|
-
}
|
|
547
|
-
recordEdits.push(addRead(sf, fillLiteral, name));
|
|
548
|
-
});
|
|
549
|
-
const recordText = applyEdits(record.getText(sf), recordEdits, record.getStart(sf));
|
|
550
|
-
return { constKey: key, constName: name, constText, comments, recordText };
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
function rewriteRecordList(sf, call, record, options, bindings, reasons, needs) {
|
|
554
|
-
const at = `line ${lineOf(sf, call.getStart(sf))}`;
|
|
555
|
-
const rows = rowsOf(options, bindings);
|
|
556
|
-
const fields = fieldEntries(record);
|
|
557
|
-
const recordIndent = indentAt(sf.text, record.getStart(sf));
|
|
558
|
-
const propIndent = indentAt(sf.text, (options.properties[0] ?? options).getStart(sf));
|
|
559
|
-
let fieldsText;
|
|
560
|
-
|
|
561
|
-
if (rows.kind === "collection") {
|
|
562
|
-
const { param } = TABLE_ROWS[rows.collection];
|
|
563
|
-
const recordEdits = boundFieldEdits(sf, at, rows, param, fields, bindings, reasons);
|
|
564
|
-
const recordText = applyEdits(record.getText(sf), recordEdits, record.getStart(sf));
|
|
565
|
-
fieldsText = `fields: (${param}) => (${reindent(recordText, recordIndent, propIndent)})`;
|
|
566
|
-
} else if (rows.kind === "value") {
|
|
567
|
-
const recordEdits = [];
|
|
568
|
-
for (const { key, call: field } of fields) {
|
|
569
|
-
const opts = field === undefined ? undefined : optionsOf(field);
|
|
570
|
-
if (opts !== undefined && sourceOf(field) === undefined) recordEdits.push(appendProperty(sf, opts, `source: row.${key}, writeback: false`));
|
|
571
|
-
}
|
|
572
|
-
const recordText = applyEdits(record.getText(sf), recordEdits, record.getStart(sf));
|
|
573
|
-
fieldsText = `fields: (row) => (${reindent(recordText, recordIndent, propIndent)})`;
|
|
574
|
-
} else if (rows.kind === "fill") {
|
|
575
|
-
const built = aiBuiltFields(sf, at, call, record, options, fields, bindings, reasons, needs);
|
|
576
|
-
if (built === undefined) return undefined;
|
|
577
|
-
const body = `${propIndent} `;
|
|
578
|
-
const fieldIndent = indentAt(sf.text, fields.find((f) => f.key === built.constKey).call.getStart(sf));
|
|
579
|
-
const constLine = reindent(built.constText, fieldIndent, body);
|
|
580
|
-
const comments = built.comments === "" ? "" : `${reindent(`${fieldIndent}${built.comments.trimStart()}`, fieldIndent, body).slice(fieldIndent.length)}\n`;
|
|
581
|
-
fieldsText =
|
|
582
|
-
`fields: () => {\n${comments === "" ? "" : body + comments}${body}const ${built.constName} = ${constLine};\n` +
|
|
583
|
-
`${body}return ${reindent(built.recordText, recordIndent, body)};\n${propIndent}}`;
|
|
584
|
-
} else {
|
|
585
|
-
reasons.push(`${at} — a record list whose \`source:\` this step cannot read; write \`list({ …, fields: (row) => ({ … }) })\` by hand`);
|
|
586
|
-
return undefined;
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
return [
|
|
590
|
-
{ start: call.expression.getStart(sf), end: call.expression.getEnd(), text: call.expression.expression.getText(sf) },
|
|
591
|
-
{ start: record.getStart(sf), end: options.getStart(sf), text: "" },
|
|
592
|
-
appendProperty(sf, options, fieldsText),
|
|
593
|
-
];
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
/** The edits that rewrite one `list.of` call, or `undefined` with a reason pushed. */
|
|
597
|
-
function rewriteCall(sf, call, bindings, reasons, needs) {
|
|
598
|
-
const [item, options] = call.arguments;
|
|
599
|
-
const at = `line ${lineOf(sf, call.getStart(sf))}`;
|
|
600
|
-
if (item === undefined || options === undefined || call.arguments.length > 2) {
|
|
601
|
-
reasons.push(`${at} — \`list.of\` has an unexpected argument list`);
|
|
602
|
-
return undefined;
|
|
603
|
-
}
|
|
604
|
-
if (!ts.isObjectLiteralExpression(options)) {
|
|
605
|
-
reasons.push(`${at} — \`list.of\` passes its options as \`${options.getText(sf)}\`, not an object literal; write \`list({ … })\` by hand`);
|
|
606
|
-
return undefined;
|
|
607
|
-
}
|
|
608
|
-
if (ts.isObjectLiteralExpression(item)) return rewriteRecordList(sf, call, item, options, bindings, reasons, needs);
|
|
609
|
-
if (ts.isCallExpression(item)) return rewriteItemList(sf, call, item, options, reasons);
|
|
610
|
-
reasons.push(`${at} — \`list.of\`'s first argument \`${item.getText(sf)}\` is neither a slot call nor a record written in place; write \`item:\` or \`fields:\` by hand`);
|
|
611
|
-
return undefined;
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
/** The edits adding the framework exports `needs` to the file's import, each where its name sorts. */
|
|
615
|
-
function importEdit(bindings, needs) {
|
|
616
|
-
const missing = [...needs].filter((name) => spelling(bindings, name) === undefined).sort();
|
|
617
|
-
if (missing.length === 0 || bindings.importDecl === undefined) return [];
|
|
618
|
-
const elements = bindings.importDecl.importClause.namedBindings.elements;
|
|
619
|
-
return missing.map((name) => {
|
|
620
|
-
const before = elements.find((e) => e.name.text.toLowerCase() > name.toLowerCase());
|
|
621
|
-
if (before !== undefined) return { start: before.getStart(), end: before.getStart(), text: `${name}, ` };
|
|
622
|
-
const last = elements[elements.length - 1];
|
|
623
|
-
return { start: last.getEnd(), end: last.getEnd(), text: `, ${name}` };
|
|
624
|
-
});
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
/** One pass: rewrites every `list.of` holding no other one. */
|
|
628
|
-
function rewritePass(fileName, text, reasons, seen) {
|
|
629
|
-
const kind = fileName.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
630
|
-
const sf = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, kind);
|
|
631
|
-
const bindings = frameworkBindings(sf);
|
|
632
|
-
if (spelling(bindings, "list") === undefined) return undefined;
|
|
633
|
-
|
|
634
|
-
const edits = [];
|
|
635
|
-
const needs = new Set();
|
|
636
|
-
const visit = (node) => {
|
|
637
|
-
let inner = false;
|
|
638
|
-
ts.forEachChild(node, (child) => {
|
|
639
|
-
if (visit(child)) inner = true;
|
|
640
|
-
});
|
|
641
|
-
if (!isListOf(node, bindings)) return inner;
|
|
642
|
-
if (inner) return true;
|
|
643
|
-
const key = text.slice(node.getStart(sf), node.getEnd());
|
|
644
|
-
if (seen.has(key)) return true;
|
|
645
|
-
const pass = [];
|
|
646
|
-
const result = rewriteCall(sf, node, bindings, pass, needs);
|
|
647
|
-
for (const reason of pass) reasons.push(reason);
|
|
648
|
-
if (result === undefined) seen.add(key);
|
|
649
|
-
else edits.push(...result);
|
|
650
|
-
return true;
|
|
651
|
-
};
|
|
652
|
-
visit(sf);
|
|
653
|
-
if (edits.length === 0) return undefined;
|
|
654
|
-
return applyEdits(text, [...edits, ...importEdit(bindings, needs)]);
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
/** Rewrites one file's text, innermost list first; `undefined` when it holds no `list.of`. */
|
|
658
|
-
export function rewriteSource(fileName, text) {
|
|
659
|
-
const reasons = [];
|
|
660
|
-
const seen = new Set();
|
|
661
|
-
let out = text;
|
|
662
|
-
for (;;) {
|
|
663
|
-
const next = rewritePass(fileName, out, reasons, seen);
|
|
664
|
-
if (next === undefined) break;
|
|
665
|
-
out = next;
|
|
666
|
-
}
|
|
667
|
-
if (out === text && reasons.length === 0) return undefined;
|
|
668
|
-
return { text: out, changed: out !== text, reasons };
|
|
669
|
-
}
|
|
670
|
-
|
|
671
|
-
export default async function listObjectForm(root) {
|
|
672
|
-
const rewritten = [];
|
|
673
|
-
const notRewritten = [];
|
|
674
|
-
for (const file of await sourceFiles(root)) {
|
|
675
|
-
const before = await readFile(file, "utf8");
|
|
676
|
-
const result = rewriteSource(file, before);
|
|
677
|
-
if (result === undefined) continue;
|
|
678
|
-
const rel = relative(root, file).split(sep).join("/");
|
|
679
|
-
if (result.changed) {
|
|
680
|
-
await writeFile(file, result.text, "utf8");
|
|
681
|
-
rewritten.push(rel);
|
|
682
|
-
}
|
|
683
|
-
for (const reason of result.reasons) notRewritten.push({ file: rel, reason });
|
|
684
|
-
}
|
|
685
|
-
return { rewritten, notRewritten };
|
|
686
|
-
}
|