@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.
Files changed (171) hide show
  1. package/CHANGELOG.md +81 -0
  2. package/README.md +1 -1
  3. package/codemods/manifest.json +5 -148
  4. package/dist/artifact/playground/client/assets/index-ByeGGedv.js +1259 -0
  5. package/dist/artifact/playground/client/assets/index-CxWPYOH2.css +1 -0
  6. package/dist/artifact/playground/client/index.html +2 -2
  7. package/dist/artifact/playground/client/island-canvas-host.mjs +1 -1
  8. package/dist/artifact/playground/host.mjs +42 -42
  9. package/dist/base.css +1 -8
  10. package/dist/cli/build/artifact.js +3 -2
  11. package/dist/cli/build/fixtures.js +2 -0
  12. package/dist/cli/check/render/inputs.js +3 -3
  13. package/dist/cli/check/stages/sections.js +6 -7
  14. package/dist/cli/dev/workspace-reader.js +10 -2
  15. package/dist/css/compile.js +3 -6
  16. package/dist/css/rules.js +1 -51
  17. package/dist/design-system/format-variants.js +1 -1
  18. package/dist/fixtures/media-store.js +3 -3
  19. package/dist/fixtures/resolve.js +28 -16
  20. package/dist/fixtures/rows.js +2 -2
  21. package/dist/fixtures/synthesize-empties.js +3 -1
  22. package/dist/fixtures.d.ts +1 -1
  23. package/dist/index.d.ts +7 -8
  24. package/dist/packages/external/concrete/package.js +1 -1
  25. package/dist/packages/internal/fill-bake/dist/bake-property-ref.js +8 -6
  26. package/dist/packages/internal/fill-bake/dist/compute-reads.js +8 -4
  27. package/dist/packages/internal/fill-bake/dist/index.js +1 -1
  28. package/dist/packages/internal/fill-bake/dist/item-merge.js +32 -6
  29. package/dist/packages/internal/fill-bake/dist/linked-merge.js +5 -5
  30. package/dist/packages/internal/fill-bake/dist/list-grammar.js +4 -26
  31. package/dist/packages/internal/fill-vocabulary/dist/holder.js +3 -14
  32. package/dist/packages/internal/fill-vocabulary/dist/resolved-decision.js +1 -1
  33. package/dist/packages/internal/media-resolution/dist/document.js +17 -16
  34. package/dist/packages/internal/media-resolution/dist/image.js +41 -95
  35. package/dist/packages/internal/media-resolution/dist/index.js +1 -1
  36. package/dist/packages/internal/media-resolution/dist/list.js +11 -11
  37. package/dist/packages/internal/media-resolution/dist/media.js +17 -16
  38. package/dist/packages/internal/media-resolution/dist/records.js +4 -2
  39. package/dist/packages/internal/media-resolution/dist/slot.js +5 -5
  40. package/dist/packages/internal/media-resolution/dist/tour.js +17 -16
  41. package/dist/packages/internal/media-resolution/dist/video.js +24 -24
  42. package/dist/packages/internal/property-facts/dist/registry-facts.js +8 -1
  43. package/dist/packages/internal/section-contracts/dist/catalog.js +1 -5
  44. package/dist/packages/internal/section-contracts/dist/directly-linked-list-contract.js +2 -3
  45. package/dist/packages/internal/section-contracts/dist/fill-verb-contract.js +6 -3
  46. package/dist/packages/internal/section-contracts/dist/index.js +0 -1
  47. package/dist/packages/internal/section-contracts/dist/object-contract.js +4 -2
  48. package/dist/packages/internal/section-contracts/dist/row-fill-contract.js +1 -1
  49. package/dist/packages/internal/section-contracts/dist/row-rule-contract.js +4 -2
  50. package/dist/packages/internal/section-contracts/dist/row-source-contract.js +7 -8
  51. package/dist/packages/internal/section-contracts/dist/runners.js +2 -11
  52. package/dist/packages/internal/section-contracts/dist/select-contract.js +7 -1
  53. package/dist/packages/internal/section-contracts/dist/source-contract.js +14 -10
  54. package/dist/packages/internal/section-contracts/dist/sources-contract.js +0 -2
  55. package/dist/packages/internal/section-contracts/dist/table-member.js +5 -8
  56. package/dist/packages/internal/source-resolution/dist/index.js +21 -188
  57. package/dist/packages/internal/template-artifact/dist/index.js +1 -1
  58. package/dist/packages/internal/template-artifact/dist/layout.js +12 -10
  59. package/dist/packages/internal/template-artifact/dist/manifest.js +1 -1
  60. package/dist/packages/internal/template-artifact/dist/verify.js +26 -14
  61. package/dist/packages/internal/template-contracts/dist/content-pick.js +24 -1
  62. package/dist/packages/internal/template-contracts/dist/document-value.d.ts +4 -6
  63. package/dist/packages/internal/template-contracts/dist/fact-keys.d.ts +3 -2
  64. package/dist/packages/internal/template-contracts/dist/fact-keys.js +19 -43
  65. package/dist/packages/internal/template-contracts/dist/fact-registry.d.ts +24 -3
  66. package/dist/packages/internal/template-contracts/dist/fact-registry.js +10 -3
  67. package/dist/packages/internal/template-contracts/dist/fill-builders.d.ts +1 -1
  68. package/dist/packages/internal/template-contracts/dist/fill-builders.js +2 -6
  69. package/dist/packages/internal/template-contracts/dist/fill-spec-schema.js +4 -1
  70. package/dist/packages/internal/template-contracts/dist/fill-spec.js +7 -6
  71. package/dist/packages/internal/template-contracts/dist/list.d.ts +1 -1
  72. package/dist/packages/internal/template-contracts/dist/list.js +7 -29
  73. package/dist/packages/internal/template-contracts/dist/mirror-kind.js +2 -2
  74. package/dist/packages/internal/template-contracts/dist/object.js +3 -2
  75. package/dist/packages/internal/template-contracts/dist/primitive-descriptors.js +1 -1
  76. package/dist/packages/internal/template-contracts/dist/process-builders.d.ts +2 -2
  77. package/dist/packages/internal/template-contracts/dist/read-graph.js +5 -32
  78. package/dist/packages/internal/template-contracts/dist/ref-locate.js +10 -6
  79. package/dist/packages/internal/template-contracts/dist/registry-facts.d.ts +1 -1
  80. package/dist/packages/internal/template-contracts/dist/registry-facts.js +13 -24
  81. package/dist/packages/internal/template-contracts/dist/registry-shape.d.ts +20 -4
  82. package/dist/packages/internal/template-contracts/dist/registry-shape.js +69 -11
  83. package/dist/packages/internal/template-contracts/dist/row-planners.js +1 -1
  84. package/dist/packages/internal/template-contracts/dist/row-ref.d.ts +4 -7
  85. package/dist/packages/internal/template-contracts/dist/row-ref.js +7 -23
  86. package/dist/packages/internal/template-contracts/dist/row-sources.js +3 -3
  87. package/dist/packages/internal/template-contracts/dist/row.d.ts +1 -1
  88. package/dist/packages/internal/template-contracts/dist/row.js +6 -7
  89. package/dist/packages/internal/template-contracts/dist/runtime-values.d.ts +11 -27
  90. package/dist/packages/internal/template-contracts/dist/section-schema-types.d.ts +1 -0
  91. package/dist/packages/internal/template-contracts/dist/slot-builders.d.ts +29 -37
  92. package/dist/packages/internal/template-contracts/dist/slot-builders.js +3 -8
  93. package/dist/packages/internal/template-contracts/dist/slot-catalog.d.ts +2 -702
  94. package/dist/packages/internal/template-contracts/dist/slot-catalog.js +2 -498
  95. package/dist/packages/internal/template-contracts/dist/slot-overrides.js +4 -1
  96. package/dist/packages/internal/template-contracts/dist/slot-ref.d.ts +10 -5
  97. package/dist/packages/internal/template-contracts/dist/slot-value.d.ts +2 -1
  98. package/dist/packages/internal/template-contracts/dist/table-members.d.ts +5 -12
  99. package/dist/packages/internal/template-contracts/dist/table-members.js +7 -37
  100. package/dist/packages/internal/template-contracts/dist/tour-value.d.ts +4 -6
  101. package/dist/packages/internal/template-contracts/dist/video-value.d.ts +3 -4
  102. package/dist/primitives/Image.d.ts +4 -4
  103. package/dist/primitives/Tour.d.ts +2 -2
  104. package/dist/primitives/Video.d.ts +2 -2
  105. package/dist/primitives/slot-bindings.js +6 -13
  106. package/dist/primitives/slot-document.d.ts +1 -1
  107. package/dist/primitives/slot-download.d.ts +4 -2
  108. package/dist/primitives/slot-media.d.ts +6 -3
  109. package/dist/primitives/slot-media.js +1 -1
  110. package/dist/rules/registry.js +0 -6
  111. package/dist/schema/fact.d.ts +19 -52
  112. package/dist/schema/fact.js +6 -25
  113. package/dist/schema/facts-view.d.ts +1 -2
  114. package/dist/schema/format-presets.d.ts +1 -1
  115. package/dist/schema/property-facts.d.ts +1 -62
  116. package/dist/schema/section-schema.d.ts +6 -6
  117. package/dist/schema/section-schema.js +1 -1
  118. package/dist/styles.css +4 -4
  119. package/dist/template/define-template.js +0 -1
  120. package/guide/reference/cli/INDEX.md +2 -2
  121. package/guide/reference/cli/build.md +1 -1
  122. package/guide/reference/cli/check.md +1 -1
  123. package/guide/reference/cli/dev.md +1 -1
  124. package/guide/reference/cli/property-add.md +1 -1
  125. package/guide/reference/cli/property-list.md +1 -1
  126. package/guide/reference/cli/publish.md +1 -1
  127. package/guide/reference/cli/upgrade.md +1 -1
  128. package/guide/reference/rules/INDEX.md +2 -4
  129. package/package.json +2 -2
  130. package/codemods/fill-is-a-source.mjs +0 -533
  131. package/codemods/framework-namespace.mjs +0 -107
  132. package/codemods/list-grammar.mjs +0 -923
  133. package/codemods/object-form-only.mjs +0 -39
  134. package/codemods/platform-namespace.mjs +0 -83
  135. package/codemods/process-builders.mjs +0 -198
  136. package/codemods/reads-replace-reconcile.mjs +0 -285
  137. package/codemods/slot-object.mjs +0 -1097
  138. package/codemods/steps/content-object-fields.mjs +0 -324
  139. package/codemods/steps/list-object-form.mjs +0 -687
  140. package/codemods/steps/select-object-form.mjs +0 -210
  141. package/codemods/table-rows.mjs +0 -568
  142. package/codemods/theme-collapse.mjs +0 -396
  143. package/codemods/unlayer-author-css.mjs +0 -194
  144. package/codemods/unwrap-object-fields.mjs +0 -310
  145. package/codemods/viewer-slot-document.mjs +0 -131
  146. package/codemods/write-property-as-facts-document.mjs +0 -716
  147. package/codemods/write-the-address-line.mjs +0 -255
  148. package/codemods/write-the-registry-members.mjs +0 -653
  149. package/codemods/write-the-select-from.mjs +0 -141
  150. package/codemods/write-the-sole-from.mjs +0 -369
  151. package/codemods/write-the-table-from.mjs +0 -267
  152. package/codemods/write-the-whole-item.mjs +0 -512
  153. package/codemods/writeback-drop.mjs +0 -168
  154. package/codemods/written-contact-address.mjs +0 -162
  155. package/codemods/written-media-category.mjs +0 -227
  156. package/codemods/written-ref-kinds-name-the-layer.mjs +0 -172
  157. package/codemods/written-uploads-replace-pool-reads.mjs +0 -454
  158. package/codemods/written-uploads-sort-into-lists.mjs +0 -405
  159. package/codemods/written-uploads-stated-in-sorted-lists.mjs +0 -295
  160. package/codemods/written-video-sources-drop.mjs +0 -144
  161. package/codemods/written-without-pick-none.mjs +0 -146
  162. package/codemods/written-without-retired-knowledge.mjs +0 -223
  163. package/codemods/written-without-the-media-key.mjs +0 -202
  164. package/codemods/written-without-the-property-type.mjs +0 -249
  165. package/dist/artifact/playground/client/assets/index-BOYbgBnP.js +0 -1259
  166. package/dist/artifact/playground/client/assets/index-DDOcM0zA.css +0 -1
  167. package/dist/packages/internal/section-contracts/dist/option-source-contract.js +0 -22
  168. package/dist/packages/internal/template-contracts/dist/comparable-address.js +0 -19
  169. package/dist/packages/internal/template-contracts/dist/facts-view.d.ts +0 -62
  170. package/dist/packages/internal/template-contracts/dist/legacy-facts.js +0 -167
  171. package/dist/packages/internal/template-contracts/dist/registry-aliases.js +0 -77
@@ -1,324 +0,0 @@
1
- // A fixture states a top-level object by its fields: `content(Hero, { address: { street,
2
- // city } })`, the shape the content doc stores (`slots.address = { street, city }`).
3
- //
4
- // This is a codemod STEP: 9.0.0's `slot-object` codemod runs it after moving slots into
5
- // objects, and it holds for every object a section declares — the objects `slot-object`
6
- // wrote from a `<SlotGroup>`, and the row objects that group a table's reads. A workspace
7
- // can also run it alone: import this file by path from the installed package and call its
8
- // default export with the workspace root. It honors the release codemod contract:
9
- // `(root) => { rewritten, notRewritten }`.
10
- //
11
- // What it rewrites, in every `.ts` and `.tsx` under the workspace: each `content(Section,
12
- // { … })` — the values written inline, or a local or imported `const` it names — moves
13
- // every key that is no slot of the section but the field of exactly one of its objects
14
- // under that object's key, beside whatever the entry already states for the object. A
15
- // second run finds nothing left to move.
16
- //
17
- // What it hands back, by line: values spread from elsewhere that state a moved field,
18
- // values it cannot read (not an object literal, nor a `const` naming one), a key that is a
19
- // field of two objects, and an object the entry already states as something other than an
20
- // object literal.
21
-
22
- import { readdir, readFile, writeFile } from "node:fs/promises";
23
- import { dirname, join, relative, sep } from "node:path";
24
-
25
- import ts from "typescript";
26
-
27
- const CONCRETE = "@homepages/concrete";
28
- const FIXTURES = "@homepages/concrete/fixtures";
29
-
30
- const EXTENSIONS = [".ts", ".tsx"];
31
- const SKIP_DIRS = new Set(["node_modules", ".concrete", ".git", "dist", "build"]);
32
-
33
- async function sourceFiles(root) {
34
- const found = [];
35
- async function walk(dir) {
36
- for (const entry of await readdir(dir, { withFileTypes: true })) {
37
- if (entry.isDirectory()) {
38
- if (!SKIP_DIRS.has(entry.name)) await walk(join(dir, entry.name));
39
- } else if (EXTENSIONS.some((ext) => entry.name.endsWith(ext)) && !entry.name.endsWith(".d.ts")) {
40
- found.push(join(dir, entry.name));
41
- }
42
- }
43
- }
44
- await walk(root);
45
- return found.sort();
46
- }
47
-
48
- /** Non-overlapping text replacements, applied back to front. */
49
- class Edits {
50
- constructor() {
51
- this.list = [];
52
- }
53
- add(start, end, text) {
54
- this.list.push({ start, end, text });
55
- }
56
- apply(source) {
57
- const sorted = [...this.list].sort((a, b) => b.start - a.start || b.end - a.end);
58
- let out = source;
59
- let floor = Infinity;
60
- for (const edit of sorted) {
61
- if (edit.end > floor) throw new Error(`content-object-fields: overlapping rewrites at ${String(edit.start)}`);
62
- out = out.slice(0, edit.start) + edit.text + out.slice(edit.end);
63
- floor = edit.start;
64
- }
65
- return out;
66
- }
67
- }
68
-
69
- function lineIndent(text, pos) {
70
- const start = text.lastIndexOf("\n", pos - 1) + 1;
71
- return /^[ \t]*/.exec(text.slice(start, pos))[0];
72
- }
73
-
74
- /** Shift every line after the first of `fragment` right by `by`. */
75
- function indentTail(fragment, by) {
76
- return fragment
77
- .split("\n")
78
- .map((line, index) => (index === 0 || line === "" ? line : `${by}${line}`))
79
- .join("\n");
80
- }
81
-
82
- function lineOf(sf, node) {
83
- return sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1;
84
- }
85
-
86
- function propertyKey(property) {
87
- if (ts.isShorthandPropertyAssignment(property)) return property.name.text;
88
- if (!ts.isPropertyAssignment(property)) return undefined;
89
- const { name } = property;
90
- return ts.isIdentifier(name) || ts.isStringLiteral(name) ? name.text : undefined;
91
- }
92
-
93
- /** The removal range of one property of an object literal: itself, its leading trivia and its comma. */
94
- function propertyRemoval(text, property, sf) {
95
- let end = property.getEnd();
96
- let at = end;
97
- while (at < text.length && /\s/.test(text[at])) at += 1;
98
- if (text[at] === ",") return { start: property.getFullStart(), end: at + 1 };
99
- let before = property.getFullStart() - 1;
100
- while (before >= 0 && /\s/.test(text[before])) before -= 1;
101
- return text[before] === "," ? { start: before, end } : { start: property.getStart(sf), end };
102
- }
103
-
104
- function unwrap(expr) {
105
- let at = expr;
106
- while (at !== undefined && (ts.isAsExpression(at) || ts.isSatisfiesExpression(at) || ts.isParenthesizedExpression(at))) at = at.expression;
107
- return at;
108
- }
109
-
110
- /** A relative import's file in the workspace, or `undefined`. */
111
- function resolveImport(from, specifier, known) {
112
- if (!specifier.startsWith(".")) return undefined;
113
- const base = join(dirname(from), specifier);
114
- const stem = base.replace(/\.(m?js|jsx)$/, "");
115
- for (const candidate of [base, `${stem}.ts`, `${stem}.tsx`, join(base, "index.ts"), join(base, "index.tsx")]) {
116
- if (known.has(candidate)) return candidate;
117
- }
118
- return undefined;
119
- }
120
-
121
- function moduleOf(file, text, known) {
122
- const sf = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, file.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS);
123
- const mod = { file, text, sf, framework: new Map(), localImports: new Map(), sections: new Map(), consts: new Map(), edits: new Edits(), refused: [] };
124
- for (const statement of sf.statements) {
125
- if (ts.isImportDeclaration(statement) && ts.isStringLiteral(statement.moduleSpecifier) && statement.importClause !== undefined) {
126
- const specifier = statement.moduleSpecifier.text;
127
- const clause = statement.importClause;
128
- if (specifier === CONCRETE || specifier === FIXTURES) {
129
- if (clause.namedBindings !== undefined && ts.isNamedImports(clause.namedBindings)) {
130
- for (const element of clause.namedBindings.elements) mod.framework.set(`${specifier}:${(element.propertyName ?? element.name).text}`, element.name.text);
131
- }
132
- continue;
133
- }
134
- const target = resolveImport(file, specifier, known);
135
- if (target === undefined) continue;
136
- if (clause.name !== undefined) mod.localImports.set(clause.name.text, { file: target, name: "default" });
137
- if (clause.namedBindings !== undefined && ts.isNamedImports(clause.namedBindings)) {
138
- for (const element of clause.namedBindings.elements) mod.localImports.set(element.name.text, { file: target, name: (element.propertyName ?? element.name).text });
139
- }
140
- }
141
- if (ts.isVariableStatement(statement)) {
142
- for (const declaration of statement.declarationList.declarations) {
143
- if (ts.isIdentifier(declaration.name) && declaration.initializer !== undefined) mod.consts.set(declaration.name.text, declaration.initializer);
144
- }
145
- }
146
- }
147
- return mod;
148
- }
149
-
150
- /** Each `const X = section(…)`: its top-level slot keys and, per object, its field keys. */
151
- function indexSections(mod) {
152
- const sectionName = mod.framework.get(`${CONCRETE}:section`);
153
- const objectName = mod.framework.get(`${CONCRETE}:object`);
154
- if (sectionName === undefined) return;
155
- const visit = (node) => {
156
- if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.initializer !== undefined && ts.isCallExpression(node.initializer)) {
157
- const call = node.initializer;
158
- if (ts.isIdentifier(call.expression) && call.expression.text === sectionName) {
159
- const options = call.arguments[0];
160
- const slotsProperty = options !== undefined && ts.isObjectLiteralExpression(options) ? options.properties.find((p) => propertyKey(p) === "slots") : undefined;
161
- const slots = slotsProperty !== undefined && ts.isPropertyAssignment(slotsProperty) ? unwrap(slotsProperty.initializer) : undefined;
162
- if (slots !== undefined && ts.isObjectLiteralExpression(slots)) {
163
- const top = new Set();
164
- const objects = new Map();
165
- for (const property of slots.properties) {
166
- const key = propertyKey(property);
167
- if (key === undefined) continue;
168
- top.add(key);
169
- const init = ts.isPropertyAssignment(property) ? unwrap(property.initializer) : undefined;
170
- if (objectName === undefined || init === undefined || !ts.isCallExpression(init) || !ts.isIdentifier(init.expression) || init.expression.text !== objectName) continue;
171
- const spec = init.arguments[0];
172
- const fields = spec !== undefined && ts.isObjectLiteralExpression(spec) ? spec.properties.find((p) => propertyKey(p) === "fields") : undefined;
173
- const fieldsLiteral = fields !== undefined && ts.isPropertyAssignment(fields) ? unwrap(fields.initializer) : undefined;
174
- if (fieldsLiteral !== undefined && ts.isObjectLiteralExpression(fieldsLiteral)) {
175
- objects.set(key, new Set(fieldsLiteral.properties.map(propertyKey).filter((k) => k !== undefined)));
176
- }
177
- }
178
- mod.sections.set(node.name.text, { top, objects });
179
- }
180
- }
181
- }
182
- ts.forEachChild(node, visit);
183
- };
184
- visit(mod.sf);
185
- }
186
-
187
- /** @param {string} root @returns {Promise<{rewritten: string[], notRewritten: {file: string, reason: string}[]}>} */
188
- export default async function contentObjectFields(root) {
189
- const files = await sourceFiles(root);
190
- const known = new Set(files);
191
- const name = (file) => relative(root, file).split(sep).join("/");
192
- const modules = new Map();
193
- for (const file of files) modules.set(file, moduleOf(file, await readFile(file, "utf8"), known));
194
- for (const mod of modules.values()) indexSections(mod);
195
-
196
- /** What `ident` names in `mod`, followed through relative imports: `{ mod, local }`. */
197
- const definition = (mod, ident) => {
198
- if (mod.consts.has(ident) || mod.sections.has(ident)) return { mod, local: ident };
199
- const imported = mod.localImports.get(ident);
200
- const target = imported === undefined ? undefined : modules.get(imported.file);
201
- if (target === undefined || imported.name === "default") return undefined;
202
- return target.consts.has(imported.name) || target.sections.has(imported.name) ? { mod: target, local: imported.name } : undefined;
203
- };
204
- const constLiteral = (mod, ident) => {
205
- const found = definition(mod, ident);
206
- const init = found === undefined ? undefined : unwrap(found.mod.consts.get(found.local));
207
- return init !== undefined && ts.isObjectLiteralExpression(init) ? { mod: found.mod, literal: init, local: found.local } : undefined;
208
- };
209
-
210
- const done = new Set();
211
- for (const mod of modules.values()) {
212
- const contentName = mod.framework.get(`${FIXTURES}:content`);
213
- if (contentName === undefined) continue;
214
- const visit = (node) => {
215
- if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === contentName && node.arguments.length >= 2) {
216
- const [sectionArg, valuesArg] = node.arguments;
217
- const found = ts.isIdentifier(sectionArg) ? definition(mod, sectionArg.text) : undefined;
218
- const section = found?.mod.sections.get(found.local);
219
- if (section !== undefined && section.objects.size > 0) {
220
- const values = unwrap(valuesArg);
221
- const line = lineOf(mod.sf, node);
222
- let target;
223
- if (ts.isObjectLiteralExpression(values)) target = { mod, literal: values };
224
- else if (ts.isIdentifier(values)) target = constLiteral(mod, values.text);
225
- if (target === undefined) {
226
- mod.refused.push(`the \`content(${sectionArg.text}, …)\` at line ${String(line)} states values this codemod cannot read — state each field of ${[...section.objects.keys()].map((o) => `"${o}"`).join(", ")} under its object by hand`);
227
- } else if (!done.has(target.literal)) {
228
- done.add(target.literal);
229
- moveFields(target.mod, target.literal, section, sectionArg.text, mod, line);
230
- }
231
- }
232
- }
233
- ts.forEachChild(node, visit);
234
- };
235
- visit(mod.sf);
236
- }
237
-
238
- /** Move each key of `literal` that is a field of one object of `section` under that object. */
239
- function moveFields(mod, literal, section, sectionText, caller, callLine) {
240
- const { sf, text } = mod;
241
- const ownersOf = (key) => [...section.objects].filter(([, fields]) => fields.has(key)).map(([object]) => object);
242
- const moves = new Map();
243
- for (const property of literal.properties) {
244
- if (ts.isSpreadAssignment(property)) {
245
- const spread = ts.isIdentifier(unwrap(property.expression)) ? constLiteral(mod, unwrap(property.expression).text) : undefined;
246
- const at = `the values spread into \`content(${sectionText}, …)\` at ${caller === mod ? "" : `${name(caller.file)} `}line ${String(lineOf(sf, property))}`;
247
- if (spread === undefined) {
248
- mod.refused.push(`${at} are not a \`const\` this codemod can read — state each field of ${[...section.objects.keys()].map((o) => `"${o}"`).join(", ")} under its object by hand`);
249
- continue;
250
- }
251
- for (const inner of spread.literal.properties) {
252
- const key = propertyKey(inner);
253
- if (key !== undefined && !section.top.has(key) && ownersOf(key).length > 0) {
254
- mod.refused.push(`${at} state "${key}", now a field of the object "${ownersOf(key)[0]}" — state it under that object by hand`);
255
- }
256
- }
257
- continue;
258
- }
259
- const key = propertyKey(property);
260
- if (key === undefined || section.top.has(key)) continue;
261
- const owners = ownersOf(key);
262
- if (owners.length > 1) {
263
- mod.refused.push(`"${key}" in \`content(${sectionText}, …)\` at line ${String(lineOf(sf, property))} is a field of ${owners.map((o) => `"${o}"`).join(" and ")} — state it under one by hand`);
264
- continue;
265
- }
266
- if (owners.length === 1) moves.set(owners[0], [...(moves.get(owners[0]) ?? []), property]);
267
- }
268
- for (const [object, properties] of moves) {
269
- const existing = literal.properties.find((p) => propertyKey(p) === object);
270
- if (existing !== undefined) {
271
- const inner = ts.isPropertyAssignment(existing) ? unwrap(existing.initializer) : undefined;
272
- if (inner === undefined || !ts.isObjectLiteralExpression(inner)) {
273
- mod.refused.push(`\`content(${sectionText}, …)\` at line ${String(callLine)} states "${object}" as something other than an object literal — move ${properties.map((p) => `"${propertyKey(p)}"`).join(", ")} into it by hand`);
274
- continue;
275
- }
276
- // Appended after what the entry already states for the object, in its layout.
277
- const last = inner.properties[inner.properties.length - 1];
278
- const multiline = text.slice(inner.getStart(sf), inner.getEnd()).includes("\n");
279
- const indent = last === undefined ? `${lineIndent(text, existing.getStart(sf))} ` : lineIndent(text, last.getStart(sf));
280
- const moved = properties.map((p) => p.getText(sf));
281
- const lines = moved.map((entry) => `\n${indent}${entry},`).join("");
282
- if (last === undefined) {
283
- mod.edits.add(inner.getStart(sf) + 1, inner.getStart(sf) + 1, multiline ? lines : ` ${moved.join(", ")} `);
284
- } else {
285
- let probe = last.getEnd();
286
- while (probe < text.length && /[ \t]/.test(text[probe])) probe += 1;
287
- const comma = text[probe] === ",";
288
- const at = comma ? probe + 1 : last.getEnd();
289
- mod.edits.add(at, at, `${comma ? "" : ","}${multiline ? lines : ` ${moved.join(", ")}${comma ? "," : ""}`}`);
290
- }
291
- for (const property of properties) {
292
- const range = propertyRemoval(text, property, sf);
293
- mod.edits.add(range.start, range.end, "");
294
- }
295
- continue;
296
- }
297
- const [first, ...rest] = properties;
298
- const multiline = text.slice(literal.getStart(sf), literal.getEnd()).includes("\n");
299
- const indent = lineIndent(text, first.getStart(sf));
300
- const block = multiline
301
- ? [`${object}: {`, ...properties.map((p) => `${indent} ${indentTail(p.getText(sf), " ")},`), `${indent}}`].join("\n")
302
- : `${object}: { ${properties.map((p) => p.getText(sf)).join(", ")} }`;
303
- mod.edits.add(first.getStart(sf), first.getEnd(), block);
304
- for (const property of rest) {
305
- const range = propertyRemoval(text, property, sf);
306
- mod.edits.add(range.start, range.end, "");
307
- }
308
- }
309
- }
310
-
311
- const rewritten = [];
312
- const notRewritten = [];
313
- for (const mod of modules.values()) {
314
- if (mod.edits.list.length > 0) {
315
- const out = mod.edits.apply(mod.text);
316
- if (out !== mod.text) {
317
- await writeFile(mod.file, out, "utf8");
318
- rewritten.push(name(mod.file));
319
- }
320
- }
321
- for (const reason of new Set(mod.refused)) notRewritten.push({ file: name(mod.file), reason });
322
- }
323
- return { rewritten, notRewritten };
324
- }