@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.
- package/LICENSE +21 -0
- package/README.md +86 -2
- package/assets/favicon.ico +0 -0
- package/assets/viewer.css +4184 -0
- package/dist/asset-routes.d.ts +4 -0
- package/dist/asset-routes.js +107 -0
- package/dist/blame.d.ts +8 -0
- package/dist/blame.js +54 -0
- package/dist/branch-changes.d.ts +8 -0
- package/dist/branch-changes.js +49 -0
- package/dist/candidate-lint-worker.d.ts +1 -0
- package/dist/candidate-lint-worker.js +67 -0
- package/dist/candidate-lint.d.ts +39 -0
- package/dist/candidate-lint.js +115 -0
- package/dist/change-banner.d.ts +38 -0
- package/dist/change-banner.js +108 -0
- package/dist/change-diff.d.ts +23 -0
- package/dist/change-diff.js +140 -0
- package/dist/change-marks.d.ts +32 -0
- package/dist/change-marks.js +363 -0
- package/dist/changes-article.d.ts +15 -0
- package/dist/changes-article.js +244 -0
- package/dist/changes-routes.d.ts +12 -0
- package/dist/changes-routes.js +271 -0
- package/dist/changes.d.ts +113 -0
- package/dist/changes.js +324 -0
- package/dist/cli-args.d.ts +21 -0
- package/dist/cli-args.js +93 -0
- package/dist/client/banner-height.d.ts +1 -0
- package/dist/client/banner-height.js +33 -0
- package/dist/client/change-marks.d.ts +1 -0
- package/dist/client/change-marks.js +56 -0
- package/dist/client/changes.d.ts +1 -0
- package/dist/client/changes.js +183 -0
- package/dist/client/chart-option.d.ts +3 -0
- package/dist/client/chart-option.js +76 -0
- package/dist/client/charts.d.ts +1 -0
- package/dist/client/charts.js +54 -0
- package/dist/client/clipboard.d.ts +1 -0
- package/dist/client/clipboard.js +43 -0
- package/dist/client/code-copy.d.ts +1 -0
- package/dist/client/code-copy.js +29 -0
- package/dist/client/column-resize.d.ts +1 -0
- package/dist/client/column-resize.js +120 -0
- package/dist/client/config-dom.d.ts +13 -0
- package/dist/client/config-dom.js +47 -0
- package/dist/client/config-draft.d.ts +73 -0
- package/dist/client/config-draft.js +218 -0
- package/dist/client/config-editor.d.ts +3 -0
- package/dist/client/config-editor.js +677 -0
- package/dist/client/config-fields.d.ts +35 -0
- package/dist/client/config-fields.js +339 -0
- package/dist/client/config-json.d.ts +18 -0
- package/dist/client/config-json.js +96 -0
- package/dist/client/config-preview.d.ts +9 -0
- package/dist/client/config-preview.js +91 -0
- package/dist/client/config-rules.d.ts +14 -0
- package/dist/client/config-rules.js +202 -0
- package/dist/client/config-types.d.ts +91 -0
- package/dist/client/config-types.js +4 -0
- package/dist/client/copy-path.d.ts +1 -0
- package/dist/client/copy-path.js +32 -0
- package/dist/client/dashboard-requests.d.ts +11 -0
- package/dist/client/dashboard-requests.js +28 -0
- package/dist/client/dashboard.d.ts +1 -0
- package/dist/client/dashboard.js +119 -0
- package/dist/client/diagnostics.d.ts +1 -0
- package/dist/client/diagnostics.js +164 -0
- package/dist/client/diagrams.d.ts +1 -0
- package/dist/client/diagrams.js +81 -0
- package/dist/client/diff-expand.d.ts +1 -0
- package/dist/client/diff-expand.js +42 -0
- package/dist/client/display-settings-model.d.ts +74 -0
- package/dist/client/display-settings-model.js +229 -0
- package/dist/client/display-settings.d.ts +1 -0
- package/dist/client/display-settings.js +411 -0
- package/dist/client/echarts.d.ts +30 -0
- package/dist/client/echarts.js +14 -0
- package/dist/client/graph-layout.d.ts +88 -0
- package/dist/client/graph-layout.js +198 -0
- package/dist/client/graph.d.ts +2 -0
- package/dist/client/graph.js +566 -0
- package/dist/client/history.d.ts +21 -0
- package/dist/client/history.js +86 -0
- package/dist/client/live-reload-model.d.ts +28 -0
- package/dist/client/live-reload-model.js +52 -0
- package/dist/client/live-reload.d.ts +9 -0
- package/dist/client/live-reload.js +138 -0
- package/dist/client/menu.d.ts +14 -0
- package/dist/client/menu.js +176 -0
- package/dist/client/navigation-feedback.d.ts +1 -0
- package/dist/client/navigation-feedback.js +141 -0
- package/dist/client/open-config.d.ts +1 -0
- package/dist/client/open-config.js +48 -0
- package/dist/client/outline-spy.d.ts +1 -0
- package/dist/client/outline-spy.js +60 -0
- package/dist/client/prompts.d.ts +7 -0
- package/dist/client/prompts.js +65 -0
- package/dist/client/review.d.ts +1 -0
- package/dist/client/review.js +379 -0
- package/dist/client/search.d.ts +1 -0
- package/dist/client/search.js +199 -0
- package/dist/client/segmented.d.ts +13 -0
- package/dist/client/segmented.js +70 -0
- package/dist/client/source-lines.d.ts +16 -0
- package/dist/client/source-lines.js +47 -0
- package/dist/client/theme-toggle.d.ts +10 -0
- package/dist/client/theme-toggle.js +100 -0
- package/dist/client/theme.d.ts +0 -0
- package/dist/client/theme.js +50 -0
- package/dist/client/tree-marks.d.ts +31 -0
- package/dist/client/tree-marks.js +102 -0
- package/dist/client/tree-state.d.ts +0 -0
- package/dist/client/tree-state.js +179 -0
- package/dist/client/viewer.d.ts +1 -0
- package/dist/client/viewer.js +53 -0
- package/dist/config-editor.d.ts +166 -0
- package/dist/config-editor.js +378 -0
- package/dist/config-routes.d.ts +18 -0
- package/dist/config-routes.js +213 -0
- package/dist/context-view.d.ts +41 -0
- package/dist/context-view.js +295 -0
- package/dist/corpus-routes.d.ts +31 -0
- package/dist/corpus-routes.js +258 -0
- package/dist/corpus.d.ts +134 -0
- package/dist/corpus.js +653 -0
- package/dist/dashboard-article.d.ts +14 -0
- package/dist/dashboard-article.js +290 -0
- package/dist/dashboard.d.ts +94 -0
- package/dist/dashboard.js +468 -0
- package/dist/diagnostics.d.ts +23 -0
- package/dist/diagnostics.js +44 -0
- package/dist/document-routes.d.ts +39 -0
- package/dist/document-routes.js +396 -0
- package/dist/event-hub.d.ts +39 -0
- package/dist/event-hub.js +120 -0
- package/dist/findings-panel.d.ts +4 -0
- package/dist/findings-panel.js +74 -0
- package/dist/folder-page.d.ts +8 -0
- package/dist/folder-page.js +74 -0
- package/dist/graph-view.d.ts +66 -0
- package/dist/graph-view.js +236 -0
- package/dist/highlight.d.ts +24 -0
- package/dist/highlight.js +100 -0
- package/dist/http-send.d.ts +9 -0
- package/dist/http-send.js +53 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +72 -0
- package/dist/open-file.d.ts +36 -0
- package/dist/open-file.js +87 -0
- package/dist/page-tree.d.ts +3 -0
- package/dist/page-tree.js +105 -0
- package/dist/page.d.ts +73 -0
- package/dist/page.js +478 -0
- package/dist/prompts.d.ts +25 -0
- package/dist/prompts.js +82 -0
- package/dist/render-cache.d.ts +23 -0
- package/dist/render-cache.js +38 -0
- package/dist/render-document.d.ts +60 -0
- package/dist/render-document.js +440 -0
- package/dist/request-path.d.ts +17 -0
- package/dist/request-path.js +51 -0
- package/dist/review-context.d.ts +24 -0
- package/dist/review-context.js +109 -0
- package/dist/review-queries.d.ts +26 -0
- package/dist/review-queries.js +69 -0
- package/dist/review-state.d.ts +106 -0
- package/dist/review-state.js +251 -0
- package/dist/review-store.d.ts +145 -0
- package/dist/review-store.js +530 -0
- package/dist/router.d.ts +28 -0
- package/dist/router.js +294 -0
- package/dist/search.d.ts +22 -0
- package/dist/search.js +164 -0
- package/dist/security.d.ts +95 -0
- package/dist/security.js +196 -0
- package/dist/server.d.ts +21 -0
- package/dist/server.js +224 -0
- package/dist/speculation-rules.d.ts +4 -0
- package/dist/speculation-rules.js +49 -0
- package/dist/state-dir.d.ts +6 -0
- package/dist/state-dir.js +31 -0
- package/dist/tree-marks.d.ts +49 -0
- package/dist/tree-marks.js +108 -0
- package/dist/viewer-context.d.ts +37 -0
- package/dist/viewer-context.js +74 -0
- package/dist/watch.d.ts +41 -0
- package/dist/watch.js +135 -0
- package/package.json +57 -4
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
import { compareStrings, getComponents, measureDocument, ruleRegistry, } from "@wastech-mdlint/core";
|
|
2
|
+
// The dashboard: one row per corpus document, with its findings, its size and its place in the link
|
|
3
|
+
// graph, filtered and sorted by what the address asks for. Every number comes from core — findings
|
|
4
|
+
// from the lint run, sizes from `measureDocument` (the counts SIZE-001 checks), links from the
|
|
5
|
+
// corpus's `ContextGraph` and `getComponents` — and this module only selects and orders them, and
|
|
6
|
+
// writes them as CSV; `dashboard-article.ts` lays them out as a page. The page script asks the server for the same page again rather than filtering on its own, so
|
|
7
|
+
// the selection a reader sees with script is the one a link without script opens.
|
|
8
|
+
export const LINK_FLAGS = ["orphan", "dead-end", "cycle", "detached"];
|
|
9
|
+
const SEVERITIES = ["error", "warning"];
|
|
10
|
+
export const SORT_KEYS = [
|
|
11
|
+
"path",
|
|
12
|
+
"errors",
|
|
13
|
+
"warnings",
|
|
14
|
+
"rules",
|
|
15
|
+
"bytes",
|
|
16
|
+
"lines",
|
|
17
|
+
"tokens",
|
|
18
|
+
"linkedFrom",
|
|
19
|
+
"linksTo",
|
|
20
|
+
"flags",
|
|
21
|
+
];
|
|
22
|
+
const NUMERIC_PARAMS = [
|
|
23
|
+
"minKb",
|
|
24
|
+
"maxKb",
|
|
25
|
+
"minLines",
|
|
26
|
+
"maxLines",
|
|
27
|
+
"minTokens",
|
|
28
|
+
"maxTokens",
|
|
29
|
+
];
|
|
30
|
+
export const DEFAULT_SORT = "path";
|
|
31
|
+
// The direction a column sorts in when its header is first clicked: names read A to Z, while for a
|
|
32
|
+
// count the interesting documents are the ones with the most.
|
|
33
|
+
export function defaultDirection(sort) {
|
|
34
|
+
return sort === "path" || sort === "rules" || sort === "flags"
|
|
35
|
+
? "asc"
|
|
36
|
+
: "desc";
|
|
37
|
+
}
|
|
38
|
+
const KNOWN_PARAMS = [
|
|
39
|
+
"severity",
|
|
40
|
+
"rule",
|
|
41
|
+
"folder",
|
|
42
|
+
"path",
|
|
43
|
+
"flag",
|
|
44
|
+
"sort",
|
|
45
|
+
"dir",
|
|
46
|
+
"changed",
|
|
47
|
+
...NUMERIC_PARAMS,
|
|
48
|
+
];
|
|
49
|
+
/**
|
|
50
|
+
* Read a dashboard query from the address.
|
|
51
|
+
*
|
|
52
|
+
* Strict, the way the config is: an unknown parameter, value or rule is refused with a message
|
|
53
|
+
* rather than dropped, because a filter that silently does nothing reads as "no document matches".
|
|
54
|
+
* An empty value is "not set", since that is what a GET form sends for an empty field. `allowed` names
|
|
55
|
+
* extra parameters a caller handles itself, such as the API's `format`.
|
|
56
|
+
*/
|
|
57
|
+
export function parseDashboardQuery(params, source, allowed = []) {
|
|
58
|
+
const known = new Set([...KNOWN_PARAMS, ...allowed]);
|
|
59
|
+
for (const key of new Set(params.keys())) {
|
|
60
|
+
if (!known.has(key)) {
|
|
61
|
+
return { ok: false, message: `Unknown filter "${key}".` };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const values = (key) => params.getAll(key).filter((value) => value !== "");
|
|
65
|
+
const severity = readSeverities(values("severity"));
|
|
66
|
+
if (!severity.ok) {
|
|
67
|
+
return severity;
|
|
68
|
+
}
|
|
69
|
+
const rule = readRules(values("rule"), source.ruleIds);
|
|
70
|
+
if (!rule.ok) {
|
|
71
|
+
return rule;
|
|
72
|
+
}
|
|
73
|
+
const flag = readFlags(values("flag"));
|
|
74
|
+
if (!flag.ok) {
|
|
75
|
+
return flag;
|
|
76
|
+
}
|
|
77
|
+
const folder = values("folder")[0] ?? "";
|
|
78
|
+
if (folder !== "" && !source.folders.includes(folder)) {
|
|
79
|
+
return {
|
|
80
|
+
ok: false,
|
|
81
|
+
message: `No folder "${folder}" holds corpus documents.`,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
const order = readSort(values("sort")[0], values("dir")[0]);
|
|
85
|
+
if (!order.ok) {
|
|
86
|
+
return order;
|
|
87
|
+
}
|
|
88
|
+
const changed = readChanged(values("changed")[0], source.changes);
|
|
89
|
+
if (!changed.ok) {
|
|
90
|
+
return changed;
|
|
91
|
+
}
|
|
92
|
+
const query = {
|
|
93
|
+
severity: severity.value,
|
|
94
|
+
rule: rule.value,
|
|
95
|
+
folder,
|
|
96
|
+
path: values("path")[0] ?? "",
|
|
97
|
+
flag: flag.value,
|
|
98
|
+
...order.value,
|
|
99
|
+
...(changed.value ? { changed: true } : {}),
|
|
100
|
+
};
|
|
101
|
+
const bounds = readBounds(values);
|
|
102
|
+
if (!bounds.ok) {
|
|
103
|
+
return bounds;
|
|
104
|
+
}
|
|
105
|
+
return { ok: true, query: { ...query, ...bounds.value } };
|
|
106
|
+
}
|
|
107
|
+
// In the order `SEVERITIES` lists them, whatever order the address gave.
|
|
108
|
+
function readSeverities(values) {
|
|
109
|
+
const severity = new Set();
|
|
110
|
+
for (const value of values) {
|
|
111
|
+
if (!SEVERITIES.includes(value)) {
|
|
112
|
+
return {
|
|
113
|
+
ok: false,
|
|
114
|
+
message: `Unknown severity "${value}": use error or warning.`,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
severity.add(value);
|
|
118
|
+
}
|
|
119
|
+
return { ok: true, value: SEVERITIES.filter((value) => severity.has(value)) };
|
|
120
|
+
}
|
|
121
|
+
function readRules(values, ruleIds) {
|
|
122
|
+
const rule = new Set();
|
|
123
|
+
for (const value of values) {
|
|
124
|
+
// A built-in rule may be named by its alias, as in the config; it filters by its canonical id.
|
|
125
|
+
const id = ruleRegistry.has(value) ? ruleRegistry.resolveId(value) : value;
|
|
126
|
+
if (!ruleIds.includes(id) && !ruleRegistry.has(id)) {
|
|
127
|
+
return { ok: false, message: `Unknown rule "${value}".` };
|
|
128
|
+
}
|
|
129
|
+
rule.add(id);
|
|
130
|
+
}
|
|
131
|
+
return { ok: true, value: [...rule].sort(compareStrings) };
|
|
132
|
+
}
|
|
133
|
+
// In the order `LINK_FLAGS` lists them, whatever order the address gave.
|
|
134
|
+
function readFlags(values) {
|
|
135
|
+
const flag = new Set();
|
|
136
|
+
for (const value of values) {
|
|
137
|
+
if (!LINK_FLAGS.includes(value)) {
|
|
138
|
+
return {
|
|
139
|
+
ok: false,
|
|
140
|
+
message: `Unknown link filter "${value}": use ${LINK_FLAGS.join(", ")}.`,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
flag.add(value);
|
|
144
|
+
}
|
|
145
|
+
return { ok: true, value: LINK_FLAGS.filter((value) => flag.has(value)) };
|
|
146
|
+
}
|
|
147
|
+
// The sort column and direction; a direction left out is the column's own first one.
|
|
148
|
+
function readSort(sortParam, dirValue) {
|
|
149
|
+
const sortValue = sortParam ?? DEFAULT_SORT;
|
|
150
|
+
if (!SORT_KEYS.includes(sortValue)) {
|
|
151
|
+
return {
|
|
152
|
+
ok: false,
|
|
153
|
+
message: `Unknown sort column "${sortValue}": use ${SORT_KEYS.join(", ")}.`,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
const sort = sortValue;
|
|
157
|
+
const dir = dirValue ?? defaultDirection(sort);
|
|
158
|
+
if (dir !== "asc" && dir !== "desc") {
|
|
159
|
+
return {
|
|
160
|
+
ok: false,
|
|
161
|
+
message: `Unknown sort direction "${dir}": use asc or desc.`,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
return { ok: true, value: { sort, dir } };
|
|
165
|
+
}
|
|
166
|
+
function readChanged(changed, changes) {
|
|
167
|
+
if (changed !== undefined && changed !== "1") {
|
|
168
|
+
return {
|
|
169
|
+
ok: false,
|
|
170
|
+
message: `"changed" takes 1, got "${changed}".`,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
if (changed !== undefined && changes === undefined) {
|
|
174
|
+
return {
|
|
175
|
+
ok: false,
|
|
176
|
+
message: "There is no branch to compare: the Changed filter needs a git work tree and a base branch.",
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
return { ok: true, value: changed !== undefined };
|
|
180
|
+
}
|
|
181
|
+
function readBounds(values) {
|
|
182
|
+
const bounds = {};
|
|
183
|
+
for (const key of NUMERIC_PARAMS) {
|
|
184
|
+
const value = values(key)[0];
|
|
185
|
+
if (value === undefined) {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
// Digits with an optional fraction only: `Number` would also take "", " 2 ", "1e3" and "0x10",
|
|
189
|
+
// and a negative bound is a typo rather than a filter.
|
|
190
|
+
if (!/^\d+(\.\d+)?$/.test(value.trim())) {
|
|
191
|
+
return {
|
|
192
|
+
ok: false,
|
|
193
|
+
message: `"${key}" must be a number of zero or more, got "${value}".`,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
bounds[key] = Number(value);
|
|
197
|
+
}
|
|
198
|
+
return { ok: true, value: bounds };
|
|
199
|
+
}
|
|
200
|
+
/** The query as URL parameters, in a fixed order and without the defaults, for links to share. */
|
|
201
|
+
export function dashboardSearchParams(query) {
|
|
202
|
+
const params = new URLSearchParams();
|
|
203
|
+
for (const value of query.severity) {
|
|
204
|
+
params.append("severity", value);
|
|
205
|
+
}
|
|
206
|
+
for (const value of query.rule) {
|
|
207
|
+
params.append("rule", value);
|
|
208
|
+
}
|
|
209
|
+
for (const value of query.flag) {
|
|
210
|
+
params.append("flag", value);
|
|
211
|
+
}
|
|
212
|
+
if (query.folder !== "") {
|
|
213
|
+
params.set("folder", query.folder);
|
|
214
|
+
}
|
|
215
|
+
if (query.path !== "") {
|
|
216
|
+
params.set("path", query.path);
|
|
217
|
+
}
|
|
218
|
+
if (query.changed === true) {
|
|
219
|
+
params.set("changed", "1");
|
|
220
|
+
}
|
|
221
|
+
for (const key of NUMERIC_PARAMS) {
|
|
222
|
+
const value = query[key];
|
|
223
|
+
if (value !== undefined) {
|
|
224
|
+
params.set(key, String(value));
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (query.sort !== DEFAULT_SORT ||
|
|
228
|
+
query.dir !== defaultDirection(query.sort)) {
|
|
229
|
+
params.set("sort", query.sort);
|
|
230
|
+
params.set("dir", query.dir);
|
|
231
|
+
}
|
|
232
|
+
return params;
|
|
233
|
+
}
|
|
234
|
+
export function dashboardHref(query) {
|
|
235
|
+
const search = dashboardSearchParams(query).toString();
|
|
236
|
+
return search === "" ? "/dashboard" : `/dashboard?${search}`;
|
|
237
|
+
}
|
|
238
|
+
// The graph and each document object are replaced, never mutated, when the corpus changes, so these
|
|
239
|
+
// caches are invalidated by identity alone. The page script asks for the dashboard on every
|
|
240
|
+
// keystroke, and measuring six hundred documents each time would be the whole cost of a request.
|
|
241
|
+
const metricsCache = new WeakMap();
|
|
242
|
+
const graphCache = new WeakMap();
|
|
243
|
+
function metricsOf(document) {
|
|
244
|
+
let metrics = metricsCache.get(document);
|
|
245
|
+
if (metrics === undefined) {
|
|
246
|
+
metrics = measureDocument(document.content);
|
|
247
|
+
metricsCache.set(document, metrics);
|
|
248
|
+
}
|
|
249
|
+
return metrics;
|
|
250
|
+
}
|
|
251
|
+
function graphFacts(graph) {
|
|
252
|
+
let facts = graphCache.get(graph);
|
|
253
|
+
if (facts !== undefined) {
|
|
254
|
+
return facts;
|
|
255
|
+
}
|
|
256
|
+
const sources = new Map();
|
|
257
|
+
const targets = new Map();
|
|
258
|
+
for (const edge of graph.edges) {
|
|
259
|
+
let from = sources.get(edge.to);
|
|
260
|
+
if (from === undefined) {
|
|
261
|
+
sources.set(edge.to, (from = new Set()));
|
|
262
|
+
}
|
|
263
|
+
from.add(edge.from);
|
|
264
|
+
let to = targets.get(edge.from);
|
|
265
|
+
if (to === undefined) {
|
|
266
|
+
targets.set(edge.from, (to = new Set()));
|
|
267
|
+
}
|
|
268
|
+
to.add(edge.to);
|
|
269
|
+
}
|
|
270
|
+
// `getComponents` lists the largest group first, ties broken by path, so "the main group" is one
|
|
271
|
+
// well-defined set even when two groups are the same size.
|
|
272
|
+
const [main = []] = getComponents(graph);
|
|
273
|
+
facts = {
|
|
274
|
+
linkedFrom: new Map([...sources].map(([path, set]) => [path, set.size])),
|
|
275
|
+
linksTo: new Map([...targets].map(([path, set]) => [path, set.size])),
|
|
276
|
+
// The cycles core reports, the ones GRP-001 judges, so a document flagged here is one the rule
|
|
277
|
+
// would name.
|
|
278
|
+
onCycle: new Set(graph.cycles.flat()),
|
|
279
|
+
mainComponent: new Set(main),
|
|
280
|
+
};
|
|
281
|
+
graphCache.set(graph, facts);
|
|
282
|
+
return facts;
|
|
283
|
+
}
|
|
284
|
+
export function buildDashboard(source, query) {
|
|
285
|
+
const facts = graphFacts(source.graph);
|
|
286
|
+
const ready = source.findings !== undefined;
|
|
287
|
+
const filter = rowFilter(query, ready);
|
|
288
|
+
const rows = [...source.documents.values()]
|
|
289
|
+
.map((document) => candidateOf(document, source, facts))
|
|
290
|
+
.filter(filter)
|
|
291
|
+
.map(({ row }) => row);
|
|
292
|
+
const direction = query.dir === "asc" ? 1 : -1;
|
|
293
|
+
rows.sort((left, right) => direction * compareBy(query.sort, left, right) ||
|
|
294
|
+
compareStrings(left.path, right.path));
|
|
295
|
+
return {
|
|
296
|
+
findings: ready ? "ready" : "pending",
|
|
297
|
+
corpusSize: source.documents.size,
|
|
298
|
+
totals: totalsOf(rows),
|
|
299
|
+
rows,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
// One document's row, with the findings it was counted from for the finding filters to judge.
|
|
303
|
+
function candidateOf(document, source, facts) {
|
|
304
|
+
const ready = source.findings !== undefined;
|
|
305
|
+
const messages = source.findings?.get(document.path) ?? [];
|
|
306
|
+
const metrics = metricsOf(document);
|
|
307
|
+
const linkedFrom = facts.linkedFrom.get(document.path) ?? 0;
|
|
308
|
+
const linksTo = facts.linksTo.get(document.path) ?? 0;
|
|
309
|
+
const errors = messages.filter((message) => message.severity === "error").length;
|
|
310
|
+
return {
|
|
311
|
+
messages,
|
|
312
|
+
row: {
|
|
313
|
+
path: document.path,
|
|
314
|
+
errors: ready ? errors : null,
|
|
315
|
+
warnings: ready ? messages.length - errors : null,
|
|
316
|
+
rules: [...new Set(messages.map((message) => message.ruleId))].sort(compareStrings),
|
|
317
|
+
bytes: metrics.bytes,
|
|
318
|
+
lines: metrics.lines,
|
|
319
|
+
tokens: metrics.tokens,
|
|
320
|
+
linkedFrom,
|
|
321
|
+
linksTo,
|
|
322
|
+
flags: LINK_FLAGS.filter((flag) => hasFlag(flag, { path: document.path, linkedFrom, linksTo }, facts)),
|
|
323
|
+
...(source.changes === undefined
|
|
324
|
+
? {}
|
|
325
|
+
: { change: source.changes.get(document.path) ?? null }),
|
|
326
|
+
},
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
function hasFlag(flag, links, facts) {
|
|
330
|
+
switch (flag) {
|
|
331
|
+
case "orphan":
|
|
332
|
+
return links.linkedFrom === 0;
|
|
333
|
+
case "dead-end":
|
|
334
|
+
return links.linksTo === 0;
|
|
335
|
+
case "cycle":
|
|
336
|
+
return facts.onCycle.has(links.path);
|
|
337
|
+
case "detached":
|
|
338
|
+
return !facts.mainComponent.has(links.path);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
// Whether a document's row is in the selection the query asks for.
|
|
342
|
+
function rowFilter(query, ready) {
|
|
343
|
+
const pathNeedle = query.path.toLowerCase();
|
|
344
|
+
const folderPrefix = query.folder === "" ? "" : `${query.folder}/`;
|
|
345
|
+
const filterFindings = ready && (query.severity.length > 0 || query.rule.length > 0);
|
|
346
|
+
return ({ row, messages }) => {
|
|
347
|
+
if (!row.path.startsWith(folderPrefix)) {
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
if (query.changed === true && row.change?.status === undefined) {
|
|
351
|
+
return false;
|
|
352
|
+
}
|
|
353
|
+
if (pathNeedle !== "" && !row.path.toLowerCase().includes(pathNeedle)) {
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
if (filterFindings &&
|
|
357
|
+
!messages.some((message) => matchesFinding(query, message))) {
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
if (query.flag.length > 0 &&
|
|
361
|
+
!query.flag.some((flag) => row.flags.includes(flag))) {
|
|
362
|
+
return false;
|
|
363
|
+
}
|
|
364
|
+
// Both bounds inclusive; a kilobyte is 1024 bytes, the unit SIZE-001's byte budgets divide into.
|
|
365
|
+
return (within(row.bytes, query.minKb, query.maxKb, 1024) &&
|
|
366
|
+
within(row.lines, query.minLines, query.maxLines, 1) &&
|
|
367
|
+
within(row.tokens, query.minTokens, query.maxTokens, 1));
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
// Severity and rule are judged on one finding together: "errors" with REF-001 is a document holding
|
|
371
|
+
// an error from REF-001, not one with an error from anything and a REF-001 warning.
|
|
372
|
+
function matchesFinding(query, message) {
|
|
373
|
+
return ((query.severity.length === 0 ||
|
|
374
|
+
query.severity.includes(message.severity)) &&
|
|
375
|
+
(query.rule.length === 0 || query.rule.includes(message.ruleId)));
|
|
376
|
+
}
|
|
377
|
+
function totalsOf(rows) {
|
|
378
|
+
const totals = {
|
|
379
|
+
documents: rows.length,
|
|
380
|
+
errors: 0,
|
|
381
|
+
warnings: 0,
|
|
382
|
+
bytes: 0,
|
|
383
|
+
lines: 0,
|
|
384
|
+
tokens: 0,
|
|
385
|
+
};
|
|
386
|
+
for (const row of rows) {
|
|
387
|
+
totals.errors += row.errors ?? 0;
|
|
388
|
+
totals.warnings += row.warnings ?? 0;
|
|
389
|
+
totals.bytes += row.bytes;
|
|
390
|
+
totals.lines += row.lines;
|
|
391
|
+
totals.tokens += row.tokens;
|
|
392
|
+
}
|
|
393
|
+
return totals;
|
|
394
|
+
}
|
|
395
|
+
function within(value, min, max, unit) {
|
|
396
|
+
return ((min === undefined || value >= min * unit) &&
|
|
397
|
+
(max === undefined || value <= max * unit));
|
|
398
|
+
}
|
|
399
|
+
function compareBy(sort, left, right) {
|
|
400
|
+
switch (sort) {
|
|
401
|
+
case "path":
|
|
402
|
+
return compareStrings(left.path, right.path);
|
|
403
|
+
case "rules":
|
|
404
|
+
return compareStrings(left.rules.join(" "), right.rules.join(" "));
|
|
405
|
+
case "flags":
|
|
406
|
+
return compareStrings(left.flags.join(" "), right.flags.join(" "));
|
|
407
|
+
case "errors":
|
|
408
|
+
case "warnings":
|
|
409
|
+
return (left[sort] ?? 0) - (right[sort] ?? 0);
|
|
410
|
+
default:
|
|
411
|
+
return left[sort] - right[sort];
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
const CSV_COLUMNS = [
|
|
415
|
+
"path",
|
|
416
|
+
"errors",
|
|
417
|
+
"warnings",
|
|
418
|
+
"rules",
|
|
419
|
+
"bytes",
|
|
420
|
+
"lines",
|
|
421
|
+
"tokens",
|
|
422
|
+
"linkedFrom",
|
|
423
|
+
"linksTo",
|
|
424
|
+
"flags",
|
|
425
|
+
];
|
|
426
|
+
const BRANCH_CSV_COLUMNS = ["change", "findingsAdded", "findingsRemoved"];
|
|
427
|
+
/**
|
|
428
|
+
* The selection as CSV: RFC 4180 quoting and CRLF rows, sizes in bytes as measured.
|
|
429
|
+
*
|
|
430
|
+
* A cell a spreadsheet would read as a formula — one starting with `=`, `+`, `-` or `@`, or with a
|
|
431
|
+
* tab or carriage return that some of them skip first — gets a leading apostrophe. A file name is
|
|
432
|
+
* the repository's to choose, and opening an export must not run what someone named a file.
|
|
433
|
+
*/
|
|
434
|
+
export function dashboardCsv(result) {
|
|
435
|
+
// The branch's columns only in a review of one, where every row carries its change.
|
|
436
|
+
const branch = result.rows.some((row) => row.change !== undefined);
|
|
437
|
+
const lines = [
|
|
438
|
+
[...CSV_COLUMNS, ...(branch ? BRANCH_CSV_COLUMNS : [])].join(","),
|
|
439
|
+
];
|
|
440
|
+
for (const row of result.rows) {
|
|
441
|
+
lines.push([
|
|
442
|
+
csvText(row.path),
|
|
443
|
+
row.errors ?? "",
|
|
444
|
+
row.warnings ?? "",
|
|
445
|
+
csvText(row.rules.join(" ")),
|
|
446
|
+
row.bytes,
|
|
447
|
+
row.lines,
|
|
448
|
+
row.tokens,
|
|
449
|
+
row.linkedFrom,
|
|
450
|
+
row.linksTo,
|
|
451
|
+
csvText(row.flags.join(" ")),
|
|
452
|
+
...(branch
|
|
453
|
+
? [
|
|
454
|
+
row.change?.status ?? "",
|
|
455
|
+
row.change?.findingsAdded ?? 0,
|
|
456
|
+
row.change?.findingsRemoved ?? 0,
|
|
457
|
+
]
|
|
458
|
+
: []),
|
|
459
|
+
].join(","));
|
|
460
|
+
}
|
|
461
|
+
return `${lines.join("\r\n")}\r\n`;
|
|
462
|
+
}
|
|
463
|
+
function csvText(value) {
|
|
464
|
+
const guarded = /^[=+\-@\t\r]/.test(value) ? `'${value}` : value;
|
|
465
|
+
return /[",\r\n]/.test(guarded)
|
|
466
|
+
? `"${guarded.replace(/"/g, '""')}"`
|
|
467
|
+
: guarded;
|
|
468
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { LintMessage, LintResult, ParsedDocument, Severity } from "@wastech-mdlint/core";
|
|
2
|
+
export type Finding = {
|
|
3
|
+
ruleId: string;
|
|
4
|
+
severity: Severity;
|
|
5
|
+
message: string;
|
|
6
|
+
line: number;
|
|
7
|
+
anchor?: string;
|
|
8
|
+
helpUri?: string;
|
|
9
|
+
};
|
|
10
|
+
export type FileCounts = {
|
|
11
|
+
errors: number;
|
|
12
|
+
warnings: number;
|
|
13
|
+
};
|
|
14
|
+
export declare class Diagnostics {
|
|
15
|
+
private byFile;
|
|
16
|
+
get ready(): boolean;
|
|
17
|
+
/** Every finding, grouped by file; `undefined` until the first lint finishes. */
|
|
18
|
+
get messagesByFile(): ReadonlyMap<string, readonly LintMessage[]> | undefined;
|
|
19
|
+
update(result: LintResult): void;
|
|
20
|
+
/** Counts per file, for files with at least one finding. */
|
|
21
|
+
summary(): Record<string, FileCounts>;
|
|
22
|
+
findings(document: ParsedDocument): Finding[];
|
|
23
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { headingAbove } from "./render-document.js";
|
|
2
|
+
export class Diagnostics {
|
|
3
|
+
// Undefined until the first lint finishes, so a page can tell "not checked yet" from "clean".
|
|
4
|
+
byFile;
|
|
5
|
+
get ready() {
|
|
6
|
+
return this.byFile !== undefined;
|
|
7
|
+
}
|
|
8
|
+
/** Every finding, grouped by file; `undefined` until the first lint finishes. */
|
|
9
|
+
get messagesByFile() {
|
|
10
|
+
return this.byFile;
|
|
11
|
+
}
|
|
12
|
+
update(result) {
|
|
13
|
+
const byFile = new Map();
|
|
14
|
+
for (const message of result.messages) {
|
|
15
|
+
const list = byFile.get(message.filePath);
|
|
16
|
+
if (list === undefined) {
|
|
17
|
+
byFile.set(message.filePath, [message]);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
list.push(message);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
this.byFile = byFile;
|
|
24
|
+
}
|
|
25
|
+
/** Counts per file, for files with at least one finding. */
|
|
26
|
+
summary() {
|
|
27
|
+
const counts = {};
|
|
28
|
+
for (const [filePath, messages] of this.byFile ?? []) {
|
|
29
|
+
const errors = messages.filter((message) => message.severity === "error").length;
|
|
30
|
+
counts[filePath] = { errors, warnings: messages.length - errors };
|
|
31
|
+
}
|
|
32
|
+
return counts;
|
|
33
|
+
}
|
|
34
|
+
findings(document) {
|
|
35
|
+
return (this.byFile?.get(document.path) ?? []).map((message) => ({
|
|
36
|
+
ruleId: message.ruleId,
|
|
37
|
+
severity: message.severity,
|
|
38
|
+
message: message.message,
|
|
39
|
+
line: message.line,
|
|
40
|
+
anchor: headingAbove(document, message.line)?.slug,
|
|
41
|
+
helpUri: message.helpUri,
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import { type DiffView } from "./change-banner.js";
|
|
3
|
+
import { type Baseline } from "./review-state.js";
|
|
4
|
+
import { type ViewerContext } from "./viewer-context.js";
|
|
5
|
+
/** A document page as its address asks for it: the path and the views it is shown in. */
|
|
6
|
+
export type DocumentRequest = {
|
|
7
|
+
relPath: string | undefined;
|
|
8
|
+
view: DiffView;
|
|
9
|
+
baseline: Baseline;
|
|
10
|
+
blame: boolean;
|
|
11
|
+
};
|
|
12
|
+
export declare class DocumentRoutes {
|
|
13
|
+
private readonly ctx;
|
|
14
|
+
private readonly marked;
|
|
15
|
+
constructor(ctx: ViewerContext);
|
|
16
|
+
private renderMarked;
|
|
17
|
+
private renderPlain;
|
|
18
|
+
/**
|
|
19
|
+
* The review of a document page's own changes, or `undefined` when it has none to show. Always the
|
|
20
|
+
* work-tree review: the page renders the file on disk, and only that review's line numbers are its
|
|
21
|
+
* lines. Its head text is compared with the text being rendered, so a page requested between a
|
|
22
|
+
* save reaching the corpus and the review being rebuilt is never marked from the other text.
|
|
23
|
+
*/
|
|
24
|
+
private reviewOf;
|
|
25
|
+
/**
|
|
26
|
+
* What the line above a changed document says about the reader's review of it, and the review
|
|
27
|
+
* since their last one when the page compares with that; `undefined` where marks are off or
|
|
28
|
+
* cannot be read, which the Changes page explains.
|
|
29
|
+
*/
|
|
30
|
+
private bannerReviewOf;
|
|
31
|
+
private blameOf;
|
|
32
|
+
sendDocument(request: IncomingMessage, response: ServerResponse, asked: DocumentRequest): Promise<void>;
|
|
33
|
+
private commonParams;
|
|
34
|
+
private documentBody;
|
|
35
|
+
private withReviewParts;
|
|
36
|
+
sendHistory(response: ServerResponse, params: URLSearchParams): Promise<void>;
|
|
37
|
+
sendLinkedFile(request: IncomingMessage, response: ServerResponse, relPath: string | undefined): Promise<void>;
|
|
38
|
+
redirectToIndex(response: ServerResponse): Promise<void>;
|
|
39
|
+
}
|