@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,512 +0,0 @@
|
|
|
1
|
-
// 9.0.0 — a picked list chooses from its item's `from`, and its item is a plain
|
|
2
|
-
// declaration holding the pick.
|
|
3
|
-
//
|
|
4
|
-
// A list's `fill.pick` named its candidates in `of`, and an item function received the
|
|
5
|
-
// picked element. A pick names no candidates now: it chooses from the list's item — an
|
|
6
|
-
// item that is itself a `from` primitive (`image({ from: pool.photos })`, a select, a
|
|
7
|
-
// row), or an object exactly one field of which declares `from` and no source. That
|
|
8
|
-
// field holds the pick, and its siblings read it by its const where they read the element.
|
|
9
|
-
// `of` on a list's pick, and an item function on a picked list, no longer compile.
|
|
10
|
-
//
|
|
11
|
-
// What this codemod does, in every `.ts` and `.tsx` under the workspace, wherever a `list`
|
|
12
|
-
// this package exports is called with an options object written in place whose `source:`
|
|
13
|
-
// is a `fill.pick({ … })` naming `of`:
|
|
14
|
-
//
|
|
15
|
-
// - an item that is a media slot: `of: fact.<pool>` goes, and the item gains
|
|
16
|
-
// `from: pool.<pool>` where it names none (`item: image()` becomes
|
|
17
|
-
// `item: image({ from: pool.photos })`), with `pool` imported beside the framework's
|
|
18
|
-
// other names where the file does not import it already.
|
|
19
|
-
// - an item that is a select whose `from` is written exactly as the `of`: `of` goes.
|
|
20
|
-
// - an item function whose element one declaration holds — `source: el` on a select
|
|
21
|
-
// or a media slot, or on a row whose `from` names the `of`'s table: `of` goes, the
|
|
22
|
-
// holding declaration loses `source: el` and moves above the statement declaring the
|
|
23
|
-
// list as a const named for the element (`const el = select({ … })`), the function's
|
|
24
|
-
// other statements with it, and the item becomes what the function returned. Every
|
|
25
|
-
// read of the element is then a read of that const. A media slot gains its `from`.
|
|
26
|
-
//
|
|
27
|
-
// An import of `fact` left with nothing to read is removed.
|
|
28
|
-
//
|
|
29
|
-
// What it hands back, by line: an `of` that is no table, pool or the select's own
|
|
30
|
-
// choices (a list slot, a sibling list, a `compute()`) — a pick chooses only from its
|
|
31
|
-
// item's `from` now; an item function whose element no one declaration holds, or two do;
|
|
32
|
-
// and a hoisted name the file already declares.
|
|
33
|
-
|
|
34
|
-
import { readdir, readFile, writeFile } from "node:fs/promises";
|
|
35
|
-
import { join, relative, sep } from "node:path";
|
|
36
|
-
|
|
37
|
-
import ts from "typescript";
|
|
38
|
-
|
|
39
|
-
const CONCRETE = "@homepages/concrete";
|
|
40
|
-
const EXTENSIONS = [".ts", ".tsx"];
|
|
41
|
-
const SKIP_DIRS = new Set(["node_modules", ".concrete", ".git", "dist", "build"]);
|
|
42
|
-
|
|
43
|
-
/** The media kinds, each with the pools it may pick from. */
|
|
44
|
-
const MEDIA = {
|
|
45
|
-
image: ["photos", "floor_plans"],
|
|
46
|
-
video: ["videos"],
|
|
47
|
-
tour: ["tours"],
|
|
48
|
-
document: ["documents"],
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
async function sourceFiles(root) {
|
|
52
|
-
const found = [];
|
|
53
|
-
async function walk(dir) {
|
|
54
|
-
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
55
|
-
if (entry.isDirectory()) {
|
|
56
|
-
if (!SKIP_DIRS.has(entry.name)) await walk(join(dir, entry.name));
|
|
57
|
-
} else if (EXTENSIONS.some((ext) => entry.name.endsWith(ext)) && !entry.name.endsWith(".d.ts")) {
|
|
58
|
-
found.push(join(dir, entry.name));
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
await walk(root);
|
|
63
|
-
return found.sort();
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function lineOf(sf, pos) {
|
|
67
|
-
return sf.getLineAndCharacterOfPosition(pos).line + 1;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/** The local names this file reaches the framework's names by, the namespaces a whole-module import binds, and the imports. */
|
|
71
|
-
function frameworkBindings(sf, isFramework) {
|
|
72
|
-
const byName = new Map();
|
|
73
|
-
const namespaces = new Set();
|
|
74
|
-
const imports = [];
|
|
75
|
-
for (const statement of sf.statements) {
|
|
76
|
-
if (!ts.isImportDeclaration(statement) || statement.importClause?.isTypeOnly) continue;
|
|
77
|
-
if (!ts.isStringLiteral(statement.moduleSpecifier) || !isFramework(statement.moduleSpecifier.text)) continue;
|
|
78
|
-
const bindings = statement.importClause?.namedBindings;
|
|
79
|
-
if (bindings === undefined) continue;
|
|
80
|
-
if (ts.isNamespaceImport(bindings)) {
|
|
81
|
-
namespaces.add(bindings.name.text);
|
|
82
|
-
continue;
|
|
83
|
-
}
|
|
84
|
-
imports.push(statement);
|
|
85
|
-
for (const element of bindings.elements) {
|
|
86
|
-
if (element.isTypeOnly) continue;
|
|
87
|
-
const name = (element.propertyName ?? element.name).text;
|
|
88
|
-
if (!byName.has(name)) byName.set(name, new Set());
|
|
89
|
-
byName.get(name).add(element.name.text);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
return { byName, namespaces, imports };
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/** Whether `node` reads the framework's `name`: a local bound to it, or `<namespace>.<name>`. */
|
|
96
|
-
function isRef(node, name, { byName, namespaces }) {
|
|
97
|
-
if (ts.isIdentifier(node)) return byName.get(name)?.has(node.text) === true;
|
|
98
|
-
return ts.isPropertyAccessExpression(node) && node.name.text === name && ts.isIdentifier(node.expression) && namespaces.has(node.expression.text);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function propertyNamed(literal, name) {
|
|
102
|
-
return literal.properties.find(
|
|
103
|
-
(p) => ts.isPropertyAssignment(p) && (ts.isIdentifier(p.name) || ts.isStringLiteral(p.name)) && p.name.text === name,
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/** The `fill.pick({ … })` options a `source:` initializer is, or `undefined`. */
|
|
108
|
-
function pickOptions(init, bindings) {
|
|
109
|
-
if (init === undefined || !ts.isCallExpression(init)) return undefined;
|
|
110
|
-
const callee = init.expression;
|
|
111
|
-
if (!ts.isPropertyAccessExpression(callee) || callee.name.text !== "pick" || !isRef(callee.expression, "fill", bindings)) return undefined;
|
|
112
|
-
const [options] = init.arguments;
|
|
113
|
-
return options !== undefined && ts.isObjectLiteralExpression(options) ? options : undefined;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/** Which framework builder a call is — `list`, `row`, `select` or a media kind — or `undefined`. */
|
|
117
|
-
function builderOf(node, bindings) {
|
|
118
|
-
if (!ts.isCallExpression(node)) return undefined;
|
|
119
|
-
return ["list", "row", "select", ...Object.keys(MEDIA)].find((name) => isRef(node.expression, name, bindings));
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/** The pool `fact.<pool>` reads, or `undefined`. */
|
|
123
|
-
function factPool(node, bindings) {
|
|
124
|
-
return ts.isPropertyAccessExpression(node) && isRef(node.expression, "fact", bindings) ? node.name.text : undefined;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/** Whether `node` reads a table off `fact`: `fact.property.<table>`. */
|
|
128
|
-
function isFactTable(node, bindings) {
|
|
129
|
-
return (
|
|
130
|
-
ts.isPropertyAccessExpression(node) &&
|
|
131
|
-
ts.isPropertyAccessExpression(node.expression) &&
|
|
132
|
-
node.expression.name.text === "property" &&
|
|
133
|
-
isRef(node.expression.expression, "fact", bindings)
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const squash = (text) => text.replace(/\s+/g, "");
|
|
138
|
-
|
|
139
|
-
/** Whether only whitespace precedes `pos` on its line. */
|
|
140
|
-
function startsLine(text, pos) {
|
|
141
|
-
const lineStart = text.lastIndexOf("\n", pos - 1) + 1;
|
|
142
|
-
return text.slice(lineStart, pos).trim() === "";
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/** The whitespace a line starts with. */
|
|
146
|
-
function indentAt(text, pos) {
|
|
147
|
-
const lineStart = text.lastIndexOf("\n", pos - 1) + 1;
|
|
148
|
-
return /^[ \t]*/.exec(text.slice(lineStart))[0];
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/** The text of `[start, end)` with every edit inside it applied — edits sorted, non-overlapping. */
|
|
152
|
-
function applyWithin(text, start, end, edits) {
|
|
153
|
-
let out = text.slice(start, end);
|
|
154
|
-
for (const edit of edits.filter((e) => e.start >= start && e.end <= end).sort((a, b) => b.start - a.start || b.end - a.end)) {
|
|
155
|
-
out = out.slice(0, edit.start - start) + edit.text + out.slice(edit.end - start);
|
|
156
|
-
}
|
|
157
|
-
return out;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/** `text`, each line after the first moved left by up to `n` columns of leading spaces. */
|
|
161
|
-
function dedentBy(text, n) {
|
|
162
|
-
if (n <= 0) return text;
|
|
163
|
-
return text
|
|
164
|
-
.split("\n")
|
|
165
|
-
.map((line, i) => (i === 0 ? line : line.slice(Math.min(n, /^ */.exec(line)[0].length))))
|
|
166
|
-
.join("\n");
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/** The edit removing one property of an object literal, with the separator it owns. */
|
|
170
|
-
function removal(sf, literal, prop) {
|
|
171
|
-
const text = sf.text;
|
|
172
|
-
const start = prop.getStart(sf);
|
|
173
|
-
let end = prop.getEnd();
|
|
174
|
-
let after = end;
|
|
175
|
-
while (text[after] === " " || text[after] === "\t") after += 1;
|
|
176
|
-
if (text[after] === ",") {
|
|
177
|
-
end = after + 1;
|
|
178
|
-
let rest = end;
|
|
179
|
-
while (text[rest] === " " || text[rest] === "\t") rest += 1;
|
|
180
|
-
if (startsLine(text, start) && (text[rest] === "\n" || text.startsWith("\r\n", rest))) {
|
|
181
|
-
const lineStart = text.lastIndexOf("\n", start - 1) + 1;
|
|
182
|
-
return { start: lineStart, end: rest + (text[rest] === "\n" ? 1 : 2), text: "" };
|
|
183
|
-
}
|
|
184
|
-
return { start, end: rest, text: "" };
|
|
185
|
-
}
|
|
186
|
-
const index = literal.properties.indexOf(prop);
|
|
187
|
-
if (index === 0) return { start, end, text: "" };
|
|
188
|
-
return { start: literal.properties[index - 1].getEnd(), end, text: "" };
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/** The edit writing `entry` into a call's options before `before`, or — no options — as its whole argument. */
|
|
192
|
-
function insertOption(sf, call, entry) {
|
|
193
|
-
const [options] = call.arguments;
|
|
194
|
-
if (options === undefined) {
|
|
195
|
-
const at = call.getEnd() - 1;
|
|
196
|
-
return { start: at, end: at, text: `{ ${entry} }` };
|
|
197
|
-
}
|
|
198
|
-
const first = options.properties[0];
|
|
199
|
-
if (first === undefined) return { start: options.getStart(sf), end: options.getEnd(), text: `{ ${entry} }` };
|
|
200
|
-
const text = sf.text;
|
|
201
|
-
const at = first.getStart(sf);
|
|
202
|
-
const multiline = text.slice(options.getStart(sf), options.getEnd()).includes("\n");
|
|
203
|
-
if (multiline && startsLine(text, at)) return { start: at, end: at, text: `${entry},\n${indentAt(text, at)}` };
|
|
204
|
-
return { start: at, end: at, text: `${entry}, ` };
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/** The edit adding `pool` to the framework import that may take it, or `undefined` where none may. */
|
|
208
|
-
function importPool(sf, bindings, poolHome) {
|
|
209
|
-
const declaration = bindings.imports.find((d) => poolHome(d.moduleSpecifier.text));
|
|
210
|
-
if (declaration === undefined) return undefined;
|
|
211
|
-
const elements = declaration.importClause.namedBindings.elements;
|
|
212
|
-
const text = sf.text;
|
|
213
|
-
const before = elements.find((e) => e.name.text.toLowerCase() > "pool");
|
|
214
|
-
if (before === undefined) {
|
|
215
|
-
const last = elements[elements.length - 1];
|
|
216
|
-
const multiline = text.slice(declaration.getStart(sf), declaration.getEnd()).includes("\n");
|
|
217
|
-
return { start: last.getEnd(), end: last.getEnd(), text: multiline ? `,\n${indentAt(text, last.getStart(sf))}pool` : ", pool" };
|
|
218
|
-
}
|
|
219
|
-
const at = before.getStart(sf);
|
|
220
|
-
return { start: at, end: at, text: startsLine(text, at) ? `pool,\n${indentAt(text, at)}` : "pool, " };
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/** The edit removing the import specifier bound to `local`, with its separator. */
|
|
224
|
-
function removeSpecifier(sf, bindings, local) {
|
|
225
|
-
for (const declaration of bindings.imports) {
|
|
226
|
-
const elements = declaration.importClause.namedBindings.elements;
|
|
227
|
-
const element = elements.find((e) => e.name.text === local);
|
|
228
|
-
if (element === undefined) continue;
|
|
229
|
-
if (elements.length === 1) return undefined;
|
|
230
|
-
const text = sf.text;
|
|
231
|
-
const index = elements.indexOf(element);
|
|
232
|
-
const start = element.getStart(sf);
|
|
233
|
-
if (index < elements.length - 1) {
|
|
234
|
-
const next = elements[index + 1].getStart(sf);
|
|
235
|
-
if (startsLine(text, start)) {
|
|
236
|
-
const lineStart = text.lastIndexOf("\n", start - 1) + 1;
|
|
237
|
-
const nextLine = text.lastIndexOf("\n", next - 1) + 1;
|
|
238
|
-
return { start: lineStart, end: nextLine, text: "" };
|
|
239
|
-
}
|
|
240
|
-
return { start, end: next, text: "" };
|
|
241
|
-
}
|
|
242
|
-
return { start: elements[index - 1].getEnd(), end: element.getEnd(), text: "" };
|
|
243
|
-
}
|
|
244
|
-
return undefined;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/** Every name the file declares at any depth — what a hoisted const may not take. */
|
|
248
|
-
function declaredNames(sf) {
|
|
249
|
-
const names = new Map();
|
|
250
|
-
const visit = (node) => {
|
|
251
|
-
const name = node.name;
|
|
252
|
-
if (
|
|
253
|
-
name !== undefined &&
|
|
254
|
-
ts.isIdentifier(name) &&
|
|
255
|
-
(ts.isVariableDeclaration(node) || ts.isParameter(node) || ts.isFunctionDeclaration(node) || ts.isBindingElement(node) || ts.isImportSpecifier(node) || ts.isClassDeclaration(node))
|
|
256
|
-
) {
|
|
257
|
-
if (!names.has(name.text)) names.set(name.text, []);
|
|
258
|
-
names.get(name.text).push(node);
|
|
259
|
-
}
|
|
260
|
-
ts.forEachChild(node, visit);
|
|
261
|
-
};
|
|
262
|
-
visit(sf);
|
|
263
|
-
return names;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/** The statement at the file's top level that `node` sits in. */
|
|
267
|
-
function topStatement(node) {
|
|
268
|
-
let at = node;
|
|
269
|
-
while (at.parent !== undefined && !ts.isSourceFile(at.parent)) at = at.parent;
|
|
270
|
-
return at;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/** Where a hoisted declaration goes: before the top-level statement's leading comments. */
|
|
274
|
-
function hoistPoint(sf, statement) {
|
|
275
|
-
const text = sf.text;
|
|
276
|
-
let at = statement.getFullStart();
|
|
277
|
-
while (at < text.length && /\s/.test(text[at])) at += 1;
|
|
278
|
-
return at;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Rewrites one file's text; `undefined` when it holds no list pick naming `of`.
|
|
283
|
-
* `isFramework` decides which import specifiers are the framework, and `poolHome` which
|
|
284
|
-
* of them may take an added `pool` (both default to this package's name).
|
|
285
|
-
*/
|
|
286
|
-
export function rewriteSource(fileName, text, { isFramework = (s) => s === CONCRETE, poolHome = (s) => s === CONCRETE } = {}) {
|
|
287
|
-
const kind = fileName.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
288
|
-
const sf = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, kind);
|
|
289
|
-
const bindings = frameworkBindings(sf, isFramework);
|
|
290
|
-
if (bindings.byName.size === 0 && bindings.namespaces.size === 0) return undefined;
|
|
291
|
-
|
|
292
|
-
const declared = declaredNames(sf);
|
|
293
|
-
const poolLocal = [...(bindings.byName.get("pool") ?? [])][0] ?? ([...bindings.namespaces][0] === undefined ? undefined : `${[...bindings.namespaces][0]}.pool`);
|
|
294
|
-
const edits = [];
|
|
295
|
-
const hoists = new Map(); // top-level statement -> declarations to place above it
|
|
296
|
-
const removed = [];
|
|
297
|
-
const reasons = [];
|
|
298
|
-
let needsPool = false;
|
|
299
|
-
|
|
300
|
-
// The `from` a media item takes for the pool its `of` read, or a refusal.
|
|
301
|
-
const mediaFrom = (call, builder, ofNode) => {
|
|
302
|
-
const pool = factPool(ofNode, bindings);
|
|
303
|
-
if (pool === undefined || !MEDIA[builder].includes(pool)) return { refuse: `its \`of\` is no pool of ${builder}'s` };
|
|
304
|
-
const [options] = call.arguments;
|
|
305
|
-
const from = options !== undefined && ts.isObjectLiteralExpression(options) ? propertyNamed(options, "from") : undefined;
|
|
306
|
-
if (from !== undefined) {
|
|
307
|
-
return squash(from.initializer.getText(sf)).endsWith(`.${pool}`) && !from.initializer.getText(sf).includes(",")
|
|
308
|
-
? { edit: undefined }
|
|
309
|
-
: { refuse: `its item offers \`from: ${from.initializer.getText(sf)}\` but the pick read \`of: ${ofNode.getText(sf)}\` — the pick now chooses from every pool in \`from\`` };
|
|
310
|
-
}
|
|
311
|
-
if (poolLocal === undefined && declared.has("pool")) return { refuse: "this file declares its own `pool`; name the item's pools in `from`" };
|
|
312
|
-
return { edit: insertOption(sf, call, `from: ${poolLocal ?? "pool"}.${pool}`), importsPool: poolLocal === undefined };
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
// An item function's element, held by one declaration: the edits turning the function
|
|
316
|
-
// into a plain item, or a refusal.
|
|
317
|
-
const plainItem = (fn, ofNode, line) => {
|
|
318
|
-
const [param] = fn.parameters;
|
|
319
|
-
if (param === undefined || !ts.isIdentifier(param.name) || fn.parameters.length !== 1) return `line ${String(line)}: its item function takes no one named element`;
|
|
320
|
-
const el = param.name.text;
|
|
321
|
-
const statements = ts.isBlock(fn.body) ? [...fn.body.statements] : [];
|
|
322
|
-
const returned = ts.isBlock(fn.body) ? statements.pop() : undefined;
|
|
323
|
-
const result = ts.isBlock(fn.body) ? (returned !== undefined && ts.isReturnStatement(returned) ? returned.expression : undefined) : fn.body;
|
|
324
|
-
if (result === undefined) return `line ${String(line)}: its item function returns no one expression`;
|
|
325
|
-
// The declarations whose `source:` is the element, anywhere in the function.
|
|
326
|
-
const holders = [];
|
|
327
|
-
const find = (node) => {
|
|
328
|
-
if (ts.isCallExpression(node)) {
|
|
329
|
-
const builder = builderOf(node, bindings);
|
|
330
|
-
const [options] = node.arguments;
|
|
331
|
-
const source = builder !== undefined && options !== undefined && ts.isObjectLiteralExpression(options) ? propertyNamed(options, "source") : undefined;
|
|
332
|
-
if (source !== undefined && ts.isIdentifier(source.initializer) && source.initializer.text === el) holders.push({ call: node, builder, options, source });
|
|
333
|
-
}
|
|
334
|
-
ts.forEachChild(node, find);
|
|
335
|
-
};
|
|
336
|
-
find(fn.body);
|
|
337
|
-
if (holders.length !== 1) {
|
|
338
|
-
return `line ${String(line)}: ${holders.length === 0 ? "no declaration in its item function holds" : `${String(holders.length)} declarations in its item function hold`} the element — declare the field holding the pick with \`from\` and no source, and read it by its const`;
|
|
339
|
-
}
|
|
340
|
-
const [holder] = holders;
|
|
341
|
-
// The edits inside the function: the holder loses `source: el` and gains its `from`.
|
|
342
|
-
const local = [removal(sf, holder.options, holder.source)];
|
|
343
|
-
let importsPool = false;
|
|
344
|
-
const from = propertyNamed(holder.options, "from");
|
|
345
|
-
if (holder.builder === "select") {
|
|
346
|
-
if (from === undefined || squash(from.initializer.getText(sf)) !== squash(ofNode.getText(sf))) {
|
|
347
|
-
return `line ${String(line)}: the select holding the element offers other choices than the pick's \`of: ${ofNode.getText(sf)}\` — make its \`from\` the choices the model may pick`;
|
|
348
|
-
}
|
|
349
|
-
} else if (holder.builder === "row") {
|
|
350
|
-
if (!isFactTable(ofNode, bindings) || (from !== undefined && squash(from.initializer.getText(sf)) !== squash(ofNode.getText(sf)))) {
|
|
351
|
-
return `line ${String(line)}: the row holding the element is no row of the pick's \`of: ${ofNode.getText(sf)}\``;
|
|
352
|
-
}
|
|
353
|
-
if (from === undefined) local.push(insertOption(sf, holder.call, `from: ${ofNode.getText(sf)}`));
|
|
354
|
-
} else if (holder.builder in MEDIA) {
|
|
355
|
-
const media = mediaFrom(holder.call, holder.builder, ofNode);
|
|
356
|
-
if (media.refuse !== undefined) return `line ${String(line)}: ${media.refuse}`;
|
|
357
|
-
if (media.edit !== undefined) local.push(media.edit);
|
|
358
|
-
importsPool = media.importsPool === true;
|
|
359
|
-
} else {
|
|
360
|
-
return `line ${String(line)}: the element is held by a ${String(holder.builder)}, which declares no \`from\``;
|
|
361
|
-
}
|
|
362
|
-
// A holder declared as a const keeps its name; one written inline as a field becomes a
|
|
363
|
-
// const named for the element, and the field reads it.
|
|
364
|
-
const declaredBy = holder.call.parent;
|
|
365
|
-
const inline = !(ts.isVariableDeclaration(declaredBy) && ts.isIdentifier(declaredBy.name));
|
|
366
|
-
const holderName = inline ? el : declaredBy.name.text;
|
|
367
|
-
const names = [
|
|
368
|
-
...(inline ? [el] : []),
|
|
369
|
-
...statements.flatMap((s) => (ts.isVariableStatement(s) ? s.declarationList.declarations.map((d) => d.name.getText(sf)) : [])),
|
|
370
|
-
];
|
|
371
|
-
for (const name of names) {
|
|
372
|
-
const elsewhere = (declared.get(name) ?? []).filter((node) => !(node.pos >= fn.pos && node.end <= fn.end));
|
|
373
|
-
if (elsewhere.length > 0) return `line ${String(line)}: the file already declares \`${name}\`, the name the item's declaration would take above the list`;
|
|
374
|
-
}
|
|
375
|
-
// The holder's own edits, which a hoisted const carries and its old place does not.
|
|
376
|
-
const inHolder = (e) => !e.replaces && e.start >= holder.call.getStart(sf) && e.end <= holder.call.getEnd();
|
|
377
|
-
if (inline) {
|
|
378
|
-
const field = holder.call.parent;
|
|
379
|
-
local.push(
|
|
380
|
-
ts.isPropertyAssignment(field) && field.name.getText(sf) === el
|
|
381
|
-
? { start: field.getStart(sf), end: field.getEnd(), text: el, replaces: true }
|
|
382
|
-
: { start: holder.call.getStart(sf), end: holder.call.getEnd(), text: el, replaces: true },
|
|
383
|
-
);
|
|
384
|
-
} else {
|
|
385
|
-
// Every other read of the element is a read of the holder's const.
|
|
386
|
-
const [sourceStart, sourceEnd] = [holder.source.getStart(sf), holder.source.getEnd()];
|
|
387
|
-
const reads = (node) => {
|
|
388
|
-
if (ts.isIdentifier(node) && node.text === el && node !== param.name) {
|
|
389
|
-
const at = node.getStart(sf);
|
|
390
|
-
if (at < sourceStart || at >= sourceEnd) local.push({ start: at, end: node.getEnd(), text: holderName });
|
|
391
|
-
}
|
|
392
|
-
ts.forEachChild(node, reads);
|
|
393
|
-
};
|
|
394
|
-
reads(fn.body);
|
|
395
|
-
}
|
|
396
|
-
// The holder goes first: it stands where the element was, which every other
|
|
397
|
-
// statement could read.
|
|
398
|
-
const placed = inline ? local.filter((e) => !inHolder(e)) : local;
|
|
399
|
-
const holderStatement = inline ? undefined : declaredBy.parent.parent;
|
|
400
|
-
const ordered = holderStatement === undefined ? statements : [holderStatement, ...statements.filter((s) => s !== holderStatement)];
|
|
401
|
-
const hoisted = ordered.map((statement) => dedentBy(applyWithin(sf.text, statement.getStart(sf), statement.getEnd(), placed), indentAt(sf.text, statement.getStart(sf)).length));
|
|
402
|
-
if (inline) {
|
|
403
|
-
const call = applyWithin(sf.text, holder.call.getStart(sf), holder.call.getEnd(), local.filter((e) => !e.replaces));
|
|
404
|
-
hoisted.unshift(`const ${el} = ${dedentBy(call, indentAt(sf.text, holder.call.getStart(sf)).length)};`);
|
|
405
|
-
}
|
|
406
|
-
const top = topStatement(fn);
|
|
407
|
-
if (!hoists.has(top)) hoists.set(top, []);
|
|
408
|
-
hoists.get(top).push(...hoisted);
|
|
409
|
-
// The item becomes what the function returned, moved left to where the function stood.
|
|
410
|
-
const body = applyWithin(sf.text, result.getStart(sf), result.getEnd(), placed);
|
|
411
|
-
const shift = indentAt(sf.text, result.getStart(sf)).length - indentAt(sf.text, fn.getStart(sf)).length;
|
|
412
|
-
edits.push({ start: fn.getStart(sf), end: fn.getEnd(), text: dedentBy(body, shift) });
|
|
413
|
-
if (importsPool) needsPool = true;
|
|
414
|
-
return undefined;
|
|
415
|
-
};
|
|
416
|
-
|
|
417
|
-
const visit = (node) => {
|
|
418
|
-
if (builderOf(node, bindings) === "list") {
|
|
419
|
-
const [options] = node.arguments;
|
|
420
|
-
const source = options !== undefined && ts.isObjectLiteralExpression(options) ? propertyNamed(options, "source") : undefined;
|
|
421
|
-
const pick = pickOptions(source?.initializer, bindings);
|
|
422
|
-
const of = pick === undefined ? undefined : propertyNamed(pick, "of");
|
|
423
|
-
if (of !== undefined) {
|
|
424
|
-
const line = lineOf(sf, of.getStart(sf));
|
|
425
|
-
const item = propertyNamed(options, "item")?.initializer;
|
|
426
|
-
const ofNode = of.initializer;
|
|
427
|
-
let refusal;
|
|
428
|
-
if (item === undefined) {
|
|
429
|
-
refusal = `line ${String(line)}: a list picked \`of: ${ofNode.getText(sf)}\` declares no item — declare the item the pick fills: a \`from\` primitive, or an object holding one`;
|
|
430
|
-
} else if (ts.isArrowFunction(item) || ts.isFunctionExpression(item)) {
|
|
431
|
-
refusal = plainItem(item, ofNode, line);
|
|
432
|
-
} else {
|
|
433
|
-
const builder = builderOf(item, bindings);
|
|
434
|
-
if (builder !== undefined && builder in MEDIA) {
|
|
435
|
-
const media = mediaFrom(item, builder, ofNode);
|
|
436
|
-
if (media.refuse !== undefined) refusal = `line ${String(line)}: ${media.refuse}`;
|
|
437
|
-
else if (media.edit !== undefined) {
|
|
438
|
-
edits.push(media.edit);
|
|
439
|
-
if (media.importsPool === true) needsPool = true;
|
|
440
|
-
}
|
|
441
|
-
} else if (builder === "select") {
|
|
442
|
-
const from = propertyNamed(item.arguments[0] ?? { properties: [] }, "from");
|
|
443
|
-
if (from === undefined || squash(from.initializer.getText(sf)) !== squash(ofNode.getText(sf))) {
|
|
444
|
-
refusal = `line ${String(line)}: the list's select item offers other choices than its pick's \`of: ${ofNode.getText(sf)}\` — make the item's \`from\` the choices the model may pick`;
|
|
445
|
-
}
|
|
446
|
-
} else {
|
|
447
|
-
refusal = `line ${String(line)}: a list picked \`of: ${ofNode.getText(sf)}\` — a pick chooses only from its item's \`from\`: a media slot's pools, a select's choices or a row's table`;
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
if (refusal === undefined) {
|
|
451
|
-
edits.push(removal(sf, pick, of));
|
|
452
|
-
removed.push([of.getStart(sf), of.getEnd()]);
|
|
453
|
-
} else {
|
|
454
|
-
reasons.push(refusal);
|
|
455
|
-
}
|
|
456
|
-
return; // a nested pick inside a rewritten item is the next run's
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
ts.forEachChild(node, visit);
|
|
460
|
-
};
|
|
461
|
-
visit(sf);
|
|
462
|
-
if (edits.length === 0 && reasons.length === 0) return undefined;
|
|
463
|
-
|
|
464
|
-
for (const [top, declarations] of hoists) {
|
|
465
|
-
const at = hoistPoint(sf, top);
|
|
466
|
-
edits.push({ start: at, end: at, text: `${declarations.join("\n")}\n\n` });
|
|
467
|
-
}
|
|
468
|
-
if (needsPool) {
|
|
469
|
-
const add = importPool(sf, bindings, poolHome);
|
|
470
|
-
if (add === undefined) reasons.push("no import of this package to add `pool` to — import `pool` beside the names this file takes from it");
|
|
471
|
-
else edits.push(add);
|
|
472
|
-
}
|
|
473
|
-
// A `fact` read only by the `of`s that went goes with them.
|
|
474
|
-
for (const local of bindings.byName.get("fact") ?? []) {
|
|
475
|
-
let reads = 0;
|
|
476
|
-
const count = (node) => {
|
|
477
|
-
if (ts.isIdentifier(node) && node.text === local && !ts.isImportSpecifier(node.parent)) {
|
|
478
|
-
const at = node.getStart(sf);
|
|
479
|
-
if (!removed.some(([start, end]) => at >= start && at < end)) reads += 1;
|
|
480
|
-
}
|
|
481
|
-
ts.forEachChild(node, count);
|
|
482
|
-
};
|
|
483
|
-
count(sf);
|
|
484
|
-
if (reads === 0 && removed.length > 0) {
|
|
485
|
-
const edit = removeSpecifier(sf, bindings, local);
|
|
486
|
-
if (edit !== undefined) edits.push(edit);
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
let out = text;
|
|
491
|
-
for (const edit of edits.sort((a, b) => b.start - a.start || b.end - a.end)) out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
|
|
492
|
-
return { text: out, changed: edits.length > 0, reasons };
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
/** @param {string} root @returns {Promise<{rewritten: string[], notRewritten: {file: string, reason: string}[]}>} */
|
|
496
|
-
export default async function writeTheWholeItem(root) {
|
|
497
|
-
const rewritten = [];
|
|
498
|
-
const notRewritten = [];
|
|
499
|
-
for (const file of await sourceFiles(root)) {
|
|
500
|
-
const before = await readFile(file, "utf8");
|
|
501
|
-
if (!before.includes("pick")) continue;
|
|
502
|
-
const result = rewriteSource(file, before);
|
|
503
|
-
if (result === undefined) continue;
|
|
504
|
-
const rel = relative(root, file).split(sep).join("/");
|
|
505
|
-
if (result.changed) {
|
|
506
|
-
await writeFile(file, result.text, "utf8");
|
|
507
|
-
rewritten.push(rel);
|
|
508
|
-
}
|
|
509
|
-
if (result.reasons.length > 0) notRewritten.push({ file: rel, reason: result.reasons.join("; ") });
|
|
510
|
-
}
|
|
511
|
-
return { rewritten, notRewritten };
|
|
512
|
-
}
|
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
// 9.0.0 — a sourced slot writes where its value is stored; `writeback` is gone.
|
|
2
|
-
//
|
|
3
|
-
// Whether a slot's edit reaches its source is derived now: a slot whose `source:` is a
|
|
4
|
-
// fact, a row's member or another slot writes where that value is stored, unless the
|
|
5
|
-
// value is not stored (a part of an address, a computed member). No builder takes
|
|
6
|
-
// `writeback`, and a key left in an options object no longer compiles.
|
|
7
|
-
//
|
|
8
|
-
// What this codemod does, in every `.ts` and `.tsx` under the workspace that imports this
|
|
9
|
-
// package: every `writeback` property of an object literal passed to a call is removed —
|
|
10
|
-
// a value slot's options, an object's label, a row's or a list's options alike — with
|
|
11
|
-
// the comma and the line it sat on when it had one to itself.
|
|
12
|
-
//
|
|
13
|
-
// What it hands back, by line, as `Manual:`: each `writeback: false` it removed from a
|
|
14
|
-
// value slot that reads a value which is stored — a fact, a row's member, another slot.
|
|
15
|
-
// That slot now writes its source. Where it should not — a value seeded from the source
|
|
16
|
-
// but free to diverge — make its source a `compute()` over that read: `source: compute({
|
|
17
|
-
// reads: [contact.name] }, (name) => name)`. Not named: a `false` on a part of an address
|
|
18
|
-
// or a computed member (`status`, `distance_m`, `price_period`), which stay read-only; on
|
|
19
|
-
// a media slot, whose edit is the asset it commits; and on a list, a row or an object,
|
|
20
|
-
// whose `writeback` never said whether a value's edit writes back.
|
|
21
|
-
|
|
22
|
-
import { readdir, readFile, writeFile } from "node:fs/promises";
|
|
23
|
-
import { join, relative, sep } from "node:path";
|
|
24
|
-
|
|
25
|
-
import ts from "typescript";
|
|
26
|
-
|
|
27
|
-
const CONCRETE = "@homepages/concrete";
|
|
28
|
-
const EXTENSIONS = [".ts", ".tsx"];
|
|
29
|
-
const SKIP_DIRS = new Set(["node_modules", ".concrete", ".git", "dist", "build"]);
|
|
30
|
-
|
|
31
|
-
// The last segment of a source that names a value no write can land on: a part of an
|
|
32
|
-
// address (the registry's and the legacy flat spellings) or a computed member.
|
|
33
|
-
const READ_ONLY_SEGMENTS = new Set([
|
|
34
|
-
"street",
|
|
35
|
-
"unit",
|
|
36
|
-
"city",
|
|
37
|
-
"state",
|
|
38
|
-
"postal",
|
|
39
|
-
"latitude",
|
|
40
|
-
"longitude",
|
|
41
|
-
"lat",
|
|
42
|
-
"lng",
|
|
43
|
-
"address_street",
|
|
44
|
-
"address_unit",
|
|
45
|
-
"address_city",
|
|
46
|
-
"address_state",
|
|
47
|
-
"address_zip",
|
|
48
|
-
"address_postal",
|
|
49
|
-
"status",
|
|
50
|
-
"distance_m",
|
|
51
|
-
"price_period",
|
|
52
|
-
]);
|
|
53
|
-
|
|
54
|
-
// The builders whose `writeback: false` is never an author's opt-out: the media slots and
|
|
55
|
-
// the structural builders, by the name the call reaches them under.
|
|
56
|
-
const NEVER_NAMED = /(^|\.)(image|video|tour|document|list|row|object)$/;
|
|
57
|
-
|
|
58
|
-
async function sourceFiles(root) {
|
|
59
|
-
const found = [];
|
|
60
|
-
async function walk(dir) {
|
|
61
|
-
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
62
|
-
if (entry.isDirectory()) {
|
|
63
|
-
if (!SKIP_DIRS.has(entry.name)) await walk(join(dir, entry.name));
|
|
64
|
-
} else if (EXTENSIONS.some((ext) => entry.name.endsWith(ext)) && !entry.name.endsWith(".d.ts")) {
|
|
65
|
-
found.push(join(dir, entry.name));
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
await walk(root);
|
|
70
|
-
return found.sort();
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function lineOf(sf, pos) {
|
|
74
|
-
return sf.getLineAndCharacterOfPosition(pos).line + 1;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function importsFramework(sf, isFramework) {
|
|
78
|
-
return sf.statements.some((s) => ts.isImportDeclaration(s) && ts.isStringLiteral(s.moduleSpecifier) && isFramework(s.moduleSpecifier.text));
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function nameOf(property) {
|
|
82
|
-
const name = property.name;
|
|
83
|
-
return name !== undefined && (ts.isIdentifier(name) || ts.isStringLiteral(name)) ? name.text : undefined;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// Whether the slot's source reads a stored value: a property-access chain or an indexed
|
|
87
|
-
// read (`fact.property.listing_intent`, `contact.name`, `Hero.slots.title`,
|
|
88
|
-
// `fact.property.contacts[0].headshot`) whose last segment names no address part or
|
|
89
|
-
// computed member. A `compute()`, a fill and an absent source read nothing it could write.
|
|
90
|
-
function readsStoredValue(options) {
|
|
91
|
-
const source = options.properties.find((p) => ts.isPropertyAssignment(p) && nameOf(p) === "source");
|
|
92
|
-
if (source === undefined) return { reads: false };
|
|
93
|
-
const value = source.initializer;
|
|
94
|
-
if (ts.isPropertyAccessExpression(value)) return { reads: !READ_ONLY_SEGMENTS.has(value.name.text), value };
|
|
95
|
-
if (ts.isElementAccessExpression(value) || ts.isIdentifier(value)) return { reads: true, value };
|
|
96
|
-
return { reads: false };
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// The span a removed property occupies: its own line when nothing else shares it, else
|
|
100
|
-
// the property and the comma (and space) that joined it to its neighbour.
|
|
101
|
-
function removalSpan(sf, text, literal, index) {
|
|
102
|
-
const property = literal.properties[index];
|
|
103
|
-
const start = property.getStart(sf);
|
|
104
|
-
let end = property.getEnd();
|
|
105
|
-
const lineStart = text.lastIndexOf("\n", start - 1) + 1;
|
|
106
|
-
if (text.slice(lineStart, start).trim() === "") {
|
|
107
|
-
const after = text.slice(end).match(/^,?[ \t]*\n/);
|
|
108
|
-
if (after !== null) return { start: lineStart, end: end + after[0].length };
|
|
109
|
-
}
|
|
110
|
-
const trailing = text.slice(end).match(/^\s*,[ \t]*/);
|
|
111
|
-
if (trailing !== null) return { start, end: end + trailing[0].length };
|
|
112
|
-
// The last property: take the comma before it instead.
|
|
113
|
-
const previous = literal.properties[index - 1];
|
|
114
|
-
return { start: previous === undefined ? start : previous.getEnd(), end };
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* Rewrites one file's text; `undefined` when it holds no `writeback` this package takes.
|
|
119
|
-
* `isFramework` decides which import specifiers are the framework.
|
|
120
|
-
*/
|
|
121
|
-
export function rewriteSource(fileName, text, isFramework = (specifier) => specifier === CONCRETE) {
|
|
122
|
-
const kind = fileName.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
123
|
-
const sf = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, kind);
|
|
124
|
-
if (!importsFramework(sf, isFramework)) return undefined;
|
|
125
|
-
|
|
126
|
-
const edits = [];
|
|
127
|
-
const reasons = [];
|
|
128
|
-
const visit = (node) => {
|
|
129
|
-
if (ts.isObjectLiteralExpression(node) && ts.isCallExpression(node.parent) && node.parent.arguments.includes(node)) {
|
|
130
|
-
node.properties.forEach((property, index) => {
|
|
131
|
-
if (!(ts.isPropertyAssignment(property) || ts.isShorthandPropertyAssignment(property)) || nameOf(property) !== "writeback") return;
|
|
132
|
-
edits.push(removalSpan(sf, text, node, index));
|
|
133
|
-
const optedOut = ts.isPropertyAssignment(property) && property.initializer.kind === ts.SyntaxKind.FalseKeyword;
|
|
134
|
-
const stored = readsStoredValue(node);
|
|
135
|
-
if (optedOut && stored.reads && !NEVER_NAMED.test(node.parent.expression.getText(sf))) {
|
|
136
|
-
const read = stored.value.getText(sf);
|
|
137
|
-
reasons.push(
|
|
138
|
-
`Manual: line ${String(lineOf(sf, property.getStart(sf)))}: \`writeback: false\` on a slot sourced by \`${read}\` — it now writes \`${read}\`; ` +
|
|
139
|
-
`if it should only be seeded from it, source it by \`compute({ reads: [${read}] }, (value) => value)\``,
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
ts.forEachChild(node, visit);
|
|
145
|
-
};
|
|
146
|
-
visit(sf);
|
|
147
|
-
if (edits.length === 0) return undefined;
|
|
148
|
-
let out = text;
|
|
149
|
-
for (const edit of edits.sort((a, b) => b.start - a.start)) out = out.slice(0, edit.start) + out.slice(edit.end);
|
|
150
|
-
return { text: out, changed: true, reasons };
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/** @param {string} root @returns {Promise<{rewritten: string[], notRewritten: {file: string, reason: string}[]}>} */
|
|
154
|
-
export default async function writebackDrop(root) {
|
|
155
|
-
const rewritten = [];
|
|
156
|
-
const notRewritten = [];
|
|
157
|
-
for (const file of await sourceFiles(root)) {
|
|
158
|
-
const before = await readFile(file, "utf8");
|
|
159
|
-
if (!before.includes("writeback")) continue;
|
|
160
|
-
const result = rewriteSource(file, before);
|
|
161
|
-
if (result === undefined) continue;
|
|
162
|
-
const rel = relative(root, file).split(sep).join("/");
|
|
163
|
-
await writeFile(file, result.text, "utf8");
|
|
164
|
-
rewritten.push(rel);
|
|
165
|
-
for (const reason of result.reasons) notRewritten.push({ file: rel, reason });
|
|
166
|
-
}
|
|
167
|
-
return { rewritten, notRewritten };
|
|
168
|
-
}
|