Skip to content

Commit f008ef6

Browse files
committed
test: add array destructuring test case and improve parent node traversal
1 parent 74469aa commit f008ef6

14 files changed

+95
-27
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-exception-handling",
3-
"version": "1.5.4",
3+
"version": "1.5.5",
44
"description": "💣 Lints unhandled functions that might throw errors. For JavaScript/TypeScript eslint.",
55
"author": {
66
"email": "[email protected]",

src/rules/no-unhandled/no-unhandled.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ await testFile(
1313
await testFile("src/rules/no-unhandled/tests/import-ok.ts", [rule.name], []);
1414
await testFile("src/rules/no-unhandled/tests/import-ok-2.ts", [rule.name], []);
1515
await testFile("src/rules/no-unhandled/tests/basic-ok.ts", [rule.name], []);
16+
await testFile("src/rules/no-unhandled/tests/basic-ok-2.ts", [rule.name], []);
1617
await testFile(
1718
"src/rules/no-unhandled/tests/basic-err.ts",
1819
[rule.name],
@@ -49,6 +50,15 @@ await testFile(
4950
},
5051
]
5152
);
53+
await testFile(
54+
"src/rules/no-unhandled/tests/basic-err-4.ts",
55+
[rule.name],
56+
[
57+
{
58+
messageId: "noUnhandled",
59+
},
60+
]
61+
);
5262
await testFile("src/rules/no-unhandled/tests/module-ok.ts", [rule.name], []);
5363
await testFile(
5464
"src/rules/no-unhandled/tests/private-identifier-ok.ts",
@@ -101,3 +111,8 @@ await testFile(
101111
]
102112
);
103113
await testFile("src/rules/no-unhandled/tests/alias-ok.ts", [rule.name], []);
114+
await testFile(
115+
"src/rules/no-unhandled/tests/array-destructuring-ok.ts",
116+
[rule.name],
117+
[]
118+
);

src/rules/no-unhandled/no-unhandled.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { isIdentifier } from "@typescript-eslint/utils/ast-utils";
1+
import { createRule } from "@/src/rules/create-rule";
22
import {
3-
findInParent,
4-
isFunctionDeclaration,
53
findInChildren,
4+
findInParent,
65
getCallExprId,
6+
isFunctionDeclaration,
77
isMethodDefinition,
88
} from "@/src/utils";
9-
import { createRule } from "@/src/rules/create-rule";
109
import { canFuncThrow, canFuncThrowClear } from "@/src/utils/can-func-throw";
10+
import { isIdentifier } from "@typescript-eslint/utils/ast-utils";
1111

1212
const name = "no-unhandled";
1313
const rule = createRule({
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const pairs = new Map<string, number>([
2+
["foo", 1],
3+
["bar", 2],
4+
]);
5+
6+
Array.from(pairs.entries())
7+
.filter(([, count]) => count > 1)
8+
.forEach(([identifier]) => {
9+
console.log(identifier);
10+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function abcd() {
2+
throw new Error("heyyyy!");
3+
}
4+
5+
abcd();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function abc() {
2+
throw new Error("heyyyy!");
3+
}

src/utils/can-func-throw.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { TSESTree } from "@typescript-eslint/utils";
2-
import { isIdentifier } from "@typescript-eslint/utils/ast-utils";
31
import {
2+
exploreChildren,
3+
findInChildren,
44
findInParent,
5+
getFunctionId,
56
isCallExpression,
7+
isCatchClause,
68
isThrowStatement,
79
isTryStatement,
8-
findInChildren,
9-
exploreChildren,
10-
getFunctionId,
1110
resolveFunc,
12-
isCatchClause,
1311
} from "@/src/utils";
14-
import { RuleContext } from "@typescript-eslint/utils/ts-eslint";
1512
import { nativeThrowing } from "@/src/utils/native-throwing";
13+
import { TSESTree } from "@typescript-eslint/utils";
14+
import { isIdentifier } from "@typescript-eslint/utils/ast-utils";
15+
import { RuleContext } from "@typescript-eslint/utils/ts-eslint";
1616

1717
const throwFunctions = new Set<string>();
1818
const scannedFunctions = new Set<string>();

src/utils/explore-children.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function exploreChildren<T>(
2929
if (val && typeof val === "object") {
3030
if (Array.isArray(val)) {
3131
for (const v of val) {
32-
if (v.type) {
32+
if (v?.type) {
3333
if (explored.has(v)) continue;
3434
explored.add(v);
3535
const res = explore(v, node);
@@ -47,3 +47,22 @@ export function exploreChildren<T>(
4747
};
4848
return explore(node, node.parent);
4949
}
50+
51+
// [
52+
// "body",
53+
// "params",
54+
// "callee",
55+
// "expression",
56+
// "arguments",
57+
// "block",
58+
// "handler",
59+
// "finalizer",
60+
// "specifiers",
61+
// "assertions",
62+
// "argument",
63+
// "property",
64+
// "id",
65+
// "object",
66+
// "attributes",
67+
// "declaration",
68+
// ];

src/utils/find-in-children.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import { exploreChildren } from "./explore-children";
55
export function findInChildren<
66
T extends TSESTree.Node,
77
F extends (x: TSESTree.Node) => x is T
8-
>(node: TSESTree.Node, predicate: F): InferGuardType<F> | undefined {
8+
>(
9+
node: TSESTree.Node,
10+
predicate: F,
11+
filter?: (node: InferGuardType<F>) => boolean
12+
): InferGuardType<F> | undefined {
913
return exploreChildren(node, (child, _parent, resolve) => {
10-
if (predicate(child)) {
14+
if (predicate(child) && (!filter || filter(child as InferGuardType<F>))) {
1115
resolve(child as InferGuardType<F>);
1216
}
1317
});

src/utils/find-in-parent.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { TSESTree } from "@typescript-eslint/types";
2+
import { findInChildren } from "./find-in-children";
23
import { InferGuardType } from "./infer-guard-type";
34

45
export function findInParent<
@@ -11,8 +12,15 @@ export function findInParent<
1112
): InferGuardType<F> | undefined {
1213
let parent: TSESTree.Node | undefined = node.parent;
1314
while (parent) {
14-
if (predicate(parent) && (!filter || filter(parent as InferGuardType<F>))) {
15-
return parent as InferGuardType<F>;
15+
const found = findInChildren(parent, predicate, (x) => {
16+
return (
17+
x.range[0] < node.range[0] &&
18+
x.range[1] > node.range[1] &&
19+
(!filter || filter(x as InferGuardType<F>))
20+
);
21+
});
22+
if (found) {
23+
return found;
1624
}
1725
parent = parent.parent;
1826
}

0 commit comments

Comments
 (0)