@murky-web/typebuddy 1.5.0 → 2.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/dist/parse.js +1 -1
- package/dist/result.js +6 -5
- package/dist/rules/async_rule.js +6 -3
- package/dist/rules/maybe_promise_rule.js +10 -7
- package/dist/rules/use_server.js +64 -0
- package/jsr.json +2 -6
- package/package.json +6 -13
- package/rules/async_rule.ts +7 -3
- package/rules/maybe_promise_rule.ts +11 -7
- package/rules/use_server.ts +116 -0
- package/src/parse.ts +1 -1
- package/src/result.ts +9 -8
- package/biome/index.ts +0 -6
- package/biome/maybe_promise_rule.grit +0 -15
- package/biome/maybe_rule.grit +0 -11
- package/biome/nullable_rule.grit +0 -3
- package/biome/optional_rule.gritt +0 -8
- package/biome/require-try-catch-async.grit +0 -5
- package/dist/biome.js +0 -7
- package/dist/maybe_promise_rule.grit +0 -15
- package/dist/maybe_rule.grit +0 -11
- package/dist/nullable_rule.grit +0 -3
- package/dist/optional_rule.gritt +0 -8
- package/dist/require-try-catch-async.grit +0 -5
package/dist/parse.js
CHANGED
|
@@ -69,7 +69,7 @@ function parseDomainName(url, defaultValue) {
|
|
|
69
69
|
let urlCandidate = normalizedValue;
|
|
70
70
|
if (!normalizedValue.includes("://")) urlCandidate = `https://${normalizedValue}`;
|
|
71
71
|
try {
|
|
72
|
-
const { hostname } = new URL(urlCandidate);
|
|
72
|
+
const { hostname } = new globalThis.URL(urlCandidate);
|
|
73
73
|
const [domainName] = hostname.replace(/^www\d?\./i, "").split(".");
|
|
74
74
|
if (domainName === void 0 || domainName === "") return defaultValue;
|
|
75
75
|
return domainName;
|
package/dist/result.js
CHANGED
|
@@ -10,12 +10,13 @@
|
|
|
10
10
|
* }
|
|
11
11
|
* ```
|
|
12
12
|
*
|
|
13
|
-
* @template
|
|
14
|
-
*
|
|
15
|
-
*
|
|
13
|
+
* @template R The result type passed in. The guard narrows it by its `isError`
|
|
14
|
+
* flag, so `value` keeps the success type without an extra `| null`.
|
|
15
|
+
* @param {R} result The result to check, which can be either a `Success<T>`
|
|
16
|
+
* or `Failed`.
|
|
16
17
|
* @returns {boolean} `true` if the result is a `Success<T>`, `false` if it is a
|
|
17
|
-
* `Failed`. This function acts as a type guard, narrowing
|
|
18
|
-
* `
|
|
18
|
+
* `Failed`. This function acts as a type guard, narrowing `result` to its
|
|
19
|
+
* `Success` member when the condition is met.
|
|
19
20
|
*/
|
|
20
21
|
function isSuccess(result) {
|
|
21
22
|
return !result.isError;
|
package/dist/rules/async_rule.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { throwsDeliberately } from "./deliberate_throw.js";
|
|
2
2
|
import { getTypeBuddyImportInsertion } from "./typebuddy_import.js";
|
|
3
|
+
import { createUseServerGuard } from "./use_server.js";
|
|
3
4
|
//#region rules/async_rule.ts
|
|
4
5
|
function hasTryCatch(nodes) {
|
|
5
6
|
return nodes.some((node) => {
|
|
@@ -77,10 +78,12 @@ const rule = {
|
|
|
77
78
|
}
|
|
78
79
|
});
|
|
79
80
|
}
|
|
81
|
+
const { Program, guard } = createUseServerGuard();
|
|
80
82
|
return {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
Program,
|
|
84
|
+
FunctionDeclaration: guard(checkFunction),
|
|
85
|
+
FunctionExpression: guard(checkFunction),
|
|
86
|
+
ArrowFunctionExpression: guard(checkFunction)
|
|
84
87
|
};
|
|
85
88
|
},
|
|
86
89
|
defaultOptions: [],
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { walkOwnSubtree } from "./own_subtree.js";
|
|
2
2
|
import { throwsDeliberately } from "./deliberate_throw.js";
|
|
3
3
|
import { getTypeBuddyImportInsertion } from "./typebuddy_import.js";
|
|
4
|
+
import { createUseServerGuard } from "./use_server.js";
|
|
4
5
|
//#region rules/maybe_promise_rule.ts
|
|
5
6
|
function isNode(value) {
|
|
6
7
|
return typeof value === "object" && value !== null && "type" in value;
|
|
@@ -273,14 +274,16 @@ const rule = {
|
|
|
273
274
|
}
|
|
274
275
|
});
|
|
275
276
|
}
|
|
277
|
+
const { Program, guard } = createUseServerGuard();
|
|
276
278
|
return {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
279
|
+
Program,
|
|
280
|
+
FunctionDeclaration: guard(checkReturnType),
|
|
281
|
+
FunctionExpression: guard(checkReturnType),
|
|
282
|
+
ArrowFunctionExpression: guard(checkReturnType),
|
|
283
|
+
TSDeclareFunction: guard(checkSignatureReturnType),
|
|
284
|
+
TSFunctionType: guard(checkSignatureReturnType),
|
|
285
|
+
TSMethodSignature: guard(checkSignatureReturnType),
|
|
286
|
+
TryStatement: guard(processTryCatch)
|
|
284
287
|
};
|
|
285
288
|
},
|
|
286
289
|
defaultOptions: [],
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
//#region rules/use_server.ts
|
|
2
|
+
const USE_SERVER = "use server";
|
|
3
|
+
const FUNCTION_TYPES = /* @__PURE__ */ new Set([
|
|
4
|
+
"ArrowFunctionExpression",
|
|
5
|
+
"FunctionDeclaration",
|
|
6
|
+
"FunctionExpression"
|
|
7
|
+
]);
|
|
8
|
+
function isUseServerNode(value) {
|
|
9
|
+
return typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Whether a statement is the `"use server"` directive.
|
|
13
|
+
*
|
|
14
|
+
* Parsers disagree on the shape: ESTree-style ASTs set `directive` on the
|
|
15
|
+
* expression statement, others only give the string literal.
|
|
16
|
+
*/
|
|
17
|
+
function isUseServerDirective(statement) {
|
|
18
|
+
if (!isUseServerNode(statement) || statement.type !== "ExpressionStatement") return false;
|
|
19
|
+
if ("directive" in statement && statement.directive === USE_SERVER) return true;
|
|
20
|
+
return "expression" in statement && isUseServerNode(statement.expression) && statement.expression.type === "Literal" && "value" in statement.expression && statement.expression.value === USE_SERVER;
|
|
21
|
+
}
|
|
22
|
+
function startsWithUseServer(statements) {
|
|
23
|
+
return Array.isArray(statements) && statements.length > 0 && isUseServerDirective(statements[0]);
|
|
24
|
+
}
|
|
25
|
+
function isUseServerFunction(node) {
|
|
26
|
+
if (!FUNCTION_TYPES.has(node.type) || !isUseServerNode(node.body)) return false;
|
|
27
|
+
return node.body.type === "BlockStatement" && startsWithUseServer(node.body.body);
|
|
28
|
+
}
|
|
29
|
+
function isInsideUseServerFunction(node) {
|
|
30
|
+
let current = node;
|
|
31
|
+
while (isUseServerNode(current)) {
|
|
32
|
+
if (isUseServerFunction(current)) return true;
|
|
33
|
+
current = current.parent;
|
|
34
|
+
}
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Exempt server code from a rule.
|
|
39
|
+
*
|
|
40
|
+
* Functions in `"use server"` modules log and throw on failure; the page
|
|
41
|
+
* catches the throw with `<Errored>`. A returned failure would resolve the
|
|
42
|
+
* caller's promise as a success, so the result-returning rules must not touch
|
|
43
|
+
* them. The exemption covers a module whose first statement is the directive
|
|
44
|
+
* and a function whose body starts with it, nested code included.
|
|
45
|
+
*
|
|
46
|
+
* `Program` has to be registered as a visitor next to the guarded ones: it runs
|
|
47
|
+
* first and records whether the whole file is server code.
|
|
48
|
+
*/
|
|
49
|
+
function createUseServerGuard() {
|
|
50
|
+
let serverModule = false;
|
|
51
|
+
return {
|
|
52
|
+
Program(node) {
|
|
53
|
+
serverModule = startsWithUseServer(node.body);
|
|
54
|
+
},
|
|
55
|
+
guard(visitor) {
|
|
56
|
+
return (node) => {
|
|
57
|
+
if (serverModule || isInsideUseServerFunction(node)) return;
|
|
58
|
+
visitor(node);
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
//#endregion
|
|
64
|
+
export { createUseServerGuard };
|
package/jsr.json
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://jsr.io/schema/config-file.v1.json",
|
|
3
3
|
"name": "@murky-web/typebuddy",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "2.1.0",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": "./src/index.ts",
|
|
8
|
-
"./biome": "./biome/index.ts",
|
|
9
8
|
"./oxlint": "./oxlint/index.ts"
|
|
10
9
|
},
|
|
11
10
|
"publish": {
|
|
@@ -13,10 +12,7 @@
|
|
|
13
12
|
"README.md",
|
|
14
13
|
"src/**/*.ts",
|
|
15
14
|
"oxlint/**/*.ts",
|
|
16
|
-
"rules/**/*.ts"
|
|
17
|
-
"biome/**/*.ts",
|
|
18
|
-
"biome/*.grit",
|
|
19
|
-
"biome/*.gritt"
|
|
15
|
+
"rules/**/*.ts"
|
|
20
16
|
],
|
|
21
17
|
"exclude": [
|
|
22
18
|
"tests/**",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@murky-web/typebuddy",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "2.1.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",
|
|
@@ -50,22 +50,17 @@
|
|
|
50
50
|
"import": "./dist/oxlint.js",
|
|
51
51
|
"default": "./dist/oxlint.js"
|
|
52
52
|
},
|
|
53
|
-
"./biome": {
|
|
54
|
-
"types": "./biome/index.ts",
|
|
55
|
-
"import": "./dist/biome.js",
|
|
56
|
-
"default": "./dist/biome.js"
|
|
57
|
-
},
|
|
58
53
|
"./package.json": "./package.json"
|
|
59
54
|
},
|
|
60
55
|
"scripts": {
|
|
61
56
|
"test": "vitest run",
|
|
62
57
|
"build:local": "bun run build",
|
|
63
|
-
"build": "bun run clean && tsdown
|
|
58
|
+
"build": "bun run clean && tsdown",
|
|
64
59
|
"clean": "rm -rf dist",
|
|
65
|
-
"lint": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.lint.json --type-aware src
|
|
66
|
-
"lint:fix": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.lint.json --type-aware --fix src
|
|
67
|
-
"format": "oxfmt -c
|
|
68
|
-
"format:check": "oxfmt -c
|
|
60
|
+
"lint": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.lint.json --type-aware src tsdown.config.ts vitest.config.ts",
|
|
61
|
+
"lint:fix": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.lint.json --type-aware --fix src tsdown.config.ts vitest.config.ts",
|
|
62
|
+
"format": "oxfmt -c ../../config/oxc/.oxfmtrc.jsonc src rules tests tsdown.config.ts vitest.config.ts",
|
|
63
|
+
"format:check": "oxfmt -c ../../config/oxc/.oxfmtrc.jsonc --check src rules tests tsdown.config.ts vitest.config.ts",
|
|
69
64
|
"smoke:globals": "bun run build && tsc --project ./smoke/globals/tsconfig.json --noEmit && mkdir -p ./smoke/globals/node_modules/@murky-web && ln -sfn ../../../.. ./smoke/globals/node_modules/@murky-web/typebuddy && tsc --project ./smoke/globals/tsconfig.types.json --noEmit",
|
|
70
65
|
"smoke:oxlint": "bun run build && bun ./smoke/oxlint/run-smoke.ts",
|
|
71
66
|
"smoke:oxlint:fix": "bun run build && bun ./smoke/oxlint/run-fix-smoke.ts",
|
|
@@ -74,7 +69,6 @@
|
|
|
74
69
|
"typecheck": "tsc --project ./tsconfig.json --noEmit && tsc --project ./tsconfig.vitest.json --noEmit"
|
|
75
70
|
},
|
|
76
71
|
"files": [
|
|
77
|
-
"biome",
|
|
78
72
|
"dist",
|
|
79
73
|
"globals.d.ts",
|
|
80
74
|
"jsr.json",
|
|
@@ -99,7 +93,6 @@
|
|
|
99
93
|
"provenance": true
|
|
100
94
|
},
|
|
101
95
|
"devDependencies": {
|
|
102
|
-
"@murky-web/config": "workspace:*",
|
|
103
96
|
"@types/node": "^25.5.0"
|
|
104
97
|
}
|
|
105
98
|
}
|
package/rules/async_rule.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { throwsDeliberately } from "./deliberate_throw.js";
|
|
2
2
|
import { getTypeBuddyImportInsertion } from "./typebuddy_import.js";
|
|
3
|
+
import { createUseServerGuard } from "./use_server.js";
|
|
3
4
|
|
|
4
5
|
type AstNode = {
|
|
5
6
|
type: string;
|
|
@@ -205,10 +206,13 @@ const rule = {
|
|
|
205
206
|
});
|
|
206
207
|
}
|
|
207
208
|
|
|
209
|
+
const { Program, guard } = createUseServerGuard();
|
|
210
|
+
|
|
208
211
|
return {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
+
Program,
|
|
213
|
+
FunctionDeclaration: guard(checkFunction),
|
|
214
|
+
FunctionExpression: guard(checkFunction),
|
|
215
|
+
ArrowFunctionExpression: guard(checkFunction),
|
|
212
216
|
};
|
|
213
217
|
},
|
|
214
218
|
defaultOptions: [],
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { throwsDeliberately } from "./deliberate_throw.js";
|
|
2
2
|
import { getTypeBuddyImportInsertion } from "./typebuddy_import.js";
|
|
3
3
|
import { walkOwnSubtree } from "./own_subtree.js";
|
|
4
|
+
import { createUseServerGuard } from "./use_server.js";
|
|
4
5
|
|
|
5
6
|
type AstNode = {
|
|
6
7
|
type: string;
|
|
@@ -545,14 +546,17 @@ const rule = {
|
|
|
545
546
|
});
|
|
546
547
|
}
|
|
547
548
|
|
|
549
|
+
const { Program, guard } = createUseServerGuard();
|
|
550
|
+
|
|
548
551
|
return {
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
552
|
+
Program,
|
|
553
|
+
FunctionDeclaration: guard(checkReturnType),
|
|
554
|
+
FunctionExpression: guard(checkReturnType),
|
|
555
|
+
ArrowFunctionExpression: guard(checkReturnType),
|
|
556
|
+
TSDeclareFunction: guard(checkSignatureReturnType),
|
|
557
|
+
TSFunctionType: guard(checkSignatureReturnType),
|
|
558
|
+
TSMethodSignature: guard(checkSignatureReturnType),
|
|
559
|
+
TryStatement: guard(processTryCatch),
|
|
556
560
|
};
|
|
557
561
|
},
|
|
558
562
|
defaultOptions: [],
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
type UseServerNode = {
|
|
2
|
+
type: string;
|
|
3
|
+
parent?: unknown;
|
|
4
|
+
body?: unknown;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
const USE_SERVER = "use server";
|
|
8
|
+
|
|
9
|
+
const FUNCTION_TYPES = new Set([
|
|
10
|
+
"ArrowFunctionExpression",
|
|
11
|
+
"FunctionDeclaration",
|
|
12
|
+
"FunctionExpression",
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
function isUseServerNode(value: unknown): value is UseServerNode {
|
|
16
|
+
return (
|
|
17
|
+
typeof value === "object" &&
|
|
18
|
+
value !== null &&
|
|
19
|
+
"type" in value &&
|
|
20
|
+
typeof value.type === "string"
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Whether a statement is the `"use server"` directive.
|
|
26
|
+
*
|
|
27
|
+
* Parsers disagree on the shape: ESTree-style ASTs set `directive` on the
|
|
28
|
+
* expression statement, others only give the string literal.
|
|
29
|
+
*/
|
|
30
|
+
function isUseServerDirective(statement: unknown): boolean {
|
|
31
|
+
if (
|
|
32
|
+
!isUseServerNode(statement) ||
|
|
33
|
+
statement.type !== "ExpressionStatement"
|
|
34
|
+
) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if ("directive" in statement && statement.directive === USE_SERVER) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return (
|
|
43
|
+
"expression" in statement &&
|
|
44
|
+
isUseServerNode(statement.expression) &&
|
|
45
|
+
statement.expression.type === "Literal" &&
|
|
46
|
+
"value" in statement.expression &&
|
|
47
|
+
statement.expression.value === USE_SERVER
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function startsWithUseServer(statements: unknown): boolean {
|
|
52
|
+
return (
|
|
53
|
+
Array.isArray(statements) &&
|
|
54
|
+
statements.length > 0 &&
|
|
55
|
+
isUseServerDirective(statements[0])
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function isUseServerFunction(node: UseServerNode): boolean {
|
|
60
|
+
if (!FUNCTION_TYPES.has(node.type) || !isUseServerNode(node.body)) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
node.body.type === "BlockStatement" && startsWithUseServer(node.body.body)
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function isInsideUseServerFunction(node: UseServerNode): boolean {
|
|
70
|
+
let current: unknown = node;
|
|
71
|
+
|
|
72
|
+
while (isUseServerNode(current)) {
|
|
73
|
+
if (isUseServerFunction(current)) {
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
current = current.parent;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Exempt server code from a rule.
|
|
85
|
+
*
|
|
86
|
+
* Functions in `"use server"` modules log and throw on failure; the page
|
|
87
|
+
* catches the throw with `<Errored>`. A returned failure would resolve the
|
|
88
|
+
* caller's promise as a success, so the result-returning rules must not touch
|
|
89
|
+
* them. The exemption covers a module whose first statement is the directive
|
|
90
|
+
* and a function whose body starts with it, nested code included.
|
|
91
|
+
*
|
|
92
|
+
* `Program` has to be registered as a visitor next to the guarded ones: it runs
|
|
93
|
+
* first and records whether the whole file is server code.
|
|
94
|
+
*/
|
|
95
|
+
function createUseServerGuard() {
|
|
96
|
+
let serverModule = false;
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
Program(node: UseServerNode): void {
|
|
100
|
+
serverModule = startsWithUseServer(node.body);
|
|
101
|
+
},
|
|
102
|
+
guard<Node extends UseServerNode>(
|
|
103
|
+
visitor: (node: Node) => void,
|
|
104
|
+
): (node: Node) => void {
|
|
105
|
+
return (node) => {
|
|
106
|
+
if (serverModule || isInsideUseServerFunction(node)) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
visitor(node);
|
|
111
|
+
};
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export { createUseServerGuard };
|
package/src/parse.ts
CHANGED
|
@@ -192,7 +192,7 @@ function parseDomainName(url: string, defaultValue?: string): Optional<string> {
|
|
|
192
192
|
// Everything that depends on a parseable URL stays inside the `try`, so
|
|
193
193
|
// there is no binding that has to be declared before it can be assigned.
|
|
194
194
|
try {
|
|
195
|
-
const { hostname } = new URL(urlCandidate);
|
|
195
|
+
const { hostname } = new globalThis.URL(urlCandidate);
|
|
196
196
|
const normalizedHostname = hostname.replace(/^www\d?\./i, "");
|
|
197
197
|
const [domainName] = normalizedHostname.split(".");
|
|
198
198
|
if (domainName === undefined || domainName === "") {
|
package/src/result.ts
CHANGED
|
@@ -17,16 +17,17 @@ import type { Failed, Result, Success } from "./types/maybe_promise.js";
|
|
|
17
17
|
* }
|
|
18
18
|
* ```
|
|
19
19
|
*
|
|
20
|
-
* @template
|
|
21
|
-
*
|
|
22
|
-
*
|
|
20
|
+
* @template R The result type passed in. The guard narrows it by its `isError`
|
|
21
|
+
* flag, so `value` keeps the success type without an extra `| null`.
|
|
22
|
+
* @param {R} result The result to check, which can be either a `Success<T>`
|
|
23
|
+
* or `Failed`.
|
|
23
24
|
* @returns {boolean} `true` if the result is a `Success<T>`, `false` if it is a
|
|
24
|
-
* `Failed`. This function acts as a type guard, narrowing
|
|
25
|
-
* `
|
|
25
|
+
* `Failed`. This function acts as a type guard, narrowing `result` to its
|
|
26
|
+
* `Success` member when the condition is met.
|
|
26
27
|
*/
|
|
27
|
-
function isSuccess<
|
|
28
|
-
result:
|
|
29
|
-
): result is
|
|
28
|
+
function isSuccess<R extends Readonly<Result<unknown>>>(
|
|
29
|
+
result: R,
|
|
30
|
+
): result is Extract<R, { isError: false }> {
|
|
30
31
|
return !result.isError;
|
|
31
32
|
}
|
|
32
33
|
|
package/biome/index.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
const resolveAsset = (asset: string): string =>
|
|
2
|
-
decodeURIComponent(new URL(asset, import.meta.url).pathname);
|
|
3
|
-
|
|
4
|
-
export const MaybePromiseRule: string = resolveAsset("./maybe_promise_rule.grit");
|
|
5
|
-
export const OptionalRule: string = resolveAsset("./optional_rule.gritt");
|
|
6
|
-
export const TryCatchAsyncRule: string = resolveAsset("./require-try-catch-async.grit");
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
or {
|
|
2
|
-
// 1. Replace Promise with MaybePromise in return types
|
|
3
|
-
`: Promise<$type>` => `: MaybePromise<$type>`,
|
|
4
|
-
|
|
5
|
-
// 2. Wrap return values in try blocks with { value, isError: false }
|
|
6
|
-
`try { $tryBody } catch ($err) { $catchBody }` where {
|
|
7
|
-
$tryBody <: contains `return $value` and {
|
|
8
|
-
$value <: not contains `{ isError: $isErrorValue }`
|
|
9
|
-
}
|
|
10
|
-
} => `try { $tryBody <: replace `return $value` => `return { value: $value, isError: false }` } catch ($err) { $catchBody }`,
|
|
11
|
-
|
|
12
|
-
// 3. Handle empty return statements in try blocks
|
|
13
|
-
`try { $tryBody } catch ($err) { $catchBody }` where {
|
|
14
|
-
$tryBody <: contains `return;`
|
|
15
|
-
} => `try { $tryBody <: replace `return;` => `return { value: undefined, isError: false };` } catch ($err) { $catchBody }`,
|
package/biome/maybe_rule.grit
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
or {
|
|
2
|
-
// T | null -> Maybe<T>
|
|
3
|
-
`$type | null` where {
|
|
4
|
-
$type <: not contains `null | undefined`
|
|
5
|
-
} => `Maybe<$type>`,
|
|
6
|
-
|
|
7
|
-
// T | null | undefined -> Nullable<T>
|
|
8
|
-
`$type | null | undefined` where {
|
|
9
|
-
$type <: not contains `null | undefined`
|
|
10
|
-
} => `Nullable<$type>`
|
|
11
|
-
}
|
package/biome/nullable_rule.grit
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
or {
|
|
2
|
-
// T | null -> Maybe<T>
|
|
3
|
-
`$type | null` where { $type <: not contains `null | undefined` } => `Maybe<$type>`,
|
|
4
|
-
// T | null | undefined -> Nullable<T>
|
|
5
|
-
`$type | null | undefined` where { $type <: not contains `null | undefined` } => `Nullable<$type>`,
|
|
6
|
-
// T | undefined -> Optional<T>
|
|
7
|
-
`$type | undefined` where { $type <: not contains `undefined` } => `Optional<$type>`
|
|
8
|
-
}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
or {
|
|
2
|
-
`async function $name($params) { $body }` where { $body <: not contains `try { $tryBody } catch ($err) { $catchBody }` } => `async function $name($params) { try { $body } catch (err) { // handle error } }`,
|
|
3
|
-
`(async function($params) { $body })` where { $body <: not contains `try { $tryBody } catch ($err) { $catchBody }` } => `(async function($params) { try { $body } catch (err) { // handle error } })`,
|
|
4
|
-
`(async ($params) => { $body })` where { $body <: not contains `try { $tryBody } catch ($err) { $catchBody }` } => `(async ($params) => { try { $body } catch (err) { // handle error } })`
|
|
5
|
-
}
|
package/dist/biome.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
//#region biome/index.ts
|
|
2
|
-
const resolveAsset = (asset) => decodeURIComponent(new URL(asset, import.meta.url).pathname);
|
|
3
|
-
const MaybePromiseRule = resolveAsset("./maybe_promise_rule.grit");
|
|
4
|
-
const OptionalRule = resolveAsset("./optional_rule.gritt");
|
|
5
|
-
const TryCatchAsyncRule = resolveAsset("./require-try-catch-async.grit");
|
|
6
|
-
//#endregion
|
|
7
|
-
export { MaybePromiseRule, OptionalRule, TryCatchAsyncRule };
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
or {
|
|
2
|
-
// 1. Replace Promise with MaybePromise in return types
|
|
3
|
-
`: Promise<$type>` => `: MaybePromise<$type>`,
|
|
4
|
-
|
|
5
|
-
// 2. Wrap return values in try blocks with { value, isError: false }
|
|
6
|
-
`try { $tryBody } catch ($err) { $catchBody }` where {
|
|
7
|
-
$tryBody <: contains `return $value` and {
|
|
8
|
-
$value <: not contains `{ isError: $isErrorValue }`
|
|
9
|
-
}
|
|
10
|
-
} => `try { $tryBody <: replace `return $value` => `return { value: $value, isError: false }` } catch ($err) { $catchBody }`,
|
|
11
|
-
|
|
12
|
-
// 3. Handle empty return statements in try blocks
|
|
13
|
-
`try { $tryBody } catch ($err) { $catchBody }` where {
|
|
14
|
-
$tryBody <: contains `return;`
|
|
15
|
-
} => `try { $tryBody <: replace `return;` => `return { value: undefined, isError: false };` } catch ($err) { $catchBody }`,
|
package/dist/maybe_rule.grit
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
or {
|
|
2
|
-
// T | null -> Maybe<T>
|
|
3
|
-
`$type | null` where {
|
|
4
|
-
$type <: not contains `null | undefined`
|
|
5
|
-
} => `Maybe<$type>`,
|
|
6
|
-
|
|
7
|
-
// T | null | undefined -> Nullable<T>
|
|
8
|
-
`$type | null | undefined` where {
|
|
9
|
-
$type <: not contains `null | undefined`
|
|
10
|
-
} => `Nullable<$type>`
|
|
11
|
-
}
|
package/dist/nullable_rule.grit
DELETED
package/dist/optional_rule.gritt
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
or {
|
|
2
|
-
// T | null -> Maybe<T>
|
|
3
|
-
`$type | null` where { $type <: not contains `null | undefined` } => `Maybe<$type>`,
|
|
4
|
-
// T | null | undefined -> Nullable<T>
|
|
5
|
-
`$type | null | undefined` where { $type <: not contains `null | undefined` } => `Nullable<$type>`,
|
|
6
|
-
// T | undefined -> Optional<T>
|
|
7
|
-
`$type | undefined` where { $type <: not contains `undefined` } => `Optional<$type>`
|
|
8
|
-
}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
or {
|
|
2
|
-
`async function $name($params) { $body }` where { $body <: not contains `try { $tryBody } catch ($err) { $catchBody }` } => `async function $name($params) { try { $body } catch (err) { // handle error } }`,
|
|
3
|
-
`(async function($params) { $body })` where { $body <: not contains `try { $tryBody } catch ($err) { $catchBody }` } => `(async function($params) { try { $body } catch (err) { // handle error } })`,
|
|
4
|
-
`(async ($params) => { $body })` where { $body <: not contains `try { $tryBody } catch ($err) { $catchBody }` } => `(async ($params) => { try { $body } catch (err) { // handle error } })`
|
|
5
|
-
}
|