@murky-web/typebuddy 1.3.0 → 1.5.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/README.md +13 -0
- package/dist/rules/async_rule.js +44 -68
- package/dist/rules/deliberate_throw.js +6 -24
- package/dist/rules/maybe_promise_rule.js +129 -65
- 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 +2 -2
- package/package.json +4 -6
- package/rules/async_rule.ts +115 -143
- package/rules/deliberate_throw.ts +7 -45
- package/rules/maybe_promise_rule.ts +232 -190
- package/rules/optional_rule.ts +5 -1
- package/rules/own_subtree.ts +70 -0
- package/rules/typebuddy_import.ts +134 -0
- package/src/index.ts +1 -1
- package/dist/globals.js +0 -0
- /package/{globals.ts → globals.d.ts} +0 -0
package/rules/async_rule.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { throwsDeliberately } from "./deliberate_throw.js";
|
|
2
|
+
import { getTypeBuddyImportInsertion } from "./typebuddy_import.js";
|
|
2
3
|
|
|
3
4
|
type AstNode = {
|
|
4
5
|
type: string;
|
|
@@ -29,99 +30,6 @@ type RuleFixer = {
|
|
|
29
30
|
insertTextBeforeRange(range: [number, number], text: string): unknown;
|
|
30
31
|
};
|
|
31
32
|
|
|
32
|
-
function isNode(value: unknown): value is AstNode {
|
|
33
|
-
return typeof value === "object" && value !== null && "type" in value;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function getProgram(node: AstNode): AstNode | null {
|
|
37
|
-
let current: AstNode | undefined = node;
|
|
38
|
-
|
|
39
|
-
while (current?.parent) {
|
|
40
|
-
current = current.parent;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return current?.type === "Program" ? current : null;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function isIdentifierNamed(node: unknown, name: string): boolean {
|
|
47
|
-
return isNode(node) && node.type === "Identifier" && node["name"] === name;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function isImportDeclaration(node: unknown): node is AstNode {
|
|
51
|
-
return isNode(node) && node.type === "ImportDeclaration";
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function getStringLiteralValue(node: unknown): string | null {
|
|
55
|
-
if (!isNode(node)) {
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (
|
|
60
|
-
(node.type === "Literal" || node.type === "StringLiteral") &&
|
|
61
|
-
typeof node["value"] === "string"
|
|
62
|
-
) {
|
|
63
|
-
return node["value"];
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function getProgramBody(node: AstNode): AstNode[] {
|
|
70
|
-
if (!Array.isArray(node.body)) {
|
|
71
|
-
return [];
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return node.body.filter(isNode);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function hasTypeBuddyHelperImport(program: AstNode, helperName: "err") {
|
|
78
|
-
return getProgramBody(program).some((statement) => {
|
|
79
|
-
if (!isImportDeclaration(statement)) {
|
|
80
|
-
return false;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (
|
|
84
|
-
getStringLiteralValue(statement["source"]) !== "@murky-web/typebuddy"
|
|
85
|
-
) {
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (statement["importKind"] === "type") {
|
|
90
|
-
return false;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const specifiers = Array.isArray(statement["specifiers"])
|
|
94
|
-
? statement["specifiers"]
|
|
95
|
-
: [];
|
|
96
|
-
|
|
97
|
-
return specifiers.some((specifier) => {
|
|
98
|
-
return (
|
|
99
|
-
isNode(specifier) &&
|
|
100
|
-
specifier.type === "ImportSpecifier" &&
|
|
101
|
-
isIdentifierNamed(specifier["local"], helperName)
|
|
102
|
-
);
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function getTypeBuddyImportInsertRange(
|
|
108
|
-
program: AstNode,
|
|
109
|
-
): [number, number] | null {
|
|
110
|
-
const body = getProgramBody(program);
|
|
111
|
-
const imports = body.filter(isImportDeclaration);
|
|
112
|
-
const anchor = imports.at(-1) ?? body[0] ?? program;
|
|
113
|
-
|
|
114
|
-
if (!Array.isArray(anchor.range) || anchor.range.length < 2) {
|
|
115
|
-
return null;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (imports.length > 0) {
|
|
119
|
-
return [anchor.range[1], anchor.range[1]];
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return [anchor.range[0], anchor.range[0]];
|
|
123
|
-
}
|
|
124
|
-
|
|
125
33
|
function hasTryCatch(nodes: AstNode[]): boolean {
|
|
126
34
|
return nodes.some((node) => {
|
|
127
35
|
return node.type === "TryStatement";
|
|
@@ -138,7 +46,9 @@ function isCallArgumentCallback(node: AstNode): boolean {
|
|
|
138
46
|
return false;
|
|
139
47
|
}
|
|
140
48
|
|
|
141
|
-
return
|
|
49
|
+
return (
|
|
50
|
+
Array.isArray(parent["arguments"]) && parent["arguments"].includes(node)
|
|
51
|
+
);
|
|
142
52
|
}
|
|
143
53
|
|
|
144
54
|
const rule = {
|
|
@@ -154,81 +64,143 @@ const rule = {
|
|
|
154
64
|
return [];
|
|
155
65
|
}
|
|
156
66
|
|
|
157
|
-
const
|
|
158
|
-
if (!
|
|
159
|
-
return [];
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const insertRange = getTypeBuddyImportInsertRange(program);
|
|
163
|
-
if (!insertRange) {
|
|
67
|
+
const insertion = getTypeBuddyImportInsertion(node, "err");
|
|
68
|
+
if (!insertion) {
|
|
164
69
|
return [];
|
|
165
70
|
}
|
|
166
71
|
|
|
167
72
|
scheduledErrImport = true;
|
|
168
73
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
: 'import { err } from "@murky-web/typebuddy";\n';
|
|
173
|
-
|
|
174
|
-
return [fixer.insertTextBeforeRange(insertRange, importText)];
|
|
74
|
+
return [
|
|
75
|
+
fixer.insertTextBeforeRange(insertion.range, insertion.text),
|
|
76
|
+
];
|
|
175
77
|
}
|
|
176
78
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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
|
+
];
|
|
184
102
|
}
|
|
185
103
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
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
|
+
}
|
|
196
142
|
|
|
197
|
-
return
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
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
|
+
];
|
|
204
153
|
}
|
|
205
154
|
|
|
206
155
|
function checkFunction(node: AstNode) {
|
|
207
156
|
if (node.async !== true) return;
|
|
208
157
|
if (isCallArgumentCallback(node)) return;
|
|
209
158
|
const bodyNode = node.body;
|
|
210
|
-
if (
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
)
|
|
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
|
+
});
|
|
215
188
|
return;
|
|
216
189
|
}
|
|
217
190
|
|
|
218
191
|
const body = Array.isArray(bodyNode.body) ? bodyNode.body : [];
|
|
219
192
|
if (hasTryCatch(body)) return;
|
|
220
|
-
// A function that throws has already decided its failure is not a
|
|
221
|
-
// value. Wrapping it in try/catch would swallow that decision.
|
|
222
|
-
if (throwsDeliberately(bodyNode)) return;
|
|
223
193
|
|
|
224
194
|
context.report({
|
|
225
195
|
node,
|
|
226
196
|
messageId: "missingTryCatch",
|
|
227
197
|
fix(fixer) {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
198
|
+
const wrapFixes = wrapBlockFixes(bodyNode, fixer);
|
|
199
|
+
if (!wrapFixes) {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return [...ensureErrImportFixes(node, fixer), ...wrapFixes];
|
|
232
204
|
},
|
|
233
205
|
});
|
|
234
206
|
}
|
|
@@ -1,17 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
type: string;
|
|
3
|
-
[key: string]: unknown;
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
const FUNCTION_TYPES = new Set([
|
|
7
|
-
"ArrowFunctionExpression",
|
|
8
|
-
"FunctionDeclaration",
|
|
9
|
-
"FunctionExpression",
|
|
10
|
-
]);
|
|
11
|
-
|
|
12
|
-
function isNode(value: unknown): value is AstNode {
|
|
13
|
-
return typeof value === "object" && value !== null && "type" in value;
|
|
14
|
-
}
|
|
1
|
+
import { walkOwnSubtree } from "./own_subtree.js";
|
|
15
2
|
|
|
16
3
|
/**
|
|
17
4
|
* Whether a function body throws on purpose.
|
|
@@ -34,40 +21,15 @@ function isNode(value: unknown): value is AstNode {
|
|
|
34
21
|
* @returns {boolean} True when the body throws outside any nested function.
|
|
35
22
|
*/
|
|
36
23
|
function throwsDeliberately(body: unknown): boolean {
|
|
37
|
-
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const pending: AstNode[] = [body];
|
|
42
|
-
while (pending.length > 0) {
|
|
43
|
-
// `pop` on a non-empty array always yields a node; the length check above is
|
|
44
|
-
// the loop condition, but `noUncheckedIndexedAccess` cannot see that.
|
|
45
|
-
const current = pending.pop();
|
|
46
|
-
if (current === undefined) {
|
|
47
|
-
break;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (current.type === "ThrowStatement") {
|
|
51
|
-
return true;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
for (const [key, value] of Object.entries(current)) {
|
|
55
|
-
// `parent` points back up the tree; following it would never terminate.
|
|
56
|
-
if (key === "parent") {
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
24
|
+
let found = false;
|
|
59
25
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
pending.push(candidate);
|
|
66
|
-
}
|
|
26
|
+
walkOwnSubtree(body, (node) => {
|
|
27
|
+
if (node.type === "ThrowStatement") {
|
|
28
|
+
found = true;
|
|
67
29
|
}
|
|
68
|
-
}
|
|
30
|
+
});
|
|
69
31
|
|
|
70
|
-
return
|
|
32
|
+
return found;
|
|
71
33
|
}
|
|
72
34
|
|
|
73
35
|
export { throwsDeliberately };
|