@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
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { throwsDeliberately } from "./deliberate_throw.js";
|
|
2
|
+
import { getTypeBuddyImportInsertion } from "./typebuddy_import.js";
|
|
3
|
+
import { walkOwnSubtree } from "./own_subtree.js";
|
|
4
|
+
|
|
1
5
|
type AstNode = {
|
|
2
6
|
type: string;
|
|
3
7
|
parent?: AstNode;
|
|
@@ -9,7 +13,7 @@ type RuleContext = {
|
|
|
9
13
|
report(descriptor: {
|
|
10
14
|
node: unknown;
|
|
11
15
|
messageId: string;
|
|
12
|
-
fix(fixer: {
|
|
16
|
+
fix?(fixer: {
|
|
13
17
|
replaceText(node: unknown, text: string): unknown;
|
|
14
18
|
insertTextBeforeRange(
|
|
15
19
|
range: [number, number],
|
|
@@ -46,48 +50,34 @@ function isCallArgumentCallback(node: AstNode): boolean {
|
|
|
46
50
|
return false;
|
|
47
51
|
}
|
|
48
52
|
|
|
49
|
-
return
|
|
53
|
+
return (
|
|
54
|
+
Array.isArray(parent["arguments"]) && parent["arguments"].includes(node)
|
|
55
|
+
);
|
|
50
56
|
}
|
|
51
57
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Return-type names whose type argument is the value an async function settles
|
|
60
|
+
* on. `AsyncResult` is the preferred spelling of `MaybePromise`, so a function
|
|
61
|
+
* annotated with it has to be read the same way — otherwise the rule goes blind
|
|
62
|
+
* on exactly the code the package tells people to write.
|
|
63
|
+
*/
|
|
64
|
+
const AWAITED_TYPE_NAMES = new Set(["AsyncResult", "MaybePromise", "Promise"]);
|
|
55
65
|
|
|
56
|
-
function
|
|
66
|
+
function isAmbient(node: AstNode): boolean {
|
|
57
67
|
let current: AstNode | undefined = node;
|
|
58
68
|
|
|
59
|
-
while (current
|
|
69
|
+
while (current) {
|
|
70
|
+
if (current["declare"] === true) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
60
73
|
current = current.parent;
|
|
61
74
|
}
|
|
62
75
|
|
|
63
|
-
return
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function isImportDeclaration(node: unknown): node is AstNode {
|
|
67
|
-
return isNode(node) && node.type === "ImportDeclaration";
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function getStringLiteralValue(node: unknown): string | null {
|
|
71
|
-
if (!isNode(node)) {
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (
|
|
76
|
-
(node.type === "Literal" || node.type === "StringLiteral") &&
|
|
77
|
-
typeof node["value"] === "string"
|
|
78
|
-
) {
|
|
79
|
-
return node["value"];
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return null;
|
|
76
|
+
return false;
|
|
83
77
|
}
|
|
84
78
|
|
|
85
|
-
function
|
|
86
|
-
|
|
87
|
-
return [];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return node["body"].filter(isNode);
|
|
79
|
+
function isIdentifierNamed(node: unknown, name: string): boolean {
|
|
80
|
+
return isNode(node) && node.type === "Identifier" && node["name"] === name;
|
|
91
81
|
}
|
|
92
82
|
|
|
93
83
|
function isResultHelperCall(node: unknown, name: "ok" | "err"): boolean {
|
|
@@ -146,61 +136,15 @@ function getObjectPropertyValue(
|
|
|
146
136
|
}
|
|
147
137
|
|
|
148
138
|
function isBooleanLiteral(node: unknown, expected: boolean): boolean {
|
|
149
|
-
return
|
|
139
|
+
return (
|
|
140
|
+
isNode(node) && node.type === "Literal" && node["value"] === expected
|
|
141
|
+
);
|
|
150
142
|
}
|
|
151
143
|
|
|
152
144
|
function isNullLiteral(node: unknown): boolean {
|
|
153
145
|
return isNode(node) && node.type === "Literal" && node["value"] === null;
|
|
154
146
|
}
|
|
155
147
|
|
|
156
|
-
function hasTypeBuddyHelperImport(program: AstNode, helperName: "ok" | "err") {
|
|
157
|
-
return getProgramBody(program).some((statement) => {
|
|
158
|
-
if (!isImportDeclaration(statement)) {
|
|
159
|
-
return false;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (
|
|
163
|
-
getStringLiteralValue(statement["source"]) !== "@murky-web/typebuddy"
|
|
164
|
-
) {
|
|
165
|
-
return false;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (statement["importKind"] === "type") {
|
|
169
|
-
return false;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const specifiers = Array.isArray(statement["specifiers"])
|
|
173
|
-
? statement["specifiers"]
|
|
174
|
-
: [];
|
|
175
|
-
|
|
176
|
-
return specifiers.some((specifier) => {
|
|
177
|
-
return (
|
|
178
|
-
isNode(specifier) &&
|
|
179
|
-
specifier.type === "ImportSpecifier" &&
|
|
180
|
-
isIdentifierNamed(specifier["local"], helperName)
|
|
181
|
-
);
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function getTypeBuddyImportInsertRange(
|
|
187
|
-
program: AstNode,
|
|
188
|
-
): [number, number] | null {
|
|
189
|
-
const body = getProgramBody(program);
|
|
190
|
-
const imports = body.filter(isImportDeclaration);
|
|
191
|
-
const anchor = imports.at(-1) ?? body[0] ?? program;
|
|
192
|
-
|
|
193
|
-
if (!Array.isArray(anchor["range"]) || anchor["range"].length < 2) {
|
|
194
|
-
return null;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
if (imports.length > 0) {
|
|
198
|
-
return [anchor["range"][1], anchor["range"][1]];
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
return [anchor["range"][0], anchor["range"][0]];
|
|
202
|
-
}
|
|
203
|
-
|
|
204
148
|
const rule = {
|
|
205
149
|
create(context: RuleContext) {
|
|
206
150
|
const sourceCode = context.getSourceCode();
|
|
@@ -214,7 +158,9 @@ const rule = {
|
|
|
214
158
|
const typeName = node["typeName"];
|
|
215
159
|
if (!isNode(typeName) || typeName.type !== "Identifier")
|
|
216
160
|
return null;
|
|
217
|
-
return typeof typeName["name"] === "string"
|
|
161
|
+
return typeof typeName["name"] === "string"
|
|
162
|
+
? typeName["name"]
|
|
163
|
+
: null;
|
|
218
164
|
}
|
|
219
165
|
|
|
220
166
|
function getTypeArgument(node: AstNode): AstNode | null {
|
|
@@ -225,8 +171,23 @@ const rule = {
|
|
|
225
171
|
return firstParam;
|
|
226
172
|
}
|
|
227
173
|
|
|
228
|
-
|
|
229
|
-
|
|
174
|
+
/**
|
|
175
|
+
* The awaited type behind an async function's return annotation.
|
|
176
|
+
*
|
|
177
|
+
* Every spelling counts. Matching only `Promise` meant the rule read the
|
|
178
|
+
* return type of code it had not migrated yet and went blind the moment
|
|
179
|
+
* `MaybePromise` was there — so a bare `return;` in a
|
|
180
|
+
* `MaybePromise<void>` function never became `return ok();`, while the
|
|
181
|
+
* identical `Promise<void>` function was fixed. It only ever worked
|
|
182
|
+
* because the rename and this lookup happen in the same pass, off the
|
|
183
|
+
* same AST.
|
|
184
|
+
*/
|
|
185
|
+
function getAwaitedTypeArgument(node: AstNode): AstNode | null {
|
|
186
|
+
const typeName = getTypeName(node);
|
|
187
|
+
if (typeName === null || !AWAITED_TYPE_NAMES.has(typeName)) {
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
|
|
230
191
|
return getTypeArgument(node);
|
|
231
192
|
}
|
|
232
193
|
|
|
@@ -239,29 +200,19 @@ const rule = {
|
|
|
239
200
|
return [];
|
|
240
201
|
}
|
|
241
202
|
|
|
242
|
-
const
|
|
243
|
-
if (!
|
|
244
|
-
return [];
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
const insertRange = getTypeBuddyImportInsertRange(program);
|
|
248
|
-
if (!insertRange) {
|
|
203
|
+
const insertion = getTypeBuddyImportInsertion(node, helperName);
|
|
204
|
+
if (!insertion) {
|
|
249
205
|
return [];
|
|
250
206
|
}
|
|
251
207
|
|
|
252
208
|
scheduledHelperImports.add(helperName);
|
|
253
209
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
: `import { ${helperName} } from "@murky-web/typebuddy";\n`;
|
|
258
|
-
|
|
259
|
-
return [fixer.insertTextBeforeRange(insertRange, importText)];
|
|
210
|
+
return [
|
|
211
|
+
fixer.insertTextBeforeRange(insertion.range, insertion.text),
|
|
212
|
+
];
|
|
260
213
|
}
|
|
261
214
|
|
|
262
|
-
function
|
|
263
|
-
if (!isAsyncFunction(node)) return;
|
|
264
|
-
if (isCallArgumentCallback(node)) return;
|
|
215
|
+
function reportPromiseReturnType(node: AstNode) {
|
|
265
216
|
if (!isNode(node["returnType"])) return;
|
|
266
217
|
|
|
267
218
|
const typeAnnotation = node["returnType"]["typeAnnotation"];
|
|
@@ -278,6 +229,33 @@ const rule = {
|
|
|
278
229
|
});
|
|
279
230
|
}
|
|
280
231
|
|
|
232
|
+
function checkReturnType(node: AstNode) {
|
|
233
|
+
if (!isAsyncFunction(node)) return;
|
|
234
|
+
if (isCallArgumentCallback(node)) return;
|
|
235
|
+
// Same exemption as `require-try-catch`: a deliberate throw is not a
|
|
236
|
+
// result waiting to be wrapped.
|
|
237
|
+
if (throwsDeliberately(node["body"])) return;
|
|
238
|
+
|
|
239
|
+
reportPromiseReturnType(node);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* A type-level signature — an interface method, a function type, an
|
|
244
|
+
* overload — can never carry `async`, so routing it through
|
|
245
|
+
* `checkReturnType` meant these three visitors never fired at all.
|
|
246
|
+
* `Promise<T>` in a signature is the same contract the rule rewrites
|
|
247
|
+
* everywhere else, so it gets reported here without the async gate.
|
|
248
|
+
*
|
|
249
|
+
* Ambient declarations are the exception: `declare` describes code the
|
|
250
|
+
* project does not own, and rewriting a third-party framework's
|
|
251
|
+
* callback shape to `MaybePromise` would be a lie about that API.
|
|
252
|
+
*/
|
|
253
|
+
function checkSignatureReturnType(node: AstNode) {
|
|
254
|
+
if (isAmbient(node)) return;
|
|
255
|
+
|
|
256
|
+
reportPromiseReturnType(node);
|
|
257
|
+
}
|
|
258
|
+
|
|
281
259
|
function wrapReturnValue(
|
|
282
260
|
node: AstNode,
|
|
283
261
|
isAsync: boolean,
|
|
@@ -378,6 +356,95 @@ const rule = {
|
|
|
378
356
|
}
|
|
379
357
|
}
|
|
380
358
|
|
|
359
|
+
function collectOwnReturns(root: unknown): AstNode[] {
|
|
360
|
+
const returns: AstNode[] = [];
|
|
361
|
+
|
|
362
|
+
walkOwnSubtree(root, (candidate) => {
|
|
363
|
+
if (candidate.type === "TryStatement") {
|
|
364
|
+
return false;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (candidate.type === "ReturnStatement") {
|
|
368
|
+
returns.push(candidate);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return true;
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
return returns;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function isSettledFailure(argument: unknown): boolean {
|
|
378
|
+
return (
|
|
379
|
+
(isNode(argument) &&
|
|
380
|
+
argument.type === "Identifier" &&
|
|
381
|
+
argument["name"] === "FAILED_PROMISE") ||
|
|
382
|
+
isResultHelperCall(argument, "err")
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Push a single `return` inside a catch block towards `err()`.
|
|
388
|
+
*
|
|
389
|
+
* Only the lossless rewrites carry a fix. Replacing
|
|
390
|
+
* `return ok("defaults")` with `return err()` would silently drop the
|
|
391
|
+
* author's fallback, and the old behaviour — appending `return err()`
|
|
392
|
+
* after it — produced unreachable code and a report that could never be
|
|
393
|
+
* satisfied. Where the fallback matters, moving it into the try block is
|
|
394
|
+
* a decision the author has to make, so the rule reports and stops.
|
|
395
|
+
*/
|
|
396
|
+
function reportCatchReturn(tryNode: AstNode, statement: AstNode) {
|
|
397
|
+
const argument = statement["argument"];
|
|
398
|
+
|
|
399
|
+
if (isSettledFailure(argument)) return;
|
|
400
|
+
|
|
401
|
+
// `return;` — adding the result is pure addition, nothing is lost.
|
|
402
|
+
if (!isNode(argument)) {
|
|
403
|
+
context.report({
|
|
404
|
+
node: statement,
|
|
405
|
+
messageId: "returnFailedPromise",
|
|
406
|
+
fix(fixer) {
|
|
407
|
+
return [
|
|
408
|
+
...ensureResultHelperImportFixes(
|
|
409
|
+
tryNode,
|
|
410
|
+
fixer,
|
|
411
|
+
"err",
|
|
412
|
+
),
|
|
413
|
+
fixer.replaceText(statement, "return err()"),
|
|
414
|
+
];
|
|
415
|
+
},
|
|
416
|
+
});
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (hasIsErrorFlag(argument, true)) {
|
|
421
|
+
// `{ isError: true, value: null }` says the right thing the
|
|
422
|
+
// long way round; `err()` is the same value.
|
|
423
|
+
if (isNullLiteral(getObjectPropertyValue(argument, "value"))) {
|
|
424
|
+
context.report({
|
|
425
|
+
node: argument,
|
|
426
|
+
messageId: "preferErrResult",
|
|
427
|
+
fix(fixer) {
|
|
428
|
+
return [
|
|
429
|
+
...ensureResultHelperImportFixes(
|
|
430
|
+
tryNode,
|
|
431
|
+
fixer,
|
|
432
|
+
"err",
|
|
433
|
+
),
|
|
434
|
+
fixer.replaceText(argument, "err()"),
|
|
435
|
+
];
|
|
436
|
+
},
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
context.report({
|
|
443
|
+
node: statement,
|
|
444
|
+
messageId: "returnFailedPromise",
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
|
|
381
448
|
function processTryCatch(node: AstNode) {
|
|
382
449
|
let parent = node.parent;
|
|
383
450
|
let isAsync = false;
|
|
@@ -398,112 +465,93 @@ const rule = {
|
|
|
398
465
|
|
|
399
466
|
if (!isAsync || !parentFunction) return;
|
|
400
467
|
if (isCallArgumentCallback(parentFunction)) return;
|
|
468
|
+
if (throwsDeliberately(parentFunction["body"])) return;
|
|
401
469
|
|
|
402
470
|
let returnType: AstNode | null = null;
|
|
403
471
|
if (isNode(parentFunction["returnType"])) {
|
|
404
|
-
const typeAnnotation =
|
|
472
|
+
const typeAnnotation =
|
|
473
|
+
parentFunction["returnType"]["typeAnnotation"];
|
|
405
474
|
if (isTypeReference(typeAnnotation)) {
|
|
406
|
-
returnType =
|
|
475
|
+
returnType = getAwaitedTypeArgument(typeAnnotation);
|
|
407
476
|
}
|
|
408
477
|
}
|
|
409
478
|
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
479
|
+
// Every return the try block owns, not just the ones sitting
|
|
480
|
+
// directly in it. `try { if (flag) return "early"; return "late"; }`
|
|
481
|
+
// used to have only `"late"` wrapped, because the walk never looked
|
|
482
|
+
// inside the `if`.
|
|
483
|
+
//
|
|
484
|
+
// A nested `try` is left alone: the visitor fires for it separately,
|
|
485
|
+
// and its own catch branch has to stay the failure path rather than
|
|
486
|
+
// be read as another try-block return.
|
|
487
|
+
for (const statement of collectOwnReturns(node["block"])) {
|
|
488
|
+
wrapReturnValue(statement, isAsync, returnType);
|
|
418
489
|
}
|
|
419
490
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
? node["handler"]["body"]["body"].filter(isNode)
|
|
423
|
-
: [];
|
|
424
|
-
|
|
425
|
-
for (const statement of catchBody) {
|
|
426
|
-
if (
|
|
427
|
-
statement.type === "ReturnStatement" &&
|
|
428
|
-
isNode(statement["argument"])
|
|
429
|
-
) {
|
|
430
|
-
wrapReturnValue(statement, isAsync, returnType);
|
|
431
|
-
}
|
|
432
|
-
}
|
|
491
|
+
const handler = node["handler"];
|
|
492
|
+
if (!isNode(handler) || !isNode(handler["body"])) return;
|
|
433
493
|
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
hasIsErrorFlag(statement["argument"], true)),
|
|
442
|
-
);
|
|
494
|
+
// The catch branch is the failure path, and it settles on `err()`
|
|
495
|
+
// — nothing else. A default belongs in the try block, where the
|
|
496
|
+
// absence is read as a value instead of being recovered from a
|
|
497
|
+
// throw. So a catch return is never offered `ok(...)`; feeding both
|
|
498
|
+
// suggestions to the same statement used to produce two reports
|
|
499
|
+
// with opposite fixes.
|
|
500
|
+
const catchReturns = collectOwnReturns(handler["body"]);
|
|
443
501
|
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
messageId: "returnFailedPromise",
|
|
448
|
-
fix(fixer) {
|
|
449
|
-
const lastStatement = catchBody.at(-1);
|
|
450
|
-
if (
|
|
451
|
-
lastStatement?.type === "ReturnStatement" &&
|
|
452
|
-
isNode(lastStatement["argument"]) &&
|
|
453
|
-
lastStatement["argument"].type === "ObjectExpression"
|
|
454
|
-
) {
|
|
455
|
-
return [
|
|
456
|
-
...ensureResultHelperImportFixes(
|
|
457
|
-
node,
|
|
458
|
-
fixer,
|
|
459
|
-
"err",
|
|
460
|
-
),
|
|
461
|
-
fixer.replaceText(
|
|
462
|
-
lastStatement,
|
|
463
|
-
"return err()",
|
|
464
|
-
),
|
|
465
|
-
];
|
|
466
|
-
}
|
|
502
|
+
for (const statement of catchReturns) {
|
|
503
|
+
reportCatchReturn(node, statement);
|
|
504
|
+
}
|
|
467
505
|
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
506
|
+
// A nested try/catch inside this catch settles the branch on its
|
|
507
|
+
// own, so its returns count here even though `collectOwnReturns`
|
|
508
|
+
// hands them to the inner `processTryCatch`. Appending another
|
|
509
|
+
// `return err()` after them would be unreachable.
|
|
510
|
+
let settlesItself = false;
|
|
511
|
+
walkOwnSubtree(handler["body"], (candidate) => {
|
|
512
|
+
if (candidate.type === "ReturnStatement") {
|
|
513
|
+
settlesItself = true;
|
|
514
|
+
}
|
|
515
|
+
});
|
|
472
516
|
|
|
473
|
-
|
|
474
|
-
if (
|
|
475
|
-
!isNode(bodyRange) ||
|
|
476
|
-
!Array.isArray(bodyRange["range"])
|
|
477
|
-
) {
|
|
478
|
-
return null;
|
|
479
|
-
}
|
|
517
|
+
if (settlesItself) return;
|
|
480
518
|
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
519
|
+
context.report({
|
|
520
|
+
node: handler,
|
|
521
|
+
messageId: "returnFailedPromise",
|
|
522
|
+
fix(fixer) {
|
|
523
|
+
const handlerBody = handler["body"];
|
|
524
|
+
if (
|
|
525
|
+
!isNode(handlerBody) ||
|
|
526
|
+
!Array.isArray(handlerBody["range"])
|
|
527
|
+
) {
|
|
528
|
+
return null;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
return [
|
|
532
|
+
...ensureResultHelperImportFixes(node, fixer, "err"),
|
|
533
|
+
fixer.insertTextBeforeRange(
|
|
534
|
+
[
|
|
535
|
+
handlerBody["range"][1] - 1,
|
|
536
|
+
handlerBody["range"][1] - 1,
|
|
537
|
+
],
|
|
538
|
+
// The newline matters: `catch { log() }` would
|
|
539
|
+
// otherwise become `catch { log() return err(); }`,
|
|
540
|
+
// which does not parse.
|
|
541
|
+
"\nreturn err();\n",
|
|
542
|
+
),
|
|
543
|
+
];
|
|
544
|
+
},
|
|
545
|
+
});
|
|
498
546
|
}
|
|
499
547
|
|
|
500
548
|
return {
|
|
501
549
|
FunctionDeclaration: checkReturnType,
|
|
502
550
|
FunctionExpression: checkReturnType,
|
|
503
551
|
ArrowFunctionExpression: checkReturnType,
|
|
504
|
-
TSDeclareFunction:
|
|
505
|
-
TSFunctionType:
|
|
506
|
-
TSMethodSignature:
|
|
552
|
+
TSDeclareFunction: checkSignatureReturnType,
|
|
553
|
+
TSFunctionType: checkSignatureReturnType,
|
|
554
|
+
TSMethodSignature: checkSignatureReturnType,
|
|
507
555
|
TryStatement: processTryCatch,
|
|
508
556
|
};
|
|
509
557
|
},
|
package/rules/optional_rule.ts
CHANGED
|
@@ -17,8 +17,12 @@ const rule = {
|
|
|
17
17
|
const undefinedType = node.types.find(
|
|
18
18
|
(typeNode) => typeNode.type === "TSUndefinedKeyword",
|
|
19
19
|
);
|
|
20
|
+
// `null | undefined` has no non-nullish member to name, and rewriting
|
|
21
|
+
// it to `Optional<null>` only restated it with more syntax.
|
|
20
22
|
const otherType = node.types.find(
|
|
21
|
-
(typeNode) =>
|
|
23
|
+
(typeNode) =>
|
|
24
|
+
typeNode.type !== "TSUndefinedKeyword" &&
|
|
25
|
+
typeNode.type !== "TSNullKeyword",
|
|
22
26
|
);
|
|
23
27
|
|
|
24
28
|
if (undefinedType && otherType) {
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
type AstNode = {
|
|
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
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Visit every node that belongs to `root`, stopping at nested functions.
|
|
18
|
+
*
|
|
19
|
+
* Both async rules need the same notion of "this function's own code". A
|
|
20
|
+
* `throw` or a `return` inside a nested function belongs to that function, not
|
|
21
|
+
* to the one being linted, so the walk never descends into one — and `parent`
|
|
22
|
+
* points back up the tree, so following it would not terminate.
|
|
23
|
+
*
|
|
24
|
+
* `visit` may return `false` to keep the walk out of a node's children. That is
|
|
25
|
+
* how a rule declines a subtree that some other visitor already owns.
|
|
26
|
+
*
|
|
27
|
+
* @param {unknown} root - The node to start from. Visited itself, and never
|
|
28
|
+
* skipped for being a function.
|
|
29
|
+
* @param {(node: AstNode) => boolean | void} visit - Called once per node.
|
|
30
|
+
* Return `false` to skip that node's children.
|
|
31
|
+
* @returns {void}
|
|
32
|
+
*/
|
|
33
|
+
function walkOwnSubtree(
|
|
34
|
+
root: unknown,
|
|
35
|
+
visit: (node: AstNode) => boolean | void,
|
|
36
|
+
): void {
|
|
37
|
+
if (!isNode(root)) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const pending: AstNode[] = [root];
|
|
42
|
+
while (pending.length > 0) {
|
|
43
|
+
// `pop` on a non-empty array always yields a node; the length check above
|
|
44
|
+
// is the loop condition, but `noUncheckedIndexedAccess` cannot see that.
|
|
45
|
+
const current = pending.pop();
|
|
46
|
+
if (current === undefined) {
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (visit(current) === false) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
for (const [key, value] of Object.entries(current)) {
|
|
55
|
+
if (key === "parent") {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const candidates = Array.isArray(value) ? value : [value];
|
|
60
|
+
for (const candidate of candidates) {
|
|
61
|
+
if (!isNode(candidate) || FUNCTION_TYPES.has(candidate.type)) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
pending.push(candidate);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export { walkOwnSubtree };
|