@cplieger/deadset-ts 8.0.0-dev.6 → 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 (193) hide show
  1. package/dist/bin/mapper-connection.js +2 -1
  2. package/dist/bin/node-host.js +24 -5
  3. package/dist/src/analysis.js +37 -56
  4. package/dist/src/automatic-types.js +10 -29
  5. package/dist/src/build-scripts.js +17 -29
  6. package/dist/src/component-layer.js +4 -2
  7. package/dist/src/components.js +2 -3
  8. package/dist/src/config.js +6 -82
  9. package/dist/src/configuration-files.js +27 -168
  10. package/dist/src/configured-declarations.js +15 -20
  11. package/dist/src/consumers.js +2 -2
  12. package/dist/src/contract/config.generated.js +544 -0
  13. package/dist/src/contract/contract.generated.js +13 -0
  14. package/dist/src/contract/report.generated.js +3 -2
  15. package/dist/src/conventions.js +20 -60
  16. package/dist/src/dependencies.js +25 -43
  17. package/dist/src/discover.js +14 -40
  18. package/dist/src/edges.js +38 -122
  19. package/dist/src/element-access.js +4 -6
  20. package/dist/src/entry-point-gaps.js +2 -8
  21. package/dist/src/entry-points.js +3 -10
  22. package/dist/src/enum-group.js +3 -10
  23. package/dist/src/erased-stores.js +4 -6
  24. package/dist/src/exempt-classes.js +0 -4
  25. package/dist/src/exempt.js +6 -22
  26. package/dist/src/file-facts.js +2 -25
  27. package/dist/src/findings/completion.js +5 -4
  28. package/dist/src/findings/emitters.js +1 -1
  29. package/dist/src/findings/intra-function.js +1 -1
  30. package/dist/src/findings/non-code-artifacts.js +2 -2
  31. package/dist/src/findings/reads-and-writes.js +2 -1
  32. package/dist/src/findings/self-check.js +3 -2
  33. package/dist/src/findings/unused-declarations.js +1 -1
  34. package/dist/src/findings/visibility-narrowing.js +2 -2
  35. package/dist/src/findings-pass.js +7 -12
  36. package/dist/src/framework-lifecycle.js +7 -7
  37. package/dist/src/fs/document.js +59 -0
  38. package/dist/src/fs/manifest.js +43 -0
  39. package/dist/src/fs/tree.js +88 -0
  40. package/dist/src/git-ignore.js +34 -25
  41. package/dist/src/html-entries.js +6 -33
  42. package/dist/src/in-operands.js +2 -1
  43. package/dist/src/injection-container.js +1 -1
  44. package/dist/src/intra-function-parts.js +10 -13
  45. package/dist/src/inventory.js +2 -21
  46. package/dist/src/json-document.js +198 -67
  47. package/dist/src/keyof-comparisons.js +4 -6
  48. package/dist/src/kinds.js +0 -7
  49. package/dist/src/manifest.js +5 -43
  50. package/dist/src/mdx-entries.js +3 -26
  51. package/dist/src/partial.js +2 -1
  52. package/dist/src/paths.js +26 -15
  53. package/dist/src/position.js +2 -1
  54. package/dist/src/print.js +11 -79
  55. package/dist/src/product.js +13 -0
  56. package/dist/src/references.js +14 -27
  57. package/dist/src/report-schema.js +3 -225
  58. package/dist/src/report.js +22 -31
  59. package/dist/src/reporters/sarif.js +4 -2
  60. package/dist/src/reporters/template.js +4 -19
  61. package/dist/src/resolve.js +245 -724
  62. package/dist/src/roots.js +7 -17
  63. package/dist/src/run.js +55 -61
  64. package/dist/src/schema.js +320 -343
  65. package/dist/src/scope.js +34 -103
  66. package/dist/src/serialization-contract.js +5 -9
  67. package/dist/src/session.js +2 -1
  68. package/dist/src/suppress-file.js +21 -25
  69. package/dist/src/suppress.js +3 -3
  70. package/dist/src/symbol-keys.js +3 -5
  71. package/dist/src/template-field.js +9 -19
  72. package/dist/src/test-globals.js +6 -28
  73. package/dist/src/test-joins.js +6 -6
  74. package/dist/src/text/compare.js +26 -0
  75. package/dist/src/text/glob.js +194 -0
  76. package/dist/src/text/shell.js +159 -0
  77. package/dist/src/{yaml-document.js → text/yaml.js} +38 -0
  78. package/dist/src/type-errors.js +14 -38
  79. package/dist/src/unseen-loads.js +2 -9
  80. package/dist/src/unwrap.js +23 -0
  81. package/dist/src/usage.js +10 -0
  82. package/dist/src/verbs/analyze.js +25 -26
  83. package/dist/src/verbs/describe.js +4 -4
  84. package/dist/src/verbs/explain.js +8 -6
  85. package/dist/src/verbs/print-projects.js +4 -3
  86. package/dist/src/verbs/print-retained.js +2 -1
  87. package/dist/src/verbs/print-roots.js +2 -1
  88. package/dist/src/verbs/version.js +2 -1
  89. package/dist/src/workflow-steps.js +9 -46
  90. package/dist/src/workspace-manifests.js +2 -2
  91. package/dist/src/workspace-programs.js +5 -3
  92. package/dist/src/workspace-resolution.js +13 -17
  93. package/dist/src/workspace.js +28 -116
  94. package/jsr.json +1 -1
  95. package/package.json +1 -1
  96. package/src/analysis.ts +55 -76
  97. package/src/automatic-types.ts +11 -29
  98. package/src/build-scripts.ts +17 -38
  99. package/src/component-layer.ts +4 -2
  100. package/src/components.ts +2 -4
  101. package/src/config.ts +31 -296
  102. package/src/configuration-files.ts +27 -170
  103. package/src/configured-declarations.ts +18 -23
  104. package/src/consumers.ts +4 -4
  105. package/src/contract/config.generated.ts +798 -0
  106. package/src/contract/contract.generated.ts +25 -0
  107. package/src/contract/report.generated.ts +3 -2
  108. package/src/conventions.ts +26 -74
  109. package/src/dependencies.ts +32 -50
  110. package/src/discover.ts +15 -40
  111. package/src/edges.ts +41 -140
  112. package/src/element-access.ts +4 -7
  113. package/src/entry-point-gaps.ts +3 -9
  114. package/src/entry-points.ts +2 -13
  115. package/src/enum-group.ts +2 -11
  116. package/src/erased-stores.ts +3 -7
  117. package/src/exempt-classes.ts +0 -5
  118. package/src/exempt.ts +8 -30
  119. package/src/file-facts.ts +6 -24
  120. package/src/findings/completion.ts +5 -6
  121. package/src/findings/emitter.ts +2 -2
  122. package/src/findings/emitters.ts +1 -1
  123. package/src/findings/intra-function.ts +1 -1
  124. package/src/findings/non-code-artifacts.ts +4 -4
  125. package/src/findings/reads-and-writes.ts +2 -1
  126. package/src/findings/self-check.ts +3 -2
  127. package/src/findings/unused-declarations.ts +1 -1
  128. package/src/findings/visibility-narrowing.ts +2 -2
  129. package/src/findings-pass.ts +9 -15
  130. package/src/framework-lifecycle.ts +7 -7
  131. package/src/fs/document.ts +74 -0
  132. package/src/fs/manifest.ts +52 -0
  133. package/src/fs/tree.ts +110 -0
  134. package/src/git-ignore.ts +38 -25
  135. package/src/host.ts +12 -5
  136. package/src/html-entries.ts +6 -33
  137. package/src/in-operands.ts +2 -1
  138. package/src/injection-container.ts +1 -1
  139. package/src/intra-function-parts.ts +10 -14
  140. package/src/inventory.ts +2 -20
  141. package/src/json-document.ts +241 -76
  142. package/src/keyof-comparisons.ts +3 -7
  143. package/src/kinds.ts +0 -8
  144. package/src/manifest.ts +5 -46
  145. package/src/mdx-entries.ts +3 -26
  146. package/src/partial.ts +2 -1
  147. package/src/paths.ts +27 -15
  148. package/src/position.ts +2 -1
  149. package/src/print.ts +12 -92
  150. package/src/product.ts +18 -0
  151. package/src/references.ts +14 -29
  152. package/src/report-schema.ts +3 -301
  153. package/src/report.ts +24 -34
  154. package/src/reporters/sarif.ts +4 -4
  155. package/src/reporters/template.ts +4 -20
  156. package/src/resolve.ts +283 -1114
  157. package/src/roots.ts +7 -20
  158. package/src/run.ts +57 -75
  159. package/src/schema.ts +391 -388
  160. package/src/scope.ts +40 -126
  161. package/src/serialization-contract.ts +5 -10
  162. package/src/session.ts +2 -1
  163. package/src/suppress-file.ts +21 -29
  164. package/src/suppress.ts +3 -3
  165. package/src/symbol-keys.ts +2 -5
  166. package/src/template-field.ts +15 -25
  167. package/src/test-globals.ts +9 -27
  168. package/src/test-joins.ts +6 -8
  169. package/src/text/compare.ts +27 -0
  170. package/src/text/glob.ts +210 -0
  171. package/src/text/shell.ts +161 -0
  172. package/src/{yaml-document.ts → text/yaml.ts} +46 -0
  173. package/src/type-errors.ts +25 -36
  174. package/src/unseen-loads.ts +1 -12
  175. package/src/unwrap.ts +34 -0
  176. package/src/usage.ts +10 -0
  177. package/src/verbs/analyze.ts +32 -42
  178. package/src/verbs/describe.ts +4 -4
  179. package/src/verbs/explain.ts +8 -10
  180. package/src/verbs/print-projects.ts +4 -3
  181. package/src/verbs/print-retained.ts +2 -1
  182. package/src/verbs/print-roots.ts +2 -1
  183. package/src/verbs/verb.ts +2 -2
  184. package/src/verbs/version.ts +2 -1
  185. package/src/workflow-steps.ts +9 -44
  186. package/src/workspace-manifests.ts +2 -2
  187. package/src/workspace-programs.ts +5 -3
  188. package/src/workspace-resolution.ts +21 -17
  189. package/src/workspace.ts +35 -121
  190. package/dist/src/glob.js +0 -65
  191. package/dist/src/pnpm-workspace.js +0 -120
  192. package/src/glob.ts +0 -67
  193. package/src/pnpm-workspace.ts +0 -135
package/src/resolve.ts CHANGED
@@ -1,1201 +1,370 @@
1
+ import { SETTINGS, type SettingRow } from "./contract/config.generated.ts";
2
+ import { FAMILY_KEY_LENGTH } from "./contract/contract.generated.ts";
1
3
  import {
2
- SEVERITIES,
3
- LANGUAGES,
4
- CONFIDENCES,
5
- FORMATS,
6
- type Confidence,
7
- type Format,
8
- type Language,
9
- type Severity,
10
- FAMILY_KEY_LENGTH,
11
- } from "./contract/contract.generated.ts";
12
- import {
13
- ConfigError,
14
- defaultConfig,
15
- type BuildConfiguration,
16
- type Cascade,
17
- type Config,
18
- type ConsumerTests,
19
- type DeclarationEntry,
20
- type GeneratedFiles,
4
+ type Document,
21
5
  type Inputs,
22
- type LifecycleContract,
23
6
  type Origin,
7
+ type PartialConfig,
24
8
  type Provenance,
25
- type Provider,
26
- type Sort,
9
+ type ResolvedConfig,
27
10
  type Source,
28
- type TargetKind,
29
- type TargetKindSetting,
30
- type TemplateDelimiters,
31
11
  } from "./config.ts";
32
12
  import { CONVENTION_ROWS } from "./convention-rows.ts";
33
- import { FIXED_SEVERITY_CODES, LIVE_CODES } from "./kinds.ts";
13
+ import { refuseOverBound } from "./fs/document.ts";
34
14
  import {
35
- checkDocument,
36
- malformed,
37
- nestFlags,
38
- SCHEMA_ROOT,
39
- SEVERITY_SECTION,
40
- type KeyNode,
41
- } from "./schema.ts";
42
-
43
- /** Names the command-line flags in a refusal, where a document is named by its path. */
44
- const FLAGS_LABEL = "the command-line flags";
45
-
46
- const SEVERITY_KEY = /^DS[0-9]{2}([0-9]{2})?$/u;
47
- const EXEMPTION_CLASS = /^[a-z][a-z0-9-]*$/u;
48
- const CONTRACT_VERSION_PATTERN = /^[0-9]+\.[0-9]+\.[0-9]+$/u;
49
- const ANALYZER_NAME = /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/u;
50
- const PROVIDER_SOURCE = /^(go|npm):[^ \t\r\n]+$/u;
51
- const PROVIDER_VERSION = /^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$/u;
52
- const PROVIDER_DIGEST = /^sha256:[0-9a-f]{64}$/u;
53
-
54
- /** The members only an acquirable provider entry carries. */
55
- const ACQUIRABLE_MEMBERS = ["source", "version", "digest"] as const;
56
-
57
- /** The members only a platform build configuration carries. */
58
- const PLATFORM_MEMBERS = ["os", "arch", "tags"] as const;
59
-
60
- /** The member only a project build configuration carries. */
61
- const PROJECT_MEMBER = "project";
15
+ dotted,
16
+ isRecord,
17
+ locatedFault,
18
+ type Place,
19
+ placeIn,
20
+ pointerOf,
21
+ readStrict,
22
+ StrictJSONError,
23
+ } from "./json-document.ts";
24
+ import { FIXED_SEVERITY_CODES } from "./kinds.ts";
25
+ import { LANGUAGE } from "./product.ts";
26
+ import { firstRefusal, type SchemaRefusal } from "./schema.ts";
27
+ import { bytewise } from "./text/compare.ts";
28
+ import { UsageError } from "./usage.ts";
29
+
30
+ const ROWS: ReadonlyMap<string, SettingRow> = new Map(SETTINGS.map((row) => [row.key, row]));
31
+
32
+ /** The schema of a section or a keyed section's value, so the validator words its refusal. */
33
+ const OBJECT = { type: "object" };
62
34
 
63
35
  /**
64
- * One segment of a project path: not empty, not `.` or `..`, and holding no
65
- * solidus or control character.
36
+ * The settings whose entries name rows of this analyzer's own data, which the
37
+ * Contract's schema cannot list: a disabled convention names a convention row.
66
38
  */
67
- const PROJECT_SEGMENT = String.raw`(?:[^/.\x00-\x1f\x7f][^/\x00-\x1f\x7f]*|\.[^/.\x00-\x1f\x7f][^/\x00-\x1f\x7f]*|\.\.[^/\x00-\x1f\x7f]+)`;
68
-
69
- /**
70
- * A project path: segments joined by a solidus, so the path is relative, below
71
- * the root it is read against, and spelled one way on every machine.
72
- */
73
- const PROJECT_PATH = new RegExp(`^${PROJECT_SEGMENT}(?:/${PROJECT_SEGMENT})*$`, "u");
74
-
75
- const TARGET_KINDS: readonly TargetKind[] = ["application", "library"];
76
- const GENERATED_FILES: readonly GeneratedFiles[] = ["exclude", "include"];
77
- const CONSUMER_TESTS: readonly ConsumerTests[] = ["test", "production"];
78
- const SORTS: readonly Sort[] = ["position", "size"];
79
- const CASCADES: readonly Cascade[] = ["roots", "full"];
80
-
81
- /**
82
- * One configuration document, read as the settings it supplies. Every member is
83
- * possibly undefined, so a key the document omits is distinguishable from one it
84
- * sets to a value equal to the default, which is what makes resolution per
85
- * setting rather than per document.
86
- */
87
- interface Doc {
88
- readonly contractVersion: string | undefined;
89
- readonly targetKind: TargetKindSetting | undefined;
90
- readonly languages: readonly Language[] | undefined;
91
- readonly minConfidence: Confidence | undefined;
92
- readonly generatedFiles: GeneratedFiles | undefined;
93
- readonly consumerTests: ConsumerTests | undefined;
94
- readonly configurations: readonly BuildConfiguration[] | undefined;
95
- readonly matrixComplete: boolean | undefined;
96
- readonly templateDirs: readonly string[] | undefined;
97
- readonly templateDelimiters: TemplateDelimiters | undefined;
98
- readonly consumersComplete: boolean | undefined;
99
- readonly rootPatterns: readonly string[] | undefined;
100
- readonly severity: ReadonlyMap<string, Severity> | undefined;
101
- readonly exemptionsDisabled: readonly string[] | undefined;
102
- readonly formats: readonly Format[] | undefined;
103
- readonly sort: Sort | undefined;
104
- readonly cascade: Cascade | undefined;
105
- readonly maxFindings: number | undefined;
106
- readonly failOn: Severity | undefined;
107
- readonly providers: readonly Provider[] | undefined;
108
- readonly testFiles: readonly string[] | undefined;
109
- readonly entryFiles: readonly string[] | undefined;
110
- readonly componentExtensions: readonly string[] | undefined;
111
- readonly disabledConventions: readonly string[] | undefined;
112
- readonly injectionRegistrations: readonly DeclarationEntry[] | undefined;
113
- readonly lifecycleContracts: readonly LifecycleContract[] | undefined;
114
- readonly serializers: readonly DeclarationEntry[] | undefined;
115
- readonly rootsComplete: boolean | undefined;
116
- }
117
-
118
- /** One document the resolution reads, with the origin its settings carry. */
119
- interface DocSource {
120
- readonly doc: Doc;
121
- readonly kind: Source;
122
- readonly label: string;
123
- readonly flagLabels: ReadonlyMap<string, string>;
124
- }
39
+ const OWN_VOCABULARIES: ReadonlyMap<string, ReadonlySet<string>> = new Map([
40
+ ["ts.disabled_conventions", new Set(CONVENTION_ROWS.map((row) => row.name))],
41
+ ]);
125
42
 
126
- /**
127
- * The origin one setting of this source carries. A flag names itself per setting,
128
- * because one invocation carries many.
129
- */
130
- function originOf(source: DocSource, path: string): Origin {
131
- if (source.kind === "flag") {
132
- return { source: "flag", label: source.flagLabels.get(path) ?? "" };
133
- }
134
- return { source: source.kind, label: source.label };
43
+ function malformed(label: string, path: string, detail: string): UsageError {
44
+ return new UsageError(path === "" ? `${label}: ${detail}` : `${label}: ${path}: ${detail}`);
135
45
  }
136
46
 
137
- function isRecord(value: unknown): value is Record<string, unknown> {
138
- return typeof value === "object" && value !== null && !Array.isArray(value);
47
+ /** A key the closed key list does not declare, with the reason where there is more to say. */
48
+ function unimplemented(label: string, path: string, reason = ""): UsageError {
49
+ const why = reason === "" ? "" : `: ${reason}`;
50
+ return new UsageError(`${label}: key ${JSON.stringify(path)} is not implemented${why}`);
139
51
  }
140
52
 
141
- function member(parent: Record<string, unknown> | undefined, name: string): unknown {
142
- if (parent === undefined || !Object.hasOwn(parent, name)) {
143
- return undefined;
53
+ /** Why a well-formed severity key the vocabulary does not hold names no setting. */
54
+ function severityKeyRefused(key: string): string {
55
+ const fixed = FIXED_SEVERITY_CODES.filter(
56
+ (code) => code === key || (key.length === FAMILY_KEY_LENGTH && code.startsWith(key)),
57
+ ).sort(bytewise);
58
+ if (fixed.length > 0) {
59
+ const named =
60
+ fixed.length < 2
61
+ ? fixed.join("")
62
+ : `${fixed.slice(0, -1).join(", ")} and ${String(fixed.at(-1))}`;
63
+ return `the Contract fixes the severity of ${named}, which this key names`;
144
64
  }
145
- return parent[name];
65
+ return key.length === FAMILY_KEY_LENGTH
66
+ ? `no issue kind carries a code of the family ${key}`
67
+ : `no issue kind carries the code ${key}`;
146
68
  }
147
69
 
148
- function section(
149
- parent: Record<string, unknown> | undefined,
150
- name: string,
151
- path: string,
152
- label: string,
153
- ): Record<string, unknown> | undefined {
154
- const value = member(parent, name);
155
- if (value === undefined) {
156
- return undefined;
70
+ /** A value with every object's members in ascending order, so it prints one way. */
71
+ function canonical(value: unknown): unknown {
72
+ if (Array.isArray(value)) {
73
+ return value.map(canonical);
157
74
  }
158
75
  if (!isRecord(value)) {
159
- throw malformed(label, path, "is not an object");
160
- }
161
- return value;
162
- }
163
-
164
- function spell(values: readonly string[]): string {
165
- return values.map((value) => JSON.stringify(value)).join(", ");
166
- }
167
-
168
- function readEnum<T extends string>(
169
- parent: Record<string, unknown> | undefined,
170
- name: string,
171
- path: string,
172
- label: string,
173
- allowed: readonly T[],
174
- ): T | undefined {
175
- const value = member(parent, name);
176
- if (value === undefined) {
177
- return undefined;
178
- }
179
- if (typeof value !== "string") {
180
- throw malformed(label, path, "is not a string");
181
- }
182
- if (!(allowed as readonly string[]).includes(value)) {
183
- throw malformed(label, path, `${JSON.stringify(value)} is not one of ${spell(allowed)}`);
184
- }
185
- return value as T;
186
- }
187
-
188
- function readString(
189
- parent: Record<string, unknown> | undefined,
190
- name: string,
191
- path: string,
192
- label: string,
193
- ): string | undefined {
194
- const value = member(parent, name);
195
- if (value === undefined) {
196
- return undefined;
76
+ return value;
197
77
  }
198
- if (typeof value !== "string") {
199
- throw malformed(label, path, "is not a string");
200
- }
201
- return value;
78
+ return Object.fromEntries(
79
+ Object.keys(value)
80
+ .sort(bytewise)
81
+ .map((name) => [name, canonical(value[name])]),
82
+ );
202
83
  }
203
84
 
204
- function readBoolean(
205
- parent: Record<string, unknown> | undefined,
206
- name: string,
207
- path: string,
208
- label: string,
209
- ): boolean | undefined {
210
- const value = member(parent, name);
211
- if (value === undefined) {
212
- return undefined;
213
- }
214
- if (typeof value !== "boolean") {
215
- throw malformed(label, path, "is not a boolean");
216
- }
217
- return value;
218
- }
219
-
220
- function readCount(
221
- parent: Record<string, unknown> | undefined,
222
- name: string,
223
- path: string,
224
- label: string,
225
- ): number | undefined {
226
- const value = member(parent, name);
227
- if (value === undefined) {
228
- return undefined;
229
- }
230
- if (typeof value !== "number" || !Number.isInteger(value)) {
231
- throw malformed(label, path, "is not an integer");
232
- }
233
- if (value < 0) {
234
- throw malformed(label, path, `${String(value)} is below the minimum of 0`);
235
- }
236
- return value;
85
+ /** What one checked source supplies: each setting's value and each keyed section's members. */
86
+ interface Supplied {
87
+ readonly values: Map<string, unknown>;
88
+ readonly keyed: Map<string, Readonly<Record<string, unknown>>>;
237
89
  }
238
90
 
239
91
  /**
240
- * One array of strings, refusing an array shorter than its minimum, one holding an
241
- * empty entry, one naming an entry twice, and, where a closed set is given, one
242
- * holding an entry outside it.
92
+ * The walk of one source over the closed key list. The members of an object are checked
93
+ * in ascending order and a key is checked whole before the next. A key this analyzer's
94
+ * language does not read is accepted unread, and of a key written per language, every
95
+ * other language's member.
243
96
  */
244
- function readStrings<T extends string>(
245
- parent: Record<string, unknown> | undefined,
246
- name: string,
247
- path: string,
248
- label: string,
249
- minItems: number,
250
- allowed?: readonly T[],
251
- ): readonly T[] | undefined {
252
- const value = member(parent, name);
253
- if (value === undefined) {
254
- return undefined;
255
- }
256
- if (!Array.isArray(value)) {
257
- throw malformed(label, path, "is not an array");
258
- }
259
- if (value.length < minItems) {
260
- throw malformed(
261
- label,
262
- path,
263
- `holds ${String(value.length)} entries, want at least ${String(minItems)}`,
264
- );
265
- }
266
- const seen = new Set<string>();
267
- for (const [index, entry] of (value as unknown[]).entries()) {
268
- const at = `${path}[${String(index)}]`;
269
- if (typeof entry !== "string") {
270
- throw malformed(label, at, "is not a string");
271
- }
272
- if (entry === "") {
273
- throw malformed(label, at, "is empty");
274
- }
275
- if (seen.has(entry)) {
276
- throw malformed(label, at, `names ${JSON.stringify(entry)} again`);
277
- }
278
- if (allowed !== undefined && !(allowed as readonly string[]).includes(entry)) {
279
- throw malformed(label, at, `${JSON.stringify(entry)} is not one of ${spell(allowed)}`);
97
+ class Walk {
98
+ private readonly label: string;
99
+ private readonly fractions: ReadonlyMap<string, string>;
100
+ private readonly into: Supplied;
101
+
102
+ constructor(label: string, fractions: ReadonlyMap<string, string>, into: Supplied) {
103
+ this.label = label;
104
+ this.fractions = fractions;
105
+ this.into = into;
106
+ }
107
+
108
+ section(at: string, members: Readonly<Record<string, unknown>>): void {
109
+ for (const name of Object.keys(members).sort(bytewise)) {
110
+ const key = at === "" ? name : `${at}.${name}`;
111
+ const row = ROWS.get(key);
112
+ if (name.includes(".")) {
113
+ const reason = row === undefined ? "" : "it is one setting, written as nested objects";
114
+ throw unimplemented(this.label, key, reason);
115
+ }
116
+ if (row === undefined) {
117
+ throw unimplemented(this.label, key);
118
+ }
119
+ if (row.read) {
120
+ this.key(row, members[name]);
121
+ }
280
122
  }
281
- seen.add(entry);
282
123
  }
283
- return value as readonly T[];
284
- }
285
124
 
286
- /**
287
- * The target kind, a string or an object naming every language. The walk has
288
- * already refused an undeclared member, so an object is refused for the first
289
- * language it leaves out, then for the first language whose value is refused,
290
- * each in the vocabulary's language order.
291
- */
292
- function readTargetKind(
293
- target: Record<string, unknown> | undefined,
294
- label: string,
295
- ): TargetKindSetting | undefined {
296
- const value = member(target, "kind");
297
- const perLanguage = `an object naming the kind of each of ${spell(LANGUAGES)}`;
298
- if (value === undefined) {
299
- return undefined;
300
- }
301
- if (typeof value === "string") {
302
- if (!(TARGET_KINDS as readonly string[]).includes(value)) {
303
- throw malformed(
304
- label,
305
- "target.kind",
306
- `${JSON.stringify(value)} is not one of ${spell(TARGET_KINDS)}, nor ${perLanguage}`,
307
- );
308
- }
309
- return value as TargetKind;
310
- }
311
- if (!isRecord(value)) {
312
- throw malformed(
313
- label,
314
- "target.kind",
315
- `${JSON.stringify(value)} is neither one of ${spell(TARGET_KINDS)} nor ${perLanguage}`,
316
- );
317
- }
318
- const missing = LANGUAGES.find((language) => !Object.hasOwn(value, language));
319
- if (missing !== undefined) {
320
- throw malformed(label, `target.kind.${missing}`, `is not set, and ${perLanguage} names each`);
321
- }
322
- for (const language of LANGUAGES) {
323
- const kind = value[language];
324
- if (typeof kind !== "string" || !(TARGET_KINDS as readonly string[]).includes(kind)) {
325
- throw malformed(
326
- label,
327
- `target.kind.${language}`,
328
- `${JSON.stringify(kind)} is not one of ${spell(TARGET_KINDS)}`,
329
- );
125
+ private key(row: SettingRow, value: unknown): void {
126
+ if (row.role === "section") {
127
+ this.refuse(row.key.split("."), value, firstRefusal(OBJECT, value));
128
+ this.section(row.key, value as Record<string, unknown>);
129
+ } else if (row.role === "setting") {
130
+ const read = row.perLanguage === undefined ? value : this.narrowed(row, value);
131
+ this.setting(row, read);
132
+ this.into.values.set(row.key, canonical(read));
133
+ } else {
134
+ const members = this.keyed(row, value);
135
+ if (row.role === "keyed") {
136
+ this.into.keyed.set(row.key, members);
137
+ }
330
138
  }
331
139
  }
332
- return { go: value["go"] as TargetKind, ts: value["ts"] as TargetKind };
333
- }
334
-
335
- /** A component file's extension: a full stop followed by letters and digits. */
336
- const COMPONENT_EXTENSION = /^\.[A-Za-z0-9]+$/u;
337
-
338
- function readComponentExtensions(
339
- ts: Record<string, unknown> | undefined,
340
- label: string,
341
- ): readonly string[] | undefined {
342
- const extensions = readStrings(ts, "component_extensions", "ts.component_extensions", label, 0);
343
- const wrong = extensions?.findIndex((one) => !COMPONENT_EXTENSION.test(one)) ?? -1;
344
- if (extensions !== undefined && wrong !== -1) {
345
- throw malformed(
346
- label,
347
- `ts.component_extensions[${String(wrong)}]`,
348
- `${JSON.stringify(extensions[wrong])} is not a full stop followed by letters and digits`,
349
- );
350
- }
351
- return extensions;
352
- }
353
-
354
- /** The names of the convention rows this analyzer's table carries. */
355
- const CONVENTION_NAMES: ReadonlySet<string> = new Set(CONVENTION_ROWS.map((row) => row.name));
356
-
357
- function readDisabledConventions(
358
- ts: Record<string, unknown> | undefined,
359
- label: string,
360
- ): readonly string[] | undefined {
361
- const names = readStrings(ts, "disabled_conventions", "ts.disabled_conventions", label, 0);
362
- const unknown = names?.findIndex((one) => !CONVENTION_NAMES.has(one)) ?? -1;
363
- if (names !== undefined && unknown !== -1) {
364
- throw malformed(
365
- label,
366
- `ts.disabled_conventions[${String(unknown)}]`,
367
- `${JSON.stringify(names[unknown])} names no convention row this analyzer carries`,
368
- );
369
- }
370
- return names;
371
- }
372
-
373
- function requireMember(value: string | undefined, path: string, label: string): string {
374
- if (value === undefined || value === "") {
375
- throw malformed(label, path, "is required and names nothing");
376
- }
377
- return value;
378
- }
379
-
380
- /** One build configuration entry as a platform: an identifier, an operating system and an architecture. */
381
- function readPlatform(
382
- entry: Record<string, unknown>,
383
- at: string,
384
- label: string,
385
- ): BuildConfiguration {
386
- return {
387
- shape: "platform",
388
- id: requireMember(readString(entry, "id", `${at}.id`, label), `${at}.id`, label),
389
- os: requireMember(readString(entry, "os", `${at}.os`, label), `${at}.os`, label),
390
- arch: requireMember(readString(entry, "arch", `${at}.arch`, label), `${at}.arch`, label),
391
- tags: readStrings(entry, "tags", `${at}.tags`, label, 0) ?? [],
392
- };
393
- }
394
140
 
395
- /** One build configuration entry as a project: an identifier and a configuration file below the target root. */
396
- function readProject(
397
- entry: Record<string, unknown>,
398
- at: string,
399
- label: string,
400
- ): BuildConfiguration {
401
- const id = requireMember(readString(entry, "id", `${at}.id`, label), `${at}.id`, label);
402
- const path = `${at}.${PROJECT_MEMBER}`;
403
- const project = requireMember(readString(entry, PROJECT_MEMBER, path, label), path, label);
404
- if (!PROJECT_PATH.test(project)) {
405
- throw malformed(
406
- label,
407
- path,
408
- `${JSON.stringify(project)} is not a path below the target root: segments joined by /, ` +
409
- "none empty, . or .., and no control character",
410
- );
411
- }
412
- return { shape: "project", id, project };
413
- }
414
-
415
- /**
416
- * One build configuration entry, read as the one shape its members name: a member
417
- * only a platform carries makes it a platform, the member only a project carries
418
- * makes it a project, and an entry naming members of both, or of neither, is
419
- * refused at the entry, because the two shapes are exclusive.
420
- */
421
- function readConfiguration(entry: unknown, at: string, label: string): BuildConfiguration {
422
- if (!isRecord(entry)) {
423
- throw malformed(label, at, "is not an object");
424
- }
425
- const platform = PLATFORM_MEMBERS.filter((name) => Object.hasOwn(entry, name));
426
- const project = Object.hasOwn(entry, PROJECT_MEMBER);
427
- if (platform.length > 0 && project) {
428
- throw malformed(
429
- label,
430
- at,
431
- `carries ${spell(platform)} of a platform and ${JSON.stringify(PROJECT_MEMBER)} of a ` +
432
- "project, and an entry is one shape or the other",
433
- );
434
- }
435
- if (project) {
436
- return readProject(entry, at, label);
437
- }
438
- if (platform.length === 0) {
439
- throw malformed(
440
- label,
441
- at,
442
- `names neither a platform's "os" and "arch" nor a project's ${JSON.stringify(PROJECT_MEMBER)}`,
141
+ /** A per-language value as this language reads it: of the object form, its own member alone. */
142
+ private narrowed(row: SettingRow, value: unknown): unknown {
143
+ if (!isRecord(value)) {
144
+ return value;
145
+ }
146
+ const others: readonly string[] = row.perLanguage ?? [];
147
+ return Object.fromEntries(
148
+ Object.entries(value).filter(([name]) => name === LANGUAGE || !others.includes(name)),
443
149
  );
444
150
  }
445
- return readPlatform(entry, at, label);
446
- }
447
-
448
- /** The build matrix, one entry in one shape at a time. */
449
- function readConfigurations(
450
- analysis: Record<string, unknown> | undefined,
451
- label: string,
452
- ): readonly BuildConfiguration[] | undefined {
453
- const value = member(analysis, "configurations");
454
- if (value === undefined) {
455
- return undefined;
456
- }
457
- if (!Array.isArray(value)) {
458
- throw malformed(label, "analysis.configurations", "is not an array");
459
- }
460
- return (value as unknown[]).map((entry, index) =>
461
- readConfiguration(entry, `analysis.configurations[${String(index)}]`, label),
462
- );
463
- }
464
151
 
465
- /**
466
- * The action delimiter pair: once a document names the object both members are
467
- * required, because a pair carrying one member names no delimiters at all, so the
468
- * refusal names the member the document left out rather than pairing the one it
469
- * named with a default.
470
- */
471
- function readDelimiters(
472
- analysis: Record<string, unknown> | undefined,
473
- label: string,
474
- ): TemplateDelimiters | undefined {
475
- const at = "analysis.template_delimiters";
476
- const pair = section(analysis, "template_delimiters", at, label);
477
- if (pair === undefined) {
478
- return undefined;
479
- }
480
- return {
481
- left: requireMember(readString(pair, "left", `${at}.left`, label), `${at}.left`, label),
482
- right: requireMember(readString(pair, "right", `${at}.right`, label), `${at}.right`, label),
483
- };
484
- }
485
-
486
- /**
487
- * A severity key that names no setting: it names no live kind, or the Contract
488
- * fixes the severity of the kind or kinds it names.
489
- */
490
- function unimplementedSeverityKey(label: string, path: string, reason: string): ConfigError {
491
- return new ConfigError(
492
- "unimplemented-key",
493
- path,
494
- `${label}: key ${JSON.stringify(path)} is not implemented: ${reason}`,
495
- );
496
- }
497
-
498
- /**
499
- * Every code whose severity the Contract fixes that one severity key names, in
500
- * ascending code order: the code itself, or every code of the family its
501
- * two-digit prefix names. A family key covers every such code, so a refusal names
502
- * them all rather than the first one found.
503
- */
504
- function fixedByContract(code: string): string[] {
505
- return FIXED_SEVERITY_CODES.filter(
506
- (candidate) =>
507
- code === candidate || (code.length === FAMILY_KEY_LENGTH && candidate.startsWith(code)),
508
- ).sort();
509
- }
510
-
511
- /** The codes a refusal names: one on its own, and several with the last joined by and. */
512
- function spellCodes(codes: readonly string[]): string {
513
- if (codes.length < 2) {
514
- return codes.join("");
515
- }
516
- return `${codes.slice(0, -1).join(", ")} and ${String(codes[codes.length - 1])}`;
517
- }
518
-
519
- /** Whether one severity key names a live kind: the code itself, or a family holding one. */
520
- function namesLiveKind(code: string): boolean {
521
- return LIVE_CODES.some(
522
- (candidate) =>
523
- code === candidate || (code.length === FAMILY_KEY_LENGTH && candidate.startsWith(code)),
524
- );
525
- }
526
-
527
- /**
528
- * The severity object, one code at a time.
529
- *
530
- * A key is one issue-kind code or one two-digit family prefix. A key naming no
531
- * live kind, and a key naming a kind whose severity the Contract fixes or a family
532
- * prefix whose range holds one, is an unimplemented key rather than a setting.
533
- *
534
- * Whether a key names a kind this analyzer reports is not decided here: a live
535
- * kind of another language is a setting the analyzer of that language reads.
536
- */
537
- function readSeverity(
538
- document: Record<string, unknown> | undefined,
539
- label: string,
540
- ): ReadonlyMap<string, Severity> | undefined {
541
- const object = section(document, SEVERITY_SECTION, SEVERITY_SECTION, label);
542
- if (object === undefined) {
543
- return undefined;
544
- }
545
- const severity = new Map<string, Severity>();
546
- for (const code of Object.keys(object).sort()) {
547
- const path = `${SEVERITY_SECTION}.${code}`;
548
- if (!SEVERITY_KEY.test(code)) {
549
- throw unimplementedSeverityKey(
550
- label,
551
- path,
552
- "a severity key is one issue-kind code or one two-digit family prefix",
553
- );
554
- }
555
- if (!namesLiveKind(code)) {
556
- throw unimplementedSeverityKey(
557
- label,
558
- path,
559
- code.length === FAMILY_KEY_LENGTH
560
- ? "the family this prefix names holds no live issue kind"
561
- : "the code names no live issue kind",
562
- );
563
- }
564
- const fixed = fixedByContract(code);
565
- if (fixed.length > 0) {
566
- throw unimplementedSeverityKey(
567
- label,
568
- path,
569
- `the Contract fixes the severity of ${spellCodes(fixed)}, which this key names`,
152
+ private setting(row: SettingRow, value: unknown): void {
153
+ this.refuse(row.key.split("."), value, firstRefusal(row.value ?? {}, value));
154
+ const text = this.fractions.get(pointerOf(row.key.split(".")));
155
+ if (typeof value === "number" && text !== undefined) {
156
+ throw malformed(
157
+ this.label,
158
+ row.key,
159
+ `holds ${text}, want an integer written as an optional minus sign and digits alone`,
570
160
  );
571
161
  }
572
- const value = readEnum(object, code, path, label, SEVERITIES);
573
- if (value !== undefined) {
574
- severity.set(code, value);
162
+ const own = OWN_VOCABULARIES.get(row.key);
163
+ if (own !== undefined) {
164
+ const names = value as readonly string[];
165
+ const unknown = names.findIndex((name) => !own.has(name));
166
+ if (unknown !== -1) {
167
+ throw malformed(
168
+ this.label,
169
+ `${row.key}[${String(unknown)}]`,
170
+ `${JSON.stringify(names[unknown])} names no row this analyzer carries`,
171
+ );
172
+ }
575
173
  }
576
174
  }
577
- return severity;
578
- }
579
175
 
580
- /** The exemption classes named for switching off. */
581
- function readExemptions(
582
- document: Record<string, unknown> | undefined,
583
- label: string,
584
- ): readonly string[] | undefined {
585
- const exemptions = section(document, "exemptions", "exemptions", label);
586
- const disabled = readStrings(exemptions, "disabled", "exemptions.disabled", label, 0);
587
- if (disabled === undefined) {
588
- return undefined;
589
- }
590
- for (const entry of disabled) {
591
- if (!EXEMPTION_CLASS.test(entry)) {
592
- throw malformed(
593
- label,
594
- "exemptions.disabled",
595
- `${JSON.stringify(entry)} is not an exemption class name`,
596
- );
176
+ /** A keyed or an ignored section: an object whose member names its key pattern admits. */
177
+ private keyed(row: SettingRow, value: unknown): Readonly<Record<string, unknown>> {
178
+ this.refuse(row.key.split("."), value, firstRefusal(OBJECT, value));
179
+ const members = value as Readonly<Record<string, unknown>>;
180
+ const shape = new RegExp(row.keyPattern ?? "", "u");
181
+ for (const name of Object.keys(members).sort(bytewise)) {
182
+ const at = `${row.key}.${name}`;
183
+ if (!shape.test(name)) {
184
+ throw unimplemented(this.label, at, `the name does not match ${String(row.keyPattern)}`);
185
+ }
186
+ if (row.values !== undefined && !row.values.includes(name)) {
187
+ throw unimplemented(this.label, at, severityKeyRefused(name));
188
+ }
189
+ const member = members[name];
190
+ this.refuse([...row.key.split("."), name], member, firstRefusal(row.value ?? {}, member));
597
191
  }
192
+ return members;
598
193
  }
599
- return disabled;
600
- }
601
194
 
602
- /** The members that name a declaration, each belonging to exactly one shape. */
603
- const SHAPE_MEMBERS = ["symbol", "module", "name", "global"] as const;
604
-
605
- /** The prefix of a stable symbol reference in the TypeScript form. */
606
- const SYMBOL_PREFIX = "ts://";
607
-
608
- /**
609
- * A bare specifier: neither relative nor absolute, and not one the package's own
610
- * imports field maps, each of which names a file of the analyzed program.
611
- */
612
- const BARE_SPECIFIER = /^[^./#]/u;
613
-
614
- /**
615
- * One declaration entry, read as the one shape its members name. An entry carrying
616
- * members of two shapes, or of none, is refused at the entry, as is a module named
617
- * without the declaration's path or a path without its module.
618
- */
619
- function readDeclaration(entry: unknown, at: string, label: string): DeclarationEntry {
620
- if (!isRecord(entry)) {
621
- throw malformed(label, at, "is not an object");
622
- }
623
- const named = SHAPE_MEMBERS.filter((name) => Object.hasOwn(entry, name));
624
- if (named.length === 1 && named[0] === "symbol") {
625
- const symbol = requireMember(
626
- readString(entry, "symbol", `${at}.symbol`, label),
627
- `${at}.symbol`,
628
- label,
629
- );
630
- if (!symbol.startsWith(SYMBOL_PREFIX)) {
631
- throw malformed(label, `${at}.symbol`, `${JSON.stringify(symbol)} is not a ts:// reference`);
195
+ /** Throws the refusal the schema makes of `value`, at `at`, naming the place inside it. */
196
+ private refuse(at: Place, value: unknown, refused: SchemaRefusal | undefined): void {
197
+ if (refused === undefined) {
198
+ return;
632
199
  }
633
- return { shape: "symbol", symbol };
634
- }
635
- if (named.length === 1 && named[0] === "global") {
636
- const path = `${at}.global`;
637
- return {
638
- shape: "global",
639
- global: requireMember(readString(entry, "global", path, label), path, label),
640
- };
641
- }
642
- if (named.length === 2 && named.includes("module") && named.includes("name")) {
643
- const module = requireMember(
644
- readString(entry, "module", `${at}.module`, label),
645
- `${at}.module`,
646
- label,
647
- );
648
- if (!BARE_SPECIFIER.test(module)) {
649
- throw malformed(
650
- label,
651
- `${at}.module`,
652
- `${JSON.stringify(module)} is not a bare specifier: a file of the analyzed program is named by its symbol`,
653
- );
200
+ const place = dotted([...at, ...placeIn(value, refused.pointer, refused.member)]);
201
+ switch (refused.keyword) {
202
+ case "additionalProperties":
203
+ throw unimplemented(this.label, place);
204
+ case "required":
205
+ throw malformed(this.label, place, "is required and absent");
654
206
  }
655
- const path = `${at}.name`;
656
- return {
657
- shape: "module",
658
- module,
659
- name: requireMember(readString(entry, "name", path, label), path, label),
660
- };
207
+ throw malformed(this.label, place, refused.reason);
661
208
  }
662
- throw malformed(
663
- label,
664
- at,
665
- named.length === 0
666
- ? 'names no declaration: an entry carries "symbol", "module" with "name", or "global"'
667
- : `carries ${spell(named)}, which is not one of "symbol", "module" with "name", or "global"`,
668
- );
669
209
  }
670
210
 
671
- /** One array of declaration entries, absent where the document does not write it. */
672
- function readDeclarations(
673
- parent: Record<string, unknown> | undefined,
674
- name: string,
675
- path: string,
676
- label: string,
677
- ): readonly DeclarationEntry[] | undefined {
678
- const value = member(parent, name);
679
- if (value === undefined) {
680
- return undefined;
681
- }
682
- if (!Array.isArray(value)) {
683
- throw malformed(label, path, "is not an array");
684
- }
685
- return (value as unknown[]).map((entry, index) =>
686
- readDeclaration(entry, `${path}[${String(index)}]`, label),
687
- );
688
- }
689
-
690
- /**
691
- * One lifecycle contract. The member names are required, and the entry names at least
692
- * one component declaration or one base class, because a contract naming neither
693
- * makes no class a component and so retains nothing.
694
- */
695
- function readLifecycleContract(entry: unknown, at: string, label: string): LifecycleContract {
696
- if (!isRecord(entry)) {
697
- throw malformed(label, at, "is not an object");
698
- }
699
- const components = readDeclarations(entry, "components", `${at}.components`, label) ?? [];
700
- const bases = readDeclarations(entry, "bases", `${at}.bases`, label) ?? [];
701
- const members = readStrings(entry, "members", `${at}.members`, label, 1);
702
- if (members === undefined) {
703
- throw malformed(label, `${at}.members`, "is required and names nothing");
704
- }
705
- if (components.length === 0 && bases.length === 0) {
706
- throw malformed(
707
- label,
708
- at,
709
- 'names no declaration in "components" and no class in "bases", so it makes no class a component',
710
- );
711
- }
712
- return { components, bases, members };
713
- }
714
-
715
- /** The lifecycle contracts, one per framework the project declares. */
716
- function readLifecycleContracts(
717
- ts: Record<string, unknown> | undefined,
718
- label: string,
719
- ): readonly LifecycleContract[] | undefined {
720
- const path = "ts.lifecycle_contracts";
721
- const value = member(ts, "lifecycle_contracts");
722
- if (value === undefined) {
723
- return undefined;
724
- }
725
- if (!Array.isArray(value)) {
726
- throw malformed(label, path, "is not an array");
727
- }
728
- return (value as unknown[]).map((entry, index) =>
729
- readLifecycleContract(entry, `${path}[${String(index)}]`, label),
730
- );
211
+ /** One source a resolution reads, with what it supplies. */
212
+ interface Held {
213
+ readonly source: Source;
214
+ readonly label: string;
215
+ readonly supplied: Supplied;
731
216
  }
732
217
 
733
- /** One string member a provider entry requires, refused where it fails `pattern`. */
734
- function readMatching(
735
- entry: Record<string, unknown>,
736
- name: string,
737
- at: string,
738
- label: string,
739
- pattern: RegExp,
740
- ): string {
741
- const path = `${at}.${name}`;
742
- const value = requireMember(readString(entry, name, path, label), path, label);
743
- if (!pattern.test(value)) {
744
- throw malformed(label, path, `${JSON.stringify(value)} does not match ${pattern.source}`);
745
- }
746
- return value;
747
- }
218
+ const supplies = (): Supplied => ({ values: new Map(), keyed: new Map() });
748
219
 
749
- /**
750
- * One provider entry, read as the one shape its members name: a member only an
751
- * acquirable analyzer carries makes it acquirable, and then it carries all three.
752
- */
753
- function readProvider(entry: unknown, at: string, label: string): Provider {
754
- if (!isRecord(entry)) {
755
- throw malformed(label, at, "is not an object");
756
- }
757
- const name = readMatching(entry, "name", at, label, ANALYZER_NAME);
758
- const languages = readStrings(entry, "languages", `${at}.languages`, label, 1, LANGUAGES);
759
- if (languages === undefined) {
760
- throw malformed(label, `${at}.languages`, "is required and names nothing");
220
+ /** Reads and checks one configuration document. */
221
+ function documentSource(source: Source, document: Document): Held {
222
+ const { text, label } = document;
223
+ refuseOverBound(label, text);
224
+ let read;
225
+ try {
226
+ read = readStrict(text);
227
+ } catch (error: unknown) {
228
+ if (!(error instanceof StrictJSONError)) {
229
+ throw error;
230
+ }
231
+ throw malformed(label, dotted(error.place), locatedFault(text, error));
761
232
  }
762
- const commandPath = `${at}.command`;
763
- const command = requireMember(
764
- readString(entry, "command", commandPath, label),
765
- commandPath,
766
- label,
767
- );
768
- if (!ACQUIRABLE_MEMBERS.some((member) => Object.hasOwn(entry, member))) {
769
- return { shape: "installed", name, languages, command };
233
+ if (!isRecord(read.value)) {
234
+ throw malformed(label, "", "is not one JSON object");
770
235
  }
771
- return {
772
- shape: "acquirable",
773
- name,
774
- languages,
775
- command,
776
- source: readMatching(entry, "source", at, label, PROVIDER_SOURCE),
777
- version: readMatching(entry, "version", at, label, PROVIDER_VERSION),
778
- digest: readMatching(entry, "digest", at, label, PROVIDER_DIGEST),
779
- };
236
+ const supplied = supplies();
237
+ new Walk(label, read.fractions, supplied).section("", read.value);
238
+ return { source, label, supplied };
780
239
  }
781
240
 
782
- /**
783
- * The provider list. Every file a run writes for an analyzer is keyed by the
784
- * entry's name, so a list naming one name twice is refused at the later entry.
785
- */
786
- function readProviders(
787
- providers: Record<string, unknown> | undefined,
788
- label: string,
789
- ): readonly Provider[] | undefined {
790
- const path = "providers.analyzers";
791
- const value = member(providers, "analyzers");
792
- if (value === undefined) {
793
- return undefined;
794
- }
795
- if (!Array.isArray(value)) {
796
- throw malformed(label, path, "is not an array");
797
- }
798
- const read = (value as unknown[]).map((entry, index) =>
799
- readProvider(entry, `${path}[${String(index)}]`, label),
800
- );
801
- const first = new Map<string, number>();
802
- read.forEach((entry, index) => {
803
- const earlier = first.get(entry.name);
804
- if (earlier !== undefined) {
805
- throw malformed(
806
- label,
807
- `${path}[${String(index)}].name`,
808
- `${JSON.stringify(entry.name)} is already the name of ${path}[${String(earlier)}], ` +
809
- "and every entry's name keys the files a run writes for it",
810
- );
241
+ /** The command line as a source: each flag checked as the one-setting document it supplies. */
242
+ function flagSource(flags: ReadonlyMap<string, string>): Held {
243
+ const supplied = supplies();
244
+ for (const key of [...flags.keys()].sort(bytewise)) {
245
+ const row = ROWS.get(key);
246
+ const text = flags.get(key) ?? "";
247
+ if (row?.option === undefined) {
248
+ throw unimplemented("the command line", key, "no flag supplies it");
811
249
  }
812
- first.set(entry.name, index);
813
- });
814
- return read;
815
- }
816
-
817
- /**
818
- * Reads one already-parsed configuration document as the settings it supplies,
819
- * refusing every constraint the closed key list declares that a parse cannot
820
- * express: a value of the wrong type, a value outside a closed set, an array that
821
- * repeats an entry or holds too few, and a count below its minimum.
822
- */
823
- function readDoc(value: unknown, label: string): Doc {
824
- if (!isRecord(value)) {
825
- throw malformed(label, "", "is not one JSON object");
826
- }
827
- const analysis = section(value, "analysis", "analysis", label);
828
- const matrix = section(analysis, "matrix", "analysis.matrix", label);
829
- const target = section(value, "target", "target", label);
830
- const consumers = section(value, "consumers", "consumers", label);
831
- const roots = section(value, "roots", "roots", label);
832
- const reporters = section(value, "reporters", "reporters", label);
833
- const providers = section(value, "providers", "providers", label);
834
- const ts = section(value, "ts", "ts", label);
835
- section(value, "go", "go", label);
836
- section(value, "provenance", "provenance", label);
837
-
838
- const contractVersion = readString(value, "contract_version", "contract_version", label);
839
- if (contractVersion !== undefined && !CONTRACT_VERSION_PATTERN.test(contractVersion)) {
840
- throw malformed(
841
- label,
842
- "contract_version",
843
- `${JSON.stringify(contractVersion)} is not a semantic version`,
250
+ const integer = row.value?.["type"] === "integer" && /^-?[0-9]+$/u.test(text);
251
+ let document: unknown = integer ? Number(text) : text;
252
+ for (const name of key.split(".").reverse()) {
253
+ document = { [name]: document };
254
+ }
255
+ new Walk(`--${row.option}`, new Map(), supplied).section(
256
+ "",
257
+ document as Record<string, unknown>,
844
258
  );
845
259
  }
846
-
847
- return {
848
- contractVersion,
849
- targetKind: readTargetKind(target, label),
850
- languages: readStrings(analysis, "languages", "analysis.languages", label, 0, LANGUAGES),
851
- minConfidence: readEnum(
852
- analysis,
853
- "min_confidence",
854
- "analysis.min_confidence",
855
- label,
856
- CONFIDENCES,
857
- ),
858
- generatedFiles: readEnum(
859
- analysis,
860
- "generated_files",
861
- "analysis.generated_files",
862
- label,
863
- GENERATED_FILES,
864
- ),
865
- consumerTests: readEnum(
866
- analysis,
867
- "consumer_tests",
868
- "analysis.consumer_tests",
869
- label,
870
- CONSUMER_TESTS,
871
- ),
872
- configurations: readConfigurations(analysis, label),
873
- matrixComplete: readBoolean(matrix, "complete", "analysis.matrix.complete", label),
874
- templateDirs: readStrings(analysis, "template_dirs", "analysis.template_dirs", label, 0),
875
- templateDelimiters: readDelimiters(analysis, label),
876
- consumersComplete: readBoolean(consumers, "complete", "consumers.complete", label),
877
- rootPatterns: readStrings(roots, "patterns", "roots.patterns", label, 0),
878
- severity: readSeverity(value, label),
879
- exemptionsDisabled: readExemptions(value, label),
880
- formats: readStrings(reporters, "formats", "reporters.formats", label, 1, FORMATS),
881
- sort: readEnum(reporters, "sort", "reporters.sort", label, SORTS),
882
- cascade: readEnum(reporters, "cascade", "reporters.cascade", label, CASCADES),
883
- maxFindings: readCount(reporters, "max_findings", "reporters.max_findings", label),
884
- failOn: readEnum(reporters, "fail_on", "reporters.fail_on", label, SEVERITIES),
885
- providers: readProviders(providers, label),
886
- testFiles: readStrings(ts, "test_files", "ts.test_files", label, 1),
887
- entryFiles: readStrings(ts, "entry_files", "ts.entry_files", label, 0),
888
- componentExtensions: readComponentExtensions(ts, label),
889
- disabledConventions: readDisabledConventions(ts, label),
890
- injectionRegistrations: readDeclarations(
891
- ts,
892
- "injection_registrations",
893
- "ts.injection_registrations",
894
- label,
895
- ),
896
- lifecycleContracts: readLifecycleContracts(ts, label),
897
- serializers: readDeclarations(ts, "serializers", "ts.serializers", label),
898
- rootsComplete: readBoolean(ts, "roots_complete", "ts.roots_complete", label),
899
- };
260
+ return { source: "flag", label: "", supplied };
900
261
  }
901
262
 
902
- /** Parses one document's text and walks it against the closed key list. */
903
- function parseDocument(text: string, node: KeyNode, label: string): unknown {
904
- let value: unknown;
905
- try {
906
- value = JSON.parse(text);
907
- } catch (error: unknown) {
908
- throw malformed(label, "", error instanceof Error ? error.message : String(error));
909
- }
910
- checkDocument(text, node, label);
911
- return value;
263
+ function originOf(held: Held, key: string): Origin {
264
+ const label = held.source === "flag" ? `--${String(ROWS.get(key)?.option)}` : held.label;
265
+ return { source: held.source, label };
912
266
  }
913
267
 
914
- /** Names a document in a refusal: the path the invocation passed, or what it is. */
915
- function labelOr(label: string | undefined, named: string): string {
916
- return label === undefined || label === "" ? named : label;
268
+ function searched(named: string, document: Document | undefined): string {
269
+ return `${named} (${document === undefined ? "not present" : document.label})`;
917
270
  }
918
271
 
919
- /**
920
- * Decodes the documents one invocation supplies, in the order they outrank each
921
- * other, skipping every source it does not carry.
922
- */
923
- function readSources(inputs: Inputs): DocSource[] {
924
- const sources: DocSource[] = [];
925
- const flagLabels = inputs.flagLabels ?? new Map<string, string>();
926
- if (inputs.flags !== undefined) {
927
- const flat = parseDocument(inputs.flags, { kind: "map" }, FLAGS_LABEL);
928
- if (!isRecord(flat)) {
929
- throw malformed(FLAGS_LABEL, "", "is not one JSON object");
930
- }
931
- sources.push({
932
- doc: readDoc(nestFlags(flat, FLAGS_LABEL), FLAGS_LABEL),
933
- kind: "flag",
934
- label: "",
935
- flagLabels,
936
- });
272
+ /** Sets the value at one dotted key of the resolved tree, whose sections the table lists first. */
273
+ function setAt(tree: Record<string, unknown>, key: string, value: unknown): void {
274
+ const names = key.split(".");
275
+ let into = tree;
276
+ for (const name of names.slice(0, -1)) {
277
+ into = into[name] as Record<string, unknown>;
937
278
  }
938
- const held: readonly { kind: Source; named: string; text?: string; label?: string }[] = [
939
- {
940
- kind: "repository",
941
- named: "the repository configuration",
942
- ...(inputs.repository === undefined ? {} : { text: inputs.repository }),
943
- ...(inputs.repositoryLabel === undefined ? {} : { label: inputs.repositoryLabel }),
944
- },
945
- {
946
- kind: "central",
947
- named: "the central configuration",
948
- ...(inputs.central === undefined ? {} : { text: inputs.central }),
949
- ...(inputs.centralLabel === undefined ? {} : { label: inputs.centralLabel }),
950
- },
951
- ];
952
- for (const entry of held) {
953
- if (entry.text === undefined) {
954
- continue;
955
- }
956
- const label = labelOr(entry.label, entry.named);
957
- sources.push({
958
- doc: readDoc(parseDocument(entry.text, SCHEMA_ROOT, label), label),
959
- kind: entry.kind,
960
- label: entry.label ?? "",
961
- flagLabels,
962
- });
963
- }
964
- return sources;
279
+ into[names.at(-1) ?? key] = value;
965
280
  }
966
281
 
967
282
  /**
968
- * The value the highest-ranked source carrying one supplies, with where it came
969
- * from recorded, or the default.
283
+ * Every setting this analyzer reads, each from the highest-ranked source carrying it,
284
+ * else its default, in the closed key list's order. A setting with no default no
285
+ * source supplies is refused where its row is required and `enforce` holds, and left
286
+ * unset otherwise. A keyed section's members overlay its default's, member by member.
970
287
  */
971
- function settle<T>(
972
- fallback: T,
973
- path: string,
974
- provenance: Map<string, Origin>,
975
- sources: readonly DocSource[],
976
- read: (doc: Doc) => T | undefined,
977
- ): T {
978
- for (const source of sources) {
979
- const value = read(source.doc);
980
- if (value !== undefined) {
981
- provenance.set(path, originOf(source, path));
982
- return value;
983
- }
288
+ function settle(
289
+ inputs: Inputs,
290
+ enforce: boolean,
291
+ ): { config: PartialConfig; provenance: Provenance } {
292
+ const sources: Held[] = [];
293
+ if (inputs.flags !== undefined && inputs.flags.size > 0) {
294
+ sources.push(flagSource(inputs.flags));
984
295
  }
985
- provenance.set(path, { source: "default", label: "" });
986
- return fallback;
987
- }
988
-
989
- /**
990
- * The severity object, resolved one code at a time so a code only the central
991
- * configuration names keeps its central value. An object that resolves empty
992
- * carries one entry for the object itself.
993
- */
994
- function settleSeverity(
995
- provenance: Map<string, Origin>,
996
- sources: readonly DocSource[],
997
- ): ReadonlyMap<string, Severity> {
998
- const codes = new Set<string>();
999
- for (const source of sources) {
1000
- for (const code of source.doc.severity?.keys() ?? []) {
1001
- codes.add(code);
1002
- }
1003
- }
1004
- const severity = new Map<string, Severity>();
1005
- for (const code of [...codes].sort()) {
1006
- const path = `${SEVERITY_SECTION}.${code}`;
1007
- for (const source of sources) {
1008
- const value = source.doc.severity?.get(code);
1009
- if (value === undefined) {
1010
- continue;
1011
- }
1012
- severity.set(code, value);
1013
- provenance.set(path, originOf(source, path));
1014
- break;
1015
- }
296
+ if (inputs.repository !== undefined) {
297
+ sources.push(documentSource("repository", inputs.repository));
1016
298
  }
1017
- if (codes.size > 0) {
1018
- return severity;
299
+ if (inputs.central !== undefined) {
300
+ sources.push(documentSource("central", inputs.central));
1019
301
  }
1020
- provenance.set(SEVERITY_SECTION, { source: "default", label: "" });
1021
- for (const source of sources) {
1022
- if (source.doc.severity !== undefined) {
1023
- provenance.set(SEVERITY_SECTION, originOf(source, SEVERITY_SECTION));
1024
- break;
302
+ const tree: Record<string, unknown> = {};
303
+ const provenance = new Map<string, Origin>();
304
+ for (const row of SETTINGS) {
305
+ if (!row.read || row.role === "ignored") {
306
+ continue;
1025
307
  }
1026
- }
1027
- return severity;
1028
- }
1029
-
1030
- /** Names one configuration source and whether the invocation carried it. */
1031
- function describeSource(
1032
- named: string,
1033
- text: string | undefined,
1034
- label: string | undefined,
1035
- ): string {
1036
- if (text === undefined) {
1037
- return `${named} (not present)`;
1038
- }
1039
- return `${named} (${labelOr(label, "unnamed")})`;
1040
- }
1041
-
1042
- /**
1043
- * Refuses a provenance entry whose source names no file or flag, which would
1044
- * print a provenance value the closed key list refuses to read back.
1045
- */
1046
- function checkLabels(provenance: ReadonlyMap<string, Origin>): void {
1047
- for (const path of [...provenance.keys()].sort()) {
1048
- const origin = provenance.get(path);
1049
- if (origin !== undefined && origin.source !== "default" && origin.label === "") {
1050
- throw malformed(
1051
- origin.source === "flag" ? FLAGS_LABEL : origin.source,
1052
- path,
1053
- `came from ${origin.source}, which names no file or flag`,
1054
- );
308
+ if (row.role === "section") {
309
+ setAt(tree, row.key, {});
310
+ } else if (row.role === "keyed") {
311
+ const members: Record<string, unknown> = isRecord(row.default) ? { ...row.default } : {};
312
+ provenance.set(row.key, { source: "default", label: "" });
313
+ for (const name of Object.keys(members)) {
314
+ provenance.set(`${row.key}.${name}`, { source: "default", label: "" });
315
+ }
316
+ for (const held of sources.toReversed()) {
317
+ const named = held.supplied.keyed.get(row.key);
318
+ if (named === undefined) {
319
+ continue;
320
+ }
321
+ provenance.set(row.key, originOf(held, row.key));
322
+ for (const [name, value] of Object.entries(named)) {
323
+ members[name] = value;
324
+ provenance.set(`${row.key}.${name}`, originOf(held, row.key));
325
+ }
326
+ }
327
+ if (Object.keys(members).length > 0) {
328
+ provenance.delete(row.key);
329
+ }
330
+ setAt(tree, row.key, canonical(members));
331
+ } else {
332
+ const held = sources.find((one) => one.supplied.values.has(row.key));
333
+ if (held !== undefined) {
334
+ setAt(tree, row.key, held.supplied.values.get(row.key));
335
+ provenance.set(row.key, originOf(held, row.key));
336
+ } else if (row.default !== undefined) {
337
+ setAt(tree, row.key, canonical(row.default));
338
+ provenance.set(row.key, { source: "default", label: "" });
339
+ } else if (enforce && row.required === true) {
340
+ throw new UsageError(
341
+ `${row.key} is not set, it has no default and is never inferred: searched ` +
342
+ `${searched("the repository configuration", inputs.repository)} and ` +
343
+ searched("the central configuration", inputs.central),
344
+ );
345
+ }
1055
346
  }
1056
347
  }
348
+ // gen-config.ts marks a member optional exactly where this loop may leave it unset.
349
+ return { config: tree as unknown as PartialConfig, provenance };
1057
350
  }
1058
351
 
1059
352
  /**
1060
- * Applies a command-line flag over the repository configuration over the central
353
+ * Applies the command-line flags over the repository configuration over the central
1061
354
  * configuration over the default each key declares, and returns the resolved
1062
- * configuration with the origin of every setting.
1063
- *
1064
- * It throws a {@link ConfigError} for a document that names a key the closed key
1065
- * list does not declare, a document that names one key twice, and a resolved
1066
- * configuration no source supplied a target kind for. Every one of them maps to
1067
- * the usage exit code.
1068
- */
1069
- export function resolve(inputs: Inputs): { config: Config; provenance: Provenance } {
1070
- const { config, provenance } = settleAll(inputs);
1071
- if (config.targetKind === "") {
1072
- throw new ConfigError(
1073
- "missing-target-kind",
1074
- "target.kind",
1075
- "target.kind is not set, it has no default and is never inferred: searched " +
1076
- `${describeSource("the repository configuration", inputs.repository, inputs.repositoryLabel)} and ` +
1077
- describeSource("the central configuration", inputs.central, inputs.centralLabel),
1078
- );
1079
- }
1080
- checkLabels(provenance);
1081
- return { config, provenance };
1082
- }
1083
-
1084
- /**
1085
- * The configuration the documents resolve to, every document read and refused as
1086
- * {@link resolve} refuses it, with no target kind required of them: which projects a
1087
- * run analyzes does not depend on the kind, so a question about the projects asks
1088
- * none. The kind is empty where no document names one.
355
+ * configuration with the origin of every setting: its keys in the closed key list's
356
+ * order, every object's members inside a value ascending. Every refusal is a
357
+ * {@link UsageError}.
1089
358
  */
1090
- export function resolveUnkinded(inputs: Inputs): Config {
1091
- return settleAll(inputs).config;
359
+ export function resolve(inputs: Inputs): { config: ResolvedConfig; provenance: Provenance } {
360
+ const { config, provenance } = settle(inputs, true);
361
+ return { config: config as ResolvedConfig, provenance };
1092
362
  }
1093
363
 
1094
364
  /**
1095
- * Every setting the documents supply, each from the highest-ranked source carrying
1096
- * it, with the origin of each. The target kind is empty where no source names one.
365
+ * The configuration the sources resolve to, refused as {@link resolve} refuses it but
366
+ * with no target kind required: which projects a run analyzes asks none.
1097
367
  */
1098
- function settleAll(inputs: Inputs): { config: Config; provenance: Map<string, Origin> } {
1099
- const sources = readSources(inputs);
1100
- const defaults = defaultConfig();
1101
- const provenance = new Map<string, Origin>();
1102
- const at = <T>(path: string, fallback: T, read: (doc: Doc) => T | undefined): T =>
1103
- settle(fallback, path, provenance, sources, read);
1104
-
1105
- const config: Config = {
1106
- contractVersion: at("contract_version", defaults.contractVersion, (doc) => doc.contractVersion),
1107
- targetKind: at<TargetKindSetting | "">("target.kind", "", (doc) => doc.targetKind),
1108
- analysis: {
1109
- languages: at("analysis.languages", defaults.analysis.languages, (doc) => doc.languages),
1110
- minConfidence: at(
1111
- "analysis.min_confidence",
1112
- defaults.analysis.minConfidence,
1113
- (doc) => doc.minConfidence,
1114
- ),
1115
- generatedFiles: at(
1116
- "analysis.generated_files",
1117
- defaults.analysis.generatedFiles,
1118
- (doc) => doc.generatedFiles,
1119
- ),
1120
- consumerTests: at(
1121
- "analysis.consumer_tests",
1122
- defaults.analysis.consumerTests,
1123
- (doc) => doc.consumerTests,
1124
- ),
1125
- configurations: at(
1126
- "analysis.configurations",
1127
- defaults.analysis.configurations,
1128
- (doc) => doc.configurations,
1129
- ),
1130
- matrixComplete: at(
1131
- "analysis.matrix.complete",
1132
- defaults.analysis.matrixComplete,
1133
- (doc) => doc.matrixComplete,
1134
- ),
1135
- templateDirs: at(
1136
- "analysis.template_dirs",
1137
- defaults.analysis.templateDirs,
1138
- (doc) => doc.templateDirs,
1139
- ),
1140
- templateDelimiters: at(
1141
- "analysis.template_delimiters",
1142
- defaults.analysis.templateDelimiters,
1143
- (doc) => doc.templateDelimiters,
1144
- ),
1145
- },
1146
- consumersComplete: at(
1147
- "consumers.complete",
1148
- defaults.consumersComplete,
1149
- (doc) => doc.consumersComplete,
1150
- ),
1151
- rootPatterns: at("roots.patterns", defaults.rootPatterns, (doc) => doc.rootPatterns),
1152
- severity: settleSeverity(provenance, sources),
1153
- exemptionsDisabled: at(
1154
- "exemptions.disabled",
1155
- defaults.exemptionsDisabled,
1156
- (doc) => doc.exemptionsDisabled,
1157
- ),
1158
- reporters: {
1159
- formats: at("reporters.formats", defaults.reporters.formats, (doc) => doc.formats),
1160
- sort: at("reporters.sort", defaults.reporters.sort, (doc) => doc.sort),
1161
- cascade: at("reporters.cascade", defaults.reporters.cascade, (doc) => doc.cascade),
1162
- maxFindings: at(
1163
- "reporters.max_findings",
1164
- defaults.reporters.maxFindings,
1165
- (doc) => doc.maxFindings,
1166
- ),
1167
- failOn: at("reporters.fail_on", defaults.reporters.failOn, (doc) => doc.failOn),
1168
- },
1169
- providers: {
1170
- analyzers: at("providers.analyzers", defaults.providers.analyzers, (doc) => doc.providers),
1171
- },
1172
- ts: {
1173
- testFiles: at("ts.test_files", defaults.ts.testFiles, (doc) => doc.testFiles),
1174
- entryFiles: at("ts.entry_files", defaults.ts.entryFiles, (doc) => doc.entryFiles),
1175
- componentExtensions: at(
1176
- "ts.component_extensions",
1177
- defaults.ts.componentExtensions,
1178
- (doc) => doc.componentExtensions,
1179
- ),
1180
- disabledConventions: at(
1181
- "ts.disabled_conventions",
1182
- defaults.ts.disabledConventions,
1183
- (doc) => doc.disabledConventions,
1184
- ),
1185
- injectionRegistrations: at(
1186
- "ts.injection_registrations",
1187
- defaults.ts.injectionRegistrations,
1188
- (doc) => doc.injectionRegistrations,
1189
- ),
1190
- lifecycleContracts: at(
1191
- "ts.lifecycle_contracts",
1192
- defaults.ts.lifecycleContracts,
1193
- (doc) => doc.lifecycleContracts,
1194
- ),
1195
- serializers: at("ts.serializers", defaults.ts.serializers, (doc) => doc.serializers),
1196
- rootsComplete: at("ts.roots_complete", defaults.ts.rootsComplete, (doc) => doc.rootsComplete),
1197
- },
1198
- };
1199
-
1200
- return { config, provenance };
368
+ export function resolvePartial(inputs: Inputs): PartialConfig {
369
+ return settle(inputs, false).config;
1201
370
  }