@murky-web/typebuddy 1.1.0 → 1.2.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/oxlint.js +5 -5
- package/dist/rules/async_rule.js +60 -7
- package/dist/rules/maybe_promise_rule.js +118 -35
- 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/dist/src/type_helper.js +4 -5
- package/jsr.json +1 -1
- package/package.json +5 -4
- package/rules/async_rule.ts +141 -8
- package/rules/maybe_promise_rule.ts +285 -59
- package/src/index.ts +9 -0
- package/src/type_helper.ts +256 -257
- package/src/types/json.ts +1 -1
package/dist/oxlint.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { rule } from "./rules/async_rule.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { requireTryCatchAsyncRule as rule } from "./rules/async_rule.js";
|
|
2
|
+
import { errorSafeAsyncRule as rule$1 } from "./rules/maybe_promise_rule.js";
|
|
3
|
+
import { maybeRule as rule$2 } from "./rules/maybe_rule.js";
|
|
4
|
+
import { nullableRule as rule$3 } from "./rules/nullable_rule.js";
|
|
5
|
+
import { optionalRule as rule$4 } from "./rules/optional_rule.js";
|
|
6
6
|
//#region oxlint/index.ts
|
|
7
7
|
const plugin = {
|
|
8
8
|
meta: { name: "typebuddy" },
|
package/dist/rules/async_rule.js
CHANGED
|
@@ -1,4 +1,45 @@
|
|
|
1
1
|
//#region rules/async_rule.ts
|
|
2
|
+
function isNode(value) {
|
|
3
|
+
return typeof value === "object" && value !== null && "type" in value;
|
|
4
|
+
}
|
|
5
|
+
function getProgram(node) {
|
|
6
|
+
let current = node;
|
|
7
|
+
while (current?.parent) current = current.parent;
|
|
8
|
+
return current?.type === "Program" ? current : null;
|
|
9
|
+
}
|
|
10
|
+
function isIdentifierNamed(node, name) {
|
|
11
|
+
return isNode(node) && node.type === "Identifier" && node["name"] === name;
|
|
12
|
+
}
|
|
13
|
+
function isImportDeclaration(node) {
|
|
14
|
+
return isNode(node) && node.type === "ImportDeclaration";
|
|
15
|
+
}
|
|
16
|
+
function getStringLiteralValue(node) {
|
|
17
|
+
if (!isNode(node)) return null;
|
|
18
|
+
if ((node.type === "Literal" || node.type === "StringLiteral") && typeof node["value"] === "string") return node["value"];
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
function getProgramBody(node) {
|
|
22
|
+
if (!Array.isArray(node.body)) return [];
|
|
23
|
+
return node.body.filter(isNode);
|
|
24
|
+
}
|
|
25
|
+
function hasTypeBuddyHelperImport(program, helperName) {
|
|
26
|
+
return getProgramBody(program).some((statement) => {
|
|
27
|
+
if (!isImportDeclaration(statement)) return false;
|
|
28
|
+
if (getStringLiteralValue(statement["source"]) !== "@murky-web/typebuddy") return false;
|
|
29
|
+
if (statement["importKind"] === "type") return false;
|
|
30
|
+
return (Array.isArray(statement["specifiers"]) ? statement["specifiers"] : []).some((specifier) => {
|
|
31
|
+
return isNode(specifier) && specifier.type === "ImportSpecifier" && isIdentifierNamed(specifier["local"], helperName);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
function getTypeBuddyImportInsertRange(program) {
|
|
36
|
+
const body = getProgramBody(program);
|
|
37
|
+
const imports = body.filter(isImportDeclaration);
|
|
38
|
+
const anchor = imports.at(-1) ?? body[0] ?? program;
|
|
39
|
+
if (!Array.isArray(anchor.range) || anchor.range.length < 2) return null;
|
|
40
|
+
if (imports.length > 0) return [anchor.range[1], anchor.range[1]];
|
|
41
|
+
return [anchor.range[0], anchor.range[0]];
|
|
42
|
+
}
|
|
2
43
|
function hasTryCatch(nodes) {
|
|
3
44
|
return nodes.some((node) => {
|
|
4
45
|
return node.type === "TryStatement";
|
|
@@ -8,13 +49,24 @@ function isCallArgumentCallback(node) {
|
|
|
8
49
|
const parent = node.parent;
|
|
9
50
|
if (!parent) return false;
|
|
10
51
|
if (parent.type !== "CallExpression" && parent.type !== "NewExpression") return false;
|
|
11
|
-
return Array.isArray(parent
|
|
52
|
+
return Array.isArray(parent["arguments"]) && parent["arguments"].includes(node);
|
|
12
53
|
}
|
|
13
54
|
const rule = {
|
|
14
55
|
create(context) {
|
|
15
56
|
const sourceCode = context.getSourceCode();
|
|
57
|
+
let scheduledErrImport = false;
|
|
58
|
+
function ensureErrImportFixes(node, fixer) {
|
|
59
|
+
if (scheduledErrImport) return [];
|
|
60
|
+
const program = getProgram(node);
|
|
61
|
+
if (!program || hasTypeBuddyHelperImport(program, "err")) return [];
|
|
62
|
+
const insertRange = getTypeBuddyImportInsertRange(program);
|
|
63
|
+
if (!insertRange) return [];
|
|
64
|
+
scheduledErrImport = true;
|
|
65
|
+
const importText = insertRange[0] !== 0 ? "\nimport { err } from \"@murky-web/typebuddy\";" : "import { err } from \"@murky-web/typebuddy\";\n";
|
|
66
|
+
return [fixer.insertTextBeforeRange(insertRange, importText)];
|
|
67
|
+
}
|
|
16
68
|
function getIndentation(node) {
|
|
17
|
-
const firstLine = sourceCode.getText(node).split("\n")[0];
|
|
69
|
+
const firstLine = sourceCode.getText(node).split("\n")[0] ?? "";
|
|
18
70
|
const match = /^\s*/.exec(firstLine);
|
|
19
71
|
return match ? match[0] : "";
|
|
20
72
|
}
|
|
@@ -25,10 +77,11 @@ const rule = {
|
|
|
25
77
|
return `{
|
|
26
78
|
${indent}try {
|
|
27
79
|
${body.map((statement) => {
|
|
28
|
-
|
|
80
|
+
const text = sourceCode.getText(statement);
|
|
81
|
+
return `${innerIndent}${text.replaceAll(/^\s*/gm, "")}`;
|
|
29
82
|
}).join("\n")}
|
|
30
|
-
${indent}} catch
|
|
31
|
-
${innerIndent}return
|
|
83
|
+
${indent}} catch {
|
|
84
|
+
${innerIndent}return err();
|
|
32
85
|
${indent}}
|
|
33
86
|
}`;
|
|
34
87
|
}
|
|
@@ -42,7 +95,7 @@ ${indent}}
|
|
|
42
95
|
node,
|
|
43
96
|
messageId: "missingTryCatch",
|
|
44
97
|
fix(fixer) {
|
|
45
|
-
return fixer.replaceText(bodyNode, wrapInTryCatch(bodyNode));
|
|
98
|
+
return [...ensureErrImportFixes(node, fixer), fixer.replaceText(bodyNode, wrapInTryCatch(bodyNode))];
|
|
46
99
|
}
|
|
47
100
|
});
|
|
48
101
|
}
|
|
@@ -62,4 +115,4 @@ ${indent}}
|
|
|
62
115
|
}
|
|
63
116
|
};
|
|
64
117
|
//#endregion
|
|
65
|
-
export { rule };
|
|
118
|
+
export { rule as requireTryCatchAsyncRule };
|
|
@@ -3,42 +3,89 @@ function isNode(value) {
|
|
|
3
3
|
return typeof value === "object" && value !== null && "type" in value;
|
|
4
4
|
}
|
|
5
5
|
function isAsyncFunction(node) {
|
|
6
|
-
return node
|
|
6
|
+
return node["async"] === true;
|
|
7
7
|
}
|
|
8
8
|
function hasTypeParameters(value) {
|
|
9
|
-
return isNode(value) && Array.isArray(value
|
|
9
|
+
return isNode(value) && Array.isArray(value["params"]);
|
|
10
10
|
}
|
|
11
11
|
function isCallArgumentCallback(node) {
|
|
12
12
|
const parent = node.parent;
|
|
13
13
|
if (!parent) return false;
|
|
14
14
|
if (parent.type !== "CallExpression" && parent.type !== "NewExpression") return false;
|
|
15
|
-
return Array.isArray(parent
|
|
15
|
+
return Array.isArray(parent["arguments"]) && parent["arguments"].includes(node);
|
|
16
16
|
}
|
|
17
17
|
function isIdentifierNamed(node, name) {
|
|
18
|
-
return isNode(node) && node.type === "Identifier" && node
|
|
18
|
+
return isNode(node) && node.type === "Identifier" && node["name"] === name;
|
|
19
|
+
}
|
|
20
|
+
function getProgram(node) {
|
|
21
|
+
let current = node;
|
|
22
|
+
while (current?.parent) current = current.parent;
|
|
23
|
+
return current?.type === "Program" ? current : null;
|
|
24
|
+
}
|
|
25
|
+
function isImportDeclaration(node) {
|
|
26
|
+
return isNode(node) && node.type === "ImportDeclaration";
|
|
27
|
+
}
|
|
28
|
+
function getStringLiteralValue(node) {
|
|
29
|
+
if (!isNode(node)) return null;
|
|
30
|
+
if ((node.type === "Literal" || node.type === "StringLiteral") && typeof node["value"] === "string") return node["value"];
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
function getProgramBody(node) {
|
|
34
|
+
if (!Array.isArray(node["body"])) return [];
|
|
35
|
+
return node["body"].filter(isNode);
|
|
19
36
|
}
|
|
20
37
|
function isResultHelperCall(node, name) {
|
|
21
|
-
return isNode(node) && node.type === "CallExpression" && isIdentifierNamed(node
|
|
38
|
+
return isNode(node) && node.type === "CallExpression" && isIdentifierNamed(node["callee"], name);
|
|
22
39
|
}
|
|
23
40
|
function hasIsErrorFlag(node, expected) {
|
|
24
|
-
if (!isNode(node) || node.type !== "ObjectExpression" || !Array.isArray(node
|
|
25
|
-
return node
|
|
26
|
-
return isNode(property) && property.type === "Property" && isIdentifierNamed(property
|
|
41
|
+
if (!isNode(node) || node.type !== "ObjectExpression" || !Array.isArray(node["properties"])) return false;
|
|
42
|
+
return node["properties"].some((property) => {
|
|
43
|
+
return isNode(property) && property.type === "Property" && isIdentifierNamed(property["key"], "isError") && isNode(property["value"]) && property["value"].type === "Literal" && property["value"]["value"] === expected;
|
|
27
44
|
});
|
|
28
45
|
}
|
|
46
|
+
function getObjectPropertyValue(node, propertyName) {
|
|
47
|
+
if (!isNode(node) || node.type !== "ObjectExpression" || !Array.isArray(node["properties"])) return null;
|
|
48
|
+
for (const property of node["properties"]) if (isNode(property) && property.type === "Property" && isIdentifierNamed(property["key"], propertyName) && isNode(property["value"])) return property["value"];
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
function isBooleanLiteral(node, expected) {
|
|
52
|
+
return isNode(node) && node.type === "Literal" && node["value"] === expected;
|
|
53
|
+
}
|
|
54
|
+
function isNullLiteral(node) {
|
|
55
|
+
return isNode(node) && node.type === "Literal" && node["value"] === null;
|
|
56
|
+
}
|
|
57
|
+
function hasTypeBuddyHelperImport(program, helperName) {
|
|
58
|
+
return getProgramBody(program).some((statement) => {
|
|
59
|
+
if (!isImportDeclaration(statement)) return false;
|
|
60
|
+
if (getStringLiteralValue(statement["source"]) !== "@murky-web/typebuddy") return false;
|
|
61
|
+
if (statement["importKind"] === "type") return false;
|
|
62
|
+
return (Array.isArray(statement["specifiers"]) ? statement["specifiers"] : []).some((specifier) => {
|
|
63
|
+
return isNode(specifier) && specifier.type === "ImportSpecifier" && isIdentifierNamed(specifier["local"], helperName);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
function getTypeBuddyImportInsertRange(program) {
|
|
68
|
+
const body = getProgramBody(program);
|
|
69
|
+
const imports = body.filter(isImportDeclaration);
|
|
70
|
+
const anchor = imports.at(-1) ?? body[0] ?? program;
|
|
71
|
+
if (!Array.isArray(anchor["range"]) || anchor["range"].length < 2) return null;
|
|
72
|
+
if (imports.length > 0) return [anchor["range"][1], anchor["range"][1]];
|
|
73
|
+
return [anchor["range"][0], anchor["range"][0]];
|
|
74
|
+
}
|
|
29
75
|
const rule = {
|
|
30
76
|
create(context) {
|
|
31
77
|
const sourceCode = context.getSourceCode();
|
|
78
|
+
const scheduledHelperImports = /* @__PURE__ */ new Set();
|
|
32
79
|
function isTypeReference(node) {
|
|
33
80
|
return isNode(node) && node.type === "TSTypeReference";
|
|
34
81
|
}
|
|
35
82
|
function getTypeName(node) {
|
|
36
|
-
const typeName = node
|
|
83
|
+
const typeName = node["typeName"];
|
|
37
84
|
if (!isNode(typeName) || typeName.type !== "Identifier") return null;
|
|
38
|
-
return typeof typeName
|
|
85
|
+
return typeof typeName["name"] === "string" ? typeName["name"] : null;
|
|
39
86
|
}
|
|
40
87
|
function getTypeArgument(node) {
|
|
41
|
-
const typeArguments = node
|
|
88
|
+
const typeArguments = node["typeArguments"];
|
|
42
89
|
if (!hasTypeParameters(typeArguments)) return null;
|
|
43
90
|
const [firstParam] = typeArguments.params;
|
|
44
91
|
if (!isNode(firstParam)) return null;
|
|
@@ -48,14 +95,24 @@ const rule = {
|
|
|
48
95
|
if (getTypeName(node) !== "Promise") return null;
|
|
49
96
|
return getTypeArgument(node);
|
|
50
97
|
}
|
|
98
|
+
function ensureResultHelperImportFixes(node, fixer, helperName) {
|
|
99
|
+
if (scheduledHelperImports.has(helperName)) return [];
|
|
100
|
+
const program = getProgram(node);
|
|
101
|
+
if (!program || hasTypeBuddyHelperImport(program, helperName)) return [];
|
|
102
|
+
const insertRange = getTypeBuddyImportInsertRange(program);
|
|
103
|
+
if (!insertRange) return [];
|
|
104
|
+
scheduledHelperImports.add(helperName);
|
|
105
|
+
const importText = insertRange[0] !== 0 ? `\nimport { ${helperName} } from "@murky-web/typebuddy";` : `import { ${helperName} } from "@murky-web/typebuddy";\n`;
|
|
106
|
+
return [fixer.insertTextBeforeRange(insertRange, importText)];
|
|
107
|
+
}
|
|
51
108
|
function checkReturnType(node) {
|
|
52
109
|
if (!isAsyncFunction(node)) return;
|
|
53
110
|
if (isCallArgumentCallback(node)) return;
|
|
54
|
-
if (!isNode(node
|
|
55
|
-
const typeAnnotation = node
|
|
111
|
+
if (!isNode(node["returnType"])) return;
|
|
112
|
+
const typeAnnotation = node["returnType"]["typeAnnotation"];
|
|
56
113
|
if (!isTypeReference(typeAnnotation)) return;
|
|
57
114
|
if (getTypeName(typeAnnotation) !== "Promise") return;
|
|
58
|
-
const typeName = typeAnnotation
|
|
115
|
+
const typeName = typeAnnotation["typeName"];
|
|
59
116
|
context.report({
|
|
60
117
|
node: typeName,
|
|
61
118
|
messageId: "replaceWithMaybePromise",
|
|
@@ -66,25 +123,48 @@ const rule = {
|
|
|
66
123
|
}
|
|
67
124
|
function wrapReturnValue(node, isAsync, returnType) {
|
|
68
125
|
if (!isAsync) return;
|
|
69
|
-
const argument = node
|
|
126
|
+
const argument = node["argument"];
|
|
70
127
|
if (!isNode(argument)) {
|
|
71
128
|
if (returnType?.type === "TSVoidKeyword") context.report({
|
|
72
129
|
node,
|
|
73
130
|
messageId: "returnVoidPromise",
|
|
74
131
|
fix(fixer) {
|
|
75
|
-
return fixer.replaceText(node, "return
|
|
132
|
+
return [...ensureResultHelperImportFixes(node, fixer, "ok"), fixer.replaceText(node, "return ok()")];
|
|
76
133
|
}
|
|
77
134
|
});
|
|
78
135
|
return;
|
|
79
136
|
}
|
|
80
|
-
if (argument.type === "Identifier" && (argument
|
|
137
|
+
if (argument.type === "Identifier" && (argument["name"] === "VOID_PROMISE" || argument["name"] === "FAILED_PROMISE")) return;
|
|
81
138
|
if (isResultHelperCall(argument, "ok") || isResultHelperCall(argument, "err")) return;
|
|
139
|
+
const isErrorValue = getObjectPropertyValue(argument, "isError");
|
|
140
|
+
const objectValue = getObjectPropertyValue(argument, "value");
|
|
141
|
+
if (isBooleanLiteral(isErrorValue, false) && objectValue) {
|
|
142
|
+
context.report({
|
|
143
|
+
node: argument,
|
|
144
|
+
messageId: "preferOkResult",
|
|
145
|
+
fix(fixer) {
|
|
146
|
+
const valueText = sourceCode.getText(objectValue);
|
|
147
|
+
return [...ensureResultHelperImportFixes(node, fixer, "ok"), fixer.replaceText(argument, `ok(${valueText})`)];
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (isBooleanLiteral(isErrorValue, true) && isNullLiteral(objectValue)) {
|
|
153
|
+
context.report({
|
|
154
|
+
node: argument,
|
|
155
|
+
messageId: "preferErrResult",
|
|
156
|
+
fix(fixer) {
|
|
157
|
+
return [...ensureResultHelperImportFixes(node, fixer, "err"), fixer.replaceText(argument, "err()")];
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
82
162
|
if (!hasIsErrorFlag(argument, true) && !hasIsErrorFlag(argument, false)) context.report({
|
|
83
163
|
node: argument,
|
|
84
164
|
messageId: "wrapReturn",
|
|
85
165
|
fix(fixer) {
|
|
86
166
|
const argumentText = sourceCode.getText(argument);
|
|
87
|
-
return fixer.replaceText(argument, `
|
|
167
|
+
return [...ensureResultHelperImportFixes(node, fixer, "ok"), fixer.replaceText(argument, `ok(${argumentText})`)];
|
|
88
168
|
}
|
|
89
169
|
});
|
|
90
170
|
}
|
|
@@ -94,7 +174,7 @@ const rule = {
|
|
|
94
174
|
let parentFunction = null;
|
|
95
175
|
while (parent) {
|
|
96
176
|
if (parent.type === "FunctionDeclaration" || parent.type === "FunctionExpression" || parent.type === "ArrowFunctionExpression") {
|
|
97
|
-
isAsync = parent
|
|
177
|
+
isAsync = parent["async"] === true;
|
|
98
178
|
parentFunction = parent;
|
|
99
179
|
break;
|
|
100
180
|
}
|
|
@@ -103,25 +183,26 @@ const rule = {
|
|
|
103
183
|
if (!isAsync || !parentFunction) return;
|
|
104
184
|
if (isCallArgumentCallback(parentFunction)) return;
|
|
105
185
|
let returnType = null;
|
|
106
|
-
if (isNode(parentFunction
|
|
107
|
-
const typeAnnotation = parentFunction
|
|
186
|
+
if (isNode(parentFunction["returnType"])) {
|
|
187
|
+
const typeAnnotation = parentFunction["returnType"]["typeAnnotation"];
|
|
108
188
|
if (isTypeReference(typeAnnotation)) returnType = getPromiseTypeArgument(typeAnnotation);
|
|
109
189
|
}
|
|
110
|
-
const blockBody = isNode(node
|
|
190
|
+
const blockBody = isNode(node["block"]) && Array.isArray(node["block"]["body"]) ? node["block"]["body"] : [];
|
|
111
191
|
for (const statement of blockBody) if (isNode(statement) && statement.type === "ReturnStatement") wrapReturnValue(statement, isAsync, returnType);
|
|
112
|
-
if (!isNode(node
|
|
113
|
-
const catchBody = Array.isArray(node
|
|
114
|
-
|
|
115
|
-
|
|
192
|
+
if (!isNode(node["handler"]) || !isNode(node["handler"]["body"])) return;
|
|
193
|
+
const catchBody = Array.isArray(node["handler"]["body"]["body"]) ? node["handler"]["body"]["body"].filter(isNode) : [];
|
|
194
|
+
for (const statement of catchBody) if (statement.type === "ReturnStatement" && isNode(statement["argument"])) wrapReturnValue(statement, isAsync, returnType);
|
|
195
|
+
if (!catchBody.some((statement) => statement.type === "ReturnStatement" && isNode(statement["argument"]) && (statement["argument"].type === "Identifier" && statement["argument"]["name"] === "FAILED_PROMISE" || isResultHelperCall(statement["argument"], "err") || hasIsErrorFlag(statement["argument"], true)))) context.report({
|
|
196
|
+
node: node["handler"],
|
|
116
197
|
messageId: "returnFailedPromise",
|
|
117
198
|
fix(fixer) {
|
|
118
199
|
const lastStatement = catchBody.at(-1);
|
|
119
|
-
if (lastStatement?.type === "ReturnStatement" && isNode(lastStatement
|
|
120
|
-
const handler = node
|
|
200
|
+
if (lastStatement?.type === "ReturnStatement" && isNode(lastStatement["argument"]) && lastStatement["argument"].type === "ObjectExpression") return [...ensureResultHelperImportFixes(node, fixer, "err"), fixer.replaceText(lastStatement, "return err()")];
|
|
201
|
+
const handler = node["handler"];
|
|
121
202
|
if (!isNode(handler)) return null;
|
|
122
|
-
const bodyRange = handler
|
|
123
|
-
if (!isNode(bodyRange) || !Array.isArray(bodyRange
|
|
124
|
-
return fixer.insertTextBeforeRange([bodyRange
|
|
203
|
+
const bodyRange = handler["body"];
|
|
204
|
+
if (!isNode(bodyRange) || !Array.isArray(bodyRange["range"])) return null;
|
|
205
|
+
return [...ensureResultHelperImportFixes(node, fixer, "err"), fixer.insertTextBeforeRange([bodyRange["range"][1] - 1, bodyRange["range"][1] - 1], "return err(); ")];
|
|
125
206
|
}
|
|
126
207
|
});
|
|
127
208
|
}
|
|
@@ -143,11 +224,13 @@ const rule = {
|
|
|
143
224
|
schema: [],
|
|
144
225
|
messages: {
|
|
145
226
|
replaceWithMaybePromise: "Use 'MaybePromise' instead of 'Promise' as the return type in async functions.",
|
|
146
|
-
wrapReturn: "Wrap return value with
|
|
147
|
-
|
|
148
|
-
|
|
227
|
+
wrapReturn: "Wrap return value with ok(value).",
|
|
228
|
+
preferOkResult: "Use ok(value) instead of an inline success result object.",
|
|
229
|
+
preferErrResult: "Use err() instead of an inline error result object.",
|
|
230
|
+
returnVoidPromise: "Return ok() for async functions with Promise<void> return type.",
|
|
231
|
+
returnFailedPromise: "Return err() in the catch block of async functions."
|
|
149
232
|
}
|
|
150
233
|
}
|
|
151
234
|
};
|
|
152
235
|
//#endregion
|
|
153
|
-
export { rule };
|
|
236
|
+
export { rule as errorSafeAsyncRule };
|
package/dist/rules/maybe_rule.js
CHANGED
package/dist/src/type_helper.js
CHANGED
|
@@ -295,15 +295,14 @@ function parseDomainName(url, defaultValue) {
|
|
|
295
295
|
if (normalizedValue === "" || normalizedValue.startsWith("/")) return defaultValue;
|
|
296
296
|
let urlCandidate = normalizedValue;
|
|
297
297
|
if (!normalizedValue.includes("://")) urlCandidate = `https://${normalizedValue}`;
|
|
298
|
-
let hostname = "";
|
|
299
298
|
try {
|
|
300
|
-
|
|
299
|
+
const { hostname } = new URL(urlCandidate);
|
|
300
|
+
const [domainName] = hostname.replace(/^www\d?\./i, "").split(".");
|
|
301
|
+
if (domainName === void 0 || domainName === "") return defaultValue;
|
|
302
|
+
return domainName;
|
|
301
303
|
} catch {
|
|
302
304
|
return defaultValue;
|
|
303
305
|
}
|
|
304
|
-
const [domainName] = hostname.replace(/^www\d?\./i, "").split(".");
|
|
305
|
-
if (!domainName) return defaultValue;
|
|
306
|
-
return domainName;
|
|
307
306
|
}
|
|
308
307
|
//#endregion
|
|
309
308
|
export { arrayContainsCommonValue, err, fastIsArray, getKeys, hasEmptyValues, isArray, isBoolean, isDate, isEmptyArray, isEmptyLike, isEmptyObject, isEmptyString, isError, isFloat, isFunction, isInstanceOf, isInteger, isMaybe, isNull, isNullable, isNumber, isObject, isOptional, isPromise, isRegExp, isString, isSuccess, isSymbol, isUlidString, isUndefined, isUuidString, ok, parseArray, parseDomainName, parseFloat, parseInteger, parseNumber, parseString };
|
package/jsr.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@murky-web/typebuddy",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Your new best friend for simple typescript guards every project needs.",
|
|
5
5
|
"private": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -38,11 +38,11 @@
|
|
|
38
38
|
"lint:fix": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.lint.json --type-aware --fix src biome tsdown.config.ts vitest.config.ts",
|
|
39
39
|
"format": "oxfmt -c ../config/oxc/.oxfmtrc.jsonc src rules biome tests tsdown.config.ts vitest.config.ts",
|
|
40
40
|
"format:check": "oxfmt -c ../config/oxc/.oxfmtrc.jsonc --check src rules biome tests tsdown.config.ts vitest.config.ts",
|
|
41
|
-
"smoke:globals": "bun run build &&
|
|
41
|
+
"smoke:globals": "bun run build && tsc --project ./smoke/globals/tsconfig.json --noEmit",
|
|
42
42
|
"smoke:oxlint": "bun run build && bun ./smoke/oxlint/run-smoke.ts",
|
|
43
43
|
"smoke:oxlint:fix": "bun run build && bun ./smoke/oxlint/run-fix-smoke.ts",
|
|
44
44
|
"smoke:treeshake": "bun run build && bun ./smoke/treeshake/run.ts",
|
|
45
|
-
"typecheck": "
|
|
45
|
+
"typecheck": "tsc --project ./tsconfig.json --noEmit && tsc --project ./tsconfig.vitest.json --noEmit"
|
|
46
46
|
},
|
|
47
47
|
"files": [
|
|
48
48
|
"dist",
|
|
@@ -72,6 +72,7 @@
|
|
|
72
72
|
"provenance": true
|
|
73
73
|
},
|
|
74
74
|
"devDependencies": {
|
|
75
|
-
"@murky-web/config": "workspace:*"
|
|
75
|
+
"@murky-web/config": "workspace:*",
|
|
76
|
+
"@types/node": "^25.5.0"
|
|
76
77
|
}
|
|
77
78
|
}
|
package/rules/async_rule.ts
CHANGED
|
@@ -14,10 +14,112 @@ type RuleContext = {
|
|
|
14
14
|
messageId: string;
|
|
15
15
|
fix(fixer: {
|
|
16
16
|
replaceText(node: unknown, text: string): unknown;
|
|
17
|
+
insertTextBeforeRange(
|
|
18
|
+
range: [number, number],
|
|
19
|
+
text: string,
|
|
20
|
+
): unknown;
|
|
17
21
|
}): unknown;
|
|
18
22
|
}): void;
|
|
19
23
|
};
|
|
20
24
|
|
|
25
|
+
type RuleFixer = {
|
|
26
|
+
replaceText(node: unknown, text: string): unknown;
|
|
27
|
+
insertTextBeforeRange(range: [number, number], text: string): unknown;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
function isNode(value: unknown): value is AstNode {
|
|
31
|
+
return typeof value === "object" && value !== null && "type" in value;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function getProgram(node: AstNode): AstNode | null {
|
|
35
|
+
let current: AstNode | undefined = node;
|
|
36
|
+
|
|
37
|
+
while (current?.parent) {
|
|
38
|
+
current = current.parent;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return current?.type === "Program" ? current : null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function isIdentifierNamed(node: unknown, name: string): boolean {
|
|
45
|
+
return isNode(node) && node.type === "Identifier" && node["name"] === name;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function isImportDeclaration(node: unknown): node is AstNode {
|
|
49
|
+
return isNode(node) && node.type === "ImportDeclaration";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function getStringLiteralValue(node: unknown): string | null {
|
|
53
|
+
if (!isNode(node)) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (
|
|
58
|
+
(node.type === "Literal" || node.type === "StringLiteral") &&
|
|
59
|
+
typeof node["value"] === "string"
|
|
60
|
+
) {
|
|
61
|
+
return node["value"];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function getProgramBody(node: AstNode): AstNode[] {
|
|
68
|
+
if (!Array.isArray(node.body)) {
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return node.body.filter(isNode);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function hasTypeBuddyHelperImport(program: AstNode, helperName: "err") {
|
|
76
|
+
return getProgramBody(program).some((statement) => {
|
|
77
|
+
if (!isImportDeclaration(statement)) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (
|
|
82
|
+
getStringLiteralValue(statement["source"]) !== "@murky-web/typebuddy"
|
|
83
|
+
) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (statement["importKind"] === "type") {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const specifiers = Array.isArray(statement["specifiers"])
|
|
92
|
+
? statement["specifiers"]
|
|
93
|
+
: [];
|
|
94
|
+
|
|
95
|
+
return specifiers.some((specifier) => {
|
|
96
|
+
return (
|
|
97
|
+
isNode(specifier) &&
|
|
98
|
+
specifier.type === "ImportSpecifier" &&
|
|
99
|
+
isIdentifierNamed(specifier["local"], helperName)
|
|
100
|
+
);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function getTypeBuddyImportInsertRange(
|
|
106
|
+
program: AstNode,
|
|
107
|
+
): [number, number] | null {
|
|
108
|
+
const body = getProgramBody(program);
|
|
109
|
+
const imports = body.filter(isImportDeclaration);
|
|
110
|
+
const anchor = imports.at(-1) ?? body[0] ?? program;
|
|
111
|
+
|
|
112
|
+
if (!Array.isArray(anchor.range) || anchor.range.length < 2) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (imports.length > 0) {
|
|
117
|
+
return [anchor.range[1], anchor.range[1]];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return [anchor.range[0], anchor.range[0]];
|
|
121
|
+
}
|
|
122
|
+
|
|
21
123
|
function hasTryCatch(nodes: AstNode[]): boolean {
|
|
22
124
|
return nodes.some((node) => {
|
|
23
125
|
return node.type === "TryStatement";
|
|
@@ -34,16 +136,47 @@ function isCallArgumentCallback(node: AstNode): boolean {
|
|
|
34
136
|
return false;
|
|
35
137
|
}
|
|
36
138
|
|
|
37
|
-
return Array.isArray(parent
|
|
139
|
+
return Array.isArray(parent["arguments"]) && parent["arguments"].includes(node);
|
|
38
140
|
}
|
|
39
141
|
|
|
40
142
|
const rule = {
|
|
41
143
|
create(context: RuleContext) {
|
|
42
144
|
const sourceCode = context.getSourceCode();
|
|
145
|
+
let scheduledErrImport = false;
|
|
146
|
+
|
|
147
|
+
function ensureErrImportFixes(
|
|
148
|
+
node: AstNode,
|
|
149
|
+
fixer: RuleFixer,
|
|
150
|
+
): unknown[] {
|
|
151
|
+
if (scheduledErrImport) {
|
|
152
|
+
return [];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const program = getProgram(node);
|
|
156
|
+
if (!program || hasTypeBuddyHelperImport(program, "err")) {
|
|
157
|
+
return [];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const insertRange = getTypeBuddyImportInsertRange(program);
|
|
161
|
+
if (!insertRange) {
|
|
162
|
+
return [];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
scheduledErrImport = true;
|
|
166
|
+
|
|
167
|
+
const hasImports = insertRange[0] !== 0;
|
|
168
|
+
const importText = hasImports
|
|
169
|
+
? '\nimport { err } from "@murky-web/typebuddy";'
|
|
170
|
+
: 'import { err } from "@murky-web/typebuddy";\n';
|
|
171
|
+
|
|
172
|
+
return [fixer.insertTextBeforeRange(insertRange, importText)];
|
|
173
|
+
}
|
|
43
174
|
|
|
44
175
|
function getIndentation(node: AstNode): string {
|
|
45
176
|
const lines = sourceCode.getText(node).split("\n");
|
|
46
|
-
|
|
177
|
+
// `split` always yields at least one element, but
|
|
178
|
+
// `noUncheckedIndexedAccess` cannot know that.
|
|
179
|
+
const firstLine = lines[0] ?? "";
|
|
47
180
|
const match = /^\s*/.exec(firstLine);
|
|
48
181
|
return match ? match[0] : "";
|
|
49
182
|
}
|
|
@@ -62,8 +195,8 @@ const rule = {
|
|
|
62
195
|
return `{
|
|
63
196
|
${indent}try {
|
|
64
197
|
${bodyText}
|
|
65
|
-
${indent}} catch
|
|
66
|
-
${innerIndent}return
|
|
198
|
+
${indent}} catch {
|
|
199
|
+
${innerIndent}return err();
|
|
67
200
|
${indent}}
|
|
68
201
|
}`;
|
|
69
202
|
}
|
|
@@ -87,10 +220,10 @@ ${indent}}
|
|
|
87
220
|
node,
|
|
88
221
|
messageId: "missingTryCatch",
|
|
89
222
|
fix(fixer) {
|
|
90
|
-
return
|
|
91
|
-
|
|
92
|
-
wrapInTryCatch(bodyNode),
|
|
93
|
-
|
|
223
|
+
return [
|
|
224
|
+
...ensureErrImportFixes(node, fixer),
|
|
225
|
+
fixer.replaceText(bodyNode, wrapInTryCatch(bodyNode)),
|
|
226
|
+
];
|
|
94
227
|
},
|
|
95
228
|
});
|
|
96
229
|
}
|