@stateward/plugin-errors 1.2.2 → 1.3.1

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 (54) hide show
  1. package/README.md +9 -8
  2. package/dist/catch-hides-unexpected.d.ts +4 -1
  3. package/dist/catch-hides-unexpected.js +12 -5
  4. package/dist/catch-misses-error.d.ts +1 -1
  5. package/dist/catch-misses-error.js +7 -4
  6. package/dist/finally-can-throw.js +3 -3
  7. package/dist/hover.js +6 -9
  8. package/dist/index.d.ts +3 -1
  9. package/dist/index.js +1 -0
  10. package/dist/inference/analysis.d.ts +109 -0
  11. package/dist/inference/analysis.js +170 -0
  12. package/dist/inference/builtins.d.ts +0 -2
  13. package/dist/inference/builtins.js +0 -33
  14. package/dist/inference/deps.d.ts +13 -2
  15. package/dist/inference/deps.js +24 -13
  16. package/dist/inference/needs.d.ts +16 -0
  17. package/dist/inference/needs.js +27 -0
  18. package/dist/inference/packages.d.ts +24 -32
  19. package/dist/inference/packages.js +156 -680
  20. package/dist/inference/project.d.ts +4 -4
  21. package/dist/inference/project.js +6 -17
  22. package/dist/inference/solve.d.ts +29 -4
  23. package/dist/inference/solve.js +42 -12
  24. package/dist/inference/summarize.d.ts +27 -15
  25. package/dist/inference/summarize.js +247 -130
  26. package/dist/inference/summary.d.ts +41 -5
  27. package/dist/inference/summary.js +84 -12
  28. package/dist/inference/unreachable.d.ts +5 -0
  29. package/dist/inference/unreachable.js +160 -0
  30. package/dist/inlay.d.ts +1 -5
  31. package/dist/inlay.js +1 -12
  32. package/dist/must-handle-error.d.ts +4 -3
  33. package/dist/must-handle-error.js +10 -13
  34. package/dist/plugin.d.ts +12 -2
  35. package/dist/plugin.js +4 -2
  36. package/dist/react/boundaries.d.ts +9 -7
  37. package/dist/react/boundaries.js +16 -24
  38. package/dist/react/tree.js +8 -4
  39. package/dist/refactors.js +1 -1
  40. package/dist/render-throw-without-boundary.d.ts +13 -4
  41. package/dist/render-throw-without-boundary.js +4 -2
  42. package/dist/result-ignored.d.ts +4 -1
  43. package/dist/result-type.js +2 -2
  44. package/dist/result-unchecked.d.ts +4 -1
  45. package/dist/result-unchecked.js +139 -61
  46. package/dist/rules.meta.d.ts +26 -14
  47. package/dist/rules.meta.js +18 -14
  48. package/dist/syntax.d.ts +0 -13
  49. package/dist/syntax.js +0 -79
  50. package/dist/text.d.ts +5 -2
  51. package/dist/text.js +11 -5
  52. package/dist/throws-doc-mismatch.d.ts +2 -2
  53. package/dist/throws-doc-mismatch.js +13 -5
  54. package/package.json +2 -2
package/dist/syntax.js CHANGED
@@ -89,83 +89,4 @@ export function throwsUncaught(fn) {
89
89
  });
90
90
  return thrown;
91
91
  }
92
- /**
93
- * Whether a `catch` tests what it caught before handling it: `e instanceof X`, `e.name === …`,
94
- * `"status" in e`, or a type guard `isApiError(e)`, in an `if`, a `switch` or a `?:`. Such a catch
95
- * says which errors it expects; `errors/catch-hides-unexpected` checks what it does with the rest.
96
- */
97
- export function testsCaught(clause) {
98
- const declaration = clause.variableDeclaration;
99
- if (declaration === undefined || !ts.isIdentifier(declaration.name))
100
- return false;
101
- const variable = declaration.name.text;
102
- const isVariable = (node) => {
103
- const inner = stripParentheses(node);
104
- return ts.isIdentifier(inner) && inner.text === variable;
105
- };
106
- const tests = (condition) => {
107
- if (ts.isBinaryExpression(condition)) {
108
- const operator = condition.operatorToken.kind;
109
- if (operator === ts.SyntaxKind.InstanceOfKeyword && isVariable(condition.left))
110
- return true;
111
- if (operator === ts.SyntaxKind.InKeyword && isVariable(condition.right))
112
- return true;
113
- }
114
- if (ts.isPropertyAccessExpression(condition) && isVariable(condition.expression))
115
- return true;
116
- if (ts.isCallExpression(condition) && condition.arguments.some(isVariable))
117
- return true;
118
- return ts.forEachChild(condition, (child) => (tests(child) ? true : undefined)) === true;
119
- };
120
- let found = false;
121
- const walk = (node) => {
122
- if (found || isFunctionBoundary(node))
123
- return;
124
- if (ts.isIfStatement(node) && tests(node.expression))
125
- found = true;
126
- if (ts.isConditionalExpression(node) && tests(node.condition))
127
- found = true;
128
- if (ts.isSwitchStatement(node)) {
129
- found ||=
130
- tests(node.expression) ||
131
- node.caseBlock.clauses.some((one) => ts.isCaseClause(one) && tests(one.expression));
132
- }
133
- ts.forEachChild(node, walk);
134
- };
135
- walk(clause.block);
136
- return found;
137
- }
138
- /** A parameter of a function around `node` with this name: `reject`, `next`, `onError`. */
139
- function isParameterOfEnclosing(name, node) {
140
- for (let parent = node.parent; !ts.isSourceFile(parent); parent = parent.parent) {
141
- if (ts.isFunctionLike(parent) &&
142
- parent.parameters.some((parameter) => ts.isIdentifier(parameter.name) && parameter.name.text === name)) {
143
- return true;
144
- }
145
- }
146
- return false;
147
- }
148
- /**
149
- * Whether `call`, in `clause`, hands the caught error to a callback the function around it was
150
- * given (`reject(e)`, `next(e)`, `onError(e)`): the caller gets it, as with a rethrow.
151
- */
152
- export function handsOnCaught(call, clause) {
153
- const declaration = clause.variableDeclaration;
154
- if (declaration === undefined || !ts.isIdentifier(declaration.name))
155
- return false;
156
- const variable = declaration.name.text;
157
- const callee = stripParentheses(call.expression);
158
- return (ts.isIdentifier(callee) &&
159
- isParameterOfEnclosing(callee.text, clause) &&
160
- call.arguments.some((argument) => {
161
- const inner = stripParentheses(argument);
162
- return ts.isIdentifier(inner) && inner.text === variable;
163
- }));
164
- }
165
- /** Whether `test` holds for `root` or a node under it, without entering nested functions or classes. */
166
- export function someRunning(root, test) {
167
- const walk = (node) => (node === root || !isFunctionBoundary(node)) &&
168
- (test(node) || ts.forEachChild(node, (child) => (walk(child) ? true : undefined)) === true);
169
- return walk(root);
170
- }
171
92
  //# sourceMappingURL=syntax.js.map
package/dist/text.d.ts CHANGED
@@ -2,10 +2,13 @@
2
2
  export declare const HEADLINE_MAX = 100;
3
3
  /** The first line of a fix is at most this long. */
4
4
  export declare const HINT_MAX = 140;
5
- /** The text on one line: all whitespace becomes a single space. */
6
- export declare function oneLine(text: string): string;
7
5
  /** The text as it is, or its first `max - 1` characters and `…` when it is longer than `max`. */
8
6
  export declare function shorten(text: string, max: number): string;
7
+ /**
8
+ * The text as it is, or `…` and its last `max - 1` characters when it is longer than `max`: for a
9
+ * called function, whose name is at the end (`…delete().from` of a long chain).
10
+ */
11
+ export declare function shortenStart(text: string, max: number): string;
9
12
  /**
10
13
  * Builds a message whose inserted names are shown whole, on one line: a developer's name is never
11
14
  * cut. A headline that is longer than the limit because of its names is allowed to be.
package/dist/text.js CHANGED
@@ -1,24 +1,30 @@
1
+ import { codeOnOneLine } from "@stateward/core";
1
2
  /** A headline is at most this long. */
2
3
  export const HEADLINE_MAX = 100;
3
4
  /** The first line of a fix is at most this long. */
4
5
  export const HINT_MAX = 140;
5
- /** The text on one line: all whitespace becomes a single space. */
6
- export function oneLine(text) {
7
- return text.replace(/\s+/g, " ").trim();
8
- }
9
6
  /** The text as it is, or its first `max - 1` characters and `…` when it is longer than `max`. */
10
7
  export function shorten(text, max) {
11
8
  if (text.length <= max)
12
9
  return text;
13
10
  return `${text.slice(0, max - 1)}…`;
14
11
  }
12
+ /**
13
+ * The text as it is, or `…` and its last `max - 1` characters when it is longer than `max`: for a
14
+ * called function, whose name is at the end (`…delete().from` of a long chain).
15
+ */
16
+ export function shortenStart(text, max) {
17
+ if (text.length <= max)
18
+ return text;
19
+ return `…${text.slice(text.length - (max - 1))}`;
20
+ }
15
21
  /**
16
22
  * Builds a message whose inserted names are shown whole, on one line: a developer's name is never
17
23
  * cut. A headline that is longer than the limit because of its names is allowed to be.
18
24
  * `build` applies the given `cut` to every name it inserts.
19
25
  */
20
26
  export function fitted(build) {
21
- return build(oneLine);
27
+ return build(codeOnOneLine);
22
28
  }
23
29
  /** The names that a list shows, at most this many; the rest are counted. */
24
30
  const MAX_LISTED = 3;
@@ -1,7 +1,7 @@
1
1
  /** The group the rule belongs to in the docs and in `stateward status`. */
2
2
  export declare const GROUP = "callers";
3
- export declare const throwsDocMismatch: import("@stateward/core").ProjectRule<import("./index.js").ThrowsSummary, {
3
+ export declare const throwsDocMismatch: import("@stateward/core").ProjectRule<import("@stateward/core").Json, {
4
4
  severities: import("@stateward/core").SeveritiesOption<"missing" | "extra">;
5
5
  ignoreClasses: import("@stateward/core").StringsOption;
6
- }, "callers">;
6
+ }, "callers", Readonly<Record<string, import("@stateward/core").Json>>>;
7
7
  //# sourceMappingURL=throws-doc-mismatch.d.ts.map
@@ -2,7 +2,8 @@ import { defineRule, matchesName, option } from "@stateward/core";
2
2
  import { catchName, className } from "./inference/format.js";
3
3
  import { analyse, collected, evaluated, relation } from "./inference/solve.js";
4
4
  import { REASON } from "./inference/summarize.js";
5
- import { summarizeOnce } from "./inference/project.js";
5
+ import { decidingFacts } from "./inference/needs.js";
6
+ import { THROWS_ANALYSIS } from "./inference/deps.js";
6
7
  import { codeList, fitted } from "./text.js";
7
8
  import { markThrowsReferences } from "./throws-reads.js";
8
9
  /** The group the rule belongs to in the docs and in `stateward status`. */
@@ -136,6 +137,8 @@ function check(entry, analysis, ctx, reported) {
136
137
  hint: `remove \`${listed}\` from the tag if \`${name}\` cannot throw it; otherwise add \`@throws\` where it comes from`,
137
138
  location,
138
139
  against,
140
+ // Every call the function cannot follow decides it, not only the one the message names.
141
+ ...decidingFacts(all.unknowns.values()),
139
142
  });
140
143
  continue;
141
144
  }
@@ -160,9 +163,12 @@ function check(entry, analysis, ctx, reported) {
160
163
  message: fitted((cut) => isCall(source)
161
164
  ? `not checked: the call \`${cut(source.text)}(…)\` cannot be followed (${source.reason})`
162
165
  : `not checked: what \`${cut(source.text)}\` throws is not known (${source.reason}), so \`@throws\` of \`${cut(name)}\` cannot be checked`),
163
- hint: fitted((cut) => `call the function itself, or state what it throws with \`@throws\` on it or \`facts\`, so \`${cut(name)}\`'s tag can be checked`),
166
+ hint: fitted((cut) => source.fact?.kind === "untyped"
167
+ ? `import the module with \`import … from\`, so its types load and the call can be named in \`facts\`; then \`${cut(name)}\`'s tag can be checked`
168
+ : `call the function itself, or state what it throws with \`@throws\` on it or \`facts\`, so \`${cut(name)}\`'s tag can be checked`),
164
169
  location: locationOf(source.file, [source.start, source.end]),
165
170
  against,
171
+ ...decidingFacts([source]),
166
172
  });
167
173
  }
168
174
  const missing = [];
@@ -213,12 +219,14 @@ export const throwsDocMismatch = defineRule({
213
219
  explain: EXPLAIN,
214
220
  proves: { tags: ["throws"] },
215
221
  },
222
+ reads: [THROWS_ANALYSIS],
223
+ // The file side only marks the references to `@throws` tags; the facts are the analysis's.
216
224
  summarize: (ctx) => {
217
225
  markThrowsReferences(ctx);
218
- return summarizeOnce(ctx);
226
+ return undefined;
219
227
  },
220
- check(summaries, ctx) {
221
- const analysis = analyse(summaries);
228
+ check(_summaries, ctx) {
229
+ const analysis = analyse(ctx.analysisSummaries(THROWS_ANALYSIS));
222
230
  const reported = new Set();
223
231
  for (const entry of analysis.functions.values())
224
232
  check(entry, analysis, ctx, reported);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stateward/plugin-errors",
3
- "version": "1.2.2",
3
+ "version": "1.3.1",
4
4
  "description": "stateward rules for errors: errors are handled where they can happen.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -32,6 +32,6 @@
32
32
  "!dist/**/*.tsbuildinfo"
33
33
  ],
34
34
  "dependencies": {
35
- "@stateward/core": "1.2.2"
35
+ "@stateward/core": "1.3.1"
36
36
  }
37
37
  }