@murky-web/typebuddy 1.1.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/dist/collections.js +28 -0
- package/dist/empty.js +53 -0
- package/dist/guards.js +132 -0
- package/dist/ids.js +29 -0
- package/dist/index.js +6 -1
- package/dist/oxlint.js +5 -5
- package/dist/parse.js +81 -0
- package/dist/result.js +38 -0
- package/dist/rules/async_rule.js +13 -10
- package/dist/rules/deliberate_throw.js +49 -0
- package/dist/rules/maybe_promise_rule.js +48 -45
- package/dist/rules/maybe_rule.js +1 -1
- package/dist/rules/nullable_rule.js +1 -1
- package/dist/rules/optional_rule.js +1 -1
- package/jsr.json +1 -1
- package/package.json +39 -9
- package/rules/async_rule.ts +17 -10
- package/rules/deliberate_throw.ts +73 -0
- package/rules/maybe_promise_rule.ts +67 -61
- package/src/collections.ts +36 -0
- package/src/empty.ts +76 -0
- package/src/guards.ts +341 -0
- package/src/ids.ts +34 -0
- package/src/index.ts +11 -0
- package/src/parse.ts +215 -0
- package/src/result.ts +57 -0
- package/src/type_helper.ts +10 -666
- package/src/types/index.ts +9 -1
- package/src/types/json.ts +1 -1
- package/src/types/maybe_promise.ts +24 -2
- package/dist/src/type_helper.js +0 -309
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { throwsDeliberately } from "./deliberate_throw.js";
|
|
2
|
+
|
|
1
3
|
type AstNode = {
|
|
2
4
|
type: string;
|
|
3
5
|
parent?: AstNode;
|
|
@@ -29,11 +31,11 @@ function isNode(value: unknown): value is AstNode {
|
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
function isAsyncFunction(node: AstNode): boolean {
|
|
32
|
-
return node
|
|
34
|
+
return node["async"] === true;
|
|
33
35
|
}
|
|
34
36
|
|
|
35
37
|
function hasTypeParameters(value: unknown): value is { params: unknown[] } {
|
|
36
|
-
return isNode(value) && Array.isArray(value
|
|
38
|
+
return isNode(value) && Array.isArray(value["params"]);
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
function isCallArgumentCallback(node: AstNode): boolean {
|
|
@@ -46,11 +48,11 @@ function isCallArgumentCallback(node: AstNode): boolean {
|
|
|
46
48
|
return false;
|
|
47
49
|
}
|
|
48
50
|
|
|
49
|
-
return Array.isArray(parent
|
|
51
|
+
return Array.isArray(parent["arguments"]) && parent["arguments"].includes(node);
|
|
50
52
|
}
|
|
51
53
|
|
|
52
54
|
function isIdentifierNamed(node: unknown, name: string): boolean {
|
|
53
|
-
return isNode(node) && node.type === "Identifier" && node
|
|
55
|
+
return isNode(node) && node.type === "Identifier" && node["name"] === name;
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
function getProgram(node: AstNode): AstNode | null {
|
|
@@ -74,27 +76,27 @@ function getStringLiteralValue(node: unknown): string | null {
|
|
|
74
76
|
|
|
75
77
|
if (
|
|
76
78
|
(node.type === "Literal" || node.type === "StringLiteral") &&
|
|
77
|
-
typeof node
|
|
79
|
+
typeof node["value"] === "string"
|
|
78
80
|
) {
|
|
79
|
-
return node
|
|
81
|
+
return node["value"];
|
|
80
82
|
}
|
|
81
83
|
|
|
82
84
|
return null;
|
|
83
85
|
}
|
|
84
86
|
|
|
85
87
|
function getProgramBody(node: AstNode): AstNode[] {
|
|
86
|
-
if (!Array.isArray(node
|
|
88
|
+
if (!Array.isArray(node["body"])) {
|
|
87
89
|
return [];
|
|
88
90
|
}
|
|
89
91
|
|
|
90
|
-
return node
|
|
92
|
+
return node["body"].filter(isNode);
|
|
91
93
|
}
|
|
92
94
|
|
|
93
95
|
function isResultHelperCall(node: unknown, name: "ok" | "err"): boolean {
|
|
94
96
|
return (
|
|
95
97
|
isNode(node) &&
|
|
96
98
|
node.type === "CallExpression" &&
|
|
97
|
-
isIdentifierNamed(node
|
|
99
|
+
isIdentifierNamed(node["callee"], name)
|
|
98
100
|
);
|
|
99
101
|
}
|
|
100
102
|
|
|
@@ -102,19 +104,19 @@ function hasIsErrorFlag(node: unknown, expected: boolean): boolean {
|
|
|
102
104
|
if (
|
|
103
105
|
!isNode(node) ||
|
|
104
106
|
node.type !== "ObjectExpression" ||
|
|
105
|
-
!Array.isArray(node
|
|
107
|
+
!Array.isArray(node["properties"])
|
|
106
108
|
) {
|
|
107
109
|
return false;
|
|
108
110
|
}
|
|
109
111
|
|
|
110
|
-
return node
|
|
112
|
+
return node["properties"].some((property) => {
|
|
111
113
|
return (
|
|
112
114
|
isNode(property) &&
|
|
113
115
|
property.type === "Property" &&
|
|
114
|
-
isIdentifierNamed(property
|
|
115
|
-
isNode(property
|
|
116
|
-
property
|
|
117
|
-
property
|
|
116
|
+
isIdentifierNamed(property["key"], "isError") &&
|
|
117
|
+
isNode(property["value"]) &&
|
|
118
|
+
property["value"].type === "Literal" &&
|
|
119
|
+
property["value"]["value"] === expected
|
|
118
120
|
);
|
|
119
121
|
});
|
|
120
122
|
}
|
|
@@ -126,19 +128,19 @@ function getObjectPropertyValue(
|
|
|
126
128
|
if (
|
|
127
129
|
!isNode(node) ||
|
|
128
130
|
node.type !== "ObjectExpression" ||
|
|
129
|
-
!Array.isArray(node
|
|
131
|
+
!Array.isArray(node["properties"])
|
|
130
132
|
) {
|
|
131
133
|
return null;
|
|
132
134
|
}
|
|
133
135
|
|
|
134
|
-
for (const property of node
|
|
136
|
+
for (const property of node["properties"]) {
|
|
135
137
|
if (
|
|
136
138
|
isNode(property) &&
|
|
137
139
|
property.type === "Property" &&
|
|
138
|
-
isIdentifierNamed(property
|
|
139
|
-
isNode(property
|
|
140
|
+
isIdentifierNamed(property["key"], propertyName) &&
|
|
141
|
+
isNode(property["value"])
|
|
140
142
|
) {
|
|
141
|
-
return property
|
|
143
|
+
return property["value"];
|
|
142
144
|
}
|
|
143
145
|
}
|
|
144
146
|
|
|
@@ -146,11 +148,11 @@ function getObjectPropertyValue(
|
|
|
146
148
|
}
|
|
147
149
|
|
|
148
150
|
function isBooleanLiteral(node: unknown, expected: boolean): boolean {
|
|
149
|
-
return isNode(node) && node.type === "Literal" && node
|
|
151
|
+
return isNode(node) && node.type === "Literal" && node["value"] === expected;
|
|
150
152
|
}
|
|
151
153
|
|
|
152
154
|
function isNullLiteral(node: unknown): boolean {
|
|
153
|
-
return isNode(node) && node.type === "Literal" && node
|
|
155
|
+
return isNode(node) && node.type === "Literal" && node["value"] === null;
|
|
154
156
|
}
|
|
155
157
|
|
|
156
158
|
function hasTypeBuddyHelperImport(program: AstNode, helperName: "ok" | "err") {
|
|
@@ -160,24 +162,24 @@ function hasTypeBuddyHelperImport(program: AstNode, helperName: "ok" | "err") {
|
|
|
160
162
|
}
|
|
161
163
|
|
|
162
164
|
if (
|
|
163
|
-
getStringLiteralValue(statement
|
|
165
|
+
getStringLiteralValue(statement["source"]) !== "@murky-web/typebuddy"
|
|
164
166
|
) {
|
|
165
167
|
return false;
|
|
166
168
|
}
|
|
167
169
|
|
|
168
|
-
if (statement
|
|
170
|
+
if (statement["importKind"] === "type") {
|
|
169
171
|
return false;
|
|
170
172
|
}
|
|
171
173
|
|
|
172
|
-
const specifiers = Array.isArray(statement
|
|
173
|
-
? statement
|
|
174
|
+
const specifiers = Array.isArray(statement["specifiers"])
|
|
175
|
+
? statement["specifiers"]
|
|
174
176
|
: [];
|
|
175
177
|
|
|
176
178
|
return specifiers.some((specifier) => {
|
|
177
179
|
return (
|
|
178
180
|
isNode(specifier) &&
|
|
179
181
|
specifier.type === "ImportSpecifier" &&
|
|
180
|
-
isIdentifierNamed(specifier
|
|
182
|
+
isIdentifierNamed(specifier["local"], helperName)
|
|
181
183
|
);
|
|
182
184
|
});
|
|
183
185
|
});
|
|
@@ -190,15 +192,15 @@ function getTypeBuddyImportInsertRange(
|
|
|
190
192
|
const imports = body.filter(isImportDeclaration);
|
|
191
193
|
const anchor = imports.at(-1) ?? body[0] ?? program;
|
|
192
194
|
|
|
193
|
-
if (!Array.isArray(anchor
|
|
195
|
+
if (!Array.isArray(anchor["range"]) || anchor["range"].length < 2) {
|
|
194
196
|
return null;
|
|
195
197
|
}
|
|
196
198
|
|
|
197
199
|
if (imports.length > 0) {
|
|
198
|
-
return [anchor
|
|
200
|
+
return [anchor["range"][1], anchor["range"][1]];
|
|
199
201
|
}
|
|
200
202
|
|
|
201
|
-
return [anchor
|
|
203
|
+
return [anchor["range"][0], anchor["range"][0]];
|
|
202
204
|
}
|
|
203
205
|
|
|
204
206
|
const rule = {
|
|
@@ -211,14 +213,14 @@ const rule = {
|
|
|
211
213
|
}
|
|
212
214
|
|
|
213
215
|
function getTypeName(node: AstNode): string | null {
|
|
214
|
-
const typeName = node
|
|
216
|
+
const typeName = node["typeName"];
|
|
215
217
|
if (!isNode(typeName) || typeName.type !== "Identifier")
|
|
216
218
|
return null;
|
|
217
|
-
return typeof typeName
|
|
219
|
+
return typeof typeName["name"] === "string" ? typeName["name"] : null;
|
|
218
220
|
}
|
|
219
221
|
|
|
220
222
|
function getTypeArgument(node: AstNode): AstNode | null {
|
|
221
|
-
const typeArguments = node
|
|
223
|
+
const typeArguments = node["typeArguments"];
|
|
222
224
|
if (!hasTypeParameters(typeArguments)) return null;
|
|
223
225
|
const [firstParam] = typeArguments.params;
|
|
224
226
|
if (!isNode(firstParam)) return null;
|
|
@@ -262,13 +264,16 @@ const rule = {
|
|
|
262
264
|
function checkReturnType(node: AstNode) {
|
|
263
265
|
if (!isAsyncFunction(node)) return;
|
|
264
266
|
if (isCallArgumentCallback(node)) return;
|
|
265
|
-
|
|
267
|
+
// Same exemption as `require-try-catch`: a deliberate throw is not a
|
|
268
|
+
// result waiting to be wrapped.
|
|
269
|
+
if (throwsDeliberately(node["body"])) return;
|
|
270
|
+
if (!isNode(node["returnType"])) return;
|
|
266
271
|
|
|
267
|
-
const typeAnnotation = node
|
|
272
|
+
const typeAnnotation = node["returnType"]["typeAnnotation"];
|
|
268
273
|
if (!isTypeReference(typeAnnotation)) return;
|
|
269
274
|
if (getTypeName(typeAnnotation) !== "Promise") return;
|
|
270
275
|
|
|
271
|
-
const typeName = typeAnnotation
|
|
276
|
+
const typeName = typeAnnotation["typeName"];
|
|
272
277
|
context.report({
|
|
273
278
|
node: typeName,
|
|
274
279
|
messageId: "replaceWithMaybePromise",
|
|
@@ -285,7 +290,7 @@ const rule = {
|
|
|
285
290
|
) {
|
|
286
291
|
if (!isAsync) return;
|
|
287
292
|
|
|
288
|
-
const argument = node
|
|
293
|
+
const argument = node["argument"];
|
|
289
294
|
if (!isNode(argument)) {
|
|
290
295
|
if (returnType?.type === "TSVoidKeyword") {
|
|
291
296
|
context.report({
|
|
@@ -308,8 +313,8 @@ const rule = {
|
|
|
308
313
|
|
|
309
314
|
if (
|
|
310
315
|
argument.type === "Identifier" &&
|
|
311
|
-
(argument
|
|
312
|
-
argument
|
|
316
|
+
(argument["name"] === "VOID_PROMISE" ||
|
|
317
|
+
argument["name"] === "FAILED_PROMISE")
|
|
313
318
|
) {
|
|
314
319
|
return;
|
|
315
320
|
}
|
|
@@ -389,7 +394,7 @@ const rule = {
|
|
|
389
394
|
parent.type === "FunctionExpression" ||
|
|
390
395
|
parent.type === "ArrowFunctionExpression"
|
|
391
396
|
) {
|
|
392
|
-
isAsync = parent
|
|
397
|
+
isAsync = parent["async"] === true;
|
|
393
398
|
parentFunction = parent;
|
|
394
399
|
break;
|
|
395
400
|
}
|
|
@@ -398,18 +403,19 @@ const rule = {
|
|
|
398
403
|
|
|
399
404
|
if (!isAsync || !parentFunction) return;
|
|
400
405
|
if (isCallArgumentCallback(parentFunction)) return;
|
|
406
|
+
if (throwsDeliberately(parentFunction["body"])) return;
|
|
401
407
|
|
|
402
408
|
let returnType: AstNode | null = null;
|
|
403
|
-
if (isNode(parentFunction
|
|
404
|
-
const typeAnnotation = parentFunction
|
|
409
|
+
if (isNode(parentFunction["returnType"])) {
|
|
410
|
+
const typeAnnotation = parentFunction["returnType"]["typeAnnotation"];
|
|
405
411
|
if (isTypeReference(typeAnnotation)) {
|
|
406
412
|
returnType = getPromiseTypeArgument(typeAnnotation);
|
|
407
413
|
}
|
|
408
414
|
}
|
|
409
415
|
|
|
410
416
|
const blockBody =
|
|
411
|
-
isNode(node
|
|
412
|
-
? node
|
|
417
|
+
isNode(node["block"]) && Array.isArray(node["block"]["body"])
|
|
418
|
+
? node["block"]["body"]
|
|
413
419
|
: [];
|
|
414
420
|
for (const statement of blockBody) {
|
|
415
421
|
if (isNode(statement) && statement.type === "ReturnStatement") {
|
|
@@ -417,15 +423,15 @@ const rule = {
|
|
|
417
423
|
}
|
|
418
424
|
}
|
|
419
425
|
|
|
420
|
-
if (!isNode(node
|
|
421
|
-
const catchBody = Array.isArray(node
|
|
422
|
-
? node
|
|
426
|
+
if (!isNode(node["handler"]) || !isNode(node["handler"]["body"])) return;
|
|
427
|
+
const catchBody = Array.isArray(node["handler"]["body"]["body"])
|
|
428
|
+
? node["handler"]["body"]["body"].filter(isNode)
|
|
423
429
|
: [];
|
|
424
430
|
|
|
425
431
|
for (const statement of catchBody) {
|
|
426
432
|
if (
|
|
427
433
|
statement.type === "ReturnStatement" &&
|
|
428
|
-
isNode(statement
|
|
434
|
+
isNode(statement["argument"])
|
|
429
435
|
) {
|
|
430
436
|
wrapReturnValue(statement, isAsync, returnType);
|
|
431
437
|
}
|
|
@@ -434,23 +440,23 @@ const rule = {
|
|
|
434
440
|
const hasCorrectReturn = catchBody.some(
|
|
435
441
|
(statement) =>
|
|
436
442
|
statement.type === "ReturnStatement" &&
|
|
437
|
-
isNode(statement
|
|
438
|
-
((statement
|
|
439
|
-
statement
|
|
440
|
-
isResultHelperCall(statement
|
|
441
|
-
hasIsErrorFlag(statement
|
|
443
|
+
isNode(statement["argument"]) &&
|
|
444
|
+
((statement["argument"].type === "Identifier" &&
|
|
445
|
+
statement["argument"]["name"] === "FAILED_PROMISE") ||
|
|
446
|
+
isResultHelperCall(statement["argument"], "err") ||
|
|
447
|
+
hasIsErrorFlag(statement["argument"], true)),
|
|
442
448
|
);
|
|
443
449
|
|
|
444
450
|
if (!hasCorrectReturn) {
|
|
445
451
|
context.report({
|
|
446
|
-
node: node
|
|
452
|
+
node: node["handler"],
|
|
447
453
|
messageId: "returnFailedPromise",
|
|
448
454
|
fix(fixer) {
|
|
449
455
|
const lastStatement = catchBody.at(-1);
|
|
450
456
|
if (
|
|
451
457
|
lastStatement?.type === "ReturnStatement" &&
|
|
452
|
-
isNode(lastStatement
|
|
453
|
-
lastStatement
|
|
458
|
+
isNode(lastStatement["argument"]) &&
|
|
459
|
+
lastStatement["argument"].type === "ObjectExpression"
|
|
454
460
|
) {
|
|
455
461
|
return [
|
|
456
462
|
...ensureResultHelperImportFixes(
|
|
@@ -465,15 +471,15 @@ const rule = {
|
|
|
465
471
|
];
|
|
466
472
|
}
|
|
467
473
|
|
|
468
|
-
const handler = node
|
|
474
|
+
const handler = node["handler"];
|
|
469
475
|
if (!isNode(handler)) {
|
|
470
476
|
return null;
|
|
471
477
|
}
|
|
472
478
|
|
|
473
|
-
const bodyRange = handler
|
|
479
|
+
const bodyRange = handler["body"];
|
|
474
480
|
if (
|
|
475
481
|
!isNode(bodyRange) ||
|
|
476
|
-
!Array.isArray(bodyRange
|
|
482
|
+
!Array.isArray(bodyRange["range"])
|
|
477
483
|
) {
|
|
478
484
|
return null;
|
|
479
485
|
}
|
|
@@ -486,8 +492,8 @@ const rule = {
|
|
|
486
492
|
),
|
|
487
493
|
fixer.insertTextBeforeRange(
|
|
488
494
|
[
|
|
489
|
-
bodyRange
|
|
490
|
-
bodyRange
|
|
495
|
+
bodyRange["range"][1] - 1,
|
|
496
|
+
bodyRange["range"][1] - 1,
|
|
491
497
|
],
|
|
492
498
|
"return err(); ",
|
|
493
499
|
),
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** Operations on collections rather than checks on them. */
|
|
2
|
+
|
|
3
|
+
import { isArray } from "./guards.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Get the keys of an object.
|
|
7
|
+
*
|
|
8
|
+
* @param {unknown} object - The object to get the keys of.
|
|
9
|
+
* @returns {Array} Keys of object.
|
|
10
|
+
*/
|
|
11
|
+
function getKeys<T extends Record<string, unknown>>(object: T): (keyof T)[] {
|
|
12
|
+
return Object.keys(object);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Compares two arrays and returns true if they have at least one common value.
|
|
17
|
+
*
|
|
18
|
+
* @param {readonly T[]} array1 - First array.
|
|
19
|
+
* @param {readonly T[]} array2 - Second array.
|
|
20
|
+
* @returns {boolean} True if the arrays have at least one common value.
|
|
21
|
+
*/
|
|
22
|
+
function arrayContainsCommonValue<T>(
|
|
23
|
+
array1: readonly T[],
|
|
24
|
+
array2: readonly T[],
|
|
25
|
+
): boolean {
|
|
26
|
+
if (!isArray(array1) || !isArray(array2)) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const valueOccurrences = new Set(array1);
|
|
31
|
+
return array2.some((value) => {
|
|
32
|
+
return valueOccurrences.has(value);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { arrayContainsCommonValue, getKeys };
|
package/src/empty.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The recursive "is this effectively blank" checks. Split out because they walk
|
|
3
|
+
* arbitrary object graphs — far more code than the one-line guards they are
|
|
4
|
+
* filed next to, and needed far less often.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
isArray,
|
|
9
|
+
isBoolean,
|
|
10
|
+
isEmptyObject,
|
|
11
|
+
isEmptyString,
|
|
12
|
+
isNull,
|
|
13
|
+
isObject,
|
|
14
|
+
isString,
|
|
15
|
+
isUndefined,
|
|
16
|
+
} from "./guards.js";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Returns true for values that behave like "empty" application input.
|
|
20
|
+
*
|
|
21
|
+
* @param {unknown} value - The value to check.
|
|
22
|
+
* @returns {boolean} True if the value is nullish, empty string, empty array,
|
|
23
|
+
* false, or a plain object whose values are all empty-like.
|
|
24
|
+
*/
|
|
25
|
+
function isEmptyLike(value: unknown): boolean {
|
|
26
|
+
if (isNull(value) || isUndefined(value)) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
if (isString(value)) {
|
|
30
|
+
return isEmptyString(value);
|
|
31
|
+
}
|
|
32
|
+
if (isArray(value)) {
|
|
33
|
+
return value.every((entry) => {
|
|
34
|
+
return isEmptyLike(entry);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (isBoolean(value)) {
|
|
38
|
+
return !value;
|
|
39
|
+
}
|
|
40
|
+
if (isObject(value)) {
|
|
41
|
+
if (Object.getPrototypeOf(value) !== Object.prototype) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
return Object.values(value).every((entry) => {
|
|
45
|
+
return isEmptyLike(entry);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Checks if the provided value contains empty values.
|
|
53
|
+
*
|
|
54
|
+
* This function determines if the given value is either an empty string, an
|
|
55
|
+
* empty object, or a string representation of an empty object.
|
|
56
|
+
*
|
|
57
|
+
* @param {unknown} value - The value to check for emptiness. It can be of any
|
|
58
|
+
* type.
|
|
59
|
+
* @returns {boolean} `true` if the value is an empty string, an empty object,
|
|
60
|
+
* or a string representation of an empty object; otherwise, `false`.
|
|
61
|
+
*/
|
|
62
|
+
function hasEmptyValues(value: unknown): boolean {
|
|
63
|
+
if (isString(value)) {
|
|
64
|
+
try {
|
|
65
|
+
if (isEmptyObject(JSON.parse(value))) {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
} catch {
|
|
69
|
+
return isEmptyString(value);
|
|
70
|
+
}
|
|
71
|
+
return isEmptyString(value);
|
|
72
|
+
}
|
|
73
|
+
return isEmptyObject(value);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export { hasEmptyValues, isEmptyLike };
|