@murky-web/typebuddy 1.2.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/collections.js +28 -0
- package/dist/empty.js +53 -0
- package/dist/guards.js +132 -0
- package/dist/ids.js +29 -0
- package/dist/index.js +6 -1
- package/dist/parse.js +81 -0
- package/dist/result.js +38 -0
- package/dist/rules/async_rule.js +45 -67
- package/dist/rules/deliberate_throw.js +31 -0
- package/dist/rules/maybe_promise_rule.js +131 -64
- package/dist/rules/optional_rule.js +1 -1
- package/dist/rules/own_subtree.js +45 -0
- package/dist/rules/typebuddy_import.js +79 -0
- package/jsr.json +1 -1
- package/package.json +35 -6
- package/rules/async_rule.ts +118 -141
- package/rules/deliberate_throw.ts +35 -0
- package/rules/maybe_promise_rule.ts +235 -187
- package/rules/optional_rule.ts +5 -1
- package/rules/own_subtree.ts +70 -0
- package/rules/typebuddy_import.ts +134 -0
- package/src/collections.ts +36 -0
- package/src/empty.ts +76 -0
- package/src/guards.ts +341 -0
- package/src/ids.ts +34 -0
- package/src/index.ts +2 -0
- package/src/parse.ts +215 -0
- package/src/result.ts +57 -0
- package/src/type_helper.ts +10 -666
- package/src/types/index.ts +9 -1
- package/src/types/maybe_promise.ts +24 -2
- package/dist/src/type_helper.js +0 -308
package/rules/async_rule.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { throwsDeliberately } from "./deliberate_throw.js";
|
|
2
|
+
import { getTypeBuddyImportInsertion } from "./typebuddy_import.js";
|
|
3
|
+
|
|
1
4
|
type AstNode = {
|
|
2
5
|
type: string;
|
|
3
6
|
body?: AstNode | AstNode[];
|
|
@@ -27,99 +30,6 @@ type RuleFixer = {
|
|
|
27
30
|
insertTextBeforeRange(range: [number, number], text: string): unknown;
|
|
28
31
|
};
|
|
29
32
|
|
|
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
|
-
|
|
123
33
|
function hasTryCatch(nodes: AstNode[]): boolean {
|
|
124
34
|
return nodes.some((node) => {
|
|
125
35
|
return node.type === "TryStatement";
|
|
@@ -136,7 +46,9 @@ function isCallArgumentCallback(node: AstNode): boolean {
|
|
|
136
46
|
return false;
|
|
137
47
|
}
|
|
138
48
|
|
|
139
|
-
return
|
|
49
|
+
return (
|
|
50
|
+
Array.isArray(parent["arguments"]) && parent["arguments"].includes(node)
|
|
51
|
+
);
|
|
140
52
|
}
|
|
141
53
|
|
|
142
54
|
const rule = {
|
|
@@ -152,64 +64,127 @@ const rule = {
|
|
|
152
64
|
return [];
|
|
153
65
|
}
|
|
154
66
|
|
|
155
|
-
const
|
|
156
|
-
if (!
|
|
157
|
-
return [];
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const insertRange = getTypeBuddyImportInsertRange(program);
|
|
161
|
-
if (!insertRange) {
|
|
67
|
+
const insertion = getTypeBuddyImportInsertion(node, "err");
|
|
68
|
+
if (!insertion) {
|
|
162
69
|
return [];
|
|
163
70
|
}
|
|
164
71
|
|
|
165
72
|
scheduledErrImport = true;
|
|
166
73
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
: 'import { err } from "@murky-web/typebuddy";\n';
|
|
171
|
-
|
|
172
|
-
return [fixer.insertTextBeforeRange(insertRange, importText)];
|
|
74
|
+
return [
|
|
75
|
+
fixer.insertTextBeforeRange(insertion.range, insertion.text),
|
|
76
|
+
];
|
|
173
77
|
}
|
|
174
78
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
79
|
+
// Two zero-width insertions leave the body's own text untouched, byte
|
|
80
|
+
// for byte. Rebuilding it statement by statement used to strip leading
|
|
81
|
+
// whitespace on every line — including lines inside template literals,
|
|
82
|
+
// which silently changed string values.
|
|
83
|
+
function wrapBlockFixes(
|
|
84
|
+
bodyNode: AstNode,
|
|
85
|
+
fixer: RuleFixer,
|
|
86
|
+
): unknown[] | null {
|
|
87
|
+
const range = bodyNode.range;
|
|
88
|
+
if (!Array.isArray(range) || range.length < 2) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return [
|
|
93
|
+
fixer.insertTextBeforeRange(
|
|
94
|
+
[range[0] + 1, range[0] + 1],
|
|
95
|
+
"\ntry {",
|
|
96
|
+
),
|
|
97
|
+
fixer.insertTextBeforeRange(
|
|
98
|
+
[range[1] - 1, range[1] - 1],
|
|
99
|
+
"} catch {\nreturn err();\n}\n",
|
|
100
|
+
),
|
|
101
|
+
];
|
|
182
102
|
}
|
|
183
103
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
104
|
+
/**
|
|
105
|
+
* Wrap an expression-bodied arrow's value in a block with try/catch.
|
|
106
|
+
*
|
|
107
|
+
* The body node's range stops inside any wrapping parentheses, so
|
|
108
|
+
* replacing it in `async () => ({ a: 1 })` would leave the parentheses
|
|
109
|
+
* around a block and produce `({ try { ... } })`. Walking left over
|
|
110
|
+
* those parentheses first, then inserting on either side, keeps the
|
|
111
|
+
* expression itself untouched — parentheses and all.
|
|
112
|
+
*/
|
|
113
|
+
function wrapExpressionBodyFixes(
|
|
114
|
+
node: AstNode,
|
|
115
|
+
bodyNode: AstNode,
|
|
116
|
+
fixer: RuleFixer,
|
|
117
|
+
): unknown[] | null {
|
|
118
|
+
const arrowRange = node.range;
|
|
119
|
+
const bodyRange = bodyNode.range;
|
|
120
|
+
if (
|
|
121
|
+
!Array.isArray(arrowRange) ||
|
|
122
|
+
arrowRange.length < 2 ||
|
|
123
|
+
!Array.isArray(bodyRange) ||
|
|
124
|
+
bodyRange.length < 2
|
|
125
|
+
) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let prefix = sourceCode
|
|
130
|
+
.getText(node)
|
|
131
|
+
.slice(0, bodyRange[0] - arrowRange[0]);
|
|
132
|
+
while (prefix.trimEnd().endsWith("(")) {
|
|
133
|
+
prefix = prefix.trimEnd().slice(0, -1);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const start = arrowRange[0] + prefix.length;
|
|
137
|
+
// The arrow's own end already sits past any closing parenthesis.
|
|
138
|
+
const end = arrowRange[1];
|
|
139
|
+
if (start >= end) {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return [
|
|
144
|
+
fixer.insertTextBeforeRange(
|
|
145
|
+
[start, start],
|
|
146
|
+
"{\ntry {\nreturn ",
|
|
147
|
+
),
|
|
148
|
+
fixer.insertTextBeforeRange(
|
|
149
|
+
[end, end],
|
|
150
|
+
";\n} catch {\nreturn err();\n}\n}",
|
|
151
|
+
),
|
|
152
|
+
];
|
|
202
153
|
}
|
|
203
154
|
|
|
204
155
|
function checkFunction(node: AstNode) {
|
|
205
156
|
if (node.async !== true) return;
|
|
206
157
|
if (isCallArgumentCallback(node)) return;
|
|
207
158
|
const bodyNode = node.body;
|
|
208
|
-
if (
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
)
|
|
159
|
+
if (!bodyNode || Array.isArray(bodyNode)) return;
|
|
160
|
+
|
|
161
|
+
// A function that throws has already decided its failure is not a
|
|
162
|
+
// value. Wrapping it in try/catch would swallow that decision.
|
|
163
|
+
if (throwsDeliberately(bodyNode)) return;
|
|
164
|
+
|
|
165
|
+
// An expression-bodied arrow (`async () => fetch(url)`) returns the
|
|
166
|
+
// one value the try/catch exists to guard, so it needs a block
|
|
167
|
+
// before it can get one.
|
|
168
|
+
if (bodyNode.type !== "BlockStatement") {
|
|
169
|
+
context.report({
|
|
170
|
+
node,
|
|
171
|
+
messageId: "missingTryCatch",
|
|
172
|
+
fix(fixer) {
|
|
173
|
+
const wrapFixes = wrapExpressionBodyFixes(
|
|
174
|
+
node,
|
|
175
|
+
bodyNode,
|
|
176
|
+
fixer,
|
|
177
|
+
);
|
|
178
|
+
if (!wrapFixes) {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return [
|
|
183
|
+
...ensureErrImportFixes(node, fixer),
|
|
184
|
+
...wrapFixes,
|
|
185
|
+
];
|
|
186
|
+
},
|
|
187
|
+
});
|
|
213
188
|
return;
|
|
214
189
|
}
|
|
215
190
|
|
|
@@ -220,10 +195,12 @@ ${indent}}
|
|
|
220
195
|
node,
|
|
221
196
|
messageId: "missingTryCatch",
|
|
222
197
|
fix(fixer) {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
198
|
+
const wrapFixes = wrapBlockFixes(bodyNode, fixer);
|
|
199
|
+
if (!wrapFixes) {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return [...ensureErrImportFixes(node, fixer), ...wrapFixes];
|
|
227
204
|
},
|
|
228
205
|
});
|
|
229
206
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { walkOwnSubtree } from "./own_subtree.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Whether a function body throws on purpose.
|
|
5
|
+
*
|
|
6
|
+
* A `throw` in a function's own body is the author saying this failure is not a
|
|
7
|
+
* value to hand back. A missing manifest for a route the app is navigating to,
|
|
8
|
+
* an unreachable branch, a broken invariant — those should be loud. Turning them
|
|
9
|
+
* into `err()` converts a bug into a silently handled outcome, and the caller
|
|
10
|
+
* degrades politely instead of surfacing that something is wrong.
|
|
11
|
+
*
|
|
12
|
+
* So both async rules leave such a function alone. The rules exist to push
|
|
13
|
+
* *recoverable* failures into the return type, not to abolish throwing.
|
|
14
|
+
*
|
|
15
|
+
* Two boundaries matter. A throw inside a nested function belongs to that
|
|
16
|
+
* function, so the walk does not descend into one. A throw inside `catch` still
|
|
17
|
+
* counts: a rethrow is as deliberate as an original throw, and it is how code
|
|
18
|
+
* says "this one is not mine to handle".
|
|
19
|
+
*
|
|
20
|
+
* @param {unknown} body - The function body node to inspect.
|
|
21
|
+
* @returns {boolean} True when the body throws outside any nested function.
|
|
22
|
+
*/
|
|
23
|
+
function throwsDeliberately(body: unknown): boolean {
|
|
24
|
+
let found = false;
|
|
25
|
+
|
|
26
|
+
walkOwnSubtree(body, (node) => {
|
|
27
|
+
if (node.type === "ThrowStatement") {
|
|
28
|
+
found = true;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
return found;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { throwsDeliberately };
|