@cplieger/deadset-ts 8.0.0-dev.7 → 8.0.0-dev.8

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 (78) hide show
  1. package/dist/bin/node-host.js +21 -3
  2. package/dist/src/analysis.js +13 -14
  3. package/dist/src/config.js +6 -83
  4. package/dist/src/configured-declarations.js +15 -20
  5. package/dist/src/consumers.js +2 -2
  6. package/dist/src/contract/config.generated.js +544 -0
  7. package/dist/src/contract/report.generated.js +3 -2
  8. package/dist/src/discover.js +1 -1
  9. package/dist/src/edges.js +36 -112
  10. package/dist/src/exempt-classes.js +0 -2
  11. package/dist/src/exempt.js +1 -1
  12. package/dist/src/findings/completion.js +5 -4
  13. package/dist/src/findings/emitters.js +1 -1
  14. package/dist/src/findings/intra-function.js +1 -1
  15. package/dist/src/findings/non-code-artifacts.js +2 -2
  16. package/dist/src/findings/unused-declarations.js +1 -1
  17. package/dist/src/findings/visibility-narrowing.js +2 -2
  18. package/dist/src/findings-pass.js +1 -1
  19. package/dist/src/framework-lifecycle.js +7 -7
  20. package/dist/src/fs/document.js +59 -0
  21. package/dist/src/injection-container.js +1 -1
  22. package/dist/src/json-document.js +194 -67
  23. package/dist/src/kinds.js +0 -7
  24. package/dist/src/print.js +10 -79
  25. package/dist/src/report-schema.js +3 -224
  26. package/dist/src/resolve.js +245 -715
  27. package/dist/src/run.js +48 -55
  28. package/dist/src/schema.js +320 -344
  29. package/dist/src/scope.js +33 -101
  30. package/dist/src/serialization-contract.js +1 -1
  31. package/dist/src/suppress-file.js +15 -17
  32. package/dist/src/suppress.js +3 -3
  33. package/dist/src/template-field.js +4 -4
  34. package/dist/src/usage.js +10 -0
  35. package/dist/src/verbs/analyze.js +12 -15
  36. package/dist/src/verbs/explain.js +3 -3
  37. package/dist/src/verbs/print-projects.js +2 -2
  38. package/jsr.json +1 -1
  39. package/package.json +1 -1
  40. package/src/analysis.ts +30 -26
  41. package/src/config.ts +31 -298
  42. package/src/configured-declarations.ts +18 -23
  43. package/src/consumers.ts +4 -4
  44. package/src/contract/config.generated.ts +798 -0
  45. package/src/contract/report.generated.ts +3 -2
  46. package/src/discover.ts +1 -1
  47. package/src/edges.ts +39 -124
  48. package/src/exempt-classes.ts +0 -5
  49. package/src/exempt.ts +4 -4
  50. package/src/findings/completion.ts +5 -6
  51. package/src/findings/emitter.ts +2 -2
  52. package/src/findings/emitters.ts +1 -1
  53. package/src/findings/intra-function.ts +1 -1
  54. package/src/findings/non-code-artifacts.ts +4 -4
  55. package/src/findings/unused-declarations.ts +1 -1
  56. package/src/findings/visibility-narrowing.ts +2 -2
  57. package/src/findings-pass.ts +3 -3
  58. package/src/framework-lifecycle.ts +7 -7
  59. package/src/fs/document.ts +74 -0
  60. package/src/host.ts +12 -5
  61. package/src/injection-container.ts +1 -1
  62. package/src/json-document.ts +236 -76
  63. package/src/kinds.ts +0 -8
  64. package/src/print.ts +11 -92
  65. package/src/report-schema.ts +3 -299
  66. package/src/resolve.ts +281 -1105
  67. package/src/run.ts +50 -63
  68. package/src/schema.ts +391 -389
  69. package/src/scope.ts +39 -119
  70. package/src/serialization-contract.ts +1 -1
  71. package/src/suppress-file.ts +15 -20
  72. package/src/suppress.ts +3 -3
  73. package/src/template-field.ts +10 -7
  74. package/src/usage.ts +10 -0
  75. package/src/verbs/analyze.ts +12 -23
  76. package/src/verbs/explain.ts +3 -7
  77. package/src/verbs/print-projects.ts +2 -2
  78. package/src/verbs/verb.ts +2 -2
@@ -1,122 +1,276 @@
1
+ import type { Position } from "./position.ts";
2
+
3
+ /** A place inside a document: each member name a string, each array index a number. */
4
+ export type Place = readonly (string | number)[];
5
+
1
6
  /**
2
- * A read of a strict JSON document beside its parse: what a parse into a value cannot
3
- * report, the offset of each record of one array and a member written twice.
7
+ * A text that is not one strict JSON document: a syntax error, data after the value, a
8
+ * member one object writes twice, or nesting past {@link MAX_DEPTH}. `place` is the
9
+ * value the fault is in, followed by the member written twice.
4
10
  */
11
+ export class StrictJSONError extends Error {
12
+ readonly offset: number;
13
+ readonly place: Place;
5
14
 
6
- import type { Position } from "./position.ts";
15
+ constructor(reason: string, offset: number, place: Place) {
16
+ super(reason);
17
+ this.name = "StrictJSONError";
18
+ this.offset = offset;
19
+ this.place = place;
20
+ }
21
+ }
7
22
 
8
- /** The token walk over a document `JSON.parse` accepted, so every token is well formed. */
9
- class Walk {
10
- readonly starts: number[] = [];
11
- /** The member written twice, where one is, as the offset of its name. */
12
- twice: { readonly name: string; readonly offset: number } | undefined;
13
- private readonly text: string;
14
- private readonly array: string;
23
+ /** One strict JSON document, read whole. */
24
+ interface StrictDocument {
25
+ readonly value: unknown;
26
+ /**
27
+ * The text of every number written with a fraction or an exponent, by its JSON
28
+ * Pointer: a value reads `100.0` as the integer 100, so only the text tells them apart.
29
+ */
30
+ readonly fractions: ReadonlyMap<string, string>;
31
+ /**
32
+ * The offset of each value inside the top-level member a read names, by its index or
33
+ * its member name, in document order.
34
+ */
35
+ readonly starts: ReadonlyMap<string | number, number>;
36
+ }
37
+
38
+ /**
39
+ * How deep values may nest. A document this analyzer reads nests a few levels, and a
40
+ * bound turns a document built to exhaust the stack into a refusal.
41
+ */
42
+ const MAX_DEPTH = 1000;
43
+
44
+ const SPACE = /[ \t\n\r]*/uy;
45
+ const STRING =
46
+ /"(?:[ !#$%&'()*+,\-./0-9:;<=>?@A-Z[\]-\u{10FFFF}]|\\(?:["\\/bfnrt]|u[0-9A-Fa-f]{4}))*"/uy;
47
+ const NUMBER = /-?(?:0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?/uy;
48
+ const LITERALS: ReadonlyMap<string, unknown> = new Map<string, unknown>([
49
+ ["true", true],
50
+ ["false", false],
51
+ ["null", null],
52
+ ]);
53
+
54
+ /** One member name spelled as a JSON Pointer token. */
55
+ const escaped = (name: string): string => name.replaceAll("~", "~0").replaceAll("/", "~1");
56
+
57
+ /** The member names and indexes a JSON Pointer holds, unescaped. */
58
+ export function pointerSegments(pointer: string): string[] {
59
+ return pointer === ""
60
+ ? []
61
+ : pointer
62
+ .slice(1)
63
+ .split("/")
64
+ .map((token) => token.replaceAll("~1", "/").replaceAll("~0", "~"));
65
+ }
66
+
67
+ /** The JSON Pointer of a place written as segments. */
68
+ export function pointerOf(segments: readonly string[]): string {
69
+ return segments.map((segment) => `/${escaped(segment)}`).join("");
70
+ }
71
+
72
+ /**
73
+ * The place a JSON Pointer names inside `value`, followed by `member` where it is not
74
+ * empty: a pointer's text does not say whether a segment of digits is an index, so the
75
+ * value it points into does.
76
+ */
77
+ export function placeIn(value: unknown, pointer: string, member = ""): (string | number)[] {
78
+ const place: (string | number)[] = [];
79
+ let held = value;
80
+ for (const segment of pointerSegments(pointer)) {
81
+ if (Array.isArray(held)) {
82
+ place.push(Number(segment));
83
+ held = held[Number(segment)];
84
+ } else {
85
+ place.push(segment);
86
+ held = isRecord(held) ? held[segment] : undefined;
87
+ }
88
+ }
89
+ return member === "" ? place : [...place, member];
90
+ }
91
+
92
+ /**
93
+ * A place as a refusal names it: member names joined by full stops and an index in
94
+ * square brackets, as `ts.serializers[0].returns`.
95
+ */
96
+ export function dotted(place: Place): string {
97
+ let path = "";
98
+ for (const segment of place) {
99
+ path +=
100
+ typeof segment === "number" ? `[${String(segment)}]` : path === "" ? segment : `.${segment}`;
101
+ }
102
+ return path;
103
+ }
104
+
105
+ class Reader {
15
106
  private at = 0;
107
+ private readonly path: (string | number)[] = [];
108
+ readonly fractions = new Map<string, string>();
109
+ readonly starts = new Map<string | number, number>();
110
+ private readonly text: string;
111
+ private readonly records: string | undefined;
16
112
 
17
- constructor(text: string, array: string) {
113
+ constructor(text: string, records: string | undefined) {
18
114
  this.text = text;
19
- this.array = array;
115
+ this.records = records;
20
116
  }
21
117
 
22
- run(): void {
23
- this.value("document");
118
+ read(): unknown {
119
+ const value = this.value(0);
120
+ this.match(SPACE);
121
+ if (this.at < this.text.length) {
122
+ this.fail("data follows the document's value");
123
+ }
124
+ return value;
24
125
  }
25
126
 
26
- private skip(): void {
27
- while (/[ \t\r\n]/u.test(this.text[this.at] ?? "")) {
28
- this.at += 1;
29
- }
127
+ private fail(reason: string, offset = this.at, member = ""): never {
128
+ throw new StrictJSONError(
129
+ reason,
130
+ offset,
131
+ member === "" ? [...this.path] : [...this.path, member],
132
+ );
30
133
  }
31
134
 
32
- private string(): string {
33
- const start = this.at;
34
- this.at += 1;
35
- while (this.text[this.at] !== '"') {
36
- this.at += this.text[this.at] === "\\" ? 2 : 1;
135
+ private match(pattern: RegExp): string | undefined {
136
+ pattern.lastIndex = this.at;
137
+ const found = pattern.exec(this.text)?.[0];
138
+ this.at = found === undefined ? this.at : pattern.lastIndex;
139
+ return found;
140
+ }
141
+
142
+ private expect(char: string, what: string): void {
143
+ this.match(SPACE);
144
+ if (this.text[this.at] !== char) {
145
+ this.fail(`${what} is expected`);
37
146
  }
38
147
  this.at += 1;
39
- return JSON.parse(this.text.slice(start, this.at)) as string;
40
148
  }
41
149
 
42
- private value(role: "document" | "array" | "record" | "other"): void {
43
- this.skip();
44
- if (role === "record") {
45
- this.starts.push(this.at);
150
+ private value(depth: number): unknown {
151
+ this.match(SPACE);
152
+ if (depth > MAX_DEPTH) {
153
+ this.fail(`values nest deeper than ${String(MAX_DEPTH)} levels`);
46
154
  }
47
155
  const char = this.text[this.at];
48
156
  if (char === "{") {
49
- this.object(role);
50
- } else if (char === "[") {
51
- this.list(role === "array" ? "record" : "other");
52
- } else if (char === '"') {
53
- this.string();
54
- } else {
55
- while (this.at < this.text.length && !/[,\]} \t\r\n]/u.test(this.text[this.at] ?? "")) {
56
- this.at += 1;
157
+ return this.object(depth);
158
+ }
159
+ if (char === "[") {
160
+ return this.array(depth);
161
+ }
162
+ const string = this.match(STRING);
163
+ if (string !== undefined) {
164
+ return JSON.parse(string) as string;
165
+ }
166
+ NUMBER.lastIndex = this.at;
167
+ const number = NUMBER.exec(this.text);
168
+ if (number !== null) {
169
+ this.at = NUMBER.lastIndex;
170
+ if (number[1] !== undefined || number[2] !== undefined) {
171
+ this.fractions.set(pointerOf(this.path.map(String)), number[0]);
172
+ }
173
+ return Number(number[0]);
174
+ }
175
+ for (const [literal, held] of LITERALS) {
176
+ if (this.text.startsWith(literal, this.at)) {
177
+ this.at += literal.length;
178
+ return held;
57
179
  }
58
180
  }
181
+ return this.fail(
182
+ char === undefined ? "the document ends where a value is expected" : "a value is expected",
183
+ );
59
184
  }
60
185
 
61
- private object(role: string): void {
62
- const seen = new Set<string>();
186
+ private object(depth: number): Record<string, unknown> {
187
+ const held: Record<string, unknown> = {};
63
188
  this.at += 1;
189
+ this.match(SPACE);
190
+ if (this.text[this.at] === "}") {
191
+ this.at += 1;
192
+ return held;
193
+ }
64
194
  for (;;) {
65
- this.skip();
66
- if (this.text[this.at] === "}") {
67
- this.at += 1;
68
- return;
195
+ this.match(SPACE);
196
+ const offset = this.at;
197
+ const spelled = this.match(STRING);
198
+ if (spelled === undefined) {
199
+ return this.fail("a member name is expected");
69
200
  }
70
- if (this.text[this.at] === ",") {
71
- this.at += 1;
72
- this.skip();
201
+ const name = JSON.parse(spelled) as string;
202
+ if (Object.hasOwn(held, name)) {
203
+ this.fail(
204
+ `the member ${JSON.stringify(name)} is written twice, so neither value is chosen`,
205
+ offset,
206
+ name,
207
+ );
73
208
  }
74
- const offset = this.at;
75
- const name = this.string();
76
- if (seen.has(name) && this.twice === undefined) {
77
- this.twice = { name, offset };
209
+ this.expect(":", "a colon after the member name");
210
+ if (depth === 1 && this.path[0] === this.records) {
211
+ this.match(SPACE);
212
+ this.starts.set(name, this.at);
78
213
  }
79
- seen.add(name);
80
- this.skip();
214
+ this.path.push(name);
215
+ Object.defineProperty(held, name, {
216
+ value: this.value(depth + 1),
217
+ enumerable: true,
218
+ writable: true,
219
+ configurable: true,
220
+ });
221
+ this.path.pop();
222
+ this.match(SPACE);
223
+ const next = this.text[this.at];
81
224
  this.at += 1;
82
- this.value(role === "document" && name === this.array ? "array" : "other");
225
+ if (next === "}") {
226
+ return held;
227
+ }
228
+ if (next !== ",") {
229
+ this.at -= 1;
230
+ this.fail("a comma or a closing brace is expected");
231
+ }
83
232
  }
84
233
  }
85
234
 
86
- private list(element: "record" | "other"): void {
235
+ private array(depth: number): unknown[] {
236
+ const held: unknown[] = [];
237
+ const records = depth === 1 && this.path[0] === this.records;
87
238
  this.at += 1;
239
+ this.match(SPACE);
240
+ if (this.text[this.at] === "]") {
241
+ this.at += 1;
242
+ return held;
243
+ }
88
244
  for (;;) {
89
- this.skip();
90
- if (this.text[this.at] === "]") {
91
- this.at += 1;
92
- return;
245
+ this.match(SPACE);
246
+ if (records) {
247
+ this.starts.set(held.length, this.at);
248
+ }
249
+ this.path.push(held.length);
250
+ held.push(this.value(depth + 1));
251
+ this.path.pop();
252
+ this.match(SPACE);
253
+ const next = this.text[this.at];
254
+ this.at += 1;
255
+ if (next === "]") {
256
+ return held;
93
257
  }
94
- if (this.text[this.at] === ",") {
95
- this.at += 1;
258
+ if (next !== ",") {
259
+ this.at -= 1;
260
+ this.fail("a comma or a closing bracket is expected");
96
261
  }
97
- this.value(element);
98
262
  }
99
263
  }
100
264
  }
101
265
 
102
- /** What a walk of one document found. */
103
- interface DocumentWalk {
104
- /** The offset of each element of the document's array member, in order: an object's `{`. */
105
- readonly starts: readonly number[];
106
- /** The first member one object writes twice, at any depth, as the offset of its name. */
107
- readonly twice: { readonly name: string; readonly offset: number } | undefined;
108
- }
109
-
110
266
  /**
111
- * Walks a document `JSON.parse` accepted, whose top-level member `array` holds the
112
- * records. A member one object writes twice reads as though it were written once, its
113
- * later value silently replacing the earlier one, so a document holding one is one a
114
- * reader names rather than reads.
267
+ * Reads `text` as one strict JSON document, or throws a {@link StrictJSONError}.
268
+ * `records` names the top-level member whose values' offsets the read keeps.
115
269
  */
116
- export function walkDocument(text: string, array: string): DocumentWalk {
117
- const walk = new Walk(text, array);
118
- walk.run();
119
- return { starts: walk.starts, twice: walk.twice };
270
+ export function readStrict(text: string, records?: string): StrictDocument {
271
+ const reader = new Reader(text, records);
272
+ const value = reader.read();
273
+ return { value, fractions: reader.fractions, starts: reader.starts };
120
274
  }
121
275
 
122
276
  /**
@@ -129,6 +283,12 @@ export function positionAt(text: string, file: string, offset: number): Position
129
283
  return { path: file, line: before.split("\n").length, column: offset - lineStart + 1 };
130
284
  }
131
285
 
286
+ /** A strict read's fault with the line and column it is at, as a refusal states it. */
287
+ export function locatedFault(text: string, error: StrictJSONError): string {
288
+ const { line, column } = positionAt(text, "", error.offset);
289
+ return `line ${String(line)}, column ${String(column)}: ${error.message}`;
290
+ }
291
+
132
292
  /** Whether a decoded JSON value is an object: not `null` and not an array. */
133
293
  export function isRecord(value: unknown): value is Record<string, unknown> {
134
294
  return typeof value === "object" && value !== null && !Array.isArray(value);
package/src/kinds.ts CHANGED
@@ -5,14 +5,6 @@ export const KINDS: ReadonlyMap<string, KindRow> = new Map(
5
5
  KIND_ROWS.map((one): [string, KindRow] => [one.code, one]),
6
6
  );
7
7
 
8
- /**
9
- * The code of every live issue kind, in ascending order. A severity key names one of
10
- * them or a family prefix whose range holds one; a key naming a retired code, an
11
- * unassigned code or a range holding no live kind is an unimplemented key, so every
12
- * severity a configuration sets is one some analyzer reports under.
13
- */
14
- export const LIVE_CODES: readonly string[] = KIND_ROWS.map((one) => one.code);
15
-
16
8
  /** The issue-kind codes whose severity the Contract fixes, in ascending order. */
17
9
  export const FIXED_SEVERITY_CODES: readonly string[] = KIND_ROWS.filter((one) => one.fixed).map(
18
10
  (one) => one.code,
package/src/print.ts CHANGED
@@ -1,97 +1,16 @@
1
- import {
2
- renderOrigin,
3
- type Config,
4
- type DeclarationEntry,
5
- type Provenance,
6
- type Provider,
7
- } from "./config.ts";
1
+ import { renderOrigin, type Provenance, type ResolvedConfig } from "./config.ts";
8
2
  import { bytewise } from "./text/compare.ts";
9
3
 
10
- /** One declaration entry as a document writes it: the members of its shape alone. */
11
- function declarationOf(entry: DeclarationEntry): Record<string, string> {
12
- switch (entry.shape) {
13
- case "symbol":
14
- return { symbol: entry.symbol };
15
- case "module":
16
- return { module: entry.module, name: entry.name };
17
- case "global":
18
- return { global: entry.global };
19
- }
20
- }
21
-
22
- /** One provider entry as a document writes it: the members of its shape alone. */
23
- function providerOf(entry: Provider): Record<string, unknown> {
24
- const installed = { name: entry.name, languages: entry.languages, command: entry.command };
25
- if (entry.shape === "installed") {
26
- return installed;
27
- }
28
- return { ...installed, source: entry.source, version: entry.version, digest: entry.digest };
29
- }
30
-
31
4
  /**
32
- * The resolved configuration and its provenance as one JSON object, indented,
33
- * ending in a newline.
34
- *
35
- * The output is an instance of the closed key list, so reading it back as a
36
- * repository configuration resolves to the same configuration: the provenance
37
- * object is a declared key resolution ignores. Keys are written in the schema's
38
- * order and the severity and provenance objects in ascending key order, so one
39
- * configuration renders to one string whatever order a source named its settings
40
- * in.
5
+ * The resolved configuration and its provenance as one JSON object, indented, ending
6
+ * in a newline: the configuration in the order resolution gives it, then the provenance
7
+ * object in ascending key order. Read back as a repository configuration it resolves to
8
+ * the same configuration, because resolution checks the provenance object and then
9
+ * ignores it.
41
10
  */
42
- export function printConfig(config: Config, provenance: Provenance): string {
43
- const document = {
44
- contract_version: config.contractVersion,
45
- target: { kind: config.targetKind },
46
- analysis: {
47
- languages: config.analysis.languages,
48
- min_confidence: config.analysis.minConfidence,
49
- generated_files: config.analysis.generatedFiles,
50
- consumer_tests: config.analysis.consumerTests,
51
- configurations: config.analysis.configurations.map((entry) =>
52
- entry.shape === "project"
53
- ? { id: entry.id, project: entry.project }
54
- : { id: entry.id, os: entry.os, arch: entry.arch, tags: entry.tags },
55
- ),
56
- matrix: { complete: config.analysis.matrixComplete },
57
- template_dirs: config.analysis.templateDirs,
58
- template_delimiters: {
59
- left: config.analysis.templateDelimiters.left,
60
- right: config.analysis.templateDelimiters.right,
61
- },
62
- },
63
- consumers: { complete: config.consumersComplete },
64
- roots: { patterns: config.rootPatterns },
65
- severity: Object.fromEntries([...config.severity].sort(([a], [b]) => bytewise(a, b))),
66
- exemptions: { disabled: config.exemptionsDisabled },
67
- reporters: {
68
- formats: config.reporters.formats,
69
- sort: config.reporters.sort,
70
- cascade: config.reporters.cascade,
71
- max_findings: config.reporters.maxFindings,
72
- fail_on: config.reporters.failOn,
73
- },
74
- providers: { analyzers: config.providers.analyzers.map(providerOf) },
75
- go: {},
76
- ts: {
77
- test_files: config.ts.testFiles,
78
- entry_files: config.ts.entryFiles,
79
- component_extensions: config.ts.componentExtensions,
80
- disabled_conventions: config.ts.disabledConventions,
81
- injection_registrations: config.ts.injectionRegistrations.map(declarationOf),
82
- lifecycle_contracts: config.ts.lifecycleContracts.map((entry) => ({
83
- components: entry.components.map(declarationOf),
84
- bases: entry.bases.map(declarationOf),
85
- members: entry.members,
86
- })),
87
- serializers: config.ts.serializers.map(declarationOf),
88
- roots_complete: config.ts.rootsComplete,
89
- },
90
- provenance: Object.fromEntries(
91
- [...provenance]
92
- .sort(([a], [b]) => bytewise(a, b))
93
- .map(([path, origin]) => [path, renderOrigin(origin)]),
94
- ),
95
- };
96
- return `${JSON.stringify(document, undefined, 2)}\n`;
11
+ export function printConfig(config: ResolvedConfig, provenance: Provenance): string {
12
+ const origins = [...provenance]
13
+ .sort(([a], [b]) => bytewise(a, b))
14
+ .map(([path, origin]): [string, string] => [path, renderOrigin(origin)]);
15
+ return `${JSON.stringify({ ...config, provenance: Object.fromEntries(origins) }, undefined, 2)}\n`;
97
16
  }