Skip to content

Commit b71e443

Browse files
committed
feat: add comprehensive list of native throwing functions and improve identifier search
1 parent f008ef6 commit b71e443

12 files changed

Lines changed: 733 additions & 125 deletions

scripts/node-runtime-scan.ts

Lines changed: 0 additions & 94 deletions
This file was deleted.

src/rules/might-throw/might-throw.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,23 @@ await testFile(
5757
);
5858
await testFile("src/rules/might-throw/tests/generic-ok.ts", [rule.name], []);
5959
await testFile("src/rules/might-throw/tests/tsx-ok.tsx", [rule.name], []);
60+
await testFile(
61+
"src/rules/might-throw/tests/native-funcs-err.ts",
62+
[rule.name],
63+
[
64+
{
65+
messageId: "mightThrow",
66+
line: 4,
67+
},
68+
]
69+
);
70+
await testFile(
71+
"src/rules/might-throw/tests/native-funcs-err-2.ts",
72+
[rule.name],
73+
[
74+
{
75+
messageId: "mightThrow",
76+
line: 4,
77+
},
78+
]
79+
);

src/rules/might-throw/might-throw.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { findInParent, isFunctionDeclaration } from "@/src/utils";
2-
import { getFunctionId } from "@/src/utils/get-function-id";
3-
import { getCallExprId } from "@/src/utils/get-call-expr-id";
41
import { createRule } from "@/src/rules/create-rule";
2+
import { findInParent, isFunctionDeclaration } from "@/src/utils";
53
import { canFuncThrow, canFuncThrowClear } from "@/src/utils/can-func-throw";
4+
import { getCallExprId } from "@/src/utils/get-call-expr-id";
5+
import { getFunctionId } from "@/src/utils/get-function-id";
66

77
const name = "might-throw";
88
const rule = createRule({
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createHash } from "node:crypto";
2+
3+
function hashFile(filePath: string, outputFile?: string) {
4+
const hash = createHash("sha256");
5+
const digest = hash.digest("hex");
6+
console.log(digest);
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { existsSync } from "fs";
2+
3+
export function hashFile() {
4+
if (!existsSync("fullPath")) {
5+
console.error(`File not found: ${"fullPath"}`);
6+
process.exit(1);
7+
}
8+
}

src/utils/explore-children.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export function exploreChildren<T>(
1414
node: TSESTree.Node,
1515
parent?: TSESTree.Node
1616
): T | undefined => {
17-
// console.log({ node });
1817
let rtrn = null;
1918
predicate(node, parent, (args) => {
2019
rtrn = args;
@@ -26,6 +25,7 @@ export function exploreChildren<T>(
2625
| TSESTree.Node
2726
| TSESTree.Node[]
2827
| undefined;
28+
2929
if (val && typeof val === "object") {
3030
if (Array.isArray(val)) {
3131
for (const v of val) {

src/utils/find-identifier-in-parents.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ export const findIdentifierInParents = (
66
node: TSESTree.Node
77
): TSESTree.Identifier | null => {
88
if (!node.parent) return null;
9-
const ids = findIdentifiersInChildren(name, [node.parent]);
9+
const ids = findIdentifiersInChildren(name, [node.parent]).filter(
10+
(x) => x != node
11+
);
1012
if (ids.length) {
1113
return ids[0];
1214
}

src/utils/find-identifiers-in-children.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { TSESTree } from "@typescript-eslint/utils";
2-
import { isIdentifier } from "@typescript-eslint/utils/ast-utils";
31
import {
42
isArrowFunctionExpression,
53
isBlockStatement,
@@ -14,6 +12,8 @@ import {
1412
isVariableDeclaration,
1513
isVariableDeclarator,
1614
} from "@/src/utils/ast-guards";
15+
import { TSESTree } from "@typescript-eslint/utils";
16+
import { isIdentifier } from "@typescript-eslint/utils/ast-utils";
1717

1818
export const findIdentifiersInChildren = (
1919
name: string,

src/utils/find-in-children-all.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import { TSESTree } from "@typescript-eslint/types";
2-
import { InferGuardType } from "./infer-guard-type";
32
import { exploreChildren } from "./explore-children";
3+
import { InferGuardType } from "./infer-guard-type";
44

55
export function findInChildrenAll<
66
T extends TSESTree.Node,
77
F extends (x: TSESTree.Node) => x is T
8-
>(node: TSESTree.Node, predicate: F) {
8+
>(
9+
node: TSESTree.Node,
10+
predicate: F,
11+
filter?: (node: InferGuardType<F>) => boolean
12+
) {
913
const result: InferGuardType<F>[] = [];
1014
exploreChildren(node, (child) => {
11-
if (predicate(child)) {
15+
if (predicate(child) && (!filter || filter(child as InferGuardType<F>))) {
1216
result.push(child as InferGuardType<F>);
1317
}
1418
});

0 commit comments

Comments
 (0)