@cplieger/deadset-ts 8.0.0-dev.7 → 8.0.0-dev.9
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/dist/bin/node-host.js +21 -3
- package/dist/src/analysis.js +94 -299
- package/dist/src/config.js +6 -83
- package/dist/src/configuration-files.js +2 -9
- package/dist/src/configured-declarations.js +15 -20
- package/dist/src/consumers.js +3 -30
- package/dist/src/contract/config.generated.js +544 -0
- package/dist/src/contract/contract.generated.js +20 -10
- package/dist/src/contract/report.generated.js +3 -2
- package/dist/src/conventions.js +3 -8
- package/dist/src/edges.js +36 -112
- package/dist/src/exempt-classes.js +0 -2
- package/dist/src/exempt.js +1 -1
- package/dist/src/failure.js +36 -0
- package/dist/src/findings/completion.js +5 -4
- package/dist/src/findings/emitters.js +1 -1
- package/dist/src/findings/intra-function.js +1 -1
- package/dist/src/findings/non-code-artifacts.js +2 -2
- package/dist/src/findings/unused-declarations.js +1 -1
- package/dist/src/findings/visibility-narrowing.js +2 -2
- package/dist/src/findings-pass.js +1 -1
- package/dist/src/framework-lifecycle.js +7 -7
- package/dist/src/fs/document.js +52 -0
- package/dist/src/fs/manifest.js +7 -0
- package/dist/src/injection-container.js +1 -1
- package/dist/src/json-document.js +194 -67
- package/dist/src/kinds.js +0 -7
- package/dist/src/load/classify.js +236 -0
- package/dist/src/load/diagnostics.js +79 -0
- package/dist/src/load/discover.js +230 -0
- package/dist/src/{emit-map.js → load/emit-map.js} +1 -1
- package/dist/src/load/expected-module.js +136 -0
- package/dist/src/load/load.js +22 -0
- package/dist/src/load/module-lookup.js +37 -0
- package/dist/src/load/plan.js +155 -0
- package/dist/src/{test-globals.js → load/test-globals.js} +51 -56
- package/dist/src/load/test-joins.js +82 -0
- package/dist/src/{workspace-manifests.js → load/workspace-manifests.js} +3 -3
- package/dist/src/{workspace-programs.js → load/workspace-programs.js} +37 -40
- package/dist/src/{workspace-resolution.js → load/workspace-resolution.js} +21 -59
- package/dist/src/{workspace-run.js → load/workspace-run.js} +4 -3
- package/dist/src/{workspace.js → load/workspace.js} +21 -17
- package/dist/src/outside-tests.js +1 -1
- package/dist/src/print.js +10 -79
- package/dist/src/report-schema.js +3 -224
- package/dist/src/resolve.js +245 -715
- package/dist/src/run.js +59 -72
- package/dist/src/schema.js +320 -344
- package/dist/src/scope.js +37 -113
- package/dist/src/serialization-contract.js +1 -1
- package/dist/src/session.js +22 -28
- package/dist/src/source-files.js +1 -1
- package/dist/src/suppress-file.js +15 -17
- package/dist/src/suppress.js +3 -3
- package/dist/src/template-field.js +4 -4
- package/dist/src/type-errors.js +11 -216
- package/dist/src/usage.js +10 -0
- package/dist/src/verbs/analyze.js +100 -122
- package/dist/src/verbs/describe.js +5 -12
- package/dist/src/verbs/explain.js +3 -3
- package/dist/src/verbs/print-projects.js +2 -2
- package/dist/src/verbs/version.js +12 -2
- package/jsr.json +1 -1
- package/package.json +1 -1
- package/src/analysis.ts +95 -460
- package/src/config.ts +31 -298
- package/src/configuration-files.ts +8 -10
- package/src/configured-declarations.ts +18 -23
- package/src/consumers.ts +6 -57
- package/src/contract/config.generated.ts +798 -0
- package/src/contract/contract.generated.ts +20 -10
- package/src/contract/report.generated.ts +3 -2
- package/src/conventions.ts +6 -12
- package/src/edges.ts +39 -124
- package/src/exempt-classes.ts +0 -5
- package/src/exempt.ts +4 -4
- package/src/failure.ts +63 -0
- package/src/findings/completion.ts +5 -6
- package/src/findings/emitter.ts +2 -2
- package/src/findings/emitters.ts +1 -1
- package/src/findings/intra-function.ts +1 -1
- package/src/findings/non-code-artifacts.ts +4 -4
- package/src/findings/unused-declarations.ts +1 -1
- package/src/findings/visibility-narrowing.ts +2 -2
- package/src/findings-pass.ts +3 -3
- package/src/framework-lifecycle.ts +7 -7
- package/src/fs/document.ts +70 -0
- package/src/fs/manifest.ts +8 -0
- package/src/host.ts +12 -5
- package/src/injection-container.ts +1 -1
- package/src/json-document.ts +236 -76
- package/src/kinds.ts +0 -8
- package/src/load/classify.ts +352 -0
- package/src/load/diagnostics.ts +141 -0
- package/src/load/discover.ts +378 -0
- package/src/{emit-map.ts → load/emit-map.ts} +1 -1
- package/src/load/expected-module.ts +188 -0
- package/src/load/load.ts +61 -0
- package/src/load/module-lookup.ts +50 -0
- package/src/load/plan.ts +258 -0
- package/src/{test-globals.ts → load/test-globals.ts} +59 -65
- package/src/load/test-joins.ts +98 -0
- package/src/{workspace-manifests.ts → load/workspace-manifests.ts} +4 -4
- package/src/{workspace-programs.ts → load/workspace-programs.ts} +59 -53
- package/src/{workspace-resolution.ts → load/workspace-resolution.ts} +31 -78
- package/src/{workspace-run.ts → load/workspace-run.ts} +14 -11
- package/src/{workspace.ts → load/workspace.ts} +28 -21
- package/src/outside-tests.ts +1 -1
- package/src/partial.ts +1 -1
- package/src/print.ts +11 -92
- package/src/report-schema.ts +3 -299
- package/src/resolve.ts +281 -1105
- package/src/run.ts +68 -80
- package/src/schema.ts +391 -389
- package/src/scope.ts +45 -131
- package/src/serialization-contract.ts +1 -1
- package/src/session.ts +52 -53
- package/src/source-files.ts +1 -1
- package/src/suppress-file.ts +15 -20
- package/src/suppress.ts +3 -3
- package/src/template-field.ts +10 -7
- package/src/type-errors.ts +18 -288
- package/src/usage.ts +10 -0
- package/src/verbs/analyze.ts +139 -157
- package/src/verbs/describe.ts +4 -11
- package/src/verbs/explain.ts +3 -7
- package/src/verbs/print-projects.ts +2 -2
- package/src/verbs/verb.ts +2 -2
- package/src/verbs/version.ts +13 -2
- package/dist/src/base-url.js +0 -18
- package/dist/src/discover.js +0 -374
- package/dist/src/setup-failure.js +0 -19
- package/dist/src/test-joins.js +0 -101
- package/src/base-url.ts +0 -24
- package/src/discover.ts +0 -548
- package/src/setup-failure.ts +0 -37
- package/src/test-joins.ts +0 -118
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one read of a document the target holds at its root, which a checkout under analysis
|
|
3
|
+
* controls: the repository configuration, the ignore file, the baseline and the edges
|
|
4
|
+
* document. The read stays inside the target root, takes a regular file alone, and holds no
|
|
5
|
+
* more of it than the Contract's document bound.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { CONFIG_MAX_BYTES } from "../contract/config.generated.ts";
|
|
9
|
+
import type { Host } from "../host.ts";
|
|
10
|
+
import { failureExit } from "../failure.ts";
|
|
11
|
+
import { joinPath, relativePath } from "../paths.ts";
|
|
12
|
+
import { message } from "../product.ts";
|
|
13
|
+
import { utf8Bytes } from "../reporters/sha256.ts";
|
|
14
|
+
import { UsageError } from "../usage.ts";
|
|
15
|
+
|
|
16
|
+
function unreadable(label: string, reason: string): Error {
|
|
17
|
+
return failureExit(
|
|
18
|
+
"unreadable-document",
|
|
19
|
+
`${label}: ${reason}: make it a regular file inside the target root that the run can read, or remove it`,
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Refuses a document past the Contract's bound with the usage code, naming it `label`. */
|
|
24
|
+
export function refuseOverBound(label: string, text: string): void {
|
|
25
|
+
if (utf8Bytes(text).length > CONFIG_MAX_BYTES) {
|
|
26
|
+
throw new UsageError(`${label}: holds more than ${String(CONFIG_MAX_BYTES)} bytes`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The text of the document `name` at the absolute `targetRoot`, undefined where nothing is
|
|
32
|
+
* there. One whose name resolves outside the target root or to anything but a regular file,
|
|
33
|
+
* or whose read fails, is refused by `refuse` naming it `label`, nothing read from it. A
|
|
34
|
+
* document past the bound is read one byte past it and refused as {@link refuseOverBound} does.
|
|
35
|
+
*/
|
|
36
|
+
export function readTargetDocument(
|
|
37
|
+
host: Host,
|
|
38
|
+
targetRoot: string,
|
|
39
|
+
name: string,
|
|
40
|
+
label = name,
|
|
41
|
+
refuse: (label: string, reason: string) => Error = unreadable,
|
|
42
|
+
): string | undefined {
|
|
43
|
+
const attempt = <T>(read: () => T): T => {
|
|
44
|
+
try {
|
|
45
|
+
return read();
|
|
46
|
+
} catch (error: unknown) {
|
|
47
|
+
throw refuse(label, message(error));
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const path = joinPath(targetRoot, name);
|
|
51
|
+
const kind = attempt(() => host.kindOf(path));
|
|
52
|
+
if (kind === "absent") {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
const real = attempt(() => host.realPath(path));
|
|
56
|
+
if (
|
|
57
|
+
relativePath(
|
|
58
|
+
attempt(() => host.realPath(targetRoot)),
|
|
59
|
+
real,
|
|
60
|
+
) === undefined
|
|
61
|
+
) {
|
|
62
|
+
throw refuse(label, `resolves to ${real}, outside the target root`);
|
|
63
|
+
}
|
|
64
|
+
if (kind !== "file") {
|
|
65
|
+
throw refuse(label, "is not a regular file");
|
|
66
|
+
}
|
|
67
|
+
const text = attempt(() => host.readFile(real, CONFIG_MAX_BYTES + 1));
|
|
68
|
+
refuseOverBound(label, text);
|
|
69
|
+
return text;
|
|
70
|
+
}
|
package/src/fs/manifest.ts
CHANGED
|
@@ -8,6 +8,14 @@ export const MANIFEST = "package.json";
|
|
|
8
8
|
/** The directory a package manager installs packages into, whose files are no project's own. */
|
|
9
9
|
export const INSTALLED_PACKAGES = "node_modules";
|
|
10
10
|
|
|
11
|
+
/** The manifest members a package declares the packages it depends on under. */
|
|
12
|
+
export const DEPENDENCY_SECTIONS = [
|
|
13
|
+
"dependencies",
|
|
14
|
+
"devDependencies",
|
|
15
|
+
"peerDependencies",
|
|
16
|
+
"optionalDependencies",
|
|
17
|
+
] as const;
|
|
18
|
+
|
|
11
19
|
/** Whether one path passes through an installed-packages directory. */
|
|
12
20
|
export function inInstalledPackages(path: string): boolean {
|
|
13
21
|
return path.split("/").includes(INSTALLED_PACKAGES);
|
package/src/host.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/** What one path names. */
|
|
2
|
-
export type PathKind = "file" | "directory" | "absent";
|
|
1
|
+
/** What one path names: `"other"` is anything else there, a named pipe, a device or a socket. */
|
|
2
|
+
export type PathKind = "file" | "directory" | "other" | "absent";
|
|
3
3
|
|
|
4
4
|
/** One entry of a directory. */
|
|
5
5
|
export interface DirectoryEntry {
|
|
@@ -17,11 +17,18 @@ export interface DirectoryEntry {
|
|
|
17
17
|
export interface Host {
|
|
18
18
|
/** The directory a relative path resolves against. */
|
|
19
19
|
workingDirectory(): string;
|
|
20
|
-
/**
|
|
21
|
-
|
|
20
|
+
/**
|
|
21
|
+
* The text of one file, or a throw naming why it cannot be read. Given `limit`, the
|
|
22
|
+
* text of no more than the file's first `limit` bytes, so a caller bounding a document
|
|
23
|
+
* asks for one byte past its bound and never holds a larger file whole.
|
|
24
|
+
*/
|
|
25
|
+
readFile(path: string, limit?: number): string;
|
|
22
26
|
/** The entries of one directory, or a throw naming why it cannot be read. */
|
|
23
27
|
readDirectory(path: string): readonly DirectoryEntry[];
|
|
24
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* What one path names once every symbolic link along it is followed, `"absent"` where
|
|
30
|
+
* nothing is there, or a throw naming why it cannot be known, as for a link loop.
|
|
31
|
+
*/
|
|
25
32
|
kindOf(path: string): PathKind;
|
|
26
33
|
/**
|
|
27
34
|
* The path one path names once every symbolic link along it is followed, or a throw
|
|
@@ -53,7 +53,7 @@ function isInjected(node: Node): boolean {
|
|
|
53
53
|
/** The classes of the target a call whose callee an entry names passes, with that call's entry. */
|
|
54
54
|
function registrations<Brand>(input: DetectorInput<Brand>): readonly Registration[] {
|
|
55
55
|
const { project, held } = input;
|
|
56
|
-
const resolved = resolveEntries(project, held, input.ts.
|
|
56
|
+
const resolved = resolveEntries(project, held, input.ts.injection_registrations);
|
|
57
57
|
if (!resolved.some(namesADeclaration)) {
|
|
58
58
|
return [];
|
|
59
59
|
}
|
package/src/json-document.ts
CHANGED
|
@@ -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
|
|
3
|
-
*
|
|
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
|
-
|
|
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
|
-
/**
|
|
9
|
-
|
|
10
|
-
readonly
|
|
11
|
-
/**
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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,
|
|
113
|
+
constructor(text: string, records: string | undefined) {
|
|
18
114
|
this.text = text;
|
|
19
|
-
this.
|
|
115
|
+
this.records = records;
|
|
20
116
|
}
|
|
21
117
|
|
|
22
|
-
|
|
23
|
-
this.value(
|
|
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
|
|
27
|
-
|
|
28
|
-
|
|
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
|
|
33
|
-
|
|
34
|
-
this.
|
|
35
|
-
|
|
36
|
-
|
|
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(
|
|
43
|
-
this.
|
|
44
|
-
if (
|
|
45
|
-
this.
|
|
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(
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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(
|
|
62
|
-
const
|
|
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.
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
this.
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
this.
|
|
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
|
-
|
|
80
|
-
|
|
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
|
-
|
|
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
|
|
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.
|
|
90
|
-
if (
|
|
91
|
-
this.at
|
|
92
|
-
|
|
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 (
|
|
95
|
-
this.at
|
|
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
|
-
*
|
|
112
|
-
* records
|
|
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
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
return {
|
|
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,
|