@murky-web/typebuddy 3.0.0 → 3.1.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 +23 -10
- package/dist/oxlint.js +2 -0
- package/dist/rules/async_rule.js +18 -1
- package/dist/rules/maybe_promise_rule.js +25 -7
- package/dist/rules/maybe_rule.js +5 -2
- package/dist/rules/nullable_rule.js +5 -2
- package/dist/rules/optional_rule.js +5 -2
- package/dist/rules/useless_undefined_rule.js +512 -0
- package/globals.d.ts +7 -7
- package/jsr.json +24 -24
- package/oxlint/index.ts +8 -4
- package/package.json +3 -3
- package/rules/async_rule.ts +47 -20
- package/rules/maybe_promise_rule.ts +60 -40
- package/rules/maybe_rule.ts +17 -13
- package/rules/nullable_rule.ts +16 -16
- package/rules/optional_rule.ts +15 -15
- package/rules/own_subtree.ts +2 -2
- package/rules/test_file.ts +4 -4
- package/rules/typebuddy_import.ts +9 -9
- package/rules/use_server.ts +3 -3
- package/rules/useless_undefined_rule.ts +982 -0
- package/src/collections.ts +1 -1
- package/src/empty.ts +1 -1
- package/src/guards.ts +5 -5
- package/src/parse.ts +4 -4
- package/src/public_api.ts +1 -1
- package/src/result.ts +3 -5
- package/src/types/globals.ts +1 -1
- package/src/types/index.ts +2 -2
package/rules/async_rule.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { throwsDeliberately } from "./deliberate_throw.js";
|
|
2
2
|
import {
|
|
3
3
|
createImportPlanner,
|
|
4
|
-
getBodyStartAfterDirectives
|
|
4
|
+
getBodyStartAfterDirectives
|
|
5
5
|
} from "./typebuddy_import.js";
|
|
6
6
|
import { createUseServerGuard } from "./use_server.js";
|
|
7
7
|
|
|
@@ -27,6 +27,7 @@ type RuleContext = {
|
|
|
27
27
|
report(descriptor: {
|
|
28
28
|
node: unknown;
|
|
29
29
|
messageId: string;
|
|
30
|
+
data: { name: string };
|
|
30
31
|
suggest?: {
|
|
31
32
|
messageId: string;
|
|
32
33
|
fix(fixer: RuleFixer): unknown;
|
|
@@ -40,6 +41,29 @@ function hasTryCatch(nodes: AstNode[]): boolean {
|
|
|
40
41
|
});
|
|
41
42
|
}
|
|
42
43
|
|
|
44
|
+
/**
|
|
45
|
+
* What the message calls the function: its own name, the name it is bound to,
|
|
46
|
+
* or else the first line of its source.
|
|
47
|
+
*/
|
|
48
|
+
function functionLabel(node: AstNode, getText: (node: unknown) => string) {
|
|
49
|
+
const candidates = [node["id"], node.parent?.["id"], node.parent?.["key"]];
|
|
50
|
+
for (const candidate of candidates) {
|
|
51
|
+
if (
|
|
52
|
+
typeof candidate === "object" &&
|
|
53
|
+
candidate !== null &&
|
|
54
|
+
"type" in candidate &&
|
|
55
|
+
candidate.type === "Identifier" &&
|
|
56
|
+
"name" in candidate &&
|
|
57
|
+
typeof candidate.name === "string"
|
|
58
|
+
) {
|
|
59
|
+
return candidate.name;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const [firstLine = ""] = getText(node).split("\n");
|
|
64
|
+
return firstLine.trim();
|
|
65
|
+
}
|
|
66
|
+
|
|
43
67
|
function isCallArgumentCallback(node: AstNode): boolean {
|
|
44
68
|
const parent = node.parent;
|
|
45
69
|
if (!parent) {
|
|
@@ -59,6 +83,7 @@ const rule = {
|
|
|
59
83
|
create(context: RuleContext) {
|
|
60
84
|
const sourceCode = context.getSourceCode();
|
|
61
85
|
const imports = createImportPlanner(sourceCode);
|
|
86
|
+
const getText = (node: unknown) => sourceCode.getText(node);
|
|
62
87
|
|
|
63
88
|
// Two zero-width insertions leave the body's own text untouched, byte
|
|
64
89
|
// for byte. Rebuilding it statement by statement used to strip leading
|
|
@@ -70,7 +95,7 @@ const rule = {
|
|
|
70
95
|
// body; inside a `try` they are plain expression statements.
|
|
71
96
|
function wrapBlockFixes(
|
|
72
97
|
bodyNode: AstNode,
|
|
73
|
-
fixer: RuleFixer
|
|
98
|
+
fixer: RuleFixer
|
|
74
99
|
): unknown[] | null {
|
|
75
100
|
const range = bodyNode.range;
|
|
76
101
|
const tryStart = getBodyStartAfterDirectives(bodyNode);
|
|
@@ -86,8 +111,8 @@ const rule = {
|
|
|
86
111
|
fixer.insertTextBeforeRange([tryStart, tryStart], "\ntry {"),
|
|
87
112
|
fixer.insertTextBeforeRange(
|
|
88
113
|
[range[1] - 1, range[1] - 1],
|
|
89
|
-
"} catch {\nreturn err();\n}\n"
|
|
90
|
-
)
|
|
114
|
+
"} catch {\nreturn err();\n}\n"
|
|
115
|
+
)
|
|
91
116
|
];
|
|
92
117
|
}
|
|
93
118
|
|
|
@@ -103,7 +128,7 @@ const rule = {
|
|
|
103
128
|
function wrapExpressionBodyFixes(
|
|
104
129
|
node: AstNode,
|
|
105
130
|
bodyNode: AstNode,
|
|
106
|
-
fixer: RuleFixer
|
|
131
|
+
fixer: RuleFixer
|
|
107
132
|
): unknown[] | null {
|
|
108
133
|
const arrowRange = node.range;
|
|
109
134
|
const bodyRange = bodyNode.range;
|
|
@@ -133,12 +158,12 @@ const rule = {
|
|
|
133
158
|
return [
|
|
134
159
|
fixer.insertTextBeforeRange(
|
|
135
160
|
[start, start],
|
|
136
|
-
"{\ntry {\nreturn "
|
|
161
|
+
"{\ntry {\nreturn "
|
|
137
162
|
),
|
|
138
163
|
fixer.insertTextBeforeRange(
|
|
139
164
|
[end, end],
|
|
140
|
-
";\n} catch {\nreturn err();\n}\n}"
|
|
141
|
-
)
|
|
165
|
+
";\n} catch {\nreturn err();\n}\n}"
|
|
166
|
+
)
|
|
142
167
|
];
|
|
143
168
|
}
|
|
144
169
|
|
|
@@ -153,7 +178,7 @@ const rule = {
|
|
|
153
178
|
*/
|
|
154
179
|
function suggestWrap(
|
|
155
180
|
scopeNode: AstNode,
|
|
156
|
-
wrap: (fixer: RuleFixer) => unknown[] | null
|
|
181
|
+
wrap: (fixer: RuleFixer) => unknown[] | null
|
|
157
182
|
) {
|
|
158
183
|
return [
|
|
159
184
|
{
|
|
@@ -168,15 +193,15 @@ const rule = {
|
|
|
168
193
|
scopeNode,
|
|
169
194
|
fixer,
|
|
170
195
|
"err",
|
|
171
|
-
"value"
|
|
196
|
+
"value"
|
|
172
197
|
);
|
|
173
198
|
if (importFixes === null) {
|
|
174
199
|
return null;
|
|
175
200
|
}
|
|
176
201
|
|
|
177
202
|
return [...importFixes, ...wrapFixes];
|
|
178
|
-
}
|
|
179
|
-
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
180
205
|
];
|
|
181
206
|
}
|
|
182
207
|
|
|
@@ -201,9 +226,10 @@ const rule = {
|
|
|
201
226
|
context.report({
|
|
202
227
|
node,
|
|
203
228
|
messageId: "missingTryCatch",
|
|
229
|
+
data: { name: functionLabel(node, getText) },
|
|
204
230
|
suggest: suggestWrap(node, (fixer) => {
|
|
205
231
|
return wrapExpressionBodyFixes(node, bodyNode, fixer);
|
|
206
|
-
})
|
|
232
|
+
})
|
|
207
233
|
});
|
|
208
234
|
return;
|
|
209
235
|
}
|
|
@@ -214,9 +240,10 @@ const rule = {
|
|
|
214
240
|
context.report({
|
|
215
241
|
node,
|
|
216
242
|
messageId: "missingTryCatch",
|
|
243
|
+
data: { name: functionLabel(node, getText) },
|
|
217
244
|
suggest: suggestWrap(bodyNode, (fixer) => {
|
|
218
245
|
return wrapBlockFixes(bodyNode, fixer);
|
|
219
|
-
})
|
|
246
|
+
})
|
|
220
247
|
});
|
|
221
248
|
}
|
|
222
249
|
|
|
@@ -226,7 +253,7 @@ const rule = {
|
|
|
226
253
|
Program,
|
|
227
254
|
FunctionDeclaration: guard(checkFunction),
|
|
228
255
|
FunctionExpression: guard(checkFunction),
|
|
229
|
-
ArrowFunctionExpression: guard(checkFunction)
|
|
256
|
+
ArrowFunctionExpression: guard(checkFunction)
|
|
230
257
|
};
|
|
231
258
|
},
|
|
232
259
|
defaultOptions: [],
|
|
@@ -235,16 +262,16 @@ const rule = {
|
|
|
235
262
|
hasSuggestions: true,
|
|
236
263
|
docs: {
|
|
237
264
|
description:
|
|
238
|
-
"Async functions should have a try-catch block returning an error result."
|
|
265
|
+
"Async functions should have a try-catch block returning an error result."
|
|
239
266
|
},
|
|
240
267
|
schema: [],
|
|
241
268
|
messages: {
|
|
242
269
|
missingTryCatch:
|
|
243
|
-
|
|
270
|
+
'`{{name}}` is async without a `try`/`catch`, so a throw inside it rejects the promise and callers that check `isError` never see the failure. Fix: put the body in `try { … } catch { return err(); }` (after a directive such as `"use cache"`) with `import { err } from "@murky-web/typebuddy";` and `return ok(value)` in the `try`; if the failure is a bug rather than an outcome for the caller, `throw` instead, which the rule accepts. Do not wrap only part of the body, and do not return `ok(...)` from the `catch`.',
|
|
244
271
|
suggestTryCatch:
|
|
245
|
-
"Wrap the body in try/catch and return err() from the catch."
|
|
246
|
-
}
|
|
247
|
-
}
|
|
272
|
+
"Wrap the body in try/catch and return err() from the catch."
|
|
273
|
+
}
|
|
274
|
+
}
|
|
248
275
|
};
|
|
249
276
|
|
|
250
277
|
export { rule as requireTryCatchAsyncRule };
|
|
@@ -27,6 +27,7 @@ type RuleContext = {
|
|
|
27
27
|
report(descriptor: {
|
|
28
28
|
node: unknown;
|
|
29
29
|
messageId: string;
|
|
30
|
+
data?: Record<string, string>;
|
|
30
31
|
fix?(fixer: RuleFixer): unknown;
|
|
31
32
|
suggest?: RuleSuggestion[];
|
|
32
33
|
}): void;
|
|
@@ -115,7 +116,7 @@ function hasIsErrorFlag(node: unknown, expected: boolean): boolean {
|
|
|
115
116
|
|
|
116
117
|
function getObjectPropertyValue(
|
|
117
118
|
node: unknown,
|
|
118
|
-
propertyName: string
|
|
119
|
+
propertyName: string
|
|
119
120
|
): AstNode | null {
|
|
120
121
|
if (
|
|
121
122
|
!isNode(node) ||
|
|
@@ -229,7 +230,7 @@ const rule = {
|
|
|
229
230
|
at: AstNode,
|
|
230
231
|
fixer: RuleFixer,
|
|
231
232
|
helperName: "ok" | "err",
|
|
232
|
-
rewrite: unknown
|
|
233
|
+
rewrite: unknown
|
|
233
234
|
): unknown[] | null {
|
|
234
235
|
const importFixes = imports.fixes(at, fixer, helperName, "value");
|
|
235
236
|
if (importFixes === null) return null;
|
|
@@ -245,7 +246,7 @@ const rule = {
|
|
|
245
246
|
at: AstNode,
|
|
246
247
|
messageId: string,
|
|
247
248
|
helperName: "ok" | "err",
|
|
248
|
-
rewrite: (fixer: RuleFixer) => unknown
|
|
249
|
+
rewrite: (fixer: RuleFixer) => unknown
|
|
249
250
|
): RuleSuggestion[] {
|
|
250
251
|
return [
|
|
251
252
|
{
|
|
@@ -255,10 +256,10 @@ const rule = {
|
|
|
255
256
|
at,
|
|
256
257
|
fixer,
|
|
257
258
|
helperName,
|
|
258
|
-
rewrite(fixer)
|
|
259
|
+
rewrite(fixer)
|
|
259
260
|
);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
262
263
|
];
|
|
263
264
|
}
|
|
264
265
|
|
|
@@ -279,18 +280,23 @@ const rule = {
|
|
|
279
280
|
if (getTypeName(typeAnnotation) !== "Promise") return;
|
|
280
281
|
|
|
281
282
|
const typeName = typeAnnotation["typeName"];
|
|
283
|
+
const awaited = getTypeArgument(typeAnnotation);
|
|
284
|
+
const data = {
|
|
285
|
+
promise: sourceCode.getText(typeAnnotation),
|
|
286
|
+
type: awaited ? sourceCode.getText(awaited) : "T"
|
|
287
|
+
};
|
|
282
288
|
const rewrite = (fixer: RuleFixer) => {
|
|
283
289
|
const typeImport = imports.fixes(
|
|
284
290
|
typeName,
|
|
285
291
|
fixer,
|
|
286
292
|
"MaybePromise",
|
|
287
|
-
"type"
|
|
293
|
+
"type"
|
|
288
294
|
);
|
|
289
295
|
if (typeImport === null) return null;
|
|
290
296
|
|
|
291
297
|
return [
|
|
292
298
|
...typeImport,
|
|
293
|
-
fixer.replaceText(typeName, "MaybePromise")
|
|
299
|
+
fixer.replaceText(typeName, "MaybePromise")
|
|
294
300
|
];
|
|
295
301
|
};
|
|
296
302
|
|
|
@@ -303,9 +309,10 @@ const rule = {
|
|
|
303
309
|
context.report({
|
|
304
310
|
node: typeName,
|
|
305
311
|
messageId: "replaceWithMaybePromise",
|
|
312
|
+
data,
|
|
306
313
|
fix(fixer) {
|
|
307
314
|
return rewrite(fixer);
|
|
308
|
-
}
|
|
315
|
+
}
|
|
309
316
|
});
|
|
310
317
|
return;
|
|
311
318
|
}
|
|
@@ -313,14 +320,15 @@ const rule = {
|
|
|
313
320
|
context.report({
|
|
314
321
|
node: typeName,
|
|
315
322
|
messageId: "replaceWithMaybePromise",
|
|
323
|
+
data,
|
|
316
324
|
suggest: [
|
|
317
325
|
{
|
|
318
326
|
messageId: "suggestMaybePromise",
|
|
319
327
|
fix(fixer) {
|
|
320
328
|
return rewrite(fixer);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
]
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
]
|
|
324
332
|
});
|
|
325
333
|
}
|
|
326
334
|
|
|
@@ -359,7 +367,7 @@ const rule = {
|
|
|
359
367
|
function wrapReturnValue(
|
|
360
368
|
node: AstNode,
|
|
361
369
|
isAsync: boolean,
|
|
362
|
-
returnType: AstNode | null
|
|
370
|
+
returnType: AstNode | null
|
|
363
371
|
) {
|
|
364
372
|
if (!isAsync) return;
|
|
365
373
|
|
|
@@ -369,14 +377,15 @@ const rule = {
|
|
|
369
377
|
context.report({
|
|
370
378
|
node,
|
|
371
379
|
messageId: "returnVoidPromise",
|
|
380
|
+
data: { code: sourceCode.getText(node) },
|
|
372
381
|
fix(fixer) {
|
|
373
382
|
return withHelperFixes(
|
|
374
383
|
node,
|
|
375
384
|
fixer,
|
|
376
385
|
"ok",
|
|
377
|
-
fixer.replaceText(node, "return ok();")
|
|
386
|
+
fixer.replaceText(node, "return ok();")
|
|
378
387
|
);
|
|
379
|
-
}
|
|
388
|
+
}
|
|
380
389
|
});
|
|
381
390
|
}
|
|
382
391
|
return;
|
|
@@ -411,15 +420,19 @@ const rule = {
|
|
|
411
420
|
context.report({
|
|
412
421
|
node: argument,
|
|
413
422
|
messageId: "preferOkResult",
|
|
423
|
+
data: {
|
|
424
|
+
object: sourceCode.getText(argument),
|
|
425
|
+
value: sourceCode.getText(objectValue)
|
|
426
|
+
},
|
|
414
427
|
fix(fixer) {
|
|
415
428
|
const valueText = sourceCode.getText(objectValue);
|
|
416
429
|
return withHelperFixes(
|
|
417
430
|
node,
|
|
418
431
|
fixer,
|
|
419
432
|
"ok",
|
|
420
|
-
fixer.replaceText(argument, `ok(${valueText})`)
|
|
433
|
+
fixer.replaceText(argument, `ok(${valueText})`)
|
|
421
434
|
);
|
|
422
|
-
}
|
|
435
|
+
}
|
|
423
436
|
});
|
|
424
437
|
return;
|
|
425
438
|
}
|
|
@@ -432,14 +445,15 @@ const rule = {
|
|
|
432
445
|
context.report({
|
|
433
446
|
node: argument,
|
|
434
447
|
messageId: "preferErrResult",
|
|
448
|
+
data: { object: sourceCode.getText(argument) },
|
|
435
449
|
fix(fixer) {
|
|
436
450
|
return withHelperFixes(
|
|
437
451
|
node,
|
|
438
452
|
fixer,
|
|
439
453
|
"err",
|
|
440
|
-
fixer.replaceText(argument, "err()")
|
|
454
|
+
fixer.replaceText(argument, "err()")
|
|
441
455
|
);
|
|
442
|
-
}
|
|
456
|
+
}
|
|
443
457
|
});
|
|
444
458
|
return;
|
|
445
459
|
}
|
|
@@ -456,6 +470,7 @@ const rule = {
|
|
|
456
470
|
context.report({
|
|
457
471
|
node: argument,
|
|
458
472
|
messageId: "wrapReturn",
|
|
473
|
+
data: { argument: sourceCode.getText(argument) },
|
|
459
474
|
suggest: suggestWithHelper(
|
|
460
475
|
node,
|
|
461
476
|
"suggestWrapReturn",
|
|
@@ -464,10 +479,10 @@ const rule = {
|
|
|
464
479
|
const argumentText = sourceCode.getText(argument);
|
|
465
480
|
return fixer.replaceText(
|
|
466
481
|
argument,
|
|
467
|
-
`ok(${argumentText})
|
|
482
|
+
`ok(${argumentText})`
|
|
468
483
|
);
|
|
469
|
-
}
|
|
470
|
-
)
|
|
484
|
+
}
|
|
485
|
+
)
|
|
471
486
|
});
|
|
472
487
|
}
|
|
473
488
|
}
|
|
@@ -521,14 +536,15 @@ const rule = {
|
|
|
521
536
|
context.report({
|
|
522
537
|
node: statement,
|
|
523
538
|
messageId: "returnFailedPromise",
|
|
539
|
+
data: { found: `\`${sourceCode.getText(statement)}\`` },
|
|
524
540
|
fix(fixer) {
|
|
525
541
|
return withHelperFixes(
|
|
526
542
|
statement,
|
|
527
543
|
fixer,
|
|
528
544
|
"err",
|
|
529
|
-
fixer.replaceText(statement, "return err();")
|
|
545
|
+
fixer.replaceText(statement, "return err();")
|
|
530
546
|
);
|
|
531
|
-
}
|
|
547
|
+
}
|
|
532
548
|
});
|
|
533
549
|
return;
|
|
534
550
|
}
|
|
@@ -543,14 +559,15 @@ const rule = {
|
|
|
543
559
|
context.report({
|
|
544
560
|
node: argument,
|
|
545
561
|
messageId: "preferErrResult",
|
|
562
|
+
data: { object: sourceCode.getText(argument) },
|
|
546
563
|
fix(fixer) {
|
|
547
564
|
return withHelperFixes(
|
|
548
565
|
statement,
|
|
549
566
|
fixer,
|
|
550
567
|
"err",
|
|
551
|
-
fixer.replaceText(argument, "err()")
|
|
568
|
+
fixer.replaceText(argument, "err()")
|
|
552
569
|
);
|
|
553
|
-
}
|
|
570
|
+
}
|
|
554
571
|
});
|
|
555
572
|
}
|
|
556
573
|
return;
|
|
@@ -559,6 +576,7 @@ const rule = {
|
|
|
559
576
|
context.report({
|
|
560
577
|
node: statement,
|
|
561
578
|
messageId: "returnFailedPromise",
|
|
579
|
+
data: { found: `\`${sourceCode.getText(statement)}\`` }
|
|
562
580
|
});
|
|
563
581
|
}
|
|
564
582
|
|
|
@@ -639,6 +657,7 @@ const rule = {
|
|
|
639
657
|
context.report({
|
|
640
658
|
node: handler,
|
|
641
659
|
messageId: "returnFailedPromise",
|
|
660
|
+
data: { found: "nothing (it has no `return`)" },
|
|
642
661
|
fix(fixer) {
|
|
643
662
|
const handlerBody = handler["body"];
|
|
644
663
|
if (
|
|
@@ -658,15 +677,15 @@ const rule = {
|
|
|
658
677
|
fixer.insertTextBeforeRange(
|
|
659
678
|
[
|
|
660
679
|
handlerBody["range"][1] - 1,
|
|
661
|
-
handlerBody["range"][1] - 1
|
|
680
|
+
handlerBody["range"][1] - 1
|
|
662
681
|
],
|
|
663
682
|
// The newline matters: `catch { log() }` would
|
|
664
683
|
// otherwise become `catch { log() return err(); }`,
|
|
665
684
|
// which does not parse.
|
|
666
|
-
"\nreturn err();\n"
|
|
667
|
-
)
|
|
685
|
+
"\nreturn err();\n"
|
|
686
|
+
)
|
|
668
687
|
);
|
|
669
|
-
}
|
|
688
|
+
}
|
|
670
689
|
});
|
|
671
690
|
}
|
|
672
691
|
|
|
@@ -680,7 +699,7 @@ const rule = {
|
|
|
680
699
|
TSDeclareFunction: guard(checkSignatureReturnType),
|
|
681
700
|
TSFunctionType: guard(checkSignatureReturnType),
|
|
682
701
|
TSMethodSignature: guard(checkSignatureReturnType),
|
|
683
|
-
TryStatement: guard(processTryCatch)
|
|
702
|
+
TryStatement: guard(processTryCatch)
|
|
684
703
|
};
|
|
685
704
|
},
|
|
686
705
|
defaultOptions: [],
|
|
@@ -688,27 +707,28 @@ const rule = {
|
|
|
688
707
|
type: "suggestion",
|
|
689
708
|
docs: {
|
|
690
709
|
description:
|
|
691
|
-
"Ensure async functions' return values follow the MaybePromise pattern."
|
|
710
|
+
"Ensure async functions' return values follow the MaybePromise pattern."
|
|
692
711
|
},
|
|
693
712
|
fixable: "code",
|
|
694
713
|
hasSuggestions: true,
|
|
695
714
|
schema: [],
|
|
696
715
|
messages: {
|
|
697
716
|
replaceWithMaybePromise:
|
|
698
|
-
|
|
717
|
+
'`{{promise}}` makes a failure a rejection the caller has to catch; typebuddy\'s async functions resolve to a result instead, which the caller checks with `isError`. Fix: `MaybePromise<{{type}}>` with `import type { MaybePromise } from "@murky-web/typebuddy";`, then `return ok(value)` from the `try` and `return err()` from the `catch`. Do not cast the returns with `as` to make them fit.',
|
|
699
718
|
suggestMaybePromise: "Replace 'Promise' with 'MaybePromise'.",
|
|
700
|
-
wrapReturn:
|
|
719
|
+
wrapReturn:
|
|
720
|
+
"`return {{argument}}` in the `try` of an async function resolves to a bare value, and a caller reading `.isError` gets `undefined`. Fix: `return ok({{argument}})`; if `{{argument}}` already is a `Result`, unwrap it: `const settled = {{argument}}; if (settled.isError) return err(); return ok(settled.value);`. Do not cast it with `as`.",
|
|
701
721
|
suggestWrapReturn: "Wrap the return value in ok(...).",
|
|
702
722
|
preferOkResult:
|
|
703
|
-
|
|
723
|
+
'`{{object}}` builds the success result by hand; without a return type its `isError` widens to `boolean` and `isSuccess` stops narrowing it. Fix: `ok({{value}})` with `import { ok } from "@murky-web/typebuddy";` (`--fix` applies it).',
|
|
704
724
|
preferErrResult:
|
|
705
|
-
|
|
725
|
+
'`{{object}}` builds the failed result by hand; without a return type its `isError` widens to `boolean` and `isSuccess` stops narrowing it. Fix: `err()` with `import { err } from "@murky-web/typebuddy";` (`--fix` applies it).',
|
|
706
726
|
returnVoidPromise:
|
|
707
|
-
|
|
727
|
+
'`{{code}}` in the `try` of an async `void` function resolves to `undefined` instead of a result, so a caller reading `.isError` throws a `TypeError`. Fix: `return ok();` with `import { ok } from "@murky-web/typebuddy";` (`--fix` applies it).',
|
|
708
728
|
returnFailedPromise:
|
|
709
|
-
|
|
710
|
-
}
|
|
711
|
-
}
|
|
729
|
+
'The `catch` of this async function settles on {{found}} instead of `err()`, so the caller gets no `Failed` result and `isError` misses the failure. Fix: end the `catch` with `return err();` and `import { err } from "@murky-web/typebuddy";`, renaming a `catch (err)` parameter first; a fallback value belongs in the `try` as `return ok(fallback)`. Do not return `ok(...)` from the `catch`.'
|
|
730
|
+
}
|
|
731
|
+
}
|
|
712
732
|
};
|
|
713
733
|
|
|
714
734
|
export { rule as errorSafeAsyncRule };
|
package/rules/maybe_rule.ts
CHANGED
|
@@ -14,7 +14,7 @@ const rule = {
|
|
|
14
14
|
report(descriptor: {
|
|
15
15
|
node: unknown;
|
|
16
16
|
messageId: string;
|
|
17
|
-
data: { type: string };
|
|
17
|
+
data: { type: string; union: string };
|
|
18
18
|
fix(fixer: RuleFixer): unknown;
|
|
19
19
|
}): void;
|
|
20
20
|
}) {
|
|
@@ -26,15 +26,15 @@ const rule = {
|
|
|
26
26
|
const types = node.types;
|
|
27
27
|
|
|
28
28
|
const nullType = types.find(
|
|
29
|
-
(typeNode) => typeNode.type === "TSNullKeyword"
|
|
29
|
+
(typeNode) => typeNode.type === "TSNullKeyword"
|
|
30
30
|
);
|
|
31
31
|
const undefinedType = types.find(
|
|
32
|
-
(typeNode) => typeNode.type === "TSUndefinedKeyword"
|
|
32
|
+
(typeNode) => typeNode.type === "TSUndefinedKeyword"
|
|
33
33
|
);
|
|
34
34
|
const otherTypes = types.filter(
|
|
35
35
|
(typeNode) =>
|
|
36
36
|
typeNode.type !== "TSNullKeyword" &&
|
|
37
|
-
typeNode.type !== "TSUndefinedKeyword"
|
|
37
|
+
typeNode.type !== "TSUndefinedKeyword"
|
|
38
38
|
);
|
|
39
39
|
|
|
40
40
|
if (nullType && !undefinedType && otherTypes.length === 1) {
|
|
@@ -42,7 +42,10 @@ const rule = {
|
|
|
42
42
|
context.report({
|
|
43
43
|
node,
|
|
44
44
|
messageId: "useMaybe",
|
|
45
|
-
data: {
|
|
45
|
+
data: {
|
|
46
|
+
type: typeText,
|
|
47
|
+
union: sourceCode.getText(node)
|
|
48
|
+
},
|
|
46
49
|
fix(fixer) {
|
|
47
50
|
// `Maybe` has to be the package's type where the
|
|
48
51
|
// fix puts it: imported, or already imported.
|
|
@@ -53,32 +56,33 @@ const rule = {
|
|
|
53
56
|
node,
|
|
54
57
|
fixer,
|
|
55
58
|
"Maybe",
|
|
56
|
-
"type"
|
|
59
|
+
"type"
|
|
57
60
|
);
|
|
58
61
|
if (importFixes === null) return null;
|
|
59
62
|
|
|
60
63
|
return [
|
|
61
64
|
...importFixes,
|
|
62
|
-
fixer.replaceText(node, `Maybe<${typeText}>`)
|
|
65
|
+
fixer.replaceText(node, `Maybe<${typeText}>`)
|
|
63
66
|
];
|
|
64
|
-
}
|
|
67
|
+
}
|
|
65
68
|
});
|
|
66
69
|
}
|
|
67
|
-
}
|
|
70
|
+
}
|
|
68
71
|
};
|
|
69
72
|
},
|
|
70
73
|
defaultOptions: [],
|
|
71
74
|
meta: {
|
|
72
75
|
type: "suggestion",
|
|
73
76
|
docs: {
|
|
74
|
-
description: "Use Maybe<T> for T | null"
|
|
77
|
+
description: "Use Maybe<T> for T | null"
|
|
75
78
|
},
|
|
76
79
|
fixable: "code",
|
|
77
80
|
schema: [],
|
|
78
81
|
messages: {
|
|
79
|
-
useMaybe:
|
|
80
|
-
|
|
81
|
-
|
|
82
|
+
useMaybe:
|
|
83
|
+
'`{{union}}` is the type typebuddy names `Maybe<{{type}}>`. Both mean the same, and one spelling keeps `T | null` recognisable as the thing `isMaybe` narrows. Fix: `Maybe<{{type}}>` with `import type { Maybe } from "@murky-web/typebuddy";` (`--fix` applies it where `Maybe` is not already another binding).'
|
|
84
|
+
}
|
|
85
|
+
}
|
|
82
86
|
};
|
|
83
87
|
|
|
84
88
|
export { rule as maybeRule };
|
package/rules/nullable_rule.ts
CHANGED
|
@@ -14,7 +14,7 @@ const rule = {
|
|
|
14
14
|
report(descriptor: {
|
|
15
15
|
node: unknown;
|
|
16
16
|
messageId: string;
|
|
17
|
-
data: { type: string };
|
|
17
|
+
data: { type: string; union: string };
|
|
18
18
|
fix(fixer: RuleFixer): unknown;
|
|
19
19
|
}): void;
|
|
20
20
|
}) {
|
|
@@ -27,15 +27,15 @@ const rule = {
|
|
|
27
27
|
if (types.length !== 3) return;
|
|
28
28
|
|
|
29
29
|
const nullType = types.find(
|
|
30
|
-
(typeNode) => typeNode.type === "TSNullKeyword"
|
|
30
|
+
(typeNode) => typeNode.type === "TSNullKeyword"
|
|
31
31
|
);
|
|
32
32
|
const undefinedType = types.find(
|
|
33
|
-
(typeNode) => typeNode.type === "TSUndefinedKeyword"
|
|
33
|
+
(typeNode) => typeNode.type === "TSUndefinedKeyword"
|
|
34
34
|
);
|
|
35
35
|
const otherType = types.find(
|
|
36
36
|
(typeNode) =>
|
|
37
37
|
typeNode.type !== "TSNullKeyword" &&
|
|
38
|
-
typeNode.type !== "TSUndefinedKeyword"
|
|
38
|
+
typeNode.type !== "TSUndefinedKeyword"
|
|
39
39
|
);
|
|
40
40
|
|
|
41
41
|
if (nullType && undefinedType && otherType) {
|
|
@@ -43,7 +43,10 @@ const rule = {
|
|
|
43
43
|
context.report({
|
|
44
44
|
node,
|
|
45
45
|
messageId: "useNullable",
|
|
46
|
-
data: {
|
|
46
|
+
data: {
|
|
47
|
+
type: typeText,
|
|
48
|
+
union: sourceCode.getText(node)
|
|
49
|
+
},
|
|
47
50
|
fix(fixer) {
|
|
48
51
|
// See `prefer-maybe`: the fix only uses the
|
|
49
52
|
// package's own `Nullable`, imported where it is
|
|
@@ -52,36 +55,33 @@ const rule = {
|
|
|
52
55
|
node,
|
|
53
56
|
fixer,
|
|
54
57
|
"Nullable",
|
|
55
|
-
"type"
|
|
58
|
+
"type"
|
|
56
59
|
);
|
|
57
60
|
if (importFixes === null) return null;
|
|
58
61
|
|
|
59
62
|
return [
|
|
60
63
|
...importFixes,
|
|
61
|
-
fixer.replaceText(
|
|
62
|
-
node,
|
|
63
|
-
`Nullable<${typeText}>`,
|
|
64
|
-
),
|
|
64
|
+
fixer.replaceText(node, `Nullable<${typeText}>`)
|
|
65
65
|
];
|
|
66
|
-
}
|
|
66
|
+
}
|
|
67
67
|
});
|
|
68
68
|
}
|
|
69
|
-
}
|
|
69
|
+
}
|
|
70
70
|
};
|
|
71
71
|
},
|
|
72
72
|
defaultOptions: [],
|
|
73
73
|
meta: {
|
|
74
74
|
type: "suggestion",
|
|
75
75
|
docs: {
|
|
76
|
-
description: "Use Nullable<T> instead of T | null | undefined"
|
|
76
|
+
description: "Use Nullable<T> instead of T | null | undefined"
|
|
77
77
|
},
|
|
78
78
|
fixable: "code",
|
|
79
79
|
schema: [],
|
|
80
80
|
messages: {
|
|
81
81
|
useNullable:
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
}
|
|
82
|
+
'`{{union}}` is the type typebuddy names `Nullable<{{type}}>`. Both mean the same, and one spelling keeps `T | null | undefined` recognisable as the thing `isNullable` narrows. Fix: `Nullable<{{type}}>` with `import type { Nullable } from "@murky-web/typebuddy";` (`--fix` applies it where `Nullable` is not already another binding).'
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
85
|
};
|
|
86
86
|
|
|
87
87
|
export { rule as nullableRule };
|