@murky-web/typebuddy 1.1.1 → 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 +11 -10
- package/dist/rules/maybe_promise_rule.js +45 -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/dist/src/type_helper.js +4 -5
- package/jsr.json +1 -1
- package/package.json +5 -4
- package/rules/async_rule.ts +12 -10
- package/rules/maybe_promise_rule.ts +61 -61
- package/src/index.ts +9 -0
- package/src/type_helper.ts +11 -11
- 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
|
@@ -8,14 +8,14 @@ function getProgram(node) {
|
|
|
8
8
|
return current?.type === "Program" ? current : null;
|
|
9
9
|
}
|
|
10
10
|
function isIdentifierNamed(node, name) {
|
|
11
|
-
return isNode(node) && node.type === "Identifier" && node
|
|
11
|
+
return isNode(node) && node.type === "Identifier" && node["name"] === name;
|
|
12
12
|
}
|
|
13
13
|
function isImportDeclaration(node) {
|
|
14
14
|
return isNode(node) && node.type === "ImportDeclaration";
|
|
15
15
|
}
|
|
16
16
|
function getStringLiteralValue(node) {
|
|
17
17
|
if (!isNode(node)) return null;
|
|
18
|
-
if ((node.type === "Literal" || node.type === "StringLiteral") && typeof node
|
|
18
|
+
if ((node.type === "Literal" || node.type === "StringLiteral") && typeof node["value"] === "string") return node["value"];
|
|
19
19
|
return null;
|
|
20
20
|
}
|
|
21
21
|
function getProgramBody(node) {
|
|
@@ -25,10 +25,10 @@ function getProgramBody(node) {
|
|
|
25
25
|
function hasTypeBuddyHelperImport(program, helperName) {
|
|
26
26
|
return getProgramBody(program).some((statement) => {
|
|
27
27
|
if (!isImportDeclaration(statement)) return false;
|
|
28
|
-
if (getStringLiteralValue(statement
|
|
29
|
-
if (statement
|
|
30
|
-
return (Array.isArray(statement
|
|
31
|
-
return isNode(specifier) && specifier.type === "ImportSpecifier" && isIdentifierNamed(specifier
|
|
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
32
|
});
|
|
33
33
|
});
|
|
34
34
|
}
|
|
@@ -49,7 +49,7 @@ function isCallArgumentCallback(node) {
|
|
|
49
49
|
const parent = node.parent;
|
|
50
50
|
if (!parent) return false;
|
|
51
51
|
if (parent.type !== "CallExpression" && parent.type !== "NewExpression") return false;
|
|
52
|
-
return Array.isArray(parent
|
|
52
|
+
return Array.isArray(parent["arguments"]) && parent["arguments"].includes(node);
|
|
53
53
|
}
|
|
54
54
|
const rule = {
|
|
55
55
|
create(context) {
|
|
@@ -66,7 +66,7 @@ const rule = {
|
|
|
66
66
|
return [fixer.insertTextBeforeRange(insertRange, importText)];
|
|
67
67
|
}
|
|
68
68
|
function getIndentation(node) {
|
|
69
|
-
const firstLine = sourceCode.getText(node).split("\n")[0];
|
|
69
|
+
const firstLine = sourceCode.getText(node).split("\n")[0] ?? "";
|
|
70
70
|
const match = /^\s*/.exec(firstLine);
|
|
71
71
|
return match ? match[0] : "";
|
|
72
72
|
}
|
|
@@ -77,7 +77,8 @@ const rule = {
|
|
|
77
77
|
return `{
|
|
78
78
|
${indent}try {
|
|
79
79
|
${body.map((statement) => {
|
|
80
|
-
|
|
80
|
+
const text = sourceCode.getText(statement);
|
|
81
|
+
return `${innerIndent}${text.replaceAll(/^\s*/gm, "")}`;
|
|
81
82
|
}).join("\n")}
|
|
82
83
|
${indent}} catch {
|
|
83
84
|
${innerIndent}return err();
|
|
@@ -114,4 +115,4 @@ ${indent}}
|
|
|
114
115
|
}
|
|
115
116
|
};
|
|
116
117
|
//#endregion
|
|
117
|
-
export { rule };
|
|
118
|
+
export { rule as requireTryCatchAsyncRule };
|
|
@@ -3,19 +3,19 @@ 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
19
|
}
|
|
20
20
|
function getProgram(node) {
|
|
21
21
|
let current = node;
|
|
@@ -27,40 +27,40 @@ function isImportDeclaration(node) {
|
|
|
27
27
|
}
|
|
28
28
|
function getStringLiteralValue(node) {
|
|
29
29
|
if (!isNode(node)) return null;
|
|
30
|
-
if ((node.type === "Literal" || node.type === "StringLiteral") && typeof node
|
|
30
|
+
if ((node.type === "Literal" || node.type === "StringLiteral") && typeof node["value"] === "string") return node["value"];
|
|
31
31
|
return null;
|
|
32
32
|
}
|
|
33
33
|
function getProgramBody(node) {
|
|
34
|
-
if (!Array.isArray(node
|
|
35
|
-
return node
|
|
34
|
+
if (!Array.isArray(node["body"])) return [];
|
|
35
|
+
return node["body"].filter(isNode);
|
|
36
36
|
}
|
|
37
37
|
function isResultHelperCall(node, name) {
|
|
38
|
-
return isNode(node) && node.type === "CallExpression" && isIdentifierNamed(node
|
|
38
|
+
return isNode(node) && node.type === "CallExpression" && isIdentifierNamed(node["callee"], name);
|
|
39
39
|
}
|
|
40
40
|
function hasIsErrorFlag(node, expected) {
|
|
41
|
-
if (!isNode(node) || node.type !== "ObjectExpression" || !Array.isArray(node
|
|
42
|
-
return node
|
|
43
|
-
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;
|
|
44
44
|
});
|
|
45
45
|
}
|
|
46
46
|
function getObjectPropertyValue(node, propertyName) {
|
|
47
|
-
if (!isNode(node) || node.type !== "ObjectExpression" || !Array.isArray(node
|
|
48
|
-
for (const property of node
|
|
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
49
|
return null;
|
|
50
50
|
}
|
|
51
51
|
function isBooleanLiteral(node, expected) {
|
|
52
|
-
return isNode(node) && node.type === "Literal" && node
|
|
52
|
+
return isNode(node) && node.type === "Literal" && node["value"] === expected;
|
|
53
53
|
}
|
|
54
54
|
function isNullLiteral(node) {
|
|
55
|
-
return isNode(node) && node.type === "Literal" && node
|
|
55
|
+
return isNode(node) && node.type === "Literal" && node["value"] === null;
|
|
56
56
|
}
|
|
57
57
|
function hasTypeBuddyHelperImport(program, helperName) {
|
|
58
58
|
return getProgramBody(program).some((statement) => {
|
|
59
59
|
if (!isImportDeclaration(statement)) return false;
|
|
60
|
-
if (getStringLiteralValue(statement
|
|
61
|
-
if (statement
|
|
62
|
-
return (Array.isArray(statement
|
|
63
|
-
return isNode(specifier) && specifier.type === "ImportSpecifier" && isIdentifierNamed(specifier
|
|
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
64
|
});
|
|
65
65
|
});
|
|
66
66
|
}
|
|
@@ -68,9 +68,9 @@ function getTypeBuddyImportInsertRange(program) {
|
|
|
68
68
|
const body = getProgramBody(program);
|
|
69
69
|
const imports = body.filter(isImportDeclaration);
|
|
70
70
|
const anchor = imports.at(-1) ?? body[0] ?? program;
|
|
71
|
-
if (!Array.isArray(anchor
|
|
72
|
-
if (imports.length > 0) return [anchor
|
|
73
|
-
return [anchor
|
|
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
74
|
}
|
|
75
75
|
const rule = {
|
|
76
76
|
create(context) {
|
|
@@ -80,12 +80,12 @@ const rule = {
|
|
|
80
80
|
return isNode(node) && node.type === "TSTypeReference";
|
|
81
81
|
}
|
|
82
82
|
function getTypeName(node) {
|
|
83
|
-
const typeName = node
|
|
83
|
+
const typeName = node["typeName"];
|
|
84
84
|
if (!isNode(typeName) || typeName.type !== "Identifier") return null;
|
|
85
|
-
return typeof typeName
|
|
85
|
+
return typeof typeName["name"] === "string" ? typeName["name"] : null;
|
|
86
86
|
}
|
|
87
87
|
function getTypeArgument(node) {
|
|
88
|
-
const typeArguments = node
|
|
88
|
+
const typeArguments = node["typeArguments"];
|
|
89
89
|
if (!hasTypeParameters(typeArguments)) return null;
|
|
90
90
|
const [firstParam] = typeArguments.params;
|
|
91
91
|
if (!isNode(firstParam)) return null;
|
|
@@ -108,11 +108,11 @@ const rule = {
|
|
|
108
108
|
function checkReturnType(node) {
|
|
109
109
|
if (!isAsyncFunction(node)) return;
|
|
110
110
|
if (isCallArgumentCallback(node)) return;
|
|
111
|
-
if (!isNode(node
|
|
112
|
-
const typeAnnotation = node
|
|
111
|
+
if (!isNode(node["returnType"])) return;
|
|
112
|
+
const typeAnnotation = node["returnType"]["typeAnnotation"];
|
|
113
113
|
if (!isTypeReference(typeAnnotation)) return;
|
|
114
114
|
if (getTypeName(typeAnnotation) !== "Promise") return;
|
|
115
|
-
const typeName = typeAnnotation
|
|
115
|
+
const typeName = typeAnnotation["typeName"];
|
|
116
116
|
context.report({
|
|
117
117
|
node: typeName,
|
|
118
118
|
messageId: "replaceWithMaybePromise",
|
|
@@ -123,7 +123,7 @@ const rule = {
|
|
|
123
123
|
}
|
|
124
124
|
function wrapReturnValue(node, isAsync, returnType) {
|
|
125
125
|
if (!isAsync) return;
|
|
126
|
-
const argument = node
|
|
126
|
+
const argument = node["argument"];
|
|
127
127
|
if (!isNode(argument)) {
|
|
128
128
|
if (returnType?.type === "TSVoidKeyword") context.report({
|
|
129
129
|
node,
|
|
@@ -134,7 +134,7 @@ const rule = {
|
|
|
134
134
|
});
|
|
135
135
|
return;
|
|
136
136
|
}
|
|
137
|
-
if (argument.type === "Identifier" && (argument
|
|
137
|
+
if (argument.type === "Identifier" && (argument["name"] === "VOID_PROMISE" || argument["name"] === "FAILED_PROMISE")) return;
|
|
138
138
|
if (isResultHelperCall(argument, "ok") || isResultHelperCall(argument, "err")) return;
|
|
139
139
|
const isErrorValue = getObjectPropertyValue(argument, "isError");
|
|
140
140
|
const objectValue = getObjectPropertyValue(argument, "value");
|
|
@@ -174,7 +174,7 @@ const rule = {
|
|
|
174
174
|
let parentFunction = null;
|
|
175
175
|
while (parent) {
|
|
176
176
|
if (parent.type === "FunctionDeclaration" || parent.type === "FunctionExpression" || parent.type === "ArrowFunctionExpression") {
|
|
177
|
-
isAsync = parent
|
|
177
|
+
isAsync = parent["async"] === true;
|
|
178
178
|
parentFunction = parent;
|
|
179
179
|
break;
|
|
180
180
|
}
|
|
@@ -183,26 +183,26 @@ const rule = {
|
|
|
183
183
|
if (!isAsync || !parentFunction) return;
|
|
184
184
|
if (isCallArgumentCallback(parentFunction)) return;
|
|
185
185
|
let returnType = null;
|
|
186
|
-
if (isNode(parentFunction
|
|
187
|
-
const typeAnnotation = parentFunction
|
|
186
|
+
if (isNode(parentFunction["returnType"])) {
|
|
187
|
+
const typeAnnotation = parentFunction["returnType"]["typeAnnotation"];
|
|
188
188
|
if (isTypeReference(typeAnnotation)) returnType = getPromiseTypeArgument(typeAnnotation);
|
|
189
189
|
}
|
|
190
|
-
const blockBody = isNode(node
|
|
190
|
+
const blockBody = isNode(node["block"]) && Array.isArray(node["block"]["body"]) ? node["block"]["body"] : [];
|
|
191
191
|
for (const statement of blockBody) if (isNode(statement) && statement.type === "ReturnStatement") wrapReturnValue(statement, isAsync, returnType);
|
|
192
|
-
if (!isNode(node
|
|
193
|
-
const catchBody = Array.isArray(node
|
|
194
|
-
for (const statement of catchBody) if (statement.type === "ReturnStatement" && isNode(statement
|
|
195
|
-
if (!catchBody.some((statement) => statement.type === "ReturnStatement" && isNode(statement
|
|
196
|
-
node: node
|
|
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"],
|
|
197
197
|
messageId: "returnFailedPromise",
|
|
198
198
|
fix(fixer) {
|
|
199
199
|
const lastStatement = catchBody.at(-1);
|
|
200
|
-
if (lastStatement?.type === "ReturnStatement" && isNode(lastStatement
|
|
201
|
-
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"];
|
|
202
202
|
if (!isNode(handler)) return null;
|
|
203
|
-
const bodyRange = handler
|
|
204
|
-
if (!isNode(bodyRange) || !Array.isArray(bodyRange
|
|
205
|
-
return [...ensureResultHelperImportFixes(node, fixer, "err"), 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(); ")];
|
|
206
206
|
}
|
|
207
207
|
});
|
|
208
208
|
}
|
|
@@ -233,4 +233,4 @@ const rule = {
|
|
|
233
233
|
}
|
|
234
234
|
};
|
|
235
235
|
//#endregion
|
|
236
|
-
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
|
@@ -42,7 +42,7 @@ function getProgram(node: AstNode): AstNode | null {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
function isIdentifierNamed(node: unknown, name: string): boolean {
|
|
45
|
-
return isNode(node) && node.type === "Identifier" && node
|
|
45
|
+
return isNode(node) && node.type === "Identifier" && node["name"] === name;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
function isImportDeclaration(node: unknown): node is AstNode {
|
|
@@ -56,9 +56,9 @@ function getStringLiteralValue(node: unknown): string | null {
|
|
|
56
56
|
|
|
57
57
|
if (
|
|
58
58
|
(node.type === "Literal" || node.type === "StringLiteral") &&
|
|
59
|
-
typeof node
|
|
59
|
+
typeof node["value"] === "string"
|
|
60
60
|
) {
|
|
61
|
-
return node
|
|
61
|
+
return node["value"];
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
return null;
|
|
@@ -79,24 +79,24 @@ function hasTypeBuddyHelperImport(program: AstNode, helperName: "err") {
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
if (
|
|
82
|
-
getStringLiteralValue(statement
|
|
82
|
+
getStringLiteralValue(statement["source"]) !== "@murky-web/typebuddy"
|
|
83
83
|
) {
|
|
84
84
|
return false;
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
if (statement
|
|
87
|
+
if (statement["importKind"] === "type") {
|
|
88
88
|
return false;
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
-
const specifiers = Array.isArray(statement
|
|
92
|
-
? statement
|
|
91
|
+
const specifiers = Array.isArray(statement["specifiers"])
|
|
92
|
+
? statement["specifiers"]
|
|
93
93
|
: [];
|
|
94
94
|
|
|
95
95
|
return specifiers.some((specifier) => {
|
|
96
96
|
return (
|
|
97
97
|
isNode(specifier) &&
|
|
98
98
|
specifier.type === "ImportSpecifier" &&
|
|
99
|
-
isIdentifierNamed(specifier
|
|
99
|
+
isIdentifierNamed(specifier["local"], helperName)
|
|
100
100
|
);
|
|
101
101
|
});
|
|
102
102
|
});
|
|
@@ -136,7 +136,7 @@ function isCallArgumentCallback(node: AstNode): boolean {
|
|
|
136
136
|
return false;
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
-
return Array.isArray(parent
|
|
139
|
+
return Array.isArray(parent["arguments"]) && parent["arguments"].includes(node);
|
|
140
140
|
}
|
|
141
141
|
|
|
142
142
|
const rule = {
|
|
@@ -174,7 +174,9 @@ const rule = {
|
|
|
174
174
|
|
|
175
175
|
function getIndentation(node: AstNode): string {
|
|
176
176
|
const lines = sourceCode.getText(node).split("\n");
|
|
177
|
-
|
|
177
|
+
// `split` always yields at least one element, but
|
|
178
|
+
// `noUncheckedIndexedAccess` cannot know that.
|
|
179
|
+
const firstLine = lines[0] ?? "";
|
|
178
180
|
const match = /^\s*/.exec(firstLine);
|
|
179
181
|
return match ? match[0] : "";
|
|
180
182
|
}
|
|
@@ -29,11 +29,11 @@ function isNode(value: unknown): value is AstNode {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
function isAsyncFunction(node: AstNode): boolean {
|
|
32
|
-
return node
|
|
32
|
+
return node["async"] === true;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
function hasTypeParameters(value: unknown): value is { params: unknown[] } {
|
|
36
|
-
return isNode(value) && Array.isArray(value
|
|
36
|
+
return isNode(value) && Array.isArray(value["params"]);
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
function isCallArgumentCallback(node: AstNode): boolean {
|
|
@@ -46,11 +46,11 @@ function isCallArgumentCallback(node: AstNode): boolean {
|
|
|
46
46
|
return false;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
return Array.isArray(parent
|
|
49
|
+
return Array.isArray(parent["arguments"]) && parent["arguments"].includes(node);
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
function isIdentifierNamed(node: unknown, name: string): boolean {
|
|
53
|
-
return isNode(node) && node.type === "Identifier" && node
|
|
53
|
+
return isNode(node) && node.type === "Identifier" && node["name"] === name;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
function getProgram(node: AstNode): AstNode | null {
|
|
@@ -74,27 +74,27 @@ function getStringLiteralValue(node: unknown): string | null {
|
|
|
74
74
|
|
|
75
75
|
if (
|
|
76
76
|
(node.type === "Literal" || node.type === "StringLiteral") &&
|
|
77
|
-
typeof node
|
|
77
|
+
typeof node["value"] === "string"
|
|
78
78
|
) {
|
|
79
|
-
return node
|
|
79
|
+
return node["value"];
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
return null;
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
function getProgramBody(node: AstNode): AstNode[] {
|
|
86
|
-
if (!Array.isArray(node
|
|
86
|
+
if (!Array.isArray(node["body"])) {
|
|
87
87
|
return [];
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
return node
|
|
90
|
+
return node["body"].filter(isNode);
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
function isResultHelperCall(node: unknown, name: "ok" | "err"): boolean {
|
|
94
94
|
return (
|
|
95
95
|
isNode(node) &&
|
|
96
96
|
node.type === "CallExpression" &&
|
|
97
|
-
isIdentifierNamed(node
|
|
97
|
+
isIdentifierNamed(node["callee"], name)
|
|
98
98
|
);
|
|
99
99
|
}
|
|
100
100
|
|
|
@@ -102,19 +102,19 @@ function hasIsErrorFlag(node: unknown, expected: boolean): boolean {
|
|
|
102
102
|
if (
|
|
103
103
|
!isNode(node) ||
|
|
104
104
|
node.type !== "ObjectExpression" ||
|
|
105
|
-
!Array.isArray(node
|
|
105
|
+
!Array.isArray(node["properties"])
|
|
106
106
|
) {
|
|
107
107
|
return false;
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
return node
|
|
110
|
+
return node["properties"].some((property) => {
|
|
111
111
|
return (
|
|
112
112
|
isNode(property) &&
|
|
113
113
|
property.type === "Property" &&
|
|
114
|
-
isIdentifierNamed(property
|
|
115
|
-
isNode(property
|
|
116
|
-
property
|
|
117
|
-
property
|
|
114
|
+
isIdentifierNamed(property["key"], "isError") &&
|
|
115
|
+
isNode(property["value"]) &&
|
|
116
|
+
property["value"].type === "Literal" &&
|
|
117
|
+
property["value"]["value"] === expected
|
|
118
118
|
);
|
|
119
119
|
});
|
|
120
120
|
}
|
|
@@ -126,19 +126,19 @@ function getObjectPropertyValue(
|
|
|
126
126
|
if (
|
|
127
127
|
!isNode(node) ||
|
|
128
128
|
node.type !== "ObjectExpression" ||
|
|
129
|
-
!Array.isArray(node
|
|
129
|
+
!Array.isArray(node["properties"])
|
|
130
130
|
) {
|
|
131
131
|
return null;
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
-
for (const property of node
|
|
134
|
+
for (const property of node["properties"]) {
|
|
135
135
|
if (
|
|
136
136
|
isNode(property) &&
|
|
137
137
|
property.type === "Property" &&
|
|
138
|
-
isIdentifierNamed(property
|
|
139
|
-
isNode(property
|
|
138
|
+
isIdentifierNamed(property["key"], propertyName) &&
|
|
139
|
+
isNode(property["value"])
|
|
140
140
|
) {
|
|
141
|
-
return property
|
|
141
|
+
return property["value"];
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
144
|
|
|
@@ -146,11 +146,11 @@ function getObjectPropertyValue(
|
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
function isBooleanLiteral(node: unknown, expected: boolean): boolean {
|
|
149
|
-
return isNode(node) && node.type === "Literal" && node
|
|
149
|
+
return isNode(node) && node.type === "Literal" && node["value"] === expected;
|
|
150
150
|
}
|
|
151
151
|
|
|
152
152
|
function isNullLiteral(node: unknown): boolean {
|
|
153
|
-
return isNode(node) && node.type === "Literal" && node
|
|
153
|
+
return isNode(node) && node.type === "Literal" && node["value"] === null;
|
|
154
154
|
}
|
|
155
155
|
|
|
156
156
|
function hasTypeBuddyHelperImport(program: AstNode, helperName: "ok" | "err") {
|
|
@@ -160,24 +160,24 @@ function hasTypeBuddyHelperImport(program: AstNode, helperName: "ok" | "err") {
|
|
|
160
160
|
}
|
|
161
161
|
|
|
162
162
|
if (
|
|
163
|
-
getStringLiteralValue(statement
|
|
163
|
+
getStringLiteralValue(statement["source"]) !== "@murky-web/typebuddy"
|
|
164
164
|
) {
|
|
165
165
|
return false;
|
|
166
166
|
}
|
|
167
167
|
|
|
168
|
-
if (statement
|
|
168
|
+
if (statement["importKind"] === "type") {
|
|
169
169
|
return false;
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
-
const specifiers = Array.isArray(statement
|
|
173
|
-
? statement
|
|
172
|
+
const specifiers = Array.isArray(statement["specifiers"])
|
|
173
|
+
? statement["specifiers"]
|
|
174
174
|
: [];
|
|
175
175
|
|
|
176
176
|
return specifiers.some((specifier) => {
|
|
177
177
|
return (
|
|
178
178
|
isNode(specifier) &&
|
|
179
179
|
specifier.type === "ImportSpecifier" &&
|
|
180
|
-
isIdentifierNamed(specifier
|
|
180
|
+
isIdentifierNamed(specifier["local"], helperName)
|
|
181
181
|
);
|
|
182
182
|
});
|
|
183
183
|
});
|
|
@@ -190,15 +190,15 @@ function getTypeBuddyImportInsertRange(
|
|
|
190
190
|
const imports = body.filter(isImportDeclaration);
|
|
191
191
|
const anchor = imports.at(-1) ?? body[0] ?? program;
|
|
192
192
|
|
|
193
|
-
if (!Array.isArray(anchor
|
|
193
|
+
if (!Array.isArray(anchor["range"]) || anchor["range"].length < 2) {
|
|
194
194
|
return null;
|
|
195
195
|
}
|
|
196
196
|
|
|
197
197
|
if (imports.length > 0) {
|
|
198
|
-
return [anchor
|
|
198
|
+
return [anchor["range"][1], anchor["range"][1]];
|
|
199
199
|
}
|
|
200
200
|
|
|
201
|
-
return [anchor
|
|
201
|
+
return [anchor["range"][0], anchor["range"][0]];
|
|
202
202
|
}
|
|
203
203
|
|
|
204
204
|
const rule = {
|
|
@@ -211,14 +211,14 @@ const rule = {
|
|
|
211
211
|
}
|
|
212
212
|
|
|
213
213
|
function getTypeName(node: AstNode): string | null {
|
|
214
|
-
const typeName = node
|
|
214
|
+
const typeName = node["typeName"];
|
|
215
215
|
if (!isNode(typeName) || typeName.type !== "Identifier")
|
|
216
216
|
return null;
|
|
217
|
-
return typeof typeName
|
|
217
|
+
return typeof typeName["name"] === "string" ? typeName["name"] : null;
|
|
218
218
|
}
|
|
219
219
|
|
|
220
220
|
function getTypeArgument(node: AstNode): AstNode | null {
|
|
221
|
-
const typeArguments = node
|
|
221
|
+
const typeArguments = node["typeArguments"];
|
|
222
222
|
if (!hasTypeParameters(typeArguments)) return null;
|
|
223
223
|
const [firstParam] = typeArguments.params;
|
|
224
224
|
if (!isNode(firstParam)) return null;
|
|
@@ -262,13 +262,13 @@ const rule = {
|
|
|
262
262
|
function checkReturnType(node: AstNode) {
|
|
263
263
|
if (!isAsyncFunction(node)) return;
|
|
264
264
|
if (isCallArgumentCallback(node)) return;
|
|
265
|
-
if (!isNode(node
|
|
265
|
+
if (!isNode(node["returnType"])) return;
|
|
266
266
|
|
|
267
|
-
const typeAnnotation = node
|
|
267
|
+
const typeAnnotation = node["returnType"]["typeAnnotation"];
|
|
268
268
|
if (!isTypeReference(typeAnnotation)) return;
|
|
269
269
|
if (getTypeName(typeAnnotation) !== "Promise") return;
|
|
270
270
|
|
|
271
|
-
const typeName = typeAnnotation
|
|
271
|
+
const typeName = typeAnnotation["typeName"];
|
|
272
272
|
context.report({
|
|
273
273
|
node: typeName,
|
|
274
274
|
messageId: "replaceWithMaybePromise",
|
|
@@ -285,7 +285,7 @@ const rule = {
|
|
|
285
285
|
) {
|
|
286
286
|
if (!isAsync) return;
|
|
287
287
|
|
|
288
|
-
const argument = node
|
|
288
|
+
const argument = node["argument"];
|
|
289
289
|
if (!isNode(argument)) {
|
|
290
290
|
if (returnType?.type === "TSVoidKeyword") {
|
|
291
291
|
context.report({
|
|
@@ -308,8 +308,8 @@ const rule = {
|
|
|
308
308
|
|
|
309
309
|
if (
|
|
310
310
|
argument.type === "Identifier" &&
|
|
311
|
-
(argument
|
|
312
|
-
argument
|
|
311
|
+
(argument["name"] === "VOID_PROMISE" ||
|
|
312
|
+
argument["name"] === "FAILED_PROMISE")
|
|
313
313
|
) {
|
|
314
314
|
return;
|
|
315
315
|
}
|
|
@@ -389,7 +389,7 @@ const rule = {
|
|
|
389
389
|
parent.type === "FunctionExpression" ||
|
|
390
390
|
parent.type === "ArrowFunctionExpression"
|
|
391
391
|
) {
|
|
392
|
-
isAsync = parent
|
|
392
|
+
isAsync = parent["async"] === true;
|
|
393
393
|
parentFunction = parent;
|
|
394
394
|
break;
|
|
395
395
|
}
|
|
@@ -400,16 +400,16 @@ const rule = {
|
|
|
400
400
|
if (isCallArgumentCallback(parentFunction)) return;
|
|
401
401
|
|
|
402
402
|
let returnType: AstNode | null = null;
|
|
403
|
-
if (isNode(parentFunction
|
|
404
|
-
const typeAnnotation = parentFunction
|
|
403
|
+
if (isNode(parentFunction["returnType"])) {
|
|
404
|
+
const typeAnnotation = parentFunction["returnType"]["typeAnnotation"];
|
|
405
405
|
if (isTypeReference(typeAnnotation)) {
|
|
406
406
|
returnType = getPromiseTypeArgument(typeAnnotation);
|
|
407
407
|
}
|
|
408
408
|
}
|
|
409
409
|
|
|
410
410
|
const blockBody =
|
|
411
|
-
isNode(node
|
|
412
|
-
? node
|
|
411
|
+
isNode(node["block"]) && Array.isArray(node["block"]["body"])
|
|
412
|
+
? node["block"]["body"]
|
|
413
413
|
: [];
|
|
414
414
|
for (const statement of blockBody) {
|
|
415
415
|
if (isNode(statement) && statement.type === "ReturnStatement") {
|
|
@@ -417,15 +417,15 @@ const rule = {
|
|
|
417
417
|
}
|
|
418
418
|
}
|
|
419
419
|
|
|
420
|
-
if (!isNode(node
|
|
421
|
-
const catchBody = Array.isArray(node
|
|
422
|
-
? node
|
|
420
|
+
if (!isNode(node["handler"]) || !isNode(node["handler"]["body"])) return;
|
|
421
|
+
const catchBody = Array.isArray(node["handler"]["body"]["body"])
|
|
422
|
+
? node["handler"]["body"]["body"].filter(isNode)
|
|
423
423
|
: [];
|
|
424
424
|
|
|
425
425
|
for (const statement of catchBody) {
|
|
426
426
|
if (
|
|
427
427
|
statement.type === "ReturnStatement" &&
|
|
428
|
-
isNode(statement
|
|
428
|
+
isNode(statement["argument"])
|
|
429
429
|
) {
|
|
430
430
|
wrapReturnValue(statement, isAsync, returnType);
|
|
431
431
|
}
|
|
@@ -434,23 +434,23 @@ const rule = {
|
|
|
434
434
|
const hasCorrectReturn = catchBody.some(
|
|
435
435
|
(statement) =>
|
|
436
436
|
statement.type === "ReturnStatement" &&
|
|
437
|
-
isNode(statement
|
|
438
|
-
((statement
|
|
439
|
-
statement
|
|
440
|
-
isResultHelperCall(statement
|
|
441
|
-
hasIsErrorFlag(statement
|
|
437
|
+
isNode(statement["argument"]) &&
|
|
438
|
+
((statement["argument"].type === "Identifier" &&
|
|
439
|
+
statement["argument"]["name"] === "FAILED_PROMISE") ||
|
|
440
|
+
isResultHelperCall(statement["argument"], "err") ||
|
|
441
|
+
hasIsErrorFlag(statement["argument"], true)),
|
|
442
442
|
);
|
|
443
443
|
|
|
444
444
|
if (!hasCorrectReturn) {
|
|
445
445
|
context.report({
|
|
446
|
-
node: node
|
|
446
|
+
node: node["handler"],
|
|
447
447
|
messageId: "returnFailedPromise",
|
|
448
448
|
fix(fixer) {
|
|
449
449
|
const lastStatement = catchBody.at(-1);
|
|
450
450
|
if (
|
|
451
451
|
lastStatement?.type === "ReturnStatement" &&
|
|
452
|
-
isNode(lastStatement
|
|
453
|
-
lastStatement
|
|
452
|
+
isNode(lastStatement["argument"]) &&
|
|
453
|
+
lastStatement["argument"].type === "ObjectExpression"
|
|
454
454
|
) {
|
|
455
455
|
return [
|
|
456
456
|
...ensureResultHelperImportFixes(
|
|
@@ -465,15 +465,15 @@ const rule = {
|
|
|
465
465
|
];
|
|
466
466
|
}
|
|
467
467
|
|
|
468
|
-
const handler = node
|
|
468
|
+
const handler = node["handler"];
|
|
469
469
|
if (!isNode(handler)) {
|
|
470
470
|
return null;
|
|
471
471
|
}
|
|
472
472
|
|
|
473
|
-
const bodyRange = handler
|
|
473
|
+
const bodyRange = handler["body"];
|
|
474
474
|
if (
|
|
475
475
|
!isNode(bodyRange) ||
|
|
476
|
-
!Array.isArray(bodyRange
|
|
476
|
+
!Array.isArray(bodyRange["range"])
|
|
477
477
|
) {
|
|
478
478
|
return null;
|
|
479
479
|
}
|
|
@@ -486,8 +486,8 @@ const rule = {
|
|
|
486
486
|
),
|
|
487
487
|
fixer.insertTextBeforeRange(
|
|
488
488
|
[
|
|
489
|
-
bodyRange
|
|
490
|
-
bodyRange
|
|
489
|
+
bodyRange["range"][1] - 1,
|
|
490
|
+
bodyRange["range"][1] - 1,
|
|
491
491
|
],
|
|
492
492
|
"return err(); ",
|
|
493
493
|
),
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
// Side-effect type import: pulls the `declare global` block into any
|
|
2
|
+
// program that imports this package, so the global helpers need no
|
|
3
|
+
// tsconfig entry of their own. The empty brace form is the only way to say
|
|
4
|
+
// "load this module for its ambient declarations" without importing a value,
|
|
5
|
+
// and `globals.ts` sits at the package root because it is a published entry
|
|
6
|
+
// point (`@murky-web/typebuddy/globals`).
|
|
7
|
+
// oxlint-disable-next-line no-empty-named-blocks, no-relative-parent-imports, require-module-specifiers
|
|
8
|
+
import type {} from "../globals.js";
|
|
9
|
+
|
|
1
10
|
export * from "./type_helper.js";
|
|
2
11
|
|
|
3
12
|
export type {
|
package/src/type_helper.ts
CHANGED
|
@@ -335,7 +335,7 @@ function isInstanceOf<T>(
|
|
|
335
335
|
* @returns {Array} Keys of object.
|
|
336
336
|
*/
|
|
337
337
|
function getKeys<T extends Record<string, unknown>>(object: T): (keyof T)[] {
|
|
338
|
-
return Object.keys(object)
|
|
338
|
+
return Object.keys(object);
|
|
339
339
|
}
|
|
340
340
|
|
|
341
341
|
/**
|
|
@@ -611,20 +611,20 @@ function parseDomainName(url: string, defaultValue?: string): Optional<string> {
|
|
|
611
611
|
urlCandidate = `https://${normalizedValue}`;
|
|
612
612
|
}
|
|
613
613
|
|
|
614
|
-
|
|
614
|
+
// Everything that depends on a parseable URL stays inside the `try`, so
|
|
615
|
+
// there is no binding that has to be declared before it can be assigned.
|
|
615
616
|
try {
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
617
|
+
const { hostname } = new URL(urlCandidate);
|
|
618
|
+
const normalizedHostname = hostname.replace(/^www\d?\./i, "");
|
|
619
|
+
const [domainName] = normalizedHostname.split(".");
|
|
620
|
+
if (domainName === undefined || domainName === "") {
|
|
621
|
+
return defaultValue;
|
|
622
|
+
}
|
|
620
623
|
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
if (!domainName) {
|
|
624
|
+
return domainName;
|
|
625
|
+
} catch {
|
|
624
626
|
return defaultValue;
|
|
625
627
|
}
|
|
626
|
-
|
|
627
|
-
return domainName;
|
|
628
628
|
}
|
|
629
629
|
|
|
630
630
|
export {
|
package/src/types/json.ts
CHANGED
|
@@ -2,7 +2,7 @@ type UnknownFunction = (...args: readonly unknown[]) => unknown;
|
|
|
2
2
|
|
|
3
3
|
type JsonifiedValue<T> = T extends string | number | null | boolean
|
|
4
4
|
? T
|
|
5
|
-
: T extends { toJSON()
|
|
5
|
+
: T extends { toJSON: () => infer R }
|
|
6
6
|
? R
|
|
7
7
|
: T extends undefined | UnknownFunction
|
|
8
8
|
? never
|