@stateward/plugin-errors 1.2.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -8
- package/dist/catch-hides-unexpected.d.ts +4 -1
- package/dist/catch-hides-unexpected.js +87 -11
- package/dist/catch-misses-error.d.ts +1 -1
- package/dist/catch-misses-error.js +63 -20
- package/dist/finally-can-throw.js +46 -6
- package/dist/index.d.ts +3 -1
- package/dist/index.js +1 -0
- package/dist/inference/analysis.d.ts +109 -0
- package/dist/inference/analysis.js +170 -0
- package/dist/inference/builtins.d.ts +7 -2
- package/dist/inference/builtins.js +20 -33
- package/dist/inference/deps.js +2 -2
- package/dist/inference/needs.d.ts +16 -0
- package/dist/inference/needs.js +27 -0
- package/dist/inference/packages.d.ts +24 -32
- package/dist/inference/packages.js +156 -675
- package/dist/inference/project.d.ts +2 -2
- package/dist/inference/project.js +3 -2
- package/dist/inference/solve.d.ts +29 -4
- package/dist/inference/solve.js +34 -5
- package/dist/inference/summarize.d.ts +28 -15
- package/dist/inference/summarize.js +339 -135
- package/dist/inference/summary.d.ts +46 -5
- package/dist/inference/summary.js +89 -13
- package/dist/inference/unreachable.d.ts +5 -0
- package/dist/inference/unreachable.js +160 -0
- package/dist/must-handle-error.d.ts +2 -1
- package/dist/must-handle-error.js +4 -4
- package/dist/narrow-try.d.ts +2 -0
- package/dist/narrow-try.js +62 -7
- package/dist/plugin.d.ts +12 -2
- package/dist/plugin.js +4 -2
- package/dist/react/fallbacks.js +35 -14
- package/dist/react/tree.d.ts +10 -0
- package/dist/react/tree.js +54 -7
- package/dist/refactors.js +35 -2
- package/dist/render-throw-without-boundary.d.ts +13 -4
- package/dist/render-throw-without-boundary.js +100 -10
- package/dist/result-ignored.d.ts +4 -1
- package/dist/result-ignored.js +51 -10
- package/dist/result-type.d.ts +2 -0
- package/dist/result-type.js +8 -2
- package/dist/result-unchecked.d.ts +4 -1
- package/dist/result-unchecked.js +320 -96
- package/dist/rules.meta.d.ts +29 -14
- package/dist/rules.meta.js +19 -14
- package/dist/syntax.d.ts +0 -13
- package/dist/syntax.js +0 -79
- package/dist/text.d.ts +15 -2
- package/dist/text.js +28 -5
- package/dist/throws-doc-mismatch.d.ts +1 -1
- package/dist/throws-doc-mismatch.js +59 -5
- package/dist/throws-reads.d.ts +3 -0
- package/dist/throws-reads.js +108 -0
- package/package.json +2 -2
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import type { FactSource, Json } from "@stateward/core";
|
|
1
|
+
import type { FactKey, FactSide, FactSource, Json } from "@stateward/core";
|
|
2
|
+
/** Why a package's function is not (fully) known: fixed strings, shown as they are. */
|
|
3
|
+
export declare const UNKNOWN_REASONS: readonly ["its errors are not stated", "its `facts` line states no `rejects`, the kind its promise needs"];
|
|
4
|
+
export type UnknownReason = (typeof UNKNOWN_REASONS)[number];
|
|
2
5
|
/**
|
|
3
6
|
* The throws facts of one file: JSON, so the engine can cache it
|
|
4
7
|
* under the file's dependency fingerprint. Every id is a qualified id (`src/api.ts#Api.get`) or,
|
|
@@ -17,6 +20,24 @@ export type ClassFact = {
|
|
|
17
20
|
readonly name: string;
|
|
18
21
|
readonly parent: string | null;
|
|
19
22
|
readonly open: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* The instance properties the class itself declares, sorted: property declarations, parameter
|
|
25
|
+
* properties, and `this.x = …` in its constructor. A reading pack decides what a field means
|
|
26
|
+
* (NestJS reads `statusCode`).
|
|
27
|
+
*/
|
|
28
|
+
readonly fields: readonly string[];
|
|
29
|
+
/**
|
|
30
|
+
* With `open`: the name the declaration writes after `extends` when it does not resolve
|
|
31
|
+
* (`TypeORMError`), so a hint can name it.
|
|
32
|
+
*/
|
|
33
|
+
readonly declared?: string;
|
|
34
|
+
/** With `open`, for a package's class: the package's version that runs, when it is known. */
|
|
35
|
+
readonly version?: string;
|
|
36
|
+
/**
|
|
37
|
+
* With `open`, for a package's class: the `facts` key that names it (core's `factKeyOf`, from its
|
|
38
|
+
* declaration); absent when its declaration is not found, so no line can name it.
|
|
39
|
+
*/
|
|
40
|
+
readonly key?: FactKey;
|
|
20
41
|
};
|
|
21
42
|
/** Which part of a called function's result a call adds: what it throws, rejects, or both. */
|
|
22
43
|
export type Part = "throws" | "rejects" | "both";
|
|
@@ -34,20 +55,31 @@ export type PackageFacts = {
|
|
|
34
55
|
readonly name: string;
|
|
35
56
|
/** "" when the package's version cannot be read. */
|
|
36
57
|
readonly version: string;
|
|
37
|
-
/**
|
|
38
|
-
|
|
58
|
+
/**
|
|
59
|
+
* The `facts` key that names the function (core's `factKeyOf`): its module as the file imports
|
|
60
|
+
* it (`typeorm`, `node:path`), its name there and the version a line is checked against; or
|
|
61
|
+
* `unnamed`, the call as written and why no key can name it.
|
|
62
|
+
*/
|
|
63
|
+
readonly key: FactKey;
|
|
64
|
+
/** What a line for the function states: `rejects` when the call gives a promise (`factSideOf`). */
|
|
65
|
+
readonly side: FactSide;
|
|
39
66
|
readonly source: FactSource;
|
|
40
67
|
readonly throws: Side;
|
|
41
68
|
readonly rejects: Side;
|
|
42
69
|
};
|
|
43
70
|
/** Something that adds to what the code around it throws. */
|
|
44
71
|
export type Leaf =
|
|
45
|
-
/**
|
|
72
|
+
/**
|
|
73
|
+
* `throw new X(…)`, or a built-in that throws X; `text` is the call that does, null for a `throw`.
|
|
74
|
+
* `platform`: the call is into the platform (TypeScript's lib, Node's types, the config's
|
|
75
|
+
* `platform`).
|
|
76
|
+
*/
|
|
46
77
|
{
|
|
47
78
|
readonly k: "class";
|
|
48
79
|
readonly id: string;
|
|
49
80
|
readonly text: string | null;
|
|
50
81
|
readonly at: Span;
|
|
82
|
+
readonly platform?: true;
|
|
51
83
|
}
|
|
52
84
|
/** What cannot be followed, and why (`no declaration`, `callback of unknown origin`, …). */
|
|
53
85
|
| {
|
|
@@ -75,13 +107,17 @@ export type Leaf =
|
|
|
75
107
|
readonly text: string;
|
|
76
108
|
readonly at: Span;
|
|
77
109
|
}
|
|
78
|
-
/**
|
|
110
|
+
/**
|
|
111
|
+
* A call into a package: adds `part` of its facts (`throws` where it is called, `rejects` when
|
|
112
|
+
* awaited). `platform`: the package is the platform's (a Node module, the config's `platform`).
|
|
113
|
+
*/
|
|
79
114
|
| {
|
|
80
115
|
readonly k: "package";
|
|
81
116
|
readonly facts: PackageFacts;
|
|
82
117
|
readonly part: "throws" | "rejects";
|
|
83
118
|
readonly text: string;
|
|
84
119
|
readonly at: Span;
|
|
120
|
+
readonly platform?: true;
|
|
85
121
|
}
|
|
86
122
|
/**
|
|
87
123
|
* `throw e` of the variable of the catch that starts at `of`: rethrows what reaches it there.
|
|
@@ -147,6 +183,11 @@ export type Doc = {
|
|
|
147
183
|
readonly entries: readonly DocEntry[];
|
|
148
184
|
/** A tag without a type (`@throws when …`): it says the function throws, not what. */
|
|
149
185
|
readonly untyped: readonly Span[];
|
|
186
|
+
/**
|
|
187
|
+
* Where the declaration the tags are on starts, as the reference ledger names it (a property by
|
|
188
|
+
* its name): a finding about the tags is against that declaration.
|
|
189
|
+
*/
|
|
190
|
+
readonly declared: number;
|
|
150
191
|
};
|
|
151
192
|
export type FunctionFact = {
|
|
152
193
|
readonly id: string;
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/** Why a package's function is not (fully) known: fixed strings, shown as they are. */
|
|
2
|
+
export const UNKNOWN_REASONS = [
|
|
3
|
+
// No `facts` line, in the config or a pack, states what the package's function throws (0041).
|
|
4
|
+
"its errors are not stated",
|
|
5
|
+
// A line states what the call throws, and not what the promise it returns rejects with: kinds
|
|
6
|
+
// are strict, so a `throws` line (1.2's style) leaves the promise not stated.
|
|
7
|
+
"its `facts` line states no `rejects`, the kind its promise needs",
|
|
8
|
+
];
|
|
1
9
|
function isList(value) {
|
|
2
10
|
return Array.isArray(value);
|
|
3
11
|
}
|
|
@@ -42,24 +50,52 @@ function readSide(value) {
|
|
|
42
50
|
const f = value === undefined ? undefined : fieldsOf(value);
|
|
43
51
|
const classes = readStrings(f?.classes);
|
|
44
52
|
const unknown = readStrings(f?.unknown);
|
|
45
|
-
|
|
53
|
+
if (classes === undefined || unknown === undefined)
|
|
54
|
+
return undefined;
|
|
55
|
+
return { classes, unknown };
|
|
46
56
|
}
|
|
47
|
-
const SOURCES = ["config", "
|
|
57
|
+
const SOURCES = ["config", "shipped", "unknown"];
|
|
48
58
|
function readSource(value) {
|
|
49
59
|
return SOURCES.find((source) => source === value);
|
|
50
60
|
}
|
|
61
|
+
const FACT_SIDES = ["throws", "rejects"];
|
|
62
|
+
/** A `FactKey` as `PackageFacts.key` writes it; undefined for anything else. */
|
|
63
|
+
function readKey(value) {
|
|
64
|
+
const f = value === undefined ? undefined : fieldsOf(value);
|
|
65
|
+
if (f === undefined || !isString(f.name))
|
|
66
|
+
return undefined;
|
|
67
|
+
if (f.kind === "unnamed") {
|
|
68
|
+
return isString(f.reason) ? { kind: "unnamed", name: f.name, reason: f.reason } : undefined;
|
|
69
|
+
}
|
|
70
|
+
if (f.kind !== "named" || !isString(f.module))
|
|
71
|
+
return undefined;
|
|
72
|
+
if (f.version !== undefined && !isString(f.version))
|
|
73
|
+
return undefined;
|
|
74
|
+
return {
|
|
75
|
+
kind: "named",
|
|
76
|
+
module: f.module,
|
|
77
|
+
name: f.name,
|
|
78
|
+
...(f.version === undefined ? {} : { version: f.version }),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
51
81
|
/** Package facts as `PackageFacts` writes them; undefined for anything else. */
|
|
52
82
|
export function readPackageFacts(value) {
|
|
53
83
|
const f = value === undefined ? undefined : fieldsOf(value);
|
|
54
|
-
if (f === undefined || !isString(f.name) || !isString(f.version)
|
|
84
|
+
if (f === undefined || !isString(f.name) || !isString(f.version))
|
|
55
85
|
return undefined;
|
|
56
|
-
|
|
86
|
+
const key = readKey(f.key);
|
|
87
|
+
const side = FACT_SIDES.find((one) => one === f.side);
|
|
57
88
|
const source = readSource(f.source);
|
|
58
89
|
const throws = readSide(f.throws);
|
|
59
90
|
const rejects = readSide(f.rejects);
|
|
60
|
-
if (
|
|
91
|
+
if (key === undefined ||
|
|
92
|
+
side === undefined ||
|
|
93
|
+
source === undefined ||
|
|
94
|
+
throws === undefined ||
|
|
95
|
+
rejects === undefined) {
|
|
61
96
|
return undefined;
|
|
62
|
-
|
|
97
|
+
}
|
|
98
|
+
return { name: f.name, version: f.version, key, side, source, throws, rejects };
|
|
63
99
|
}
|
|
64
100
|
function readEffect(value) {
|
|
65
101
|
const f = fieldsOf(value);
|
|
@@ -67,10 +103,19 @@ function readEffect(value) {
|
|
|
67
103
|
if (f === undefined || at === undefined || !isString(f.k))
|
|
68
104
|
return undefined;
|
|
69
105
|
switch (f.k) {
|
|
70
|
-
case "class":
|
|
71
|
-
return isString(f.id) &&
|
|
72
|
-
|
|
106
|
+
case "class": {
|
|
107
|
+
return isString(f.id) &&
|
|
108
|
+
(f.text === null || isString(f.text)) &&
|
|
109
|
+
(f.platform === undefined || f.platform === true)
|
|
110
|
+
? {
|
|
111
|
+
k: "class",
|
|
112
|
+
id: f.id,
|
|
113
|
+
text: f.text,
|
|
114
|
+
at,
|
|
115
|
+
...(f.platform === true ? { platform: true } : {}),
|
|
116
|
+
}
|
|
73
117
|
: undefined;
|
|
118
|
+
}
|
|
74
119
|
case "unknown":
|
|
75
120
|
return isString(f.reason) && isString(f.text)
|
|
76
121
|
? { k: "unknown", reason: f.reason, text: f.text, at }
|
|
@@ -85,8 +130,16 @@ function readEffect(value) {
|
|
|
85
130
|
const facts = readPackageFacts(f.facts);
|
|
86
131
|
return facts !== undefined &&
|
|
87
132
|
isString(f.text) &&
|
|
88
|
-
(f.part === "throws" || f.part === "rejects")
|
|
89
|
-
|
|
133
|
+
(f.part === "throws" || f.part === "rejects") &&
|
|
134
|
+
(f.platform === undefined || f.platform === true)
|
|
135
|
+
? {
|
|
136
|
+
k: "package",
|
|
137
|
+
facts,
|
|
138
|
+
part: f.part,
|
|
139
|
+
text: f.text,
|
|
140
|
+
at,
|
|
141
|
+
...(f.platform === true ? { platform: true } : {}),
|
|
142
|
+
}
|
|
90
143
|
: undefined;
|
|
91
144
|
}
|
|
92
145
|
case "maybe": {
|
|
@@ -176,7 +229,11 @@ function readDoc(value) {
|
|
|
176
229
|
return { id: entry.id, name: entry.name, at };
|
|
177
230
|
});
|
|
178
231
|
const untyped = each(f.untyped, spanOf);
|
|
179
|
-
|
|
232
|
+
if (typeof f.declared !== "number")
|
|
233
|
+
return undefined;
|
|
234
|
+
return entries === undefined || untyped === undefined
|
|
235
|
+
? undefined
|
|
236
|
+
: { entries, untyped, declared: f.declared };
|
|
180
237
|
}
|
|
181
238
|
function readFunction(value) {
|
|
182
239
|
const f = fieldsOf(value);
|
|
@@ -210,7 +267,26 @@ export function readClass(value) {
|
|
|
210
267
|
return undefined;
|
|
211
268
|
if ((f.parent !== null && !isString(f.parent)) || typeof f.open !== "boolean")
|
|
212
269
|
return undefined;
|
|
213
|
-
|
|
270
|
+
const fields = each(f.fields, (item) => (isString(item) ? item : undefined));
|
|
271
|
+
if (fields === undefined)
|
|
272
|
+
return undefined;
|
|
273
|
+
if (f.declared !== undefined && !isString(f.declared))
|
|
274
|
+
return undefined;
|
|
275
|
+
if (f.version !== undefined && !isString(f.version))
|
|
276
|
+
return undefined;
|
|
277
|
+
const key = f.key === undefined ? undefined : readKey(f.key);
|
|
278
|
+
if (f.key !== undefined && key === undefined)
|
|
279
|
+
return undefined;
|
|
280
|
+
return {
|
|
281
|
+
id: f.id,
|
|
282
|
+
name: f.name,
|
|
283
|
+
parent: f.parent,
|
|
284
|
+
open: f.open,
|
|
285
|
+
fields,
|
|
286
|
+
...(f.declared === undefined ? {} : { declared: f.declared }),
|
|
287
|
+
...(f.version === undefined ? {} : { version: f.version }),
|
|
288
|
+
...(key === undefined ? {} : { key }),
|
|
289
|
+
};
|
|
214
290
|
}
|
|
215
291
|
/** Read once per summary object: the engine hands out the same frozen object while it is unchanged. */
|
|
216
292
|
const read = new WeakMap();
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import ts from "@stateward/core/typescript";
|
|
2
|
+
import type { TypeQueries } from "@stateward/core";
|
|
3
|
+
/** Whether a `throw` cannot run while the types hold (see above). */
|
|
4
|
+
export declare function cannotRun(types: TypeQueries, node: ts.ThrowStatement): boolean;
|
|
5
|
+
//# sourceMappingURL=unreachable.d.ts.map
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import ts from "@stateward/core/typescript";
|
|
2
|
+
import { stripParentheses } from "../syntax.js";
|
|
3
|
+
/*
|
|
4
|
+
* A `throw` that cannot run because the value it is about has no case left: the `default` of a
|
|
5
|
+
* `switch` whose cases cover every value of its type, a `throw` after an `if` chain that returns
|
|
6
|
+
* for every value, or a `throw` that names a value TypeScript has narrowed to `never`. Code
|
|
7
|
+
* written to fail loudly if the type ever grows (`default: throw new Error("unknown kind")`) is not
|
|
8
|
+
* an error a caller can get while the types hold.
|
|
9
|
+
*/
|
|
10
|
+
function keyOf(literal) {
|
|
11
|
+
return `${typeof literal.value}:${String(literal.value)}`;
|
|
12
|
+
}
|
|
13
|
+
/** The values of `node`'s type, or undefined when they are not a finite set of literals. */
|
|
14
|
+
function valuesOf(types, node) {
|
|
15
|
+
const literals = types.literalsOf(node);
|
|
16
|
+
return literals === "not-finite" ? undefined : new Set(literals.map(keyOf));
|
|
17
|
+
}
|
|
18
|
+
/** Whether every value of `all` is in `covered`. */
|
|
19
|
+
function covers(covered, all) {
|
|
20
|
+
return [...all].every((value) => covered.has(value));
|
|
21
|
+
}
|
|
22
|
+
/** The `default` clause the statement sits in directly (in its statements, or a block of them). */
|
|
23
|
+
function defaultClauseOf(node) {
|
|
24
|
+
let at = node.parent;
|
|
25
|
+
while (ts.isBlock(at))
|
|
26
|
+
at = at.parent;
|
|
27
|
+
return ts.isDefaultClause(at) ? at : undefined;
|
|
28
|
+
}
|
|
29
|
+
/** A `default` that no value of the switched expression reaches: the cases cover its type. */
|
|
30
|
+
function unreachableDefault(types, clause) {
|
|
31
|
+
const clauses = clause.parent.clauses;
|
|
32
|
+
const index = clauses.indexOf(clause);
|
|
33
|
+
// A case with no statements just before it falls through into the default.
|
|
34
|
+
const before = index > 0 ? clauses[index - 1] : undefined;
|
|
35
|
+
if (before?.statements.length === 0)
|
|
36
|
+
return false;
|
|
37
|
+
const all = valuesOf(types, clause.parent.parent.expression);
|
|
38
|
+
if (all === undefined)
|
|
39
|
+
return false;
|
|
40
|
+
if (all.size === 0)
|
|
41
|
+
return false;
|
|
42
|
+
const covered = new Set();
|
|
43
|
+
for (const one of clauses) {
|
|
44
|
+
if (!ts.isCaseClause(one))
|
|
45
|
+
continue;
|
|
46
|
+
const values = valuesOf(types, one.expression);
|
|
47
|
+
if (values?.size !== 1)
|
|
48
|
+
return false;
|
|
49
|
+
for (const value of values)
|
|
50
|
+
covered.add(value);
|
|
51
|
+
}
|
|
52
|
+
return covers(covered, all);
|
|
53
|
+
}
|
|
54
|
+
/** `x === A`, `A === x`, or several joined by `||`: the reference's text and the tested values. */
|
|
55
|
+
function testedValues(types, condition) {
|
|
56
|
+
const node = stripParentheses(condition);
|
|
57
|
+
if (!ts.isBinaryExpression(node))
|
|
58
|
+
return undefined;
|
|
59
|
+
if (node.operatorToken.kind === ts.SyntaxKind.BarBarToken) {
|
|
60
|
+
const left = testedValues(types, node.left);
|
|
61
|
+
const right = testedValues(types, node.right);
|
|
62
|
+
if (left === undefined || left.text !== right?.text)
|
|
63
|
+
return undefined;
|
|
64
|
+
return { ...left, values: new Set([...left.values, ...right.values]) };
|
|
65
|
+
}
|
|
66
|
+
if (node.operatorToken.kind !== ts.SyntaxKind.EqualsEqualsEqualsToken &&
|
|
67
|
+
node.operatorToken.kind !== ts.SyntaxKind.EqualsEqualsToken) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
for (const [reference, literal] of [
|
|
71
|
+
[node.left, node.right],
|
|
72
|
+
[node.right, node.left],
|
|
73
|
+
]) {
|
|
74
|
+
if (!ts.isIdentifier(reference) && !ts.isPropertyAccessExpression(reference))
|
|
75
|
+
continue;
|
|
76
|
+
const values = valuesOf(types, literal);
|
|
77
|
+
if (values?.size !== 1)
|
|
78
|
+
continue;
|
|
79
|
+
return { reference, text: reference.getText(), values };
|
|
80
|
+
}
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
function alwaysExits(node) {
|
|
84
|
+
if (ts.isBlock(node)) {
|
|
85
|
+
const last = node.statements.at(-1);
|
|
86
|
+
return last !== undefined && alwaysExits(last);
|
|
87
|
+
}
|
|
88
|
+
return ts.isReturnStatement(node) || ts.isThrowStatement(node);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* A `throw` after `if` statements of its block that test one value against literals and exit,
|
|
92
|
+
* covering every value of its type: `if (k === "a") return 1; if (k === "b") return 2; throw …`.
|
|
93
|
+
*/
|
|
94
|
+
function afterCoveringChain(types, node) {
|
|
95
|
+
const block = node.parent;
|
|
96
|
+
if (!ts.isBlock(block) && !ts.isSourceFile(block))
|
|
97
|
+
return false;
|
|
98
|
+
const index = block.statements.indexOf(node);
|
|
99
|
+
let first;
|
|
100
|
+
let text;
|
|
101
|
+
const covered = new Set();
|
|
102
|
+
for (let at = index - 1; at >= 0; at -= 1) {
|
|
103
|
+
const statement = block.statements[at];
|
|
104
|
+
if (statement === undefined || !ts.isIfStatement(statement))
|
|
105
|
+
break;
|
|
106
|
+
let chain = statement;
|
|
107
|
+
const ifs = [];
|
|
108
|
+
// `if … else if …` with every branch exiting, and no final `else`.
|
|
109
|
+
while (chain !== undefined) {
|
|
110
|
+
ifs.push(chain);
|
|
111
|
+
const next = chain.elseStatement;
|
|
112
|
+
chain = next !== undefined && ts.isIfStatement(next) ? next : undefined;
|
|
113
|
+
if (next !== undefined && !ts.isIfStatement(next))
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
for (const one of ifs) {
|
|
117
|
+
if (!alwaysExits(one.thenStatement))
|
|
118
|
+
return false;
|
|
119
|
+
const tested = testedValues(types, one.expression);
|
|
120
|
+
if (tested === undefined)
|
|
121
|
+
return false;
|
|
122
|
+
if (text !== undefined && tested.text !== text)
|
|
123
|
+
return false;
|
|
124
|
+
text = tested.text;
|
|
125
|
+
first = tested.reference;
|
|
126
|
+
for (const value of tested.values)
|
|
127
|
+
covered.add(value);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (first === undefined)
|
|
131
|
+
return false;
|
|
132
|
+
const all = valuesOf(types, first);
|
|
133
|
+
return all !== undefined && all.size > 0 && covers(covered, all);
|
|
134
|
+
}
|
|
135
|
+
/** A reference in the thrown expression whose type TypeScript has narrowed to `never`. */
|
|
136
|
+
function namesNever(types, node) {
|
|
137
|
+
let found = false;
|
|
138
|
+
const visit = (inner) => {
|
|
139
|
+
if (found || ts.isFunctionLike(inner))
|
|
140
|
+
return;
|
|
141
|
+
if ((ts.isIdentifier(inner) && !ts.isPropertyAccessExpression(inner.parent)) ||
|
|
142
|
+
ts.isPropertyAccessExpression(inner)) {
|
|
143
|
+
if (types.typeTextOf(inner) === "never") {
|
|
144
|
+
found = true;
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
ts.forEachChild(inner, visit);
|
|
149
|
+
};
|
|
150
|
+
visit(node.expression);
|
|
151
|
+
return found;
|
|
152
|
+
}
|
|
153
|
+
/** Whether a `throw` cannot run while the types hold (see above). */
|
|
154
|
+
export function cannotRun(types, node) {
|
|
155
|
+
const clause = defaultClauseOf(node);
|
|
156
|
+
if (clause !== undefined && unreachableDefault(types, clause))
|
|
157
|
+
return true;
|
|
158
|
+
return afterCoveringChain(types, node) || namesNever(types, node);
|
|
159
|
+
}
|
|
160
|
+
//# sourceMappingURL=unreachable.js.map
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Json } from "@stateward/core";
|
|
1
2
|
import type { Span, ThrowsSummary } from "./inference/summary.js";
|
|
2
3
|
/** The group the rule belongs to in the docs and in `stateward status`. */
|
|
3
4
|
export declare const GROUP = "callers";
|
|
@@ -50,6 +51,6 @@ export type MustHandleSummary = {
|
|
|
50
51
|
readonly throws: ThrowsSummary;
|
|
51
52
|
readonly mustHandle: MustHandleFacts;
|
|
52
53
|
};
|
|
53
|
-
export declare const mustHandleError: import("@stateward/core").ProjectRule<MustHandleSummary, Readonly<Record<string, never>>, "callers"
|
|
54
|
+
export declare const mustHandleError: import("@stateward/core").ProjectRule<MustHandleSummary, Readonly<Record<string, never>>, "callers", Readonly<Record<string, Json>>>;
|
|
54
55
|
export {};
|
|
55
56
|
//# sourceMappingURL=must-handle-error.d.ts.map
|
|
@@ -3,11 +3,11 @@ import { defineRule } from "@stateward/core";
|
|
|
3
3
|
import { catchName, className } from "./inference/format.js";
|
|
4
4
|
import { summarizeOnce } from "./inference/project.js";
|
|
5
5
|
import { analyse, evaluated, relation } from "./inference/solve.js";
|
|
6
|
-
import { ANONYMOUS,
|
|
6
|
+
import { ANONYMOUS, errorClassIdOf } from "./inference/summarize.js";
|
|
7
7
|
import { each, fieldsOf, isString, spanOf } from "./inference/summary.js";
|
|
8
8
|
import { throwsOf } from "./react/boundaries.js";
|
|
9
9
|
import { stripParentheses } from "./syntax.js";
|
|
10
|
-
import { fitted,
|
|
10
|
+
import { fitted, shorten, withExample } from "./text.js";
|
|
11
11
|
/** The group the rule belongs to in the docs and in `stateward status`. */
|
|
12
12
|
export const GROUP = "callers";
|
|
13
13
|
const EXPLAIN = `# errors/must-handle-error
|
|
@@ -170,7 +170,7 @@ function errorHelperOf(fn, id, types) {
|
|
|
170
170
|
const named = instanceofNames(first.expression, variable);
|
|
171
171
|
if (named === undefined || throwsVariable(first.thenStatement, variable))
|
|
172
172
|
continue;
|
|
173
|
-
const classes = named.map((one) =>
|
|
173
|
+
const classes = named.map((one) => errorClassIdOf(types, one));
|
|
174
174
|
if (classes.some((one) => one === undefined))
|
|
175
175
|
return undefined;
|
|
176
176
|
return { id, parameter, classes: classes.filter((one) => one !== undefined) };
|
|
@@ -226,7 +226,7 @@ function summarizeMustHandle(ctx) {
|
|
|
226
226
|
tags.push({
|
|
227
227
|
id: symbol.id,
|
|
228
228
|
name: symbol.name,
|
|
229
|
-
how:
|
|
229
|
+
how: (ts.getTextOfJSDocComment(tag.comment) ?? "").replace(/\s+/g, " ").trim(),
|
|
230
230
|
at: spanOfNode(tag),
|
|
231
231
|
});
|
|
232
232
|
}
|
package/dist/narrow-try.d.ts
CHANGED
|
@@ -38,6 +38,8 @@ type TryShape = {
|
|
|
38
38
|
readonly at: Span;
|
|
39
39
|
/** The `catch` tests the error or rethrows it: bugs already get through. */
|
|
40
40
|
readonly passesBugsOn: boolean;
|
|
41
|
+
/** The name the `catch` gives the error (`catch (e)`), or null when it binds none. */
|
|
42
|
+
readonly variable: string | null;
|
|
41
43
|
/** The `catch` block ends with `return` or `throw`. */
|
|
42
44
|
readonly catchExits: boolean;
|
|
43
45
|
readonly hasFinally: boolean;
|
package/dist/narrow-try.js
CHANGED
|
@@ -3,10 +3,12 @@ import { defineRule } from "@stateward/core";
|
|
|
3
3
|
import { summarizeOnce } from "./inference/project.js";
|
|
4
4
|
import { REASON } from "./inference/summarize.js";
|
|
5
5
|
import { analyse, collected, evaluate } from "./inference/solve.js";
|
|
6
|
+
import { catchName } from "./inference/format.js";
|
|
7
|
+
import { isBuiltin } from "./inference/builtins.js";
|
|
6
8
|
import { each, fieldsOf, isString, readStrings, spanOf } from "./inference/summary.js";
|
|
7
9
|
import { throwsOf } from "./react/boundaries.js";
|
|
8
10
|
import { handsOnCaught, isFunctionBoundary, someRunning, stripParentheses, testsCaught, } from "./syntax.js";
|
|
9
|
-
import { fitted, oneLine, shorten } from "./text.js";
|
|
11
|
+
import { codeList, fitted, oneLine, shorten } from "./text.js";
|
|
10
12
|
/** The group the rule belongs to in the docs and in `stateward status`. */
|
|
11
13
|
export const GROUP = "catch";
|
|
12
14
|
const EXPLAIN = `# errors/narrow-try
|
|
@@ -57,6 +59,8 @@ export async function submit(plan: string, close: () => void): Promise<void> {
|
|
|
57
59
|
|
|
58
60
|
The quick fix moves the lines after the \`try\`. It is offered only when the move keeps what runs the same for every outcome but a bug: the \`catch\` ends with \`return\` or \`throw\`, there is no \`finally\`, and the moved lines use nothing declared in the \`try\`. Where they use the result, declare it before the \`try\` (\`let plan: Plan;\`), assign it inside, and return from the \`catch\`.
|
|
59
61
|
|
|
62
|
+
The other reading keeps the line where it is and makes the \`catch\` pick its error: \`if (e instanceof RequestError) { … } else { throw e; }\`, with the classes the call throws (a \`TypeError\` is left out, since a bug throws it too). The rule then counts the \`catch\` as one that lets bugs through (see below), and the message shows this form with your names.
|
|
63
|
+
|
|
60
64
|
## Why it matters
|
|
61
65
|
|
|
62
66
|
The \`catch\` is for a failed save. When \`close\` has a bug (it reads a property of \`undefined\`, say), the \`TypeError\` is handled like a failed request: the user sees "Could not save", the console shows nothing useful, and the bug is never reported. With only the request inside the \`try\`, a bug in the code after it surfaces as the bug it is.
|
|
@@ -363,6 +367,9 @@ function shapeOf(node, sourceFile) {
|
|
|
363
367
|
return {
|
|
364
368
|
at: [node.getStart(sourceFile), node.getEnd()],
|
|
365
369
|
passesBugsOn: passesBugsOn(clause),
|
|
370
|
+
variable: clause.variableDeclaration !== undefined && ts.isIdentifier(clause.variableDeclaration.name)
|
|
371
|
+
? clause.variableDeclaration.name.text
|
|
372
|
+
: null,
|
|
366
373
|
catchExits: endsWithExit(clause.block),
|
|
367
374
|
hasFinally: node.finallyBlock !== undefined,
|
|
368
375
|
indent: indentationAt(node.getStart(sourceFile), text),
|
|
@@ -433,7 +440,18 @@ function readShape(value) {
|
|
|
433
440
|
!isString(inner)) {
|
|
434
441
|
return undefined;
|
|
435
442
|
}
|
|
436
|
-
|
|
443
|
+
const variable = isString(f.variable) ? f.variable : null;
|
|
444
|
+
return {
|
|
445
|
+
at,
|
|
446
|
+
passesBugsOn: passes,
|
|
447
|
+
variable,
|
|
448
|
+
catchExits,
|
|
449
|
+
hasFinally,
|
|
450
|
+
indent,
|
|
451
|
+
inner,
|
|
452
|
+
lines,
|
|
453
|
+
around,
|
|
454
|
+
};
|
|
437
455
|
}
|
|
438
456
|
function readTries(value) {
|
|
439
457
|
return each(fieldsOf(value)?.tries, readShape);
|
|
@@ -475,6 +493,8 @@ function spotsOf(analysis, file, effects, out) {
|
|
|
475
493
|
packageOnly: false,
|
|
476
494
|
unknown: false,
|
|
477
495
|
project: null,
|
|
496
|
+
classes: [],
|
|
497
|
+
imports: [],
|
|
478
498
|
});
|
|
479
499
|
continue;
|
|
480
500
|
}
|
|
@@ -493,6 +513,11 @@ function spotsOf(analysis, file, effects, out) {
|
|
|
493
513
|
: effect.k === "unknown" && effect.reason === REASON.callback
|
|
494
514
|
? "callback"
|
|
495
515
|
: null,
|
|
516
|
+
classes: [...into.classes.keys()].map((id) => catchName(id, analysis.classes)).sort(),
|
|
517
|
+
imports: [...into.classes.keys()]
|
|
518
|
+
.filter((id) => !isBuiltin(id))
|
|
519
|
+
.map((id) => catchName(id, analysis.classes))
|
|
520
|
+
.sort(),
|
|
496
521
|
});
|
|
497
522
|
if (effect.k === "try") {
|
|
498
523
|
visit(effect.body, effect.at);
|
|
@@ -541,6 +566,8 @@ function expectedIn(line, spots) {
|
|
|
541
566
|
packageOnly: false,
|
|
542
567
|
unknown: true,
|
|
543
568
|
project: null,
|
|
569
|
+
classes: [],
|
|
570
|
+
imports: [],
|
|
544
571
|
}
|
|
545
572
|
: undefined;
|
|
546
573
|
}
|
|
@@ -596,15 +623,43 @@ function nameOf(spot, line) {
|
|
|
596
623
|
function headline(line, expected) {
|
|
597
624
|
return fitted((cut) => `the \`try\` for \`${cut(expected)}\` also catches errors from \`${asCode(cut(line))}\``);
|
|
598
625
|
}
|
|
599
|
-
function hint(expected, fixable, count) {
|
|
626
|
+
function hint(expected, fixable, count, test) {
|
|
600
627
|
const [lines, them] = count === 1 ? ["the line", "it"] : ["the lines", "them"];
|
|
601
628
|
const why = `so a bug in ${them} is not handled like a failed \`${expected}\``;
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
629
|
+
const binding = test.bound ? "" : `, binding it as \`catch (${test.variable})\``;
|
|
630
|
+
const imports = test.imports.length === 0
|
|
631
|
+
? ""
|
|
632
|
+
: ` (import ${codeList(test.imports, oneLine)} if the file does not)`;
|
|
633
|
+
const leftOut = test.leftOut ? ", leaving out `TypeError`, which a bug throws too" : "";
|
|
634
|
+
return [
|
|
635
|
+
"one of",
|
|
636
|
+
fixable
|
|
637
|
+
? `- move ${lines} after the \`try\`, ${why} (quick fix)`
|
|
638
|
+
: `- move ${lines} after the \`try\` and return from the \`catch\` (declare what ${count === 1 ? "it uses" : "they use"} before the \`try\`), ${why}`,
|
|
639
|
+
test.classes.length === 0
|
|
640
|
+
? `- or test in the \`catch\` that the error is what \`${expected}\` fails with, and rethrow the rest with \`throw ${test.variable};\`${binding}, ${why}`
|
|
641
|
+
: `- or test the error in the \`catch\` and rethrow the rest${imports}${binding}${leftOut}:`,
|
|
642
|
+
...(test.classes.length === 0
|
|
643
|
+
? []
|
|
644
|
+
: [
|
|
645
|
+
` if (${test.classes.map((name) => `${test.variable} instanceof ${name}`).join(" || ")}) { … } else { throw ${test.variable}; }`,
|
|
646
|
+
]),
|
|
647
|
+
].join("\n");
|
|
605
648
|
}
|
|
606
649
|
/** A promise's own `resolve` and `reject` (excalidraw): they never throw. */
|
|
607
650
|
const SETTLES = new Set(["resolve(…)", "reject(…)"]);
|
|
651
|
+
/** The `instanceof` test the `catch` can make for the failure the `try` is for. */
|
|
652
|
+
function testWay(shape, expected) {
|
|
653
|
+
const kept = expected.classes.filter((name) => name !== "TypeError");
|
|
654
|
+
const many = kept.length > 3;
|
|
655
|
+
return {
|
|
656
|
+
variable: shape.variable ?? "error",
|
|
657
|
+
bound: shape.variable !== null,
|
|
658
|
+
classes: many ? [] : kept,
|
|
659
|
+
imports: many ? [] : expected.imports.filter((name) => kept.includes(name)),
|
|
660
|
+
leftOut: !many && kept.length < expected.classes.length,
|
|
661
|
+
};
|
|
662
|
+
}
|
|
608
663
|
function checkTry(shape, spots, file, report) {
|
|
609
664
|
if (shape.passesBugsOn)
|
|
610
665
|
return;
|
|
@@ -637,7 +692,7 @@ function checkTry(shape, spots, file, report) {
|
|
|
637
692
|
const fix = moveFix(shape, kept, moved, file);
|
|
638
693
|
report({
|
|
639
694
|
message: headline(first.text, target),
|
|
640
|
-
hint: hint(target, fix !== undefined, moved.length),
|
|
695
|
+
hint: hint(target, fix !== undefined, moved.length, testWay(shape, expected)),
|
|
641
696
|
at: first.at,
|
|
642
697
|
fixes: fix === undefined ? [] : [fix],
|
|
643
698
|
});
|
package/dist/plugin.d.ts
CHANGED
|
@@ -66,7 +66,6 @@ export declare function errors(): import("@stateward/core").PluginOf<"errors", {
|
|
|
66
66
|
};
|
|
67
67
|
}, "render">;
|
|
68
68
|
readonly "must-handle-error": import("@stateward/core").LazyRule<Readonly<Record<string, import("@stateward/core").OptionSpec>>, "callers">;
|
|
69
|
-
readonly "narrow-try": import("@stateward/core").LazyRule<Readonly<Record<string, import("@stateward/core").OptionSpec>>, "catch">;
|
|
70
69
|
readonly "result-ignored": import("@stateward/core").LazyRule<{
|
|
71
70
|
readonly allowVoid: {
|
|
72
71
|
readonly kind: "boolean";
|
|
@@ -84,6 +83,17 @@ export declare function errors(): import("@stateward/core").PluginOf<"errors", {
|
|
|
84
83
|
readonly description: "Functions whose Result may be dropped, by the name they are called with (`logEvent`, `*.track`; globs allowed).";
|
|
85
84
|
};
|
|
86
85
|
}, "callers">;
|
|
87
|
-
readonly "result-unchecked": import("@stateward/core").LazyRule<
|
|
86
|
+
readonly "result-unchecked": import("@stateward/core").LazyRule<{
|
|
87
|
+
readonly failureNames: {
|
|
88
|
+
readonly kind: "strings";
|
|
89
|
+
readonly default: readonly [];
|
|
90
|
+
readonly description: "Names (or globs like isErr*) that are true when the call failed, besides the result's own `error`: a property of the result (`isError`), or a function called with it (`isFailure(result)`). Its type does not say what such a name means.";
|
|
91
|
+
};
|
|
92
|
+
readonly successNames: {
|
|
93
|
+
readonly kind: "strings";
|
|
94
|
+
readonly default: readonly [];
|
|
95
|
+
readonly description: "Names (or globs) that are true when the call worked, besides the result's own `data`, `ok` and `success`: a property of the result (`isSuccess`, `value`), or a function called with it (`isOk(result)`).";
|
|
96
|
+
};
|
|
97
|
+
}, "callers">;
|
|
88
98
|
}>;
|
|
89
99
|
//# sourceMappingURL=plugin.d.ts.map
|