@homepages/concrete 9.1.0 → 11.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 +81 -0
- package/README.md +1 -1
- package/codemods/manifest.json +5 -148
- package/dist/artifact/playground/client/assets/index-ByeGGedv.js +1259 -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 +42 -42
- package/dist/base.css +1 -8
- package/dist/cli/build/artifact.js +3 -2
- package/dist/cli/build/fixtures.js +2 -0
- package/dist/cli/check/render/inputs.js +3 -3
- package/dist/cli/check/stages/sections.js +6 -7
- package/dist/cli/dev/workspace-reader.js +10 -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/media-store.js +3 -3
- package/dist/fixtures/resolve.js +28 -16
- package/dist/fixtures/rows.js +2 -2
- package/dist/fixtures/synthesize-empties.js +3 -1
- package/dist/fixtures.d.ts +1 -1
- package/dist/index.d.ts +7 -8
- package/dist/packages/external/concrete/package.js +1 -1
- package/dist/packages/internal/fill-bake/dist/bake-property-ref.js +8 -6
- package/dist/packages/internal/fill-bake/dist/compute-reads.js +8 -4
- package/dist/packages/internal/fill-bake/dist/index.js +1 -1
- package/dist/packages/internal/fill-bake/dist/item-merge.js +32 -6
- package/dist/packages/internal/fill-bake/dist/linked-merge.js +5 -5
- package/dist/packages/internal/fill-bake/dist/list-grammar.js +4 -26
- package/dist/packages/internal/fill-vocabulary/dist/holder.js +3 -14
- package/dist/packages/internal/fill-vocabulary/dist/resolved-decision.js +1 -1
- package/dist/packages/internal/media-resolution/dist/document.js +17 -16
- package/dist/packages/internal/media-resolution/dist/image.js +41 -95
- package/dist/packages/internal/media-resolution/dist/index.js +1 -1
- package/dist/packages/internal/media-resolution/dist/list.js +11 -11
- package/dist/packages/internal/media-resolution/dist/media.js +17 -16
- package/dist/packages/internal/media-resolution/dist/records.js +4 -2
- package/dist/packages/internal/media-resolution/dist/slot.js +5 -5
- package/dist/packages/internal/media-resolution/dist/tour.js +17 -16
- package/dist/packages/internal/media-resolution/dist/video.js +24 -24
- package/dist/packages/internal/property-facts/dist/registry-facts.js +8 -1
- package/dist/packages/internal/section-contracts/dist/catalog.js +1 -5
- package/dist/packages/internal/section-contracts/dist/directly-linked-list-contract.js +2 -3
- package/dist/packages/internal/section-contracts/dist/fill-verb-contract.js +6 -3
- package/dist/packages/internal/section-contracts/dist/index.js +0 -1
- package/dist/packages/internal/section-contracts/dist/object-contract.js +4 -2
- package/dist/packages/internal/section-contracts/dist/row-fill-contract.js +1 -1
- package/dist/packages/internal/section-contracts/dist/row-rule-contract.js +4 -2
- package/dist/packages/internal/section-contracts/dist/row-source-contract.js +7 -8
- package/dist/packages/internal/section-contracts/dist/runners.js +2 -11
- package/dist/packages/internal/section-contracts/dist/select-contract.js +7 -1
- package/dist/packages/internal/section-contracts/dist/source-contract.js +14 -10
- package/dist/packages/internal/section-contracts/dist/sources-contract.js +0 -2
- package/dist/packages/internal/section-contracts/dist/table-member.js +5 -8
- package/dist/packages/internal/source-resolution/dist/index.js +21 -188
- package/dist/packages/internal/template-artifact/dist/index.js +1 -1
- package/dist/packages/internal/template-artifact/dist/layout.js +12 -10
- package/dist/packages/internal/template-artifact/dist/manifest.js +1 -1
- package/dist/packages/internal/template-artifact/dist/verify.js +26 -14
- package/dist/packages/internal/template-contracts/dist/content-pick.js +24 -1
- package/dist/packages/internal/template-contracts/dist/document-value.d.ts +4 -6
- package/dist/packages/internal/template-contracts/dist/fact-keys.d.ts +3 -2
- package/dist/packages/internal/template-contracts/dist/fact-keys.js +19 -43
- package/dist/packages/internal/template-contracts/dist/fact-registry.d.ts +24 -3
- package/dist/packages/internal/template-contracts/dist/fact-registry.js +10 -3
- package/dist/packages/internal/template-contracts/dist/fill-builders.d.ts +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/fill-spec.js +7 -6
- package/dist/packages/internal/template-contracts/dist/list.d.ts +1 -1
- package/dist/packages/internal/template-contracts/dist/list.js +7 -29
- package/dist/packages/internal/template-contracts/dist/mirror-kind.js +2 -2
- package/dist/packages/internal/template-contracts/dist/object.js +3 -2
- package/dist/packages/internal/template-contracts/dist/primitive-descriptors.js +1 -1
- package/dist/packages/internal/template-contracts/dist/process-builders.d.ts +2 -2
- package/dist/packages/internal/template-contracts/dist/read-graph.js +5 -32
- package/dist/packages/internal/template-contracts/dist/ref-locate.js +10 -6
- package/dist/packages/internal/template-contracts/dist/registry-facts.d.ts +1 -1
- package/dist/packages/internal/template-contracts/dist/registry-facts.js +13 -24
- package/dist/packages/internal/template-contracts/dist/registry-shape.d.ts +20 -4
- package/dist/packages/internal/template-contracts/dist/registry-shape.js +69 -11
- package/dist/packages/internal/template-contracts/dist/row-planners.js +1 -1
- package/dist/packages/internal/template-contracts/dist/row-ref.d.ts +4 -7
- package/dist/packages/internal/template-contracts/dist/row-ref.js +7 -23
- package/dist/packages/internal/template-contracts/dist/row-sources.js +3 -3
- package/dist/packages/internal/template-contracts/dist/row.d.ts +1 -1
- package/dist/packages/internal/template-contracts/dist/row.js +6 -7
- package/dist/packages/internal/template-contracts/dist/runtime-values.d.ts +11 -27
- package/dist/packages/internal/template-contracts/dist/section-schema-types.d.ts +1 -0
- package/dist/packages/internal/template-contracts/dist/slot-builders.d.ts +29 -37
- package/dist/packages/internal/template-contracts/dist/slot-builders.js +3 -8
- package/dist/packages/internal/template-contracts/dist/slot-catalog.d.ts +2 -702
- package/dist/packages/internal/template-contracts/dist/slot-catalog.js +2 -498
- package/dist/packages/internal/template-contracts/dist/slot-overrides.js +4 -1
- package/dist/packages/internal/template-contracts/dist/slot-ref.d.ts +10 -5
- package/dist/packages/internal/template-contracts/dist/slot-value.d.ts +2 -1
- package/dist/packages/internal/template-contracts/dist/table-members.d.ts +5 -12
- package/dist/packages/internal/template-contracts/dist/table-members.js +7 -37
- package/dist/packages/internal/template-contracts/dist/tour-value.d.ts +4 -6
- package/dist/packages/internal/template-contracts/dist/video-value.d.ts +3 -4
- package/dist/primitives/Image.d.ts +4 -4
- 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 +4 -2
- package/dist/primitives/slot-media.d.ts +6 -3
- package/dist/primitives/slot-media.js +1 -1
- package/dist/rules/registry.js +0 -6
- package/dist/schema/fact.d.ts +19 -52
- package/dist/schema/fact.js +6 -25
- package/dist/schema/facts-view.d.ts +1 -2
- package/dist/schema/format-presets.d.ts +1 -1
- package/dist/schema/property-facts.d.ts +1 -62
- package/dist/schema/section-schema.d.ts +6 -6
- package/dist/schema/section-schema.js +1 -1
- package/dist/styles.css +4 -4
- package/dist/template/define-template.js +0 -1
- 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 -4
- 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/slot-object.mjs +0 -1097
- package/codemods/steps/content-object-fields.mjs +0 -324
- package/codemods/steps/list-object-form.mjs +0 -687
- 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-BOYbgBnP.js +0 -1259
- package/dist/artifact/playground/client/assets/index-DDOcM0zA.css +0 -1
- package/dist/packages/internal/section-contracts/dist/option-source-contract.js +0 -22
- package/dist/packages/internal/template-contracts/dist/comparable-address.js +0 -19
- package/dist/packages/internal/template-contracts/dist/facts-view.d.ts +0 -62
- package/dist/packages/internal/template-contracts/dist/legacy-facts.js +0 -167
- package/dist/packages/internal/template-contracts/dist/registry-aliases.js +0 -77
|
@@ -1,405 +0,0 @@
|
|
|
1
|
-
// A media slot names the list it picks from in `from`; `category` is gone.
|
|
2
|
-
//
|
|
3
|
-
// The property's uploads are sorted into two lists — `fact.property.media`, what shows the
|
|
4
|
-
// property (its photos, videos and tours), and `fact.property.floor_plans`, its plans — and
|
|
5
|
-
// a media slot picks `from` one of them, or from `fact.property.uploads`. A media slot with
|
|
6
|
-
// no `from` now OWNS its asset: the user uploads one into it, and nothing picks it.
|
|
7
|
-
//
|
|
8
|
-
// What this codemod does, in every `.ts` and `.tsx` under the workspace that imports this
|
|
9
|
-
// package:
|
|
10
|
-
//
|
|
11
|
-
// - in the options object of an `image`, `video`, `tour` or `document` call,
|
|
12
|
-
// `category: "property"` → `from: fact.property.media`
|
|
13
|
-
// `category: "floor_plan"` → `from: fact.property.floor_plans`
|
|
14
|
-
// and a slot stating neither a `category` nor a `from` — picked from the uploads until
|
|
15
|
-
// now — gains `from: fact.property.uploads`, unless it is bound to a fact (its `source`
|
|
16
|
-
// a fact, a row's member, an element or another slot) or is the item of a list a
|
|
17
|
-
// source other than a fill supplies, which holds what that source hands it;
|
|
18
|
-
// - a `compute()` reading `fact.property.uploads` only to filter it by
|
|
19
|
-
// `asset.category === "photo"` or `=== "floor_plan"` reads the sorted list instead:
|
|
20
|
-
// `(uploads) => uploads.filter((asset) => asset.kind === "image" && asset.category === "photo")`
|
|
21
|
-
// → `(media) => media.filter((asset) => asset.kind === "image")` over `fact.property.media`
|
|
22
|
-
// `(uploads) => uploads.filter((asset) => asset.kind === "image" && asset.category === "floor_plan")`
|
|
23
|
-
// → `(plans) => plans` over `fact.property.floor_plans`
|
|
24
|
-
// - and `fact` joins the import where a rewrite names it.
|
|
25
|
-
//
|
|
26
|
-
// What it hands back, by line: a `category` it cannot map (`"logo"`, `"headshot"`, one
|
|
27
|
-
// not spelled as a string), a media call whose options are not an object literal, and any
|
|
28
|
-
// other read of an asset's `category` — each of which the author settles by hand.
|
|
29
|
-
|
|
30
|
-
import { readdir, readFile, writeFile } from "node:fs/promises";
|
|
31
|
-
import { join, relative, sep } from "node:path";
|
|
32
|
-
|
|
33
|
-
import ts from "typescript";
|
|
34
|
-
|
|
35
|
-
const CONCRETE = "@homepages/concrete";
|
|
36
|
-
const EXTENSIONS = [".ts", ".tsx"];
|
|
37
|
-
const SKIP_DIRS = new Set(["node_modules", ".concrete", ".git", "dist", "build"]);
|
|
38
|
-
|
|
39
|
-
// The media builders a section draws, by the name a call reaches them under (`image`, `c.image`).
|
|
40
|
-
const MEDIA_CALL = /(^|\.)(image|video|tour|document)$/;
|
|
41
|
-
const LIST_CALL = /(^|\.)list$/;
|
|
42
|
-
const COMPUTE_CALL = /(^|\.)compute$/;
|
|
43
|
-
// A fill, in either spelling — `fill.pick(…)` or the older `fill(…)` — supplies no binding.
|
|
44
|
-
const FILL_CALL = /(^|\.)(fill|pick|write|find)$/;
|
|
45
|
-
|
|
46
|
-
// A stated category to the sorted list it names now.
|
|
47
|
-
const LIST_OF_CATEGORY = { property: "media", floor_plan: "floor_plans" };
|
|
48
|
-
// A record's category, as a compute filtered the uploads by it, to the sorted list holding it.
|
|
49
|
-
const LIST_OF_RECORD_CATEGORY = { photo: "media", floor_plan: "floor_plans" };
|
|
50
|
-
// The parameter a rewritten compute names the list by, first free name first.
|
|
51
|
-
const PARAMETER_NAMES = { media: ["media", "propertyMedia"], floor_plans: ["plans", "floorPlans"] };
|
|
52
|
-
// The record categories a compute may still compare against, which the author settles.
|
|
53
|
-
const RECORD_CATEGORIES = new Set(["photo", "floor_plan", "logo"]);
|
|
54
|
-
|
|
55
|
-
async function sourceFiles(root) {
|
|
56
|
-
const found = [];
|
|
57
|
-
async function walk(dir) {
|
|
58
|
-
for (const entry of await readdir(dir, { withFileTypes: true })) {
|
|
59
|
-
if (entry.isDirectory()) {
|
|
60
|
-
if (!SKIP_DIRS.has(entry.name)) await walk(join(dir, entry.name));
|
|
61
|
-
} else if (EXTENSIONS.some((ext) => entry.name.endsWith(ext)) && !entry.name.endsWith(".d.ts")) {
|
|
62
|
-
found.push(join(dir, entry.name));
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
await walk(root);
|
|
67
|
-
return found.sort();
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function lineOf(sf, pos) {
|
|
71
|
-
return sf.getLineAndCharacterOfPosition(pos).line + 1;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function nameOf(property) {
|
|
75
|
-
const name = property.name;
|
|
76
|
-
return name !== undefined && (ts.isIdentifier(name) || ts.isStringLiteral(name)) ? name.text : undefined;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function propertyNamed(literal, key) {
|
|
80
|
-
return literal.properties.find((property) => ts.isPropertyAssignment(property) && nameOf(property) === key);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// The framework's imports: its named-import clause (where `fact` joins), the local name
|
|
84
|
-
// `fact` is bound to, and the receivers a namespace import reaches it through.
|
|
85
|
-
function frameworkImports(sf, isFramework) {
|
|
86
|
-
let named;
|
|
87
|
-
let fact;
|
|
88
|
-
const namespaces = [];
|
|
89
|
-
for (const statement of sf.statements) {
|
|
90
|
-
if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier) || !isFramework(statement.moduleSpecifier.text)) continue;
|
|
91
|
-
const bindings = statement.importClause?.namedBindings;
|
|
92
|
-
if (bindings === undefined) continue;
|
|
93
|
-
// A type-only import names no value a rewrite could spell `fact` through.
|
|
94
|
-
if (statement.getText(sf).startsWith("import type")) continue;
|
|
95
|
-
if (ts.isNamespaceImport(bindings)) {
|
|
96
|
-
namespaces.push(bindings.name.text);
|
|
97
|
-
continue;
|
|
98
|
-
}
|
|
99
|
-
named ??= bindings;
|
|
100
|
-
const element = bindings.elements.find((e) => !e.isTypeOnly && (e.propertyName ?? e.name).text === "fact");
|
|
101
|
-
if (element !== undefined) fact = element.name.text;
|
|
102
|
-
}
|
|
103
|
-
return { named, fact, namespaces };
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// What a slot's `source` is: a fill, a compute — neither of which binds the slot — or a binding.
|
|
107
|
-
function sourceKind(sf, initializer) {
|
|
108
|
-
if (ts.isCallExpression(initializer)) {
|
|
109
|
-
const callee = initializer.expression.getText(sf);
|
|
110
|
-
if (FILL_CALL.test(callee)) return "fill";
|
|
111
|
-
if (COMPUTE_CALL.test(callee)) return "compute";
|
|
112
|
-
}
|
|
113
|
-
return "binding";
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// Whether a media call is the item of a list a source other than a fill supplies: the
|
|
117
|
-
// item holds what that source hands it.
|
|
118
|
-
function suppliedItem(sf, call) {
|
|
119
|
-
const assignment = call.parent;
|
|
120
|
-
if (!ts.isPropertyAssignment(assignment) || nameOf(assignment) !== "item") return false;
|
|
121
|
-
const literal = assignment.parent;
|
|
122
|
-
if (!ts.isObjectLiteralExpression(literal) || !ts.isCallExpression(literal.parent) || !LIST_CALL.test(literal.parent.expression.getText(sf))) return false;
|
|
123
|
-
const source = propertyNamed(literal, "source");
|
|
124
|
-
return source !== undefined && sourceKind(sf, source.initializer) !== "fill";
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// The indentation `node` starts its line with, or `undefined` when it shares its line.
|
|
128
|
-
function ownLineIndent(sf, text, node) {
|
|
129
|
-
const start = node.getStart(sf);
|
|
130
|
-
const leading = text.slice(text.lastIndexOf("\n", start - 1) + 1, start);
|
|
131
|
-
return leading.trim() === "" ? leading : undefined;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// The edit inserting `insert` as an object literal's property just before `anchor` — on a
|
|
135
|
-
// line of its own where the anchor has one.
|
|
136
|
-
function insertBefore(sf, text, anchor, insert) {
|
|
137
|
-
const start = anchor.getStart(sf);
|
|
138
|
-
const indent = ownLineIndent(sf, text, anchor);
|
|
139
|
-
return { start, end: start, insert: indent === undefined ? `${insert}, ` : `${insert},\n${indent}` };
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// The edit inserting `insert` as an object literal's property just after `anchor`, its
|
|
143
|
-
// trailing comma kept where it had one.
|
|
144
|
-
function insertAfter(sf, text, anchor, insert) {
|
|
145
|
-
const end = anchor.getEnd();
|
|
146
|
-
const trailing = text.slice(end).startsWith(",") ? "," : "";
|
|
147
|
-
const indent = ownLineIndent(sf, text, anchor);
|
|
148
|
-
return { start: end, end: end + trailing.length, insert: indent === undefined ? `, ${insert}${trailing}` : `,\n${indent}${insert}${trailing}` };
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// A `(a) => a.kind === "image" && a.category === "photo"` predicate, read: its parameter,
|
|
152
|
-
// its `&&`, and the kind and record category it compares — or `undefined` for any other.
|
|
153
|
-
function categoryPredicate(fn) {
|
|
154
|
-
if (!(ts.isArrowFunction(fn) || ts.isFunctionExpression(fn)) || fn.parameters.length !== 1 || !ts.isIdentifier(fn.parameters[0].name)) return undefined;
|
|
155
|
-
const param = fn.parameters[0].name.text;
|
|
156
|
-
let body = fn.body;
|
|
157
|
-
if (ts.isBlock(body)) {
|
|
158
|
-
const only = body.statements.length === 1 ? body.statements[0] : undefined;
|
|
159
|
-
if (only === undefined || !ts.isReturnStatement(only) || only.expression === undefined) return undefined;
|
|
160
|
-
body = only.expression;
|
|
161
|
-
}
|
|
162
|
-
while (ts.isParenthesizedExpression(body)) body = body.expression;
|
|
163
|
-
if (!ts.isBinaryExpression(body) || body.operatorToken.kind !== ts.SyntaxKind.AmpersandAmpersandToken) return undefined;
|
|
164
|
-
const compared = {};
|
|
165
|
-
for (const side of [body.left, body.right]) {
|
|
166
|
-
if (!ts.isBinaryExpression(side) || side.operatorToken.kind !== ts.SyntaxKind.EqualsEqualsEqualsToken) return undefined;
|
|
167
|
-
const [member, literal] = ts.isStringLiteral(side.right) ? [side.left, side.right] : [side.right, side.left];
|
|
168
|
-
if (!ts.isStringLiteral(literal) || !ts.isPropertyAccessExpression(member)) return undefined;
|
|
169
|
-
if (!ts.isIdentifier(member.expression) || member.expression.text !== param) return undefined;
|
|
170
|
-
compared[member.name.text] = { value: literal.text, side };
|
|
171
|
-
}
|
|
172
|
-
if (compared.kind === undefined || compared.category === undefined) return undefined;
|
|
173
|
-
return { kind: compared.kind.value, category: compared.category.value, kindSide: compared.kind.side, body };
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// Every identifier in `node` that reads the binding `name` — not a member's name, not a
|
|
177
|
-
// property's key.
|
|
178
|
-
function readsOf(node, name) {
|
|
179
|
-
const found = [];
|
|
180
|
-
const visit = (n) => {
|
|
181
|
-
if (ts.isIdentifier(n) && n.text === name) {
|
|
182
|
-
const parent = n.parent;
|
|
183
|
-
const memberName = ts.isPropertyAccessExpression(parent) && parent.name === n;
|
|
184
|
-
const key = (ts.isPropertyAssignment(parent) || ts.isMethodDeclaration(parent)) && parent.name === n;
|
|
185
|
-
if (!memberName && !key) found.push(n);
|
|
186
|
-
}
|
|
187
|
-
ts.forEachChild(n, visit);
|
|
188
|
-
};
|
|
189
|
-
visit(node);
|
|
190
|
-
return found;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
// Whether `name` appears as any identifier inside `node` — a new parameter of that name would shadow it.
|
|
194
|
-
function mentions(node, name) {
|
|
195
|
-
let found = false;
|
|
196
|
-
const visit = (n) => {
|
|
197
|
-
if (found) return;
|
|
198
|
-
if (ts.isIdentifier(n) && n.text === name) found = true;
|
|
199
|
-
else ts.forEachChild(n, visit);
|
|
200
|
-
};
|
|
201
|
-
visit(node);
|
|
202
|
-
return found;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// The edits repointing one `compute()` from the uploads to the sorted list it filters them
|
|
206
|
-
// to, or the reason it cannot be — `undefined` when it reads the uploads by no category.
|
|
207
|
-
function computeEdits(sf, call, isUploads, listRef) {
|
|
208
|
-
const [options, fn] = call.arguments;
|
|
209
|
-
if (options === undefined || fn === undefined || !ts.isObjectLiteralExpression(options)) return undefined;
|
|
210
|
-
if (!(ts.isArrowFunction(fn) || ts.isFunctionExpression(fn))) return undefined;
|
|
211
|
-
const reads = propertyNamed(options, "reads");
|
|
212
|
-
if (reads === undefined || !ts.isArrayLiteralExpression(reads.initializer)) return undefined;
|
|
213
|
-
const edits = [];
|
|
214
|
-
const handled = [];
|
|
215
|
-
const reasons = [];
|
|
216
|
-
reads.initializer.elements.forEach((read, index) => {
|
|
217
|
-
if (!isUploads(read)) return;
|
|
218
|
-
const param = fn.parameters[index];
|
|
219
|
-
if (param === undefined || !ts.isIdentifier(param.name)) return;
|
|
220
|
-
const uses = readsOf(fn.body, param.name.text);
|
|
221
|
-
const filters = uses.map((use) => {
|
|
222
|
-
const member = use.parent;
|
|
223
|
-
const filter = ts.isPropertyAccessExpression(member) && member.expression === use && member.name.text === "filter" ? member.parent : undefined;
|
|
224
|
-
if (filter === undefined || !ts.isCallExpression(filter) || filter.expression !== member || filter.arguments.length !== 1) return undefined;
|
|
225
|
-
const predicate = categoryPredicate(filter.arguments[0]);
|
|
226
|
-
const list = predicate?.kind === "image" ? LIST_OF_RECORD_CATEGORY[predicate.category] : undefined;
|
|
227
|
-
return list === undefined ? undefined : { filter, predicate, list };
|
|
228
|
-
});
|
|
229
|
-
if (filters.every((f) => f === undefined)) return;
|
|
230
|
-
const lists = new Set(filters.map((f) => f?.list));
|
|
231
|
-
if (filters.some((f) => f === undefined) || lists.size !== 1) {
|
|
232
|
-
reasons.push(
|
|
233
|
-
`line ${String(lineOf(sf, call.getStart(sf)))}: a \`compute()\` filters \`fact.property.uploads\` by \`asset.category\` and reads it otherwise too — ` +
|
|
234
|
-
"read `fact.property.media` (the photos, videos and tours) or `fact.property.floor_plans` (the plans) for each subset, and drop `.category`",
|
|
235
|
-
);
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
const list = [...lists][0];
|
|
239
|
-
const name = PARAMETER_NAMES[list].find((candidate) => !mentions(fn, candidate));
|
|
240
|
-
if (name === undefined) {
|
|
241
|
-
reasons.push(`line ${String(lineOf(sf, call.getStart(sf)))}: a \`compute()\` filters \`fact.property.uploads\` by \`asset.category\` — read \`fact.property.${list}\` instead, and drop \`.category\``);
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
244
|
-
edits.push({ start: read.getStart(sf), end: read.getEnd(), insert: listRef(read, list) });
|
|
245
|
-
edits.push({ start: param.name.getStart(sf), end: param.name.getEnd(), insert: name });
|
|
246
|
-
for (const { filter, predicate } of filters) {
|
|
247
|
-
handled.push(filter);
|
|
248
|
-
if (list === "floor_plans") {
|
|
249
|
-
// The plans are images only: the filter keeps every one.
|
|
250
|
-
edits.push({ start: filter.getStart(sf), end: filter.getEnd(), insert: name });
|
|
251
|
-
} else {
|
|
252
|
-
const use = filter.expression.expression;
|
|
253
|
-
edits.push({ start: use.getStart(sf), end: use.getEnd(), insert: name });
|
|
254
|
-
edits.push({ start: predicate.body.getStart(sf), end: predicate.body.getEnd(), insert: predicate.kindSide.getText(sf) });
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
});
|
|
258
|
-
return { edits, handled, reasons };
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* Rewrites one file's text; `undefined` when it names nothing this codemod reads.
|
|
263
|
-
* `isFramework` decides which import specifiers are the framework.
|
|
264
|
-
*/
|
|
265
|
-
export function rewriteSource(fileName, text, isFramework = (specifier) => specifier === CONCRETE) {
|
|
266
|
-
if (!text.includes("image") && !text.includes("video") && !text.includes("tour") && !text.includes("document") && !text.includes("category")) return undefined;
|
|
267
|
-
const kind = fileName.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
268
|
-
const sf = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true, kind);
|
|
269
|
-
const imports = frameworkImports(sf, isFramework);
|
|
270
|
-
if (imports.named === undefined && imports.namespaces.length === 0) return undefined;
|
|
271
|
-
|
|
272
|
-
// How a rewrite spells a fact ref: through `fact` as imported, else a namespace, else
|
|
273
|
-
// a `fact` the import gains.
|
|
274
|
-
let needsFact = false;
|
|
275
|
-
const factName = () => {
|
|
276
|
-
if (imports.fact !== undefined) return imports.fact;
|
|
277
|
-
if (imports.namespaces.length > 0) return `${imports.namespaces[0]}.fact`;
|
|
278
|
-
needsFact = true;
|
|
279
|
-
return "fact";
|
|
280
|
-
};
|
|
281
|
-
const factPrefixes = [imports.fact, ...imports.namespaces.map((ns) => `${ns}.fact`)].filter((p) => p !== undefined);
|
|
282
|
-
const isUploads = (node) => factPrefixes.some((prefix) => node.getText(sf).replace(/\s+/g, "") === `${prefix}.property.uploads`);
|
|
283
|
-
// A rewritten read keeps the receiver it was spelled through.
|
|
284
|
-
const listRef = (read, list) => `${read.getText(sf).replace(/\s+/g, "").slice(0, -"uploads".length)}${list}`;
|
|
285
|
-
|
|
286
|
-
const edits = [];
|
|
287
|
-
const reasons = [];
|
|
288
|
-
const handledFilters = [];
|
|
289
|
-
const visit = (node) => {
|
|
290
|
-
if (ts.isCallExpression(node)) {
|
|
291
|
-
const callee = node.expression.getText(sf);
|
|
292
|
-
if (MEDIA_CALL.test(callee)) mediaCall(node, callee);
|
|
293
|
-
else if (COMPUTE_CALL.test(callee) && factPrefixes.length > 0) {
|
|
294
|
-
const result = computeEdits(sf, node, isUploads, listRef);
|
|
295
|
-
if (result !== undefined) {
|
|
296
|
-
edits.push(...result.edits);
|
|
297
|
-
handledFilters.push(...result.handled);
|
|
298
|
-
reasons.push(...result.reasons);
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
// Any other comparison of an asset's category is the author's.
|
|
303
|
-
if (
|
|
304
|
-
ts.isBinaryExpression(node) &&
|
|
305
|
-
node.operatorToken.kind === ts.SyntaxKind.EqualsEqualsEqualsToken &&
|
|
306
|
-
[node.left, node.right].some((side) => ts.isPropertyAccessExpression(side) && side.name.text === "category") &&
|
|
307
|
-
[node.left, node.right].some((side) => ts.isStringLiteral(side) && RECORD_CATEGORIES.has(side.text)) &&
|
|
308
|
-
!handledFilters.some((f) => f.pos <= node.pos && node.end <= f.end)
|
|
309
|
-
) {
|
|
310
|
-
reasons.push(
|
|
311
|
-
`line ${String(lineOf(sf, node.getStart(sf)))}: \`${node.getText(sf)}\` — an asset's category is gone; read \`fact.property.media\` or \`fact.property.floor_plans\` for a subset of the uploads`,
|
|
312
|
-
);
|
|
313
|
-
}
|
|
314
|
-
ts.forEachChild(node, visit);
|
|
315
|
-
};
|
|
316
|
-
|
|
317
|
-
function mediaCall(call, callee) {
|
|
318
|
-
const media = MEDIA_CALL.exec(callee)[2];
|
|
319
|
-
const at = `line ${String(lineOf(sf, call.getStart(sf)))}`;
|
|
320
|
-
const options = call.arguments[0];
|
|
321
|
-
if (options === undefined) {
|
|
322
|
-
if (!suppliedItem(sf, call)) {
|
|
323
|
-
const open = call.getEnd() - 1;
|
|
324
|
-
edits.push({ start: open, end: open, insert: `{ from: ${factName()}.property.uploads }` });
|
|
325
|
-
}
|
|
326
|
-
return;
|
|
327
|
-
}
|
|
328
|
-
if (!ts.isObjectLiteralExpression(options)) {
|
|
329
|
-
reasons.push(`${at}: ${media}(…) takes options that are not an object literal — name the list it picks from in \`from\`, or none where it owns its asset`);
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
if (propertyNamed(options, "from") !== undefined) return;
|
|
333
|
-
const category = propertyNamed(options, "category");
|
|
334
|
-
if (category !== undefined) {
|
|
335
|
-
const stated = ts.isStringLiteral(category.initializer) ? category.initializer.text : undefined;
|
|
336
|
-
const list = stated === undefined ? undefined : LIST_OF_CATEGORY[stated];
|
|
337
|
-
if (list !== undefined) {
|
|
338
|
-
edits.push({ start: category.getStart(sf), end: category.getEnd(), insert: `from: ${factName()}.property.${list}` });
|
|
339
|
-
return;
|
|
340
|
-
}
|
|
341
|
-
edits.push({ start: category.getStart(sf), end: category.getEnd(), insert: `from: ${factName()}.property.uploads` });
|
|
342
|
-
reasons.push(
|
|
343
|
-
`${at}: ${media} narrowed by \`${category.getText(sf)}\` picks from every upload of its kind now — narrow it by its brief, or bind it to the contact's ${stated === "headshot" ? "headshot" : "logo"} (\`source: agent.${stated === "headshot" ? "headshot" : "logo_dark"}\`)`,
|
|
344
|
-
);
|
|
345
|
-
return;
|
|
346
|
-
}
|
|
347
|
-
const source = propertyNamed(options, "source");
|
|
348
|
-
if (source !== undefined && sourceKind(sf, source.initializer) === "binding") return;
|
|
349
|
-
if (suppliedItem(sf, call)) return;
|
|
350
|
-
const from = `from: ${factName()}.property.uploads`;
|
|
351
|
-
if (options.properties.length === 0) {
|
|
352
|
-
edits.push({ start: options.getStart(sf), end: options.getEnd(), insert: `{ ${from} }` });
|
|
353
|
-
return;
|
|
354
|
-
}
|
|
355
|
-
edits.push(source === undefined ? insertAfter(sf, text, options.properties[options.properties.length - 1], from) : insertBefore(sf, text, source, from));
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
visit(sf);
|
|
359
|
-
if (edits.length === 0) return reasons.length > 0 ? { text, changed: false, reasons } : undefined;
|
|
360
|
-
|
|
361
|
-
if (needsFact) edits.push(factImport(sf, text, imports.named));
|
|
362
|
-
let out = text;
|
|
363
|
-
for (const edit of edits.sort((a, b) => b.start - a.start || b.end - a.end)) out = out.slice(0, edit.start) + edit.insert + out.slice(edit.end);
|
|
364
|
-
return { text: out, changed: true, reasons };
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
// The edit adding `fact` to the framework's named imports, in name order.
|
|
368
|
-
function factImport(sf, text, bindings) {
|
|
369
|
-
const elements = bindings.elements;
|
|
370
|
-
const multiline = bindings.getText(sf).includes("\n");
|
|
371
|
-
const next = elements.find((e) => e.name.text.localeCompare("fact") > 0);
|
|
372
|
-
if (next !== undefined) {
|
|
373
|
-
const start = next.getStart(sf);
|
|
374
|
-
if (multiline) {
|
|
375
|
-
const lineStart = text.lastIndexOf("\n", start - 1) + 1;
|
|
376
|
-
return { start, end: start, insert: `fact,\n${text.slice(lineStart, start)}` };
|
|
377
|
-
}
|
|
378
|
-
return { start, end: start, insert: "fact, " };
|
|
379
|
-
}
|
|
380
|
-
const last = elements[elements.length - 1];
|
|
381
|
-
if (last === undefined) return { start: bindings.getStart(sf) + 1, end: bindings.getStart(sf) + 1, insert: " fact " };
|
|
382
|
-
if (multiline) {
|
|
383
|
-
const lineStart = text.lastIndexOf("\n", last.getStart(sf) - 1) + 1;
|
|
384
|
-
return { start: last.getEnd(), end: last.getEnd(), insert: `,\n${text.slice(lineStart, last.getStart(sf))}fact` };
|
|
385
|
-
}
|
|
386
|
-
return { start: last.getEnd(), end: last.getEnd(), insert: ", fact" };
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
/** @param {string} root @returns {Promise<{rewritten: string[], notRewritten: {file: string, reason: string}[]}>} */
|
|
390
|
-
export default async function uploadsSortIntoLists(root) {
|
|
391
|
-
const rewritten = [];
|
|
392
|
-
const notRewritten = [];
|
|
393
|
-
for (const file of await sourceFiles(root)) {
|
|
394
|
-
const before = await readFile(file, "utf8");
|
|
395
|
-
const result = rewriteSource(file, before);
|
|
396
|
-
if (result === undefined) continue;
|
|
397
|
-
const rel = relative(root, file).split(sep).join("/");
|
|
398
|
-
if (result.changed) {
|
|
399
|
-
await writeFile(file, result.text, "utf8");
|
|
400
|
-
rewritten.push(rel);
|
|
401
|
-
}
|
|
402
|
-
for (const reason of result.reasons) notRewritten.push({ file: rel, reason });
|
|
403
|
-
}
|
|
404
|
-
return { rewritten, notRewritten };
|
|
405
|
-
}
|