@murky-web/typebuddy 2.0.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/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 +1 -1
- package/package.json +1 -1
- package/rules/async_rule.ts +7 -3
- package/rules/maybe_promise_rule.ts +11 -7
- package/rules/use_server.ts +116 -0
- package/src/result.ts +9 -8
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
package/package.json
CHANGED
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/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
|
|