@murky-web/typebuddy 2.1.0 → 3.0.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 +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 +3 -2
- package/oxlint/index.ts +7 -5
- package/package.json +92 -91
- package/rules/async_rule.ts +80 -64
- package/rules/deliberate_throw.ts +11 -11
- package/rules/maybe_promise_rule.ts +218 -91
- package/rules/maybe_rule.ts +74 -47
- package/rules/nullable_rule.ts +78 -50
- package/rules/optional_rule.ts +80 -51
- 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 +2 -1
- package/src/empty.ts +57 -19
- package/src/guards.ts +96 -30
- package/src/ids.ts +10 -4
- package/src/index.ts +1 -18
- package/src/parse.ts +104 -25
- package/src/public_api.ts +22 -0
- package/src/result.ts +5 -4
- 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/dist/rules/async_rule.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { throwsDeliberately } from "./deliberate_throw.js";
|
|
2
|
-
import {
|
|
2
|
+
import { createImportPlanner, getBodyStartAfterDirectives } from "./typebuddy_import.js";
|
|
3
3
|
import { createUseServerGuard } from "./use_server.js";
|
|
4
|
-
|
|
4
|
+
// rules/async_rule.ts
|
|
5
5
|
function hasTryCatch(nodes) {
|
|
6
6
|
return nodes.some((node) => {
|
|
7
7
|
return node.type === "TryStatement";
|
|
@@ -16,18 +16,12 @@ function isCallArgumentCallback(node) {
|
|
|
16
16
|
const rule = {
|
|
17
17
|
create(context) {
|
|
18
18
|
const sourceCode = context.getSourceCode();
|
|
19
|
-
|
|
20
|
-
function ensureErrImportFixes(node, fixer) {
|
|
21
|
-
if (scheduledErrImport) return [];
|
|
22
|
-
const insertion = getTypeBuddyImportInsertion(node, "err");
|
|
23
|
-
if (!insertion) return [];
|
|
24
|
-
scheduledErrImport = true;
|
|
25
|
-
return [fixer.insertTextBeforeRange(insertion.range, insertion.text)];
|
|
26
|
-
}
|
|
19
|
+
const imports = createImportPlanner(sourceCode);
|
|
27
20
|
function wrapBlockFixes(bodyNode, fixer) {
|
|
28
21
|
const range = bodyNode.range;
|
|
29
|
-
|
|
30
|
-
|
|
22
|
+
const tryStart = getBodyStartAfterDirectives(bodyNode);
|
|
23
|
+
if (!Array.isArray(range) || range.length < 2 || tryStart === null) return null;
|
|
24
|
+
return [fixer.insertTextBeforeRange([tryStart, tryStart], "\ntry {"), fixer.insertTextBeforeRange([range[1] - 1, range[1] - 1], "} catch {\nreturn err();\n}\n")];
|
|
31
25
|
}
|
|
32
26
|
/**
|
|
33
27
|
* Wrap an expression-bodied arrow's value in a block with try/catch.
|
|
@@ -49,8 +43,30 @@ const rule = {
|
|
|
49
43
|
if (start >= end) return null;
|
|
50
44
|
return [fixer.insertTextBeforeRange([start, start], "{\ntry {\nreturn "), fixer.insertTextBeforeRange([end, end], ";\n} catch {\nreturn err();\n}\n}")];
|
|
51
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* The wrap is a suggestion, never a fix. It turns every exception of
|
|
48
|
+
* the function into `err()`, so a failure nobody handled becomes a
|
|
49
|
+
* value somebody has to remember to check, and `catch { return err();
|
|
50
|
+
* }` hides what went wrong. Whether that is wanted is the author's
|
|
51
|
+
* call. `err` has to be free at that spot: a parameter or local called
|
|
52
|
+
* `err` would make `err()` call something else, and then there is no
|
|
53
|
+
* suggestion.
|
|
54
|
+
*/
|
|
55
|
+
function suggestWrap(scopeNode, wrap) {
|
|
56
|
+
return [{
|
|
57
|
+
messageId: "suggestTryCatch",
|
|
58
|
+
fix(fixer) {
|
|
59
|
+
const wrapFixes = wrap(fixer);
|
|
60
|
+
if (!wrapFixes) return null;
|
|
61
|
+
const importFixes = imports.fixes(scopeNode, fixer, "err", "value");
|
|
62
|
+
if (importFixes === null) return null;
|
|
63
|
+
return [...importFixes, ...wrapFixes];
|
|
64
|
+
}
|
|
65
|
+
}];
|
|
66
|
+
}
|
|
52
67
|
function checkFunction(node) {
|
|
53
68
|
if (node.async !== true) return;
|
|
69
|
+
if (node["generator"] === true) return;
|
|
54
70
|
if (isCallArgumentCallback(node)) return;
|
|
55
71
|
const bodyNode = node.body;
|
|
56
72
|
if (!bodyNode || Array.isArray(bodyNode)) return;
|
|
@@ -59,11 +75,9 @@ const rule = {
|
|
|
59
75
|
context.report({
|
|
60
76
|
node,
|
|
61
77
|
messageId: "missingTryCatch",
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return [...ensureErrImportFixes(node, fixer), ...wrapFixes];
|
|
66
|
-
}
|
|
78
|
+
suggest: suggestWrap(node, (fixer) => {
|
|
79
|
+
return wrapExpressionBodyFixes(node, bodyNode, fixer);
|
|
80
|
+
})
|
|
67
81
|
});
|
|
68
82
|
return;
|
|
69
83
|
}
|
|
@@ -71,11 +85,9 @@ const rule = {
|
|
|
71
85
|
context.report({
|
|
72
86
|
node,
|
|
73
87
|
messageId: "missingTryCatch",
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
return [...ensureErrImportFixes(node, fixer), ...wrapFixes];
|
|
78
|
-
}
|
|
88
|
+
suggest: suggestWrap(bodyNode, (fixer) => {
|
|
89
|
+
return wrapBlockFixes(bodyNode, fixer);
|
|
90
|
+
})
|
|
79
91
|
});
|
|
80
92
|
}
|
|
81
93
|
const { Program, guard } = createUseServerGuard();
|
|
@@ -89,11 +101,13 @@ const rule = {
|
|
|
89
101
|
defaultOptions: [],
|
|
90
102
|
meta: {
|
|
91
103
|
type: "suggestion",
|
|
104
|
+
hasSuggestions: true,
|
|
92
105
|
docs: { description: "Async functions should have a try-catch block returning an error result." },
|
|
93
|
-
fixable: "code",
|
|
94
106
|
schema: [],
|
|
95
|
-
messages: {
|
|
107
|
+
messages: {
|
|
108
|
+
missingTryCatch: "Async functions must have a try-catch block returning an error result.",
|
|
109
|
+
suggestTryCatch: "Wrap the body in try/catch and return err() from the catch."
|
|
110
|
+
}
|
|
96
111
|
}
|
|
97
112
|
};
|
|
98
|
-
//#endregion
|
|
99
113
|
export { rule as requireTryCatchAsyncRule };
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { walkOwnSubtree } from "./own_subtree.js";
|
|
2
|
-
|
|
2
|
+
// rules/deliberate_throw.ts
|
|
3
3
|
/**
|
|
4
4
|
* Whether a function body throws on purpose.
|
|
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
|
|
@@ -27,5 +27,4 @@ function throwsDeliberately(body) {
|
|
|
27
27
|
});
|
|
28
28
|
return found;
|
|
29
29
|
}
|
|
30
|
-
//#endregion
|
|
31
30
|
export { throwsDeliberately };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { walkOwnSubtree } from "./own_subtree.js";
|
|
2
1
|
import { throwsDeliberately } from "./deliberate_throw.js";
|
|
3
|
-
import {
|
|
2
|
+
import { walkOwnSubtree } from "./own_subtree.js";
|
|
3
|
+
import { canAddImport, createImportPlanner } from "./typebuddy_import.js";
|
|
4
4
|
import { createUseServerGuard } from "./use_server.js";
|
|
5
|
-
|
|
5
|
+
// rules/maybe_promise_rule.ts
|
|
6
6
|
function isNode(value) {
|
|
7
7
|
return typeof value === "object" && value !== null && "type" in value;
|
|
8
8
|
}
|
|
@@ -54,6 +54,14 @@ function getObjectPropertyValue(node, propertyName) {
|
|
|
54
54
|
for (const property of node["properties"]) if (isNode(property) && property.type === "Property" && isIdentifierNamed(property["key"], propertyName) && isNode(property["value"])) return property["value"];
|
|
55
55
|
return null;
|
|
56
56
|
}
|
|
57
|
+
const RESULT_PROPERTY_COUNT = 2;
|
|
58
|
+
/** An object literal with exactly `isError` and `value`: no spread, no extras. */
|
|
59
|
+
function hasOnlyResultProperties(node) {
|
|
60
|
+
if (!isNode(node) || node.type !== "ObjectExpression" || !Array.isArray(node["properties"]) || node["properties"].length !== RESULT_PROPERTY_COUNT) return false;
|
|
61
|
+
return node["properties"].every((property) => {
|
|
62
|
+
return isNode(property) && property.type === "Property" && property["computed"] !== true && (isIdentifierNamed(property["key"], "isError") || isIdentifierNamed(property["key"], "value"));
|
|
63
|
+
});
|
|
64
|
+
}
|
|
57
65
|
function isBooleanLiteral(node, expected) {
|
|
58
66
|
return isNode(node) && node.type === "Literal" && node["value"] === expected;
|
|
59
67
|
}
|
|
@@ -63,7 +71,7 @@ function isNullLiteral(node) {
|
|
|
63
71
|
const rule = {
|
|
64
72
|
create(context) {
|
|
65
73
|
const sourceCode = context.getSourceCode();
|
|
66
|
-
const
|
|
74
|
+
const imports = createImportPlanner(sourceCode);
|
|
67
75
|
function isTypeReference(node) {
|
|
68
76
|
return isNode(node) && node.type === "TSTypeReference";
|
|
69
77
|
}
|
|
@@ -82,9 +90,9 @@ const rule = {
|
|
|
82
90
|
/**
|
|
83
91
|
* The awaited type behind an async function's return annotation.
|
|
84
92
|
*
|
|
85
|
-
* Every spelling counts. Matching only `Promise` meant the rule read
|
|
86
|
-
* return type of code it had not migrated yet and went blind the
|
|
87
|
-
* `MaybePromise` was there — so a bare `return;` in a
|
|
93
|
+
* Every spelling counts. Matching only `Promise` meant the rule read
|
|
94
|
+
* the return type of code it had not migrated yet and went blind the
|
|
95
|
+
* moment `MaybePromise` was there — so a bare `return;` in a
|
|
88
96
|
* `MaybePromise<void>` function never became `return ok();`, while the
|
|
89
97
|
* identical `Promise<void>` function was fixed. It only ever worked
|
|
90
98
|
* because the rename and this lookup happen in the same pass, off the
|
|
@@ -95,32 +103,76 @@ const rule = {
|
|
|
95
103
|
if (typeName === null || !AWAITED_TYPE_NAMES.has(typeName)) return null;
|
|
96
104
|
return getTypeArgument(node);
|
|
97
105
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
106
|
+
/**
|
|
107
|
+
* The fixes that rewrite `at` and make `helperName` available there, or
|
|
108
|
+
* `null` when the name is something else at that spot (a catch
|
|
109
|
+
* parameter called `err`, a local called `ok`): the rule then reports
|
|
110
|
+
* without offering a fix, because a fix would call the wrong thing.
|
|
111
|
+
*/
|
|
112
|
+
function withHelperFixes(at, fixer, helperName, rewrite) {
|
|
113
|
+
const importFixes = imports.fixes(at, fixer, helperName, "value");
|
|
114
|
+
if (importFixes === null) return null;
|
|
115
|
+
return [...importFixes, rewrite];
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Same as `withHelperFixes`, as a suggestion. A suggestion is applied
|
|
119
|
+
* alone, so it carries its own import too.
|
|
120
|
+
*/
|
|
121
|
+
function suggestWithHelper(at, messageId, helperName, rewrite) {
|
|
122
|
+
return [{
|
|
123
|
+
messageId,
|
|
124
|
+
fix(fixer) {
|
|
125
|
+
return withHelperFixes(at, fixer, helperName, rewrite(fixer));
|
|
126
|
+
}
|
|
127
|
+
}];
|
|
104
128
|
}
|
|
105
|
-
|
|
129
|
+
/**
|
|
130
|
+
* `Promise<T>` to `MaybePromise<T>`.
|
|
131
|
+
*
|
|
132
|
+
* Only on a function this file defines (`own`) is it a fix: the author
|
|
133
|
+
* is moving that function's contract to `MaybePromise`. A function
|
|
134
|
+
* type, an interface method or an overload describes a shape that other
|
|
135
|
+
* code, often a library's, has to match, so there it is a suggestion
|
|
136
|
+
* the author decides on.
|
|
137
|
+
*/
|
|
138
|
+
function reportPromiseReturnType(node, own) {
|
|
106
139
|
if (!isNode(node["returnType"])) return;
|
|
107
140
|
const typeAnnotation = node["returnType"]["typeAnnotation"];
|
|
108
141
|
if (!isTypeReference(typeAnnotation)) return;
|
|
109
142
|
if (getTypeName(typeAnnotation) !== "Promise") return;
|
|
110
143
|
const typeName = typeAnnotation["typeName"];
|
|
144
|
+
const rewrite = (fixer) => {
|
|
145
|
+
const typeImport = imports.fixes(typeName, fixer, "MaybePromise", "type");
|
|
146
|
+
if (typeImport === null) return null;
|
|
147
|
+
return [...typeImport, fixer.replaceText(typeName, "MaybePromise")];
|
|
148
|
+
};
|
|
149
|
+
if (own && canAddImport(node)) {
|
|
150
|
+
context.report({
|
|
151
|
+
node: typeName,
|
|
152
|
+
messageId: "replaceWithMaybePromise",
|
|
153
|
+
fix(fixer) {
|
|
154
|
+
return rewrite(fixer);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
111
159
|
context.report({
|
|
112
160
|
node: typeName,
|
|
113
161
|
messageId: "replaceWithMaybePromise",
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
162
|
+
suggest: [{
|
|
163
|
+
messageId: "suggestMaybePromise",
|
|
164
|
+
fix(fixer) {
|
|
165
|
+
return rewrite(fixer);
|
|
166
|
+
}
|
|
167
|
+
}]
|
|
117
168
|
});
|
|
118
169
|
}
|
|
119
170
|
function checkReturnType(node) {
|
|
120
171
|
if (!isAsyncFunction(node)) return;
|
|
172
|
+
if (node["generator"] === true) return;
|
|
121
173
|
if (isCallArgumentCallback(node)) return;
|
|
122
174
|
if (throwsDeliberately(node["body"])) return;
|
|
123
|
-
reportPromiseReturnType(node);
|
|
175
|
+
reportPromiseReturnType(node, true);
|
|
124
176
|
}
|
|
125
177
|
/**
|
|
126
178
|
* A type-level signature — an interface method, a function type, an
|
|
@@ -132,10 +184,14 @@ const rule = {
|
|
|
132
184
|
* Ambient declarations are the exception: `declare` describes code the
|
|
133
185
|
* project does not own, and rewriting a third-party framework's
|
|
134
186
|
* callback shape to `MaybePromise` would be a lie about that API.
|
|
187
|
+
*
|
|
188
|
+
* Every other signature is a suggestion, never a fix: a function type
|
|
189
|
+
* or an interface method is often the shape a library hands in or
|
|
190
|
+
* expects back, and only the author knows which.
|
|
135
191
|
*/
|
|
136
192
|
function checkSignatureReturnType(node) {
|
|
137
193
|
if (isAmbient(node)) return;
|
|
138
|
-
reportPromiseReturnType(node);
|
|
194
|
+
reportPromiseReturnType(node, false);
|
|
139
195
|
}
|
|
140
196
|
function wrapReturnValue(node, isAsync, returnType) {
|
|
141
197
|
if (!isAsync) return;
|
|
@@ -145,7 +201,7 @@ const rule = {
|
|
|
145
201
|
node,
|
|
146
202
|
messageId: "returnVoidPromise",
|
|
147
203
|
fix(fixer) {
|
|
148
|
-
return
|
|
204
|
+
return withHelperFixes(node, fixer, "ok", fixer.replaceText(node, "return ok();"));
|
|
149
205
|
}
|
|
150
206
|
});
|
|
151
207
|
return;
|
|
@@ -154,23 +210,23 @@ const rule = {
|
|
|
154
210
|
if (isResultHelperCall(argument, "ok") || isResultHelperCall(argument, "err")) return;
|
|
155
211
|
const isErrorValue = getObjectPropertyValue(argument, "isError");
|
|
156
212
|
const objectValue = getObjectPropertyValue(argument, "value");
|
|
157
|
-
if (isBooleanLiteral(isErrorValue, false) && objectValue) {
|
|
213
|
+
if (isBooleanLiteral(isErrorValue, false) && objectValue && hasOnlyResultProperties(argument)) {
|
|
158
214
|
context.report({
|
|
159
215
|
node: argument,
|
|
160
216
|
messageId: "preferOkResult",
|
|
161
217
|
fix(fixer) {
|
|
162
218
|
const valueText = sourceCode.getText(objectValue);
|
|
163
|
-
return
|
|
219
|
+
return withHelperFixes(node, fixer, "ok", fixer.replaceText(argument, `ok(${valueText})`));
|
|
164
220
|
}
|
|
165
221
|
});
|
|
166
222
|
return;
|
|
167
223
|
}
|
|
168
|
-
if (isBooleanLiteral(isErrorValue, true) && isNullLiteral(objectValue)) {
|
|
224
|
+
if (isBooleanLiteral(isErrorValue, true) && isNullLiteral(objectValue) && hasOnlyResultProperties(argument)) {
|
|
169
225
|
context.report({
|
|
170
226
|
node: argument,
|
|
171
227
|
messageId: "preferErrResult",
|
|
172
228
|
fix(fixer) {
|
|
173
|
-
return
|
|
229
|
+
return withHelperFixes(node, fixer, "err", fixer.replaceText(argument, "err()"));
|
|
174
230
|
}
|
|
175
231
|
});
|
|
176
232
|
return;
|
|
@@ -178,10 +234,10 @@ const rule = {
|
|
|
178
234
|
if (!hasIsErrorFlag(argument, true) && !hasIsErrorFlag(argument, false)) context.report({
|
|
179
235
|
node: argument,
|
|
180
236
|
messageId: "wrapReturn",
|
|
181
|
-
|
|
237
|
+
suggest: suggestWithHelper(node, "suggestWrapReturn", "ok", (fixer) => {
|
|
182
238
|
const argumentText = sourceCode.getText(argument);
|
|
183
|
-
return
|
|
184
|
-
}
|
|
239
|
+
return fixer.replaceText(argument, `ok(${argumentText})`);
|
|
240
|
+
})
|
|
185
241
|
});
|
|
186
242
|
}
|
|
187
243
|
function collectOwnReturns(root) {
|
|
@@ -199,14 +255,14 @@ const rule = {
|
|
|
199
255
|
/**
|
|
200
256
|
* Push a single `return` inside a catch block towards `err()`.
|
|
201
257
|
*
|
|
202
|
-
* Only the lossless rewrites carry a fix. Replacing
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
258
|
+
* Only the lossless rewrites carry a fix. Replacing `return
|
|
259
|
+
* ok("defaults")` with `return err()` would silently drop the author's
|
|
260
|
+
* fallback, and the old behaviour — appending `return err()` after it —
|
|
261
|
+
* produced unreachable code and a report that could never be satisfied.
|
|
262
|
+
* Where the fallback matters, moving it into the try block is a
|
|
263
|
+
* decision the author has to make, so the rule reports and stops.
|
|
208
264
|
*/
|
|
209
|
-
function reportCatchReturn(
|
|
265
|
+
function reportCatchReturn(statement) {
|
|
210
266
|
const argument = statement["argument"];
|
|
211
267
|
if (isSettledFailure(argument)) return;
|
|
212
268
|
if (!isNode(argument)) {
|
|
@@ -214,17 +270,17 @@ const rule = {
|
|
|
214
270
|
node: statement,
|
|
215
271
|
messageId: "returnFailedPromise",
|
|
216
272
|
fix(fixer) {
|
|
217
|
-
return
|
|
273
|
+
return withHelperFixes(statement, fixer, "err", fixer.replaceText(statement, "return err();"));
|
|
218
274
|
}
|
|
219
275
|
});
|
|
220
276
|
return;
|
|
221
277
|
}
|
|
222
278
|
if (hasIsErrorFlag(argument, true)) {
|
|
223
|
-
if (isNullLiteral(getObjectPropertyValue(argument, "value"))) context.report({
|
|
279
|
+
if (isNullLiteral(getObjectPropertyValue(argument, "value")) && hasOnlyResultProperties(argument)) context.report({
|
|
224
280
|
node: argument,
|
|
225
281
|
messageId: "preferErrResult",
|
|
226
282
|
fix(fixer) {
|
|
227
|
-
return
|
|
283
|
+
return withHelperFixes(statement, fixer, "err", fixer.replaceText(argument, "err()"));
|
|
228
284
|
}
|
|
229
285
|
});
|
|
230
286
|
return;
|
|
@@ -247,6 +303,7 @@ const rule = {
|
|
|
247
303
|
parent = parent.parent;
|
|
248
304
|
}
|
|
249
305
|
if (!isAsync || !parentFunction) return;
|
|
306
|
+
if (parentFunction["generator"] === true) return;
|
|
250
307
|
if (isCallArgumentCallback(parentFunction)) return;
|
|
251
308
|
if (throwsDeliberately(parentFunction["body"])) return;
|
|
252
309
|
let returnType = null;
|
|
@@ -258,7 +315,7 @@ const rule = {
|
|
|
258
315
|
const handler = node["handler"];
|
|
259
316
|
if (!isNode(handler) || !isNode(handler["body"])) return;
|
|
260
317
|
const catchReturns = collectOwnReturns(handler["body"]);
|
|
261
|
-
for (const statement of catchReturns) reportCatchReturn(
|
|
318
|
+
for (const statement of catchReturns) reportCatchReturn(statement);
|
|
262
319
|
let settlesItself = false;
|
|
263
320
|
walkOwnSubtree(handler["body"], (candidate) => {
|
|
264
321
|
if (candidate.type === "ReturnStatement") settlesItself = true;
|
|
@@ -270,7 +327,7 @@ const rule = {
|
|
|
270
327
|
fix(fixer) {
|
|
271
328
|
const handlerBody = handler["body"];
|
|
272
329
|
if (!isNode(handlerBody) || !Array.isArray(handlerBody["range"])) return null;
|
|
273
|
-
return
|
|
330
|
+
return withHelperFixes(handlerBody, fixer, "err", fixer.insertTextBeforeRange([handlerBody["range"][1] - 1, handlerBody["range"][1] - 1], "\nreturn err();\n"));
|
|
274
331
|
}
|
|
275
332
|
});
|
|
276
333
|
}
|
|
@@ -291,10 +348,13 @@ const rule = {
|
|
|
291
348
|
type: "suggestion",
|
|
292
349
|
docs: { description: "Ensure async functions' return values follow the MaybePromise pattern." },
|
|
293
350
|
fixable: "code",
|
|
351
|
+
hasSuggestions: true,
|
|
294
352
|
schema: [],
|
|
295
353
|
messages: {
|
|
296
354
|
replaceWithMaybePromise: "Use 'MaybePromise' instead of 'Promise' as the return type in async functions.",
|
|
355
|
+
suggestMaybePromise: "Replace 'Promise' with 'MaybePromise'.",
|
|
297
356
|
wrapReturn: "Wrap return value with ok(value).",
|
|
357
|
+
suggestWrapReturn: "Wrap the return value in ok(...).",
|
|
298
358
|
preferOkResult: "Use ok(value) instead of an inline success result object.",
|
|
299
359
|
preferErrResult: "Use err() instead of an inline error result object.",
|
|
300
360
|
returnVoidPromise: "Return ok() for async functions with Promise<void> return type.",
|
|
@@ -302,5 +362,4 @@ const rule = {
|
|
|
302
362
|
}
|
|
303
363
|
}
|
|
304
364
|
};
|
|
305
|
-
//#endregion
|
|
306
365
|
export { rule as errorSafeAsyncRule };
|
package/dist/rules/maybe_rule.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import { createImportPlanner } from "./typebuddy_import.js";
|
|
2
|
+
// rules/maybe_rule.ts
|
|
2
3
|
const rule = {
|
|
3
4
|
create(context) {
|
|
5
|
+
const sourceCode = context.getSourceCode();
|
|
6
|
+
const imports = createImportPlanner(sourceCode);
|
|
4
7
|
return { TSUnionType(node) {
|
|
5
8
|
const types = node.types;
|
|
6
|
-
const sourceCode = context.getSourceCode();
|
|
7
9
|
const nullType = types.find((typeNode) => typeNode.type === "TSNullKeyword");
|
|
8
10
|
const undefinedType = types.find((typeNode) => typeNode.type === "TSUndefinedKeyword");
|
|
9
11
|
const otherTypes = types.filter((typeNode) => typeNode.type !== "TSNullKeyword" && typeNode.type !== "TSUndefinedKeyword");
|
|
@@ -14,7 +16,9 @@ const rule = {
|
|
|
14
16
|
messageId: "useMaybe",
|
|
15
17
|
data: { type: typeText },
|
|
16
18
|
fix(fixer) {
|
|
17
|
-
|
|
19
|
+
const importFixes = imports.fixes(node, fixer, "Maybe", "type");
|
|
20
|
+
if (importFixes === null) return null;
|
|
21
|
+
return [...importFixes, fixer.replaceText(node, `Maybe<${typeText}>`)];
|
|
18
22
|
}
|
|
19
23
|
});
|
|
20
24
|
}
|
|
@@ -23,12 +27,10 @@ const rule = {
|
|
|
23
27
|
defaultOptions: [],
|
|
24
28
|
meta: {
|
|
25
29
|
type: "suggestion",
|
|
26
|
-
hasSuggestions: true,
|
|
27
30
|
docs: { description: "Use Maybe<T> for T | null" },
|
|
28
31
|
fixable: "code",
|
|
29
32
|
schema: [],
|
|
30
33
|
messages: { useMaybe: "Use Maybe<{{type}}> instead of {{type}} | null" }
|
|
31
34
|
}
|
|
32
35
|
};
|
|
33
|
-
//#endregion
|
|
34
36
|
export { rule as maybeRule };
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
import { createImportPlanner } from "./typebuddy_import.js";
|
|
2
|
+
// rules/nullable_rule.ts
|
|
2
3
|
const rule = {
|
|
3
4
|
create(context) {
|
|
5
|
+
const sourceCode = context.getSourceCode();
|
|
6
|
+
const imports = createImportPlanner(sourceCode);
|
|
4
7
|
return { TSUnionType(node) {
|
|
5
8
|
const types = node.types;
|
|
6
9
|
if (types.length !== 3) return;
|
|
7
|
-
const sourceCode = context.getSourceCode();
|
|
8
10
|
const nullType = types.find((typeNode) => typeNode.type === "TSNullKeyword");
|
|
9
11
|
const undefinedType = types.find((typeNode) => typeNode.type === "TSUndefinedKeyword");
|
|
10
12
|
const otherType = types.find((typeNode) => typeNode.type !== "TSNullKeyword" && typeNode.type !== "TSUndefinedKeyword");
|
|
@@ -15,7 +17,9 @@ const rule = {
|
|
|
15
17
|
messageId: "useNullable",
|
|
16
18
|
data: { type: typeText },
|
|
17
19
|
fix(fixer) {
|
|
18
|
-
|
|
20
|
+
const importFixes = imports.fixes(node, fixer, "Nullable", "type");
|
|
21
|
+
if (importFixes === null) return null;
|
|
22
|
+
return [...importFixes, fixer.replaceText(node, `Nullable<${typeText}>`)];
|
|
19
23
|
}
|
|
20
24
|
});
|
|
21
25
|
}
|
|
@@ -24,12 +28,10 @@ const rule = {
|
|
|
24
28
|
defaultOptions: [],
|
|
25
29
|
meta: {
|
|
26
30
|
type: "suggestion",
|
|
27
|
-
hasSuggestions: true,
|
|
28
31
|
docs: { description: "Use Nullable<T> instead of T | null | undefined" },
|
|
29
32
|
fixable: "code",
|
|
30
33
|
schema: [],
|
|
31
34
|
messages: { useNullable: "Use Nullable<{{type}}> instead of {{type}} | null | undefined" }
|
|
32
35
|
}
|
|
33
36
|
};
|
|
34
|
-
//#endregion
|
|
35
37
|
export { rule as nullableRule };
|
|
@@ -1,18 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
import { createImportPlanner } from "./typebuddy_import.js";
|
|
2
|
+
// rules/optional_rule.ts
|
|
2
3
|
const rule = {
|
|
3
4
|
create(context) {
|
|
5
|
+
const sourceCode = context.getSourceCode();
|
|
6
|
+
const imports = createImportPlanner(sourceCode);
|
|
4
7
|
return { TSUnionType(node) {
|
|
5
8
|
if (node.types.length !== 2) return;
|
|
6
9
|
const undefinedType = node.types.find((typeNode) => typeNode.type === "TSUndefinedKeyword");
|
|
7
10
|
const otherType = node.types.find((typeNode) => typeNode.type !== "TSUndefinedKeyword" && typeNode.type !== "TSNullKeyword");
|
|
8
11
|
if (undefinedType && otherType) {
|
|
9
|
-
const typeText =
|
|
12
|
+
const typeText = sourceCode.getText(otherType);
|
|
10
13
|
context.report({
|
|
11
14
|
node,
|
|
12
15
|
messageId: "preferOptional",
|
|
13
16
|
data: { type: typeText },
|
|
14
17
|
fix(fixer) {
|
|
15
|
-
|
|
18
|
+
const importFixes = imports.fixes(node, fixer, "Optional", "type");
|
|
19
|
+
if (importFixes === null) return null;
|
|
20
|
+
return [...importFixes, fixer.replaceText(node, `Optional<${typeText}>`)];
|
|
16
21
|
}
|
|
17
22
|
});
|
|
18
23
|
}
|
|
@@ -27,5 +32,4 @@ const rule = {
|
|
|
27
32
|
messages: { preferOptional: "Use Optional<{{type}}> instead of {{type}} | undefined" }
|
|
28
33
|
}
|
|
29
34
|
};
|
|
30
|
-
//#endregion
|
|
31
35
|
export { rule as optionalRule };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// rules/own_subtree.ts
|
|
2
2
|
const FUNCTION_TYPES = /* @__PURE__ */ new Set([
|
|
3
3
|
"ArrowFunctionExpression",
|
|
4
4
|
"FunctionDeclaration",
|
|
@@ -41,5 +41,4 @@ function walkOwnSubtree(root, visit) {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
|
-
//#endregion
|
|
45
44
|
export { walkOwnSubtree };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// rules/test_file.ts
|
|
2
|
+
const TEST_DIRECTORIES = /* @__PURE__ */ new Set([
|
|
3
|
+
"__mocks__",
|
|
4
|
+
"__tests__",
|
|
5
|
+
"test",
|
|
6
|
+
"tests"
|
|
7
|
+
]);
|
|
8
|
+
const TEST_FILE_NAME = /\.(?:bench|spec|test)\.[^.]+/;
|
|
9
|
+
const SETUP_FILE_NAME = /^(?:setupTests|vitest-setup|vitest\.setup)\.[^.]+$/;
|
|
10
|
+
const ABSOLUTE_PATH = /^(?:\/|[A-Za-z]:\/)/;
|
|
11
|
+
function toSlashes(path) {
|
|
12
|
+
return path.replaceAll("\\", "/");
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The directory segments of `filename` that count for the directory match: the
|
|
16
|
+
* ones below the project's working directory `cwd`. A relative filename is
|
|
17
|
+
* relative to the project already. An absolute one needs `cwd` to tell the
|
|
18
|
+
* project's own directories from the ones above it (`/tmp/test/app/src/x.ts` is
|
|
19
|
+
* not under a test directory); without `cwd`, or outside it, no directory
|
|
20
|
+
* counts, so the mistake is a test helper that gets linted, never a whole
|
|
21
|
+
* project that goes silent.
|
|
22
|
+
*/
|
|
23
|
+
function projectDirectories(path, cwd) {
|
|
24
|
+
const segments = path.split("/");
|
|
25
|
+
segments.pop();
|
|
26
|
+
if (!ABSOLUTE_PATH.test(path)) return segments;
|
|
27
|
+
if (cwd === void 0) return [];
|
|
28
|
+
const root = toSlashes(cwd).replace(/\/+$/, "");
|
|
29
|
+
if (root === "" || !path.startsWith(`${root}/`)) return [];
|
|
30
|
+
return path.slice(root.length + 1).split("/").slice(0, -1);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Whether `filename` is test code: test, spec and bench files, files under a
|
|
34
|
+
* `__tests__`, `tests`, `test` or `__mocks__` directory of the project rooted
|
|
35
|
+
* at `cwd`, and test setup files. Both `/` and `\` separate path segments.
|
|
36
|
+
*/
|
|
37
|
+
function isTestFile(filename, cwd) {
|
|
38
|
+
const path = toSlashes(filename);
|
|
39
|
+
const baseName = path.slice(path.lastIndexOf("/") + 1);
|
|
40
|
+
return TEST_FILE_NAME.test(baseName) || SETUP_FILE_NAME.test(baseName) || projectDirectories(path, cwd).some((segment) => TEST_DIRECTORIES.has(segment));
|
|
41
|
+
}
|
|
42
|
+
function getFilename(context) {
|
|
43
|
+
if ("filename" in context && typeof context.filename === "string") return context.filename;
|
|
44
|
+
return "";
|
|
45
|
+
}
|
|
46
|
+
function getCwd(context) {
|
|
47
|
+
if ("cwd" in context && typeof context.cwd === "string") return context.cwd;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Make a rule a no-op for test files: its `create` returns no visitors, so test
|
|
51
|
+
* helpers, fixtures and mocks are not asked to follow the typebuddy conventions
|
|
52
|
+
* (`MaybePromise` results, `Maybe`/`Nullable`/`Optional` types).
|
|
53
|
+
*/
|
|
54
|
+
function withoutTestFiles(rule) {
|
|
55
|
+
return {
|
|
56
|
+
...rule,
|
|
57
|
+
create(context) {
|
|
58
|
+
if (isTestFile(getFilename(context), getCwd(context))) return {};
|
|
59
|
+
return rule.create(context);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
export { isTestFile, withoutTestFiles };
|