@wastech-mdlint/viewer 0.0.0-stage → 0.4.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 (189) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +86 -2
  3. package/assets/favicon.ico +0 -0
  4. package/assets/viewer.css +4184 -0
  5. package/dist/asset-routes.d.ts +4 -0
  6. package/dist/asset-routes.js +107 -0
  7. package/dist/blame.d.ts +8 -0
  8. package/dist/blame.js +54 -0
  9. package/dist/branch-changes.d.ts +8 -0
  10. package/dist/branch-changes.js +49 -0
  11. package/dist/candidate-lint-worker.d.ts +1 -0
  12. package/dist/candidate-lint-worker.js +67 -0
  13. package/dist/candidate-lint.d.ts +39 -0
  14. package/dist/candidate-lint.js +115 -0
  15. package/dist/change-banner.d.ts +38 -0
  16. package/dist/change-banner.js +108 -0
  17. package/dist/change-diff.d.ts +23 -0
  18. package/dist/change-diff.js +140 -0
  19. package/dist/change-marks.d.ts +32 -0
  20. package/dist/change-marks.js +363 -0
  21. package/dist/changes-article.d.ts +15 -0
  22. package/dist/changes-article.js +244 -0
  23. package/dist/changes-routes.d.ts +12 -0
  24. package/dist/changes-routes.js +271 -0
  25. package/dist/changes.d.ts +113 -0
  26. package/dist/changes.js +324 -0
  27. package/dist/cli-args.d.ts +21 -0
  28. package/dist/cli-args.js +93 -0
  29. package/dist/client/banner-height.d.ts +1 -0
  30. package/dist/client/banner-height.js +33 -0
  31. package/dist/client/change-marks.d.ts +1 -0
  32. package/dist/client/change-marks.js +56 -0
  33. package/dist/client/changes.d.ts +1 -0
  34. package/dist/client/changes.js +183 -0
  35. package/dist/client/chart-option.d.ts +3 -0
  36. package/dist/client/chart-option.js +76 -0
  37. package/dist/client/charts.d.ts +1 -0
  38. package/dist/client/charts.js +54 -0
  39. package/dist/client/clipboard.d.ts +1 -0
  40. package/dist/client/clipboard.js +43 -0
  41. package/dist/client/code-copy.d.ts +1 -0
  42. package/dist/client/code-copy.js +29 -0
  43. package/dist/client/column-resize.d.ts +1 -0
  44. package/dist/client/column-resize.js +120 -0
  45. package/dist/client/config-dom.d.ts +13 -0
  46. package/dist/client/config-dom.js +47 -0
  47. package/dist/client/config-draft.d.ts +73 -0
  48. package/dist/client/config-draft.js +218 -0
  49. package/dist/client/config-editor.d.ts +3 -0
  50. package/dist/client/config-editor.js +677 -0
  51. package/dist/client/config-fields.d.ts +35 -0
  52. package/dist/client/config-fields.js +339 -0
  53. package/dist/client/config-json.d.ts +18 -0
  54. package/dist/client/config-json.js +96 -0
  55. package/dist/client/config-preview.d.ts +9 -0
  56. package/dist/client/config-preview.js +91 -0
  57. package/dist/client/config-rules.d.ts +14 -0
  58. package/dist/client/config-rules.js +202 -0
  59. package/dist/client/config-types.d.ts +91 -0
  60. package/dist/client/config-types.js +4 -0
  61. package/dist/client/copy-path.d.ts +1 -0
  62. package/dist/client/copy-path.js +32 -0
  63. package/dist/client/dashboard-requests.d.ts +11 -0
  64. package/dist/client/dashboard-requests.js +28 -0
  65. package/dist/client/dashboard.d.ts +1 -0
  66. package/dist/client/dashboard.js +119 -0
  67. package/dist/client/diagnostics.d.ts +1 -0
  68. package/dist/client/diagnostics.js +164 -0
  69. package/dist/client/diagrams.d.ts +1 -0
  70. package/dist/client/diagrams.js +81 -0
  71. package/dist/client/diff-expand.d.ts +1 -0
  72. package/dist/client/diff-expand.js +42 -0
  73. package/dist/client/display-settings-model.d.ts +74 -0
  74. package/dist/client/display-settings-model.js +229 -0
  75. package/dist/client/display-settings.d.ts +1 -0
  76. package/dist/client/display-settings.js +411 -0
  77. package/dist/client/echarts.d.ts +30 -0
  78. package/dist/client/echarts.js +14 -0
  79. package/dist/client/graph-layout.d.ts +88 -0
  80. package/dist/client/graph-layout.js +198 -0
  81. package/dist/client/graph.d.ts +2 -0
  82. package/dist/client/graph.js +566 -0
  83. package/dist/client/history.d.ts +21 -0
  84. package/dist/client/history.js +86 -0
  85. package/dist/client/live-reload-model.d.ts +28 -0
  86. package/dist/client/live-reload-model.js +52 -0
  87. package/dist/client/live-reload.d.ts +9 -0
  88. package/dist/client/live-reload.js +138 -0
  89. package/dist/client/menu.d.ts +14 -0
  90. package/dist/client/menu.js +176 -0
  91. package/dist/client/navigation-feedback.d.ts +1 -0
  92. package/dist/client/navigation-feedback.js +141 -0
  93. package/dist/client/open-config.d.ts +1 -0
  94. package/dist/client/open-config.js +48 -0
  95. package/dist/client/outline-spy.d.ts +1 -0
  96. package/dist/client/outline-spy.js +60 -0
  97. package/dist/client/prompts.d.ts +7 -0
  98. package/dist/client/prompts.js +65 -0
  99. package/dist/client/review.d.ts +1 -0
  100. package/dist/client/review.js +379 -0
  101. package/dist/client/search.d.ts +1 -0
  102. package/dist/client/search.js +199 -0
  103. package/dist/client/segmented.d.ts +13 -0
  104. package/dist/client/segmented.js +70 -0
  105. package/dist/client/source-lines.d.ts +16 -0
  106. package/dist/client/source-lines.js +47 -0
  107. package/dist/client/theme-toggle.d.ts +10 -0
  108. package/dist/client/theme-toggle.js +100 -0
  109. package/dist/client/theme.d.ts +0 -0
  110. package/dist/client/theme.js +50 -0
  111. package/dist/client/tree-marks.d.ts +31 -0
  112. package/dist/client/tree-marks.js +102 -0
  113. package/dist/client/tree-state.d.ts +0 -0
  114. package/dist/client/tree-state.js +179 -0
  115. package/dist/client/viewer.d.ts +1 -0
  116. package/dist/client/viewer.js +53 -0
  117. package/dist/config-editor.d.ts +166 -0
  118. package/dist/config-editor.js +378 -0
  119. package/dist/config-routes.d.ts +18 -0
  120. package/dist/config-routes.js +213 -0
  121. package/dist/context-view.d.ts +41 -0
  122. package/dist/context-view.js +295 -0
  123. package/dist/corpus-routes.d.ts +31 -0
  124. package/dist/corpus-routes.js +258 -0
  125. package/dist/corpus.d.ts +134 -0
  126. package/dist/corpus.js +653 -0
  127. package/dist/dashboard-article.d.ts +14 -0
  128. package/dist/dashboard-article.js +290 -0
  129. package/dist/dashboard.d.ts +94 -0
  130. package/dist/dashboard.js +468 -0
  131. package/dist/diagnostics.d.ts +23 -0
  132. package/dist/diagnostics.js +44 -0
  133. package/dist/document-routes.d.ts +39 -0
  134. package/dist/document-routes.js +396 -0
  135. package/dist/event-hub.d.ts +39 -0
  136. package/dist/event-hub.js +120 -0
  137. package/dist/findings-panel.d.ts +4 -0
  138. package/dist/findings-panel.js +74 -0
  139. package/dist/folder-page.d.ts +8 -0
  140. package/dist/folder-page.js +74 -0
  141. package/dist/graph-view.d.ts +66 -0
  142. package/dist/graph-view.js +236 -0
  143. package/dist/highlight.d.ts +24 -0
  144. package/dist/highlight.js +100 -0
  145. package/dist/http-send.d.ts +9 -0
  146. package/dist/http-send.js +53 -0
  147. package/dist/index.d.ts +2 -0
  148. package/dist/index.js +72 -0
  149. package/dist/open-file.d.ts +36 -0
  150. package/dist/open-file.js +87 -0
  151. package/dist/page-tree.d.ts +3 -0
  152. package/dist/page-tree.js +105 -0
  153. package/dist/page.d.ts +73 -0
  154. package/dist/page.js +478 -0
  155. package/dist/prompts.d.ts +25 -0
  156. package/dist/prompts.js +82 -0
  157. package/dist/render-cache.d.ts +23 -0
  158. package/dist/render-cache.js +38 -0
  159. package/dist/render-document.d.ts +60 -0
  160. package/dist/render-document.js +440 -0
  161. package/dist/request-path.d.ts +17 -0
  162. package/dist/request-path.js +51 -0
  163. package/dist/review-context.d.ts +24 -0
  164. package/dist/review-context.js +109 -0
  165. package/dist/review-queries.d.ts +26 -0
  166. package/dist/review-queries.js +69 -0
  167. package/dist/review-state.d.ts +106 -0
  168. package/dist/review-state.js +251 -0
  169. package/dist/review-store.d.ts +145 -0
  170. package/dist/review-store.js +530 -0
  171. package/dist/router.d.ts +28 -0
  172. package/dist/router.js +294 -0
  173. package/dist/search.d.ts +22 -0
  174. package/dist/search.js +164 -0
  175. package/dist/security.d.ts +95 -0
  176. package/dist/security.js +196 -0
  177. package/dist/server.d.ts +21 -0
  178. package/dist/server.js +224 -0
  179. package/dist/speculation-rules.d.ts +4 -0
  180. package/dist/speculation-rules.js +49 -0
  181. package/dist/state-dir.d.ts +6 -0
  182. package/dist/state-dir.js +31 -0
  183. package/dist/tree-marks.d.ts +49 -0
  184. package/dist/tree-marks.js +108 -0
  185. package/dist/viewer-context.d.ts +37 -0
  186. package/dist/viewer-context.js +74 -0
  187. package/dist/watch.d.ts +41 -0
  188. package/dist/watch.js +135 -0
  189. package/package.json +57 -4
@@ -0,0 +1,35 @@
1
+ import type { Json, JsonSchema, Path } from "./config-types.js";
2
+ /** What a field reads and changes: the editor's model, by path. */
3
+ export type FieldModel = {
4
+ get(path: Path): Json | undefined;
5
+ set(path: Path, value: Json): void;
6
+ remove(path: Path): void;
7
+ insert(path: Path, value: Json): void;
8
+ };
9
+ export type FieldSpec = {
10
+ schema: JsonSchema;
11
+ path: Path;
12
+ label: string;
13
+ required: boolean;
14
+ unsetLabel?: string;
15
+ inList?: boolean;
16
+ };
17
+ export declare class FieldRenderer {
18
+ private readonly model;
19
+ constructor(model: FieldModel);
20
+ /** One input, or a group of them, for the value the schema describes at `path`. */
21
+ field(spec: FieldSpec): HTMLElement;
22
+ private objectField;
23
+ private arrayField;
24
+ private choiceField;
25
+ private segmentedField;
26
+ private selectField;
27
+ private inputField;
28
+ private applyInput;
29
+ private kindField;
30
+ private listField;
31
+ private emptyListNote;
32
+ private listRow;
33
+ private jsonField;
34
+ }
35
+ export declare function isPrimitive(schema: JsonSchema): boolean;
@@ -0,0 +1,339 @@
1
+ import { button, element, hint, labelled, labelText, paragraph, } from "./config-dom.js";
2
+ import { displayText } from "./config-json.js";
3
+ import { segmentedControl } from "./segmented.js";
4
+ export class FieldRenderer {
5
+ model;
6
+ constructor(model) {
7
+ this.model = model;
8
+ }
9
+ /** One input, or a group of them, for the value the schema describes at `path`. */
10
+ field(spec) {
11
+ const { schema } = spec;
12
+ if (isKindUnion(schema)) {
13
+ return this.kindField(schema.oneOf, spec);
14
+ }
15
+ if (schema.type === "object" && schema.properties !== undefined) {
16
+ return this.objectField(spec, schema.properties);
17
+ }
18
+ if (schema.type === "array" && schema.items !== undefined) {
19
+ return this.arrayField(spec, schema.items);
20
+ }
21
+ if (schema.enum !== undefined || schema.type === "boolean") {
22
+ return this.choiceField(spec, schema.enum ?? [true, false]);
23
+ }
24
+ if (schema.type === "string" ||
25
+ schema.type === "number" ||
26
+ schema.type === "integer") {
27
+ return this.inputField(spec);
28
+ }
29
+ return this.jsonField(spec);
30
+ }
31
+ objectField(spec, properties) {
32
+ const group = element("fieldset", "config-group");
33
+ const legend = element("legend");
34
+ legend.textContent = labelText(spec.label, spec.required);
35
+ group.append(legend);
36
+ if (spec.schema.description !== undefined) {
37
+ group.append(hint(spec.schema.description));
38
+ }
39
+ const requiredKeys = new Set(spec.schema.required ?? []);
40
+ for (const [key, child] of Object.entries(properties)) {
41
+ group.append(this.field({
42
+ schema: child,
43
+ path: [...spec.path, key],
44
+ label: key,
45
+ required: requiredKeys.has(key),
46
+ }));
47
+ }
48
+ return group;
49
+ }
50
+ arrayField(spec, items) {
51
+ if (isPrimitive(items) ||
52
+ (items.type === "object" && items.properties !== undefined)) {
53
+ return this.listField(spec, items);
54
+ }
55
+ return this.jsonField(spec);
56
+ }
57
+ // A few choices — a boolean, a severity — are shown all at once as a segmented control, one click
58
+ // to change; a longer list stays a select, where a row of segments would run off the form.
59
+ choiceField(spec, choices) {
60
+ return choices.length <= 3
61
+ ? this.segmentedField(spec, choices)
62
+ : this.selectField(spec, choices);
63
+ }
64
+ segmentedField(spec, choices) {
65
+ const { path } = spec;
66
+ const control = segmentedControl(labelText(spec.label, spec.required), [
67
+ ["", unsetLabel(spec, "not set")],
68
+ ...choices.map((choice) => [JSON.stringify(choice), String(choice)]),
69
+ ], (chosen) => {
70
+ if (chosen === "") {
71
+ this.model.remove(path);
72
+ }
73
+ else {
74
+ this.model.set(path, JSON.parse(chosen));
75
+ }
76
+ // Bubbled like a select's change, so a rule card's summary follows the new severity.
77
+ control.element.dispatchEvent(new Event("change", { bubbles: true }));
78
+ });
79
+ const value = this.model.get(path);
80
+ control.render(value === undefined ? "" : JSON.stringify(value));
81
+ return labelled(control.element, {
82
+ label: spec.label,
83
+ required: spec.required,
84
+ description: spec.schema.description,
85
+ wrapperTag: "div",
86
+ });
87
+ }
88
+ selectField(spec, choices) {
89
+ const { path } = spec;
90
+ const select = document.createElement("select");
91
+ select.append(new Option(unsetLabel(spec, "—"), ""));
92
+ for (const choice of choices) {
93
+ select.append(new Option(String(choice), JSON.stringify(choice)));
94
+ }
95
+ const value = this.model.get(path);
96
+ select.value = value === undefined ? "" : JSON.stringify(value);
97
+ select.addEventListener("change", () => {
98
+ if (select.value === "") {
99
+ this.model.remove(path);
100
+ }
101
+ else {
102
+ this.model.set(path, JSON.parse(select.value));
103
+ }
104
+ });
105
+ return labelled(select, {
106
+ label: spec.label,
107
+ required: spec.required,
108
+ description: spec.schema.description,
109
+ });
110
+ }
111
+ inputField(spec) {
112
+ const input = document.createElement("input");
113
+ const numeric = spec.schema.type !== "string";
114
+ configureInput(input, spec.schema, numeric);
115
+ const value = this.model.get(spec.path);
116
+ input.value =
117
+ value === undefined || value === null ? "" : displayText(value);
118
+ input.addEventListener("input", () => {
119
+ this.applyInput(input, spec, numeric);
120
+ });
121
+ return spec.inList === true
122
+ ? input
123
+ : labelled(input, {
124
+ label: spec.label,
125
+ required: spec.required,
126
+ description: spec.schema.description,
127
+ });
128
+ }
129
+ // An emptied field is left out of the config rather than set to "", except in a list, where the
130
+ // row stays until it is removed. A number that does not parse yet is not applied at all.
131
+ applyInput(input, spec, numeric) {
132
+ if (input.value === "" && spec.inList !== true) {
133
+ this.model.remove(spec.path);
134
+ return;
135
+ }
136
+ if (!numeric) {
137
+ this.model.set(spec.path, input.value);
138
+ return;
139
+ }
140
+ const number = Number(input.value);
141
+ if (input.value !== "" && Number.isFinite(number)) {
142
+ this.model.set(spec.path, number);
143
+ }
144
+ }
145
+ // A union told apart by its `kind`, such as a custom rule's assertion: pick the kind, then fill in
146
+ // that kind's fields. Picking another kind starts the value over, since the fields differ.
147
+ kindField(branches, spec) {
148
+ const { path } = spec;
149
+ const group = element("fieldset", "config-group");
150
+ const legend = element("legend");
151
+ legend.textContent = labelText(spec.label, spec.required);
152
+ const current = this.model.get(path);
153
+ const select = document.createElement("select");
154
+ for (const branch of branches) {
155
+ const kind = String(branch.properties.kind.const);
156
+ select.append(new Option(kind, kind));
157
+ }
158
+ select.value = displayText(current?.kind);
159
+ const fields = element("div");
160
+ const renderFields = () => {
161
+ const branch = branches.find((candidate) => candidate.properties.kind.const === select.value);
162
+ const requiredKeys = new Set(branch?.required ?? []);
163
+ fields.replaceChildren(...Object.entries(branch?.properties ?? {})
164
+ .filter(([key]) => key !== "kind")
165
+ .map(([key, child]) => this.field({
166
+ schema: child,
167
+ path: [...path, key],
168
+ label: key,
169
+ required: requiredKeys.has(key),
170
+ })));
171
+ };
172
+ select.addEventListener("change", () => {
173
+ this.model.set(path, { kind: select.value });
174
+ renderFields();
175
+ });
176
+ group.append(legend, labelled(select, { label: "kind", required: true }), fields);
177
+ renderFields();
178
+ return group;
179
+ }
180
+ listField(spec, items) {
181
+ const { path, required } = spec;
182
+ const group = element("fieldset", "config-group config-list");
183
+ const legend = element("legend");
184
+ legend.textContent = labelText(spec.label, required);
185
+ const list = element("div", "config-list-items");
186
+ const add = button("Add", "config-add");
187
+ const emptyNote = this.emptyListNote(path, () => {
188
+ render();
189
+ });
190
+ const render = (focusLast = false) => {
191
+ const values = this.model.get(path);
192
+ const array = Array.isArray(values) ? values : [];
193
+ emptyNote.hidden = required || !Array.isArray(values) || array.length > 0;
194
+ list.replaceChildren(...array.map((_, index) => this.listRow({
195
+ spec,
196
+ items,
197
+ index,
198
+ count: array.length,
199
+ rerender: render,
200
+ })));
201
+ if (focusLast) {
202
+ list
203
+ .querySelector(".config-list-row:last-child input, .config-list-object:last-child input")
204
+ ?.focus();
205
+ }
206
+ };
207
+ add.addEventListener("click", () => {
208
+ const fresh = freshItem(items);
209
+ const values = this.model.get(path);
210
+ if (Array.isArray(values)) {
211
+ this.model.insert([...path, values.length], fresh);
212
+ }
213
+ else {
214
+ this.model.set(path, [fresh]);
215
+ }
216
+ render(true);
217
+ });
218
+ group.append(legend);
219
+ if (spec.schema.description !== undefined) {
220
+ group.append(hint(spec.schema.description));
221
+ }
222
+ group.append(emptyNote, list, add);
223
+ render();
224
+ return group;
225
+ }
226
+ // An optional list that is present but empty is not the same as one left out: an empty `files`
227
+ // matches no file at all, so the rule it scopes checks nothing. Removing the last entry therefore
228
+ // removes the list, and an empty one already in the file is pointed out with a way to remove it.
229
+ emptyListNote(path, rerender) {
230
+ const emptyNote = element("div", "config-list-empty");
231
+ const emptyText = paragraph("This list is empty, which is not the same as leaving it out: an empty list matches nothing.", "config-hint");
232
+ const dropEmpty = button("Remove the empty list", "config-remove");
233
+ dropEmpty.addEventListener("click", () => {
234
+ this.model.remove(path);
235
+ rerender();
236
+ });
237
+ emptyNote.append(emptyText, dropEmpty);
238
+ return emptyNote;
239
+ }
240
+ listRow(row) {
241
+ const { spec, items, index } = row;
242
+ const primitive = isPrimitive(items);
243
+ const container = element("div", primitive ? "config-list-row" : "config-list-object");
244
+ const itemPath = [...spec.path, index];
245
+ const itemLabel = `${spec.label} ${index + 1}`;
246
+ const control = this.field({
247
+ schema: items,
248
+ path: itemPath,
249
+ label: itemLabel,
250
+ required: true,
251
+ inList: primitive,
252
+ });
253
+ if (control instanceof HTMLInputElement ||
254
+ control instanceof HTMLSelectElement) {
255
+ control.setAttribute("aria-label", itemLabel);
256
+ }
257
+ const remove = button("Remove", "config-remove");
258
+ remove.setAttribute("aria-label", `Remove ${itemLabel}`);
259
+ remove.addEventListener("click", () => {
260
+ this.model.remove(!spec.required && row.count === 1 ? spec.path : itemPath);
261
+ row.rerender();
262
+ });
263
+ container.append(control, remove);
264
+ return container;
265
+ }
266
+ // For a value the form has no inputs for — a map from names to lists, say — the value itself, as
267
+ // JSON. It is applied when it parses, and core judges it like any other.
268
+ jsonField(spec) {
269
+ const { path } = spec;
270
+ const textarea = document.createElement("textarea");
271
+ textarea.className = "config-json";
272
+ textarea.spellcheck = false;
273
+ const value = this.model.get(path);
274
+ textarea.value = value === undefined ? "" : JSON.stringify(value, null, 2);
275
+ const problem = element("span", "config-json-problem");
276
+ textarea.addEventListener("change", () => {
277
+ if (textarea.value.trim() === "") {
278
+ problem.textContent = "";
279
+ this.model.remove(path);
280
+ return;
281
+ }
282
+ try {
283
+ const parsed = JSON.parse(textarea.value);
284
+ problem.textContent = "";
285
+ this.model.set(path, parsed);
286
+ }
287
+ catch {
288
+ problem.textContent = "Not valid JSON yet.";
289
+ }
290
+ });
291
+ const wrapper = labelled(textarea, {
292
+ label: `${spec.label} (JSON)`,
293
+ required: spec.required,
294
+ description: spec.schema.description,
295
+ });
296
+ wrapper.append(problem);
297
+ return wrapper;
298
+ }
299
+ }
300
+ function isKindUnion(schema) {
301
+ return (schema.oneOf?.every((branch) => branch.properties?.kind?.const !== undefined) === true);
302
+ }
303
+ export function isPrimitive(schema) {
304
+ return (schema.type === "string" ||
305
+ schema.type === "number" ||
306
+ schema.type === "integer" ||
307
+ schema.type === "boolean");
308
+ }
309
+ // The "not set" choice says what not setting means when the schema has a default.
310
+ function unsetLabel(spec, fallback) {
311
+ if (spec.unsetLabel !== undefined) {
312
+ return spec.unsetLabel;
313
+ }
314
+ return spec.schema.default === undefined
315
+ ? fallback
316
+ : `default (${displayText(spec.schema.default)})`;
317
+ }
318
+ function configureInput(input, schema, numeric) {
319
+ input.type = numeric ? "number" : "text";
320
+ if (numeric) {
321
+ input.step = schema.type === "integer" ? "1" : "any";
322
+ const min = schema.minimum ?? schema.exclusiveMinimum;
323
+ if (min !== undefined) {
324
+ input.min = String(min);
325
+ }
326
+ return;
327
+ }
328
+ input.spellcheck = false;
329
+ if (schema.pattern !== undefined) {
330
+ input.title = `Pattern: ${schema.pattern}`;
331
+ }
332
+ }
333
+ // What a new row starts as: empty text, zero, or an object whose fields are then filled in.
334
+ function freshItem(items) {
335
+ if (!isPrimitive(items)) {
336
+ return {};
337
+ }
338
+ return items.type === "string" ? "" : 0;
339
+ }
@@ -0,0 +1,18 @@
1
+ import type { Edit, Json, JsonObject, Path } from "./config-types.js";
2
+ export declare function clone<T>(value: T): T;
3
+ export declare function samePath(left: Path, right: Path): boolean;
4
+ export declare function getAt(root: Json | null, path: Path): Json | undefined;
5
+ export declare function setAt(root: JsonObject, path: Path, value: Json): JsonObject;
6
+ export declare function removeAt(root: JsonObject, path: Path): void;
7
+ export declare function insertAt(root: JsonObject, path: Path, value: Json): void;
8
+ /**
9
+ * Replays edits onto a model, in order, as the server applies them to the text. Stops once the model
10
+ * is gone: an edit can only be replayed onto a config that loaded.
11
+ */
12
+ export declare function replayEdits(model: JsonObject | null, edits: readonly Edit[]): JsonObject | null;
13
+ /**
14
+ * A JSON value as a label or a field shows it: a string as itself, another scalar as its literal,
15
+ * nothing as empty, and an array or object as its JSON — never the `[object Object]` that `String`
16
+ * would give it.
17
+ */
18
+ export declare function displayText(value: unknown): string;
@@ -0,0 +1,96 @@
1
+ // The config editor's local copy of the config is plain JSON, changed by path. These helpers keep
2
+ // that copy in the shape the server's edits give the file, so the form shows what the text will say.
3
+ export function clone(value) {
4
+ return value === undefined ? value : JSON.parse(JSON.stringify(value));
5
+ }
6
+ export function samePath(left, right) {
7
+ return (left.length === right.length &&
8
+ left.every((segment, index) => segment === right[index]));
9
+ }
10
+ export function getAt(root, path) {
11
+ let node = root ?? undefined;
12
+ for (const segment of path) {
13
+ if (node === undefined || node === null || typeof node !== "object") {
14
+ return undefined;
15
+ }
16
+ node = node[segment];
17
+ }
18
+ return node;
19
+ }
20
+ // The same shape `editConfigText` gives the file: objects created on the way down for a missing
21
+ // name, so the form and the text agree on what an edit made.
22
+ export function setAt(root, path, value) {
23
+ let node = root;
24
+ path.forEach((segment, index) => {
25
+ const container = node;
26
+ if (index === path.length - 1) {
27
+ container[segment] = value;
28
+ return;
29
+ }
30
+ const next = container[segment];
31
+ if (next === undefined || next === null || typeof next !== "object") {
32
+ container[segment] = typeof path[index + 1] === "number" ? [] : {};
33
+ }
34
+ node = container[segment];
35
+ });
36
+ return root;
37
+ }
38
+ export function removeAt(root, path) {
39
+ const parent = getAt(root, path.slice(0, -1));
40
+ const last = path.at(-1);
41
+ if (Array.isArray(parent) && typeof last === "number") {
42
+ parent.splice(last, 1);
43
+ }
44
+ else if (parent !== undefined &&
45
+ parent !== null &&
46
+ typeof parent === "object") {
47
+ Reflect.deleteProperty(parent, String(last));
48
+ }
49
+ }
50
+ export function insertAt(root, path, value) {
51
+ const parent = getAt(root, path.slice(0, -1));
52
+ if (Array.isArray(parent)) {
53
+ parent.splice(Number(path.at(-1)), 0, value);
54
+ }
55
+ }
56
+ /**
57
+ * Replays edits onto a model, in order, as the server applies them to the text. Stops once the model
58
+ * is gone: an edit can only be replayed onto a config that loaded.
59
+ */
60
+ export function replayEdits(model, edits) {
61
+ let current = model;
62
+ for (const edit of edits) {
63
+ if (current === null) {
64
+ break;
65
+ }
66
+ if (edit.op === "set") {
67
+ current = setAt(current, edit.path, clone(edit.value ?? null));
68
+ }
69
+ else if (edit.op === "remove") {
70
+ removeAt(current, edit.path);
71
+ }
72
+ else {
73
+ insertAt(current, edit.path, clone(edit.value ?? null));
74
+ }
75
+ }
76
+ return current;
77
+ }
78
+ /**
79
+ * A JSON value as a label or a field shows it: a string as itself, another scalar as its literal,
80
+ * nothing as empty, and an array or object as its JSON — never the `[object Object]` that `String`
81
+ * would give it.
82
+ */
83
+ export function displayText(value) {
84
+ if (value === undefined) {
85
+ return "";
86
+ }
87
+ if (typeof value === "string") {
88
+ return value;
89
+ }
90
+ if (value === null ||
91
+ typeof value === "number" ||
92
+ typeof value === "boolean") {
93
+ return String(value);
94
+ }
95
+ return JSON.stringify(value);
96
+ }
@@ -0,0 +1,9 @@
1
+ import type { OkPreview } from "./config-types.js";
2
+ /** Why the linter would not load the config, line by line as core reported it. */
3
+ export declare function invalidPreview(errors: readonly string[]): HTMLElement[];
4
+ /** The findings before and after, which rules' counts move, and the file as it would be written. */
5
+ export declare function okPreview(preview: OkPreview, filePath: string): HTMLElement[];
6
+ export declare function counts(side: {
7
+ errors: number;
8
+ warnings: number;
9
+ }): string;
@@ -0,0 +1,91 @@
1
+ import { element, paragraph } from "./config-dom.js";
2
+ // The preview's content: what the server's answer says the change would do to the lint run. Built
3
+ // from the answer alone, so what is shown is core's verdict, never a judgment made on this page.
4
+ /** Why the linter would not load the config, line by line as core reported it. */
5
+ export function invalidPreview(errors) {
6
+ const list = element("ul", "config-errors");
7
+ for (const line of errors) {
8
+ const item = element("li");
9
+ item.textContent = line.replace(/^- /, "");
10
+ list.append(item);
11
+ }
12
+ return [
13
+ paragraph("The linter would not load this config:", "config-invalid"),
14
+ list,
15
+ ];
16
+ }
17
+ /** The findings before and after, which rules' counts move, and the file as it would be written. */
18
+ export function okPreview(preview, filePath) {
19
+ const { before, after } = preview.findings;
20
+ const parts = [
21
+ paragraph(`Now: ${counts(before)}. With this config: ${counts(after)} — ${preview.addedCount} new, ${preview.removedCount} gone.`, "config-summary"),
22
+ ];
23
+ if (preview.documents.before !== preview.documents.after) {
24
+ parts.push(paragraph(`The linted corpus would hold ${preview.documents.after} documents instead of ${preview.documents.before}.`));
25
+ }
26
+ const changedRules = preview.byRule.filter((rule) => rule.before !== rule.after);
27
+ if (changedRules.length > 0) {
28
+ parts.push(byRuleTable(changedRules));
29
+ }
30
+ parts.push(findingList("New findings", preview.added, preview.addedCount), findingList("Gone findings", preview.removed, preview.removedCount));
31
+ const file = document.createElement("details");
32
+ file.className = "config-result";
33
+ const fileSummary = document.createElement("summary");
34
+ fileSummary.textContent = `${filePath} with this change`;
35
+ const pre = document.createElement("pre");
36
+ pre.textContent = preview.text;
37
+ file.append(fileSummary, pre);
38
+ parts.push(file);
39
+ return parts;
40
+ }
41
+ function byRuleTable(rules) {
42
+ const table = element("table", "config-by-rule");
43
+ const head = element("tr");
44
+ for (const title of ["Rule", "Now", "With this config"]) {
45
+ const cell = element("th");
46
+ cell.textContent = title;
47
+ head.append(cell);
48
+ }
49
+ table.append(head);
50
+ for (const rule of rules) {
51
+ const row = element("tr");
52
+ for (const text of [rule.ruleId, String(rule.before), String(rule.after)]) {
53
+ const cell = element("td");
54
+ cell.textContent = text;
55
+ row.append(cell);
56
+ }
57
+ table.append(row);
58
+ }
59
+ return table;
60
+ }
61
+ function findingList(title, findings, total) {
62
+ const details = document.createElement("details");
63
+ details.className = "config-findings";
64
+ const summary = document.createElement("summary");
65
+ summary.textContent = `${title} (${total})`;
66
+ const list = element("ul");
67
+ for (const finding of findings) {
68
+ const item = element("li", `config-finding config-finding-${finding.severity}`);
69
+ const where = element("code");
70
+ where.textContent =
71
+ finding.line > 0
72
+ ? `${finding.filePath}:${finding.line}`
73
+ : finding.filePath;
74
+ const rule = element("span", "config-finding-rule");
75
+ rule.textContent = `${finding.ruleId} ${finding.severity}`;
76
+ const message = element("span");
77
+ message.textContent = finding.message;
78
+ item.append(where, " ", rule, " ", message);
79
+ list.append(item);
80
+ }
81
+ if (total > findings.length) {
82
+ const more = element("li", "config-hint");
83
+ more.textContent = `…and ${total - findings.length} more.`;
84
+ list.append(more);
85
+ }
86
+ details.append(summary, list);
87
+ return details;
88
+ }
89
+ export function counts(side) {
90
+ return `${side.errors} error${side.errors === 1 ? "" : "s"}, ${side.warnings} warning${side.warnings === 1 ? "" : "s"}`;
91
+ }
@@ -0,0 +1,14 @@
1
+ import type { FieldRenderer } from "./config-fields.js";
2
+ import type { ConfigApi, Json, JsonObject, Path } from "./config-types.js";
3
+ /** What the rule cards read from the editor and ask it to do. */
4
+ export type RuleListHost = {
5
+ readonly api: ConfigApi;
6
+ readonly fields: FieldRenderer;
7
+ ruleId(index: number): string | null;
8
+ configuredIds(): Set<string>;
9
+ isOpen(index: number): boolean;
10
+ current(path: Path): Json | undefined;
11
+ addRule(entry: JsonObject, id: string | null): void;
12
+ removeRule(index: number): void;
13
+ };
14
+ export declare function rulesList(host: RuleListHost, entries: readonly Json[]): HTMLElement;