@murky-web/typebuddy 2.1.0 → 3.0.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.
- package/README.md +184 -72
- package/dist/collections.js +1 -2
- package/dist/empty.js +34 -13
- package/dist/guards.js +33 -9
- package/dist/ids.js +6 -6
- package/dist/oxlint.js +7 -7
- package/dist/parse.js +39 -15
- package/dist/result.js +6 -6
- package/dist/rules/async_rule.js +39 -25
- package/dist/rules/deliberate_throw.js +5 -6
- package/dist/rules/maybe_promise_rule.js +99 -40
- package/dist/rules/maybe_rule.js +7 -5
- package/dist/rules/nullable_rule.js +7 -5
- package/dist/rules/optional_rule.js +8 -4
- package/dist/rules/own_subtree.js +1 -2
- package/dist/rules/test_file.js +63 -0
- package/dist/rules/typebuddy_import.js +242 -35
- package/dist/rules/use_server.js +1 -2
- package/globals.d.ts +49 -44
- package/jsr.json +24 -23
- package/oxlint/index.ts +9 -7
- package/package.json +92 -91
- package/rules/async_rule.ts +91 -75
- package/rules/deliberate_throw.ts +11 -11
- package/rules/maybe_promise_rule.ts +238 -111
- package/rules/maybe_rule.ts +76 -49
- package/rules/nullable_rule.ts +77 -52
- package/rules/optional_rule.ts +79 -53
- package/rules/own_subtree.ts +33 -33
- package/rules/test_file.ts +114 -0
- package/rules/typebuddy_import.ts +479 -87
- package/rules/use_server.ts +5 -4
- package/src/collections.ts +1 -1
- package/src/empty.ts +58 -20
- package/src/guards.ts +100 -34
- package/src/ids.ts +10 -4
- package/src/index.ts +1 -18
- package/src/parse.ts +108 -29
- package/src/public_api.ts +22 -0
- package/src/result.ts +8 -7
- package/src/types/globals.ts +11 -11
- package/src/types/index.ts +21 -21
- package/src/types/json.ts +48 -11
- package/src/types/maybe_promise.ts +17 -12
- package/src/types/nullable.ts +2 -2
- package/src/types/optional.ts +6 -6
package/package.json
CHANGED
|
@@ -1,98 +1,99 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
"exports": {
|
|
10
|
-
".": {
|
|
11
|
-
"types": "./src/index.ts",
|
|
12
|
-
"import": "./dist/index.js",
|
|
13
|
-
"default": "./dist/index.js"
|
|
2
|
+
"name": "@murky-web/typebuddy",
|
|
3
|
+
"version": "3.0.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Your new best friend for simple typescript guards every project needs.",
|
|
6
|
+
"homepage": "https://github.com/MurkyTheMurloc/web_dev_tools/tree/main/packages/typebuddy",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/MurkyTheMurloc/web_dev_tools/issues"
|
|
14
9
|
},
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/MurkyTheMurloc/web_dev_tools.git",
|
|
14
|
+
"directory": "packages/typebuddy"
|
|
19
15
|
},
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"globals.d.ts",
|
|
19
|
+
"jsr.json",
|
|
20
|
+
"oxlint/index.ts",
|
|
21
|
+
"rules",
|
|
22
|
+
"src"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"main": "./dist/index.js",
|
|
27
|
+
"module": "./dist/index.js",
|
|
28
|
+
"types": "./src/index.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./src/index.ts",
|
|
32
|
+
"import": "./dist/index.js",
|
|
33
|
+
"default": "./dist/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./guards": {
|
|
36
|
+
"types": "./src/guards.ts",
|
|
37
|
+
"import": "./dist/guards.js",
|
|
38
|
+
"default": "./dist/guards.js"
|
|
39
|
+
},
|
|
40
|
+
"./result": {
|
|
41
|
+
"types": "./src/result.ts",
|
|
42
|
+
"import": "./dist/result.js",
|
|
43
|
+
"default": "./dist/result.js"
|
|
44
|
+
},
|
|
45
|
+
"./parse": {
|
|
46
|
+
"types": "./src/parse.ts",
|
|
47
|
+
"import": "./dist/parse.js",
|
|
48
|
+
"default": "./dist/parse.js"
|
|
49
|
+
},
|
|
50
|
+
"./empty": {
|
|
51
|
+
"types": "./src/empty.ts",
|
|
52
|
+
"import": "./dist/empty.js",
|
|
53
|
+
"default": "./dist/empty.js"
|
|
54
|
+
},
|
|
55
|
+
"./ids": {
|
|
56
|
+
"types": "./src/ids.ts",
|
|
57
|
+
"import": "./dist/ids.js",
|
|
58
|
+
"default": "./dist/ids.js"
|
|
59
|
+
},
|
|
60
|
+
"./collections": {
|
|
61
|
+
"types": "./src/collections.ts",
|
|
62
|
+
"import": "./dist/collections.js",
|
|
63
|
+
"default": "./dist/collections.js"
|
|
64
|
+
},
|
|
65
|
+
"./globals": {
|
|
66
|
+
"types": "./globals.d.ts"
|
|
67
|
+
},
|
|
68
|
+
"./oxlint": {
|
|
69
|
+
"types": "./oxlint/index.ts",
|
|
70
|
+
"import": "./dist/oxlint.js",
|
|
71
|
+
"default": "./dist/oxlint.js"
|
|
72
|
+
},
|
|
73
|
+
"./package.json": "./package.json"
|
|
24
74
|
},
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"default": "./dist/parse.js"
|
|
75
|
+
"publishConfig": {
|
|
76
|
+
"access": "public",
|
|
77
|
+
"provenance": true
|
|
29
78
|
},
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
79
|
+
"scripts": {
|
|
80
|
+
"test": "vitest run",
|
|
81
|
+
"build:local": "bun run build",
|
|
82
|
+
"build": "bun run clean && tsdown",
|
|
83
|
+
"clean": "rm -rf dist",
|
|
84
|
+
"lint": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.lint.json --type-aware src tsdown.config.ts vitest.config.ts",
|
|
85
|
+
"lint:fix": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.lint.json --type-aware --fix src tsdown.config.ts vitest.config.ts",
|
|
86
|
+
"format": "oxfmt -c ../../config/oxc/.oxfmtrc.jsonc src rules tests tsdown.config.ts vitest.config.ts",
|
|
87
|
+
"format:check": "oxfmt -c ../../config/oxc/.oxfmtrc.jsonc --check src rules tests tsdown.config.ts vitest.config.ts",
|
|
88
|
+
"smoke:globals": "bun run build && tsc --project ./smoke/globals/tsconfig.json --noEmit && mkdir -p ./smoke/globals/node_modules/@murky-web && ln -sfn ../../../.. ./smoke/globals/node_modules/@murky-web/typebuddy && tsc --project ./smoke/globals/tsconfig.types.json --noEmit",
|
|
89
|
+
"smoke:oxlint": "bun run build && bun ./smoke/oxlint/run-smoke.ts",
|
|
90
|
+
"smoke:oxlint:fix": "bun run build && bun ./smoke/oxlint/run-fix-smoke.ts",
|
|
91
|
+
"smoke:subpath": "bun run build && bun ./smoke/subpath/run.ts",
|
|
92
|
+
"smoke:treeshake": "bun run build && bun ./smoke/treeshake/run.ts",
|
|
93
|
+
"typecheck": "tsc --project ./tsconfig.json --noEmit && tsc --project ./tsconfig.vitest.json --noEmit"
|
|
34
94
|
},
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
},
|
|
40
|
-
"./collections": {
|
|
41
|
-
"types": "./src/collections.ts",
|
|
42
|
-
"import": "./dist/collections.js",
|
|
43
|
-
"default": "./dist/collections.js"
|
|
44
|
-
},
|
|
45
|
-
"./globals": {
|
|
46
|
-
"types": "./globals.d.ts"
|
|
47
|
-
},
|
|
48
|
-
"./oxlint": {
|
|
49
|
-
"types": "./oxlint/index.ts",
|
|
50
|
-
"import": "./dist/oxlint.js",
|
|
51
|
-
"default": "./dist/oxlint.js"
|
|
52
|
-
},
|
|
53
|
-
"./package.json": "./package.json"
|
|
54
|
-
},
|
|
55
|
-
"scripts": {
|
|
56
|
-
"test": "vitest run",
|
|
57
|
-
"build:local": "bun run build",
|
|
58
|
-
"build": "bun run clean && tsdown",
|
|
59
|
-
"clean": "rm -rf dist",
|
|
60
|
-
"lint": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.lint.json --type-aware src tsdown.config.ts vitest.config.ts",
|
|
61
|
-
"lint:fix": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.lint.json --type-aware --fix src tsdown.config.ts vitest.config.ts",
|
|
62
|
-
"format": "oxfmt -c ../../config/oxc/.oxfmtrc.jsonc src rules tests tsdown.config.ts vitest.config.ts",
|
|
63
|
-
"format:check": "oxfmt -c ../../config/oxc/.oxfmtrc.jsonc --check src rules tests tsdown.config.ts vitest.config.ts",
|
|
64
|
-
"smoke:globals": "bun run build && tsc --project ./smoke/globals/tsconfig.json --noEmit && mkdir -p ./smoke/globals/node_modules/@murky-web && ln -sfn ../../../.. ./smoke/globals/node_modules/@murky-web/typebuddy && tsc --project ./smoke/globals/tsconfig.types.json --noEmit",
|
|
65
|
-
"smoke:oxlint": "bun run build && bun ./smoke/oxlint/run-smoke.ts",
|
|
66
|
-
"smoke:oxlint:fix": "bun run build && bun ./smoke/oxlint/run-fix-smoke.ts",
|
|
67
|
-
"smoke:subpath": "bun run build && bun ./smoke/subpath/run.ts",
|
|
68
|
-
"smoke:treeshake": "bun run build && bun ./smoke/treeshake/run.ts",
|
|
69
|
-
"typecheck": "tsc --project ./tsconfig.json --noEmit && tsc --project ./tsconfig.vitest.json --noEmit"
|
|
70
|
-
},
|
|
71
|
-
"files": [
|
|
72
|
-
"dist",
|
|
73
|
-
"globals.d.ts",
|
|
74
|
-
"jsr.json",
|
|
75
|
-
"oxlint/index.ts",
|
|
76
|
-
"rules",
|
|
77
|
-
"src"
|
|
78
|
-
],
|
|
79
|
-
"repository": {
|
|
80
|
-
"type": "git",
|
|
81
|
-
"url": "git+https://github.com/MurkyTheMurloc/web_dev_tools.git",
|
|
82
|
-
"directory": "packages/typebuddy"
|
|
83
|
-
},
|
|
84
|
-
"homepage": "https://github.com/MurkyTheMurloc/web_dev_tools/tree/main/packages/typebuddy",
|
|
85
|
-
"bugs": {
|
|
86
|
-
"url": "https://github.com/MurkyTheMurloc/web_dev_tools/issues"
|
|
87
|
-
},
|
|
88
|
-
"license": "ISC",
|
|
89
|
-
"type": "module",
|
|
90
|
-
"sideEffects": false,
|
|
91
|
-
"publishConfig": {
|
|
92
|
-
"access": "public",
|
|
93
|
-
"provenance": true
|
|
94
|
-
},
|
|
95
|
-
"devDependencies": {
|
|
96
|
-
"@types/node": "^25.5.0"
|
|
97
|
-
}
|
|
95
|
+
"devDependencies": {
|
|
96
|
+
"@types/node": "^24.19.2",
|
|
97
|
+
"oxlint": "^1.94.0"
|
|
98
|
+
}
|
|
98
99
|
}
|
package/rules/async_rule.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { throwsDeliberately } from "./deliberate_throw.js";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
createImportPlanner,
|
|
4
|
+
getBodyStartAfterDirectives
|
|
5
|
+
} from "./typebuddy_import.js";
|
|
3
6
|
import { createUseServerGuard } from "./use_server.js";
|
|
4
7
|
|
|
5
8
|
type AstNode = {
|
|
@@ -11,26 +14,26 @@ type AstNode = {
|
|
|
11
14
|
[key: string]: unknown;
|
|
12
15
|
};
|
|
13
16
|
|
|
17
|
+
type RuleFixer = {
|
|
18
|
+
replaceText(node: unknown, text: string): unknown;
|
|
19
|
+
insertTextBeforeRange(range: [number, number], text: string): unknown;
|
|
20
|
+
};
|
|
21
|
+
|
|
14
22
|
type RuleContext = {
|
|
15
|
-
getSourceCode(): {
|
|
23
|
+
getSourceCode(): {
|
|
24
|
+
getText(node: unknown): string;
|
|
25
|
+
getScope(node: unknown): unknown;
|
|
26
|
+
};
|
|
16
27
|
report(descriptor: {
|
|
17
28
|
node: unknown;
|
|
18
29
|
messageId: string;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
text: string,
|
|
24
|
-
): unknown;
|
|
25
|
-
}): unknown;
|
|
30
|
+
suggest?: {
|
|
31
|
+
messageId: string;
|
|
32
|
+
fix(fixer: RuleFixer): unknown;
|
|
33
|
+
}[];
|
|
26
34
|
}): void;
|
|
27
35
|
};
|
|
28
36
|
|
|
29
|
-
type RuleFixer = {
|
|
30
|
-
replaceText(node: unknown, text: string): unknown;
|
|
31
|
-
insertTextBeforeRange(range: [number, number], text: string): unknown;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
37
|
function hasTryCatch(nodes: AstNode[]): boolean {
|
|
35
38
|
return nodes.some((node) => {
|
|
36
39
|
return node.type === "TryStatement";
|
|
@@ -55,50 +58,36 @@ function isCallArgumentCallback(node: AstNode): boolean {
|
|
|
55
58
|
const rule = {
|
|
56
59
|
create(context: RuleContext) {
|
|
57
60
|
const sourceCode = context.getSourceCode();
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
function ensureErrImportFixes(
|
|
61
|
-
node: AstNode,
|
|
62
|
-
fixer: RuleFixer,
|
|
63
|
-
): unknown[] {
|
|
64
|
-
if (scheduledErrImport) {
|
|
65
|
-
return [];
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const insertion = getTypeBuddyImportInsertion(node, "err");
|
|
69
|
-
if (!insertion) {
|
|
70
|
-
return [];
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
scheduledErrImport = true;
|
|
74
|
-
|
|
75
|
-
return [
|
|
76
|
-
fixer.insertTextBeforeRange(insertion.range, insertion.text),
|
|
77
|
-
];
|
|
78
|
-
}
|
|
61
|
+
const imports = createImportPlanner(sourceCode);
|
|
79
62
|
|
|
80
63
|
// Two zero-width insertions leave the body's own text untouched, byte
|
|
81
64
|
// for byte. Rebuilding it statement by statement used to strip leading
|
|
82
65
|
// whitespace on every line — including lines inside template literals,
|
|
83
66
|
// which silently changed string values.
|
|
67
|
+
//
|
|
68
|
+
// The `try {` goes after the directive prologue. `"use cache"` and
|
|
69
|
+
// `"use server"` only mean something as the first statements of the
|
|
70
|
+
// body; inside a `try` they are plain expression statements.
|
|
84
71
|
function wrapBlockFixes(
|
|
85
72
|
bodyNode: AstNode,
|
|
86
|
-
fixer: RuleFixer
|
|
73
|
+
fixer: RuleFixer
|
|
87
74
|
): unknown[] | null {
|
|
88
75
|
const range = bodyNode.range;
|
|
89
|
-
|
|
76
|
+
const tryStart = getBodyStartAfterDirectives(bodyNode);
|
|
77
|
+
if (
|
|
78
|
+
!Array.isArray(range) ||
|
|
79
|
+
range.length < 2 ||
|
|
80
|
+
tryStart === null
|
|
81
|
+
) {
|
|
90
82
|
return null;
|
|
91
83
|
}
|
|
92
84
|
|
|
93
85
|
return [
|
|
94
|
-
fixer.insertTextBeforeRange(
|
|
95
|
-
[range[0] + 1, range[0] + 1],
|
|
96
|
-
"\ntry {",
|
|
97
|
-
),
|
|
86
|
+
fixer.insertTextBeforeRange([tryStart, tryStart], "\ntry {"),
|
|
98
87
|
fixer.insertTextBeforeRange(
|
|
99
88
|
[range[1] - 1, range[1] - 1],
|
|
100
|
-
"} catch {\nreturn err();\n}\n"
|
|
101
|
-
)
|
|
89
|
+
"} catch {\nreturn err();\n}\n"
|
|
90
|
+
)
|
|
102
91
|
];
|
|
103
92
|
}
|
|
104
93
|
|
|
@@ -114,7 +103,7 @@ const rule = {
|
|
|
114
103
|
function wrapExpressionBodyFixes(
|
|
115
104
|
node: AstNode,
|
|
116
105
|
bodyNode: AstNode,
|
|
117
|
-
fixer: RuleFixer
|
|
106
|
+
fixer: RuleFixer
|
|
118
107
|
): unknown[] | null {
|
|
119
108
|
const arrowRange = node.range;
|
|
120
109
|
const bodyRange = bodyNode.range;
|
|
@@ -144,17 +133,59 @@ const rule = {
|
|
|
144
133
|
return [
|
|
145
134
|
fixer.insertTextBeforeRange(
|
|
146
135
|
[start, start],
|
|
147
|
-
"{\ntry {\nreturn "
|
|
136
|
+
"{\ntry {\nreturn "
|
|
148
137
|
),
|
|
149
138
|
fixer.insertTextBeforeRange(
|
|
150
139
|
[end, end],
|
|
151
|
-
";\n} catch {\nreturn err();\n}\n}"
|
|
152
|
-
)
|
|
140
|
+
";\n} catch {\nreturn err();\n}\n}"
|
|
141
|
+
)
|
|
142
|
+
];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The wrap is a suggestion, never a fix. It turns every exception of
|
|
147
|
+
* the function into `err()`, so a failure nobody handled becomes a
|
|
148
|
+
* value somebody has to remember to check, and `catch { return err();
|
|
149
|
+
* }` hides what went wrong. Whether that is wanted is the author's
|
|
150
|
+
* call. `err` has to be free at that spot: a parameter or local called
|
|
151
|
+
* `err` would make `err()` call something else, and then there is no
|
|
152
|
+
* suggestion.
|
|
153
|
+
*/
|
|
154
|
+
function suggestWrap(
|
|
155
|
+
scopeNode: AstNode,
|
|
156
|
+
wrap: (fixer: RuleFixer) => unknown[] | null
|
|
157
|
+
) {
|
|
158
|
+
return [
|
|
159
|
+
{
|
|
160
|
+
messageId: "suggestTryCatch",
|
|
161
|
+
fix(fixer: RuleFixer) {
|
|
162
|
+
const wrapFixes = wrap(fixer);
|
|
163
|
+
if (!wrapFixes) {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const importFixes = imports.fixes(
|
|
168
|
+
scopeNode,
|
|
169
|
+
fixer,
|
|
170
|
+
"err",
|
|
171
|
+
"value"
|
|
172
|
+
);
|
|
173
|
+
if (importFixes === null) {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return [...importFixes, ...wrapFixes];
|
|
178
|
+
}
|
|
179
|
+
}
|
|
153
180
|
];
|
|
154
181
|
}
|
|
155
182
|
|
|
156
183
|
function checkFunction(node: AstNode) {
|
|
157
184
|
if (node.async !== true) return;
|
|
185
|
+
// An async generator yields its values; there is no single result
|
|
186
|
+
// to wrap, and a `catch` that returns would end the iteration and
|
|
187
|
+
// swallow the error.
|
|
188
|
+
if (node["generator"] === true) return;
|
|
158
189
|
if (isCallArgumentCallback(node)) return;
|
|
159
190
|
const bodyNode = node.body;
|
|
160
191
|
if (!bodyNode || Array.isArray(bodyNode)) return;
|
|
@@ -170,21 +201,9 @@ const rule = {
|
|
|
170
201
|
context.report({
|
|
171
202
|
node,
|
|
172
203
|
messageId: "missingTryCatch",
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
bodyNode,
|
|
177
|
-
fixer,
|
|
178
|
-
);
|
|
179
|
-
if (!wrapFixes) {
|
|
180
|
-
return null;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
return [
|
|
184
|
-
...ensureErrImportFixes(node, fixer),
|
|
185
|
-
...wrapFixes,
|
|
186
|
-
];
|
|
187
|
-
},
|
|
204
|
+
suggest: suggestWrap(node, (fixer) => {
|
|
205
|
+
return wrapExpressionBodyFixes(node, bodyNode, fixer);
|
|
206
|
+
})
|
|
188
207
|
});
|
|
189
208
|
return;
|
|
190
209
|
}
|
|
@@ -195,14 +214,9 @@ const rule = {
|
|
|
195
214
|
context.report({
|
|
196
215
|
node,
|
|
197
216
|
messageId: "missingTryCatch",
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
return null;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
return [...ensureErrImportFixes(node, fixer), ...wrapFixes];
|
|
205
|
-
},
|
|
217
|
+
suggest: suggestWrap(bodyNode, (fixer) => {
|
|
218
|
+
return wrapBlockFixes(bodyNode, fixer);
|
|
219
|
+
})
|
|
206
220
|
});
|
|
207
221
|
}
|
|
208
222
|
|
|
@@ -212,23 +226,25 @@ const rule = {
|
|
|
212
226
|
Program,
|
|
213
227
|
FunctionDeclaration: guard(checkFunction),
|
|
214
228
|
FunctionExpression: guard(checkFunction),
|
|
215
|
-
ArrowFunctionExpression: guard(checkFunction)
|
|
229
|
+
ArrowFunctionExpression: guard(checkFunction)
|
|
216
230
|
};
|
|
217
231
|
},
|
|
218
232
|
defaultOptions: [],
|
|
219
233
|
meta: {
|
|
220
234
|
type: "suggestion",
|
|
235
|
+
hasSuggestions: true,
|
|
221
236
|
docs: {
|
|
222
237
|
description:
|
|
223
|
-
"Async functions should have a try-catch block returning an error result."
|
|
238
|
+
"Async functions should have a try-catch block returning an error result."
|
|
224
239
|
},
|
|
225
|
-
fixable: "code",
|
|
226
240
|
schema: [],
|
|
227
241
|
messages: {
|
|
228
242
|
missingTryCatch:
|
|
229
243
|
"Async functions must have a try-catch block returning an error result.",
|
|
230
|
-
|
|
231
|
-
|
|
244
|
+
suggestTryCatch:
|
|
245
|
+
"Wrap the body in try/catch and return err() from the catch."
|
|
246
|
+
}
|
|
247
|
+
}
|
|
232
248
|
};
|
|
233
249
|
|
|
234
250
|
export { rule as requireTryCatchAsyncRule };
|
|
@@ -5,12 +5,12 @@ import { walkOwnSubtree } from "./own_subtree.js";
|
|
|
5
5
|
*
|
|
6
6
|
* A `throw` in a function's own body is the author saying this failure is not a
|
|
7
7
|
* value to hand back. A missing manifest for a route the app is navigating to,
|
|
8
|
-
* an unreachable branch, a broken invariant — those should be loud. Turning
|
|
9
|
-
* into `err()` converts a bug into a silently handled outcome, and the
|
|
10
|
-
* degrades politely instead of surfacing that something is wrong.
|
|
8
|
+
* an unreachable branch, a broken invariant — those should be loud. Turning
|
|
9
|
+
* them into `err()` converts a bug into a silently handled outcome, and the
|
|
10
|
+
* caller degrades politely instead of surfacing that something is wrong.
|
|
11
11
|
*
|
|
12
12
|
* So both async rules leave such a function alone. The rules exist to push
|
|
13
|
-
*
|
|
13
|
+
* _recoverable_ failures into the return type, not to abolish throwing.
|
|
14
14
|
*
|
|
15
15
|
* Two boundaries matter. A throw inside a nested function belongs to that
|
|
16
16
|
* function, so the walk does not descend into one. A throw inside `catch` still
|
|
@@ -21,15 +21,15 @@ import { walkOwnSubtree } from "./own_subtree.js";
|
|
|
21
21
|
* @returns {boolean} True when the body throws outside any nested function.
|
|
22
22
|
*/
|
|
23
23
|
function throwsDeliberately(body: unknown): boolean {
|
|
24
|
-
|
|
24
|
+
let found = false;
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
walkOwnSubtree(body, (node) => {
|
|
27
|
+
if (node.type === "ThrowStatement") {
|
|
28
|
+
found = true;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
return found;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
export { throwsDeliberately };
|