@murky-web/typebuddy 1.0.0 → 1.1.1
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 +22 -0
- package/dist/index.js +2 -2
- package/dist/rules/async_rule.js +64 -5
- package/dist/rules/maybe_promise_rule.js +113 -9
- package/dist/src/type_helper.js +15 -1
- package/jsr.json +1 -1
- package/package.json +1 -1
- package/rules/async_rule.ts +229 -76
- package/rules/maybe_promise_rule.ts +478 -190
- package/src/type_helper.ts +287 -262
package/rules/async_rule.ts
CHANGED
|
@@ -1,98 +1,251 @@
|
|
|
1
1
|
type AstNode = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
type: string;
|
|
3
|
+
body?: AstNode | AstNode[];
|
|
4
|
+
async?: boolean;
|
|
5
|
+
parent?: AstNode;
|
|
6
|
+
range?: [number, number];
|
|
7
|
+
[key: string]: unknown;
|
|
7
8
|
};
|
|
8
9
|
|
|
9
10
|
type RuleContext = {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
getSourceCode(): { getText(node: unknown): string };
|
|
12
|
+
report(descriptor: {
|
|
13
|
+
node: unknown;
|
|
14
|
+
messageId: string;
|
|
15
|
+
fix(fixer: {
|
|
16
|
+
replaceText(node: unknown, text: string): unknown;
|
|
17
|
+
insertTextBeforeRange(
|
|
18
|
+
range: [number, number],
|
|
19
|
+
text: string,
|
|
20
|
+
): unknown;
|
|
21
|
+
}): unknown;
|
|
22
|
+
}): void;
|
|
20
23
|
};
|
|
21
24
|
|
|
25
|
+
type RuleFixer = {
|
|
26
|
+
replaceText(node: unknown, text: string): unknown;
|
|
27
|
+
insertTextBeforeRange(range: [number, number], text: string): unknown;
|
|
28
|
+
};
|
|
29
|
+
|
|
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
|
+
|
|
22
123
|
function hasTryCatch(nodes: AstNode[]): boolean {
|
|
23
|
-
|
|
124
|
+
return nodes.some((node) => {
|
|
125
|
+
return node.type === "TryStatement";
|
|
126
|
+
});
|
|
24
127
|
}
|
|
25
128
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
function getIndentation(node: AstNode): string {
|
|
31
|
-
const lines = sourceCode.getText(node).split("\n");
|
|
32
|
-
const firstLine = lines[0];
|
|
33
|
-
const match = /^\s*/.exec(firstLine);
|
|
34
|
-
return match ? match[0] : "";
|
|
129
|
+
function isCallArgumentCallback(node: AstNode): boolean {
|
|
130
|
+
const parent = node.parent;
|
|
131
|
+
if (!parent) {
|
|
132
|
+
return false;
|
|
35
133
|
}
|
|
36
134
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
.
|
|
47
|
-
|
|
48
|
-
|
|
135
|
+
if (parent.type !== "CallExpression" && parent.type !== "NewExpression") {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return Array.isArray(parent.arguments) && parent.arguments.includes(node);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const rule = {
|
|
143
|
+
create(context: RuleContext) {
|
|
144
|
+
const sourceCode = context.getSourceCode();
|
|
145
|
+
let scheduledErrImport = false;
|
|
146
|
+
|
|
147
|
+
function ensureErrImportFixes(
|
|
148
|
+
node: AstNode,
|
|
149
|
+
fixer: RuleFixer,
|
|
150
|
+
): unknown[] {
|
|
151
|
+
if (scheduledErrImport) {
|
|
152
|
+
return [];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const program = getProgram(node);
|
|
156
|
+
if (!program || hasTypeBuddyHelperImport(program, "err")) {
|
|
157
|
+
return [];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const insertRange = getTypeBuddyImportInsertRange(program);
|
|
161
|
+
if (!insertRange) {
|
|
162
|
+
return [];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
scheduledErrImport = true;
|
|
166
|
+
|
|
167
|
+
const hasImports = insertRange[0] !== 0;
|
|
168
|
+
const importText = hasImports
|
|
169
|
+
? '\nimport { err } from "@murky-web/typebuddy";'
|
|
170
|
+
: 'import { err } from "@murky-web/typebuddy";\n';
|
|
171
|
+
|
|
172
|
+
return [fixer.insertTextBeforeRange(insertRange, importText)];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function getIndentation(node: AstNode): string {
|
|
176
|
+
const lines = sourceCode.getText(node).split("\n");
|
|
177
|
+
const firstLine = lines[0];
|
|
178
|
+
const match = /^\s*/.exec(firstLine);
|
|
179
|
+
return match ? match[0] : "";
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function wrapInTryCatch(node: AstNode): string {
|
|
183
|
+
const body = Array.isArray(node.body) ? node.body : [];
|
|
184
|
+
const indent = getIndentation(node);
|
|
185
|
+
const innerIndent = `${indent} `;
|
|
186
|
+
const bodyText = body
|
|
187
|
+
.map((statement) => {
|
|
188
|
+
const text = sourceCode.getText(statement);
|
|
189
|
+
return `${innerIndent}${text.replaceAll(/^\s*/gm, "")}`;
|
|
190
|
+
})
|
|
191
|
+
.join("\n");
|
|
192
|
+
|
|
193
|
+
return `{
|
|
49
194
|
${indent}try {
|
|
50
195
|
${bodyText}
|
|
51
|
-
${indent}} catch
|
|
52
|
-
${innerIndent}return
|
|
196
|
+
${indent}} catch {
|
|
197
|
+
${innerIndent}return err();
|
|
53
198
|
${indent}}
|
|
54
199
|
}`;
|
|
55
|
-
|
|
200
|
+
}
|
|
56
201
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return fixer.replaceText(bodyNode, wrapInTryCatch(bodyNode));
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
}
|
|
202
|
+
function checkFunction(node: AstNode) {
|
|
203
|
+
if (node.async !== true) return;
|
|
204
|
+
if (isCallArgumentCallback(node)) return;
|
|
205
|
+
const bodyNode = node.body;
|
|
206
|
+
if (
|
|
207
|
+
!bodyNode ||
|
|
208
|
+
Array.isArray(bodyNode) ||
|
|
209
|
+
bodyNode.type !== "BlockStatement"
|
|
210
|
+
) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const body = Array.isArray(bodyNode.body) ? bodyNode.body : [];
|
|
215
|
+
if (hasTryCatch(body)) return;
|
|
75
216
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
217
|
+
context.report({
|
|
218
|
+
node,
|
|
219
|
+
messageId: "missingTryCatch",
|
|
220
|
+
fix(fixer) {
|
|
221
|
+
return [
|
|
222
|
+
...ensureErrImportFixes(node, fixer),
|
|
223
|
+
fixer.replaceText(bodyNode, wrapInTryCatch(bodyNode)),
|
|
224
|
+
];
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
FunctionDeclaration: checkFunction,
|
|
231
|
+
FunctionExpression: checkFunction,
|
|
232
|
+
ArrowFunctionExpression: checkFunction,
|
|
233
|
+
};
|
|
88
234
|
},
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
235
|
+
defaultOptions: [],
|
|
236
|
+
meta: {
|
|
237
|
+
type: "suggestion",
|
|
238
|
+
docs: {
|
|
239
|
+
description:
|
|
240
|
+
"Async functions should have a try-catch block returning an error result.",
|
|
241
|
+
},
|
|
242
|
+
fixable: "code",
|
|
243
|
+
schema: [],
|
|
244
|
+
messages: {
|
|
245
|
+
missingTryCatch:
|
|
246
|
+
"Async functions must have a try-catch block returning an error result.",
|
|
247
|
+
},
|
|
94
248
|
},
|
|
95
|
-
},
|
|
96
249
|
};
|
|
97
250
|
|
|
98
251
|
export { rule as requireTryCatchAsyncRule };
|