Skip to content

Commit 4d40ce0

Browse files
committed
feat: handling node_modules
1 parent bf3ca91 commit 4d40ce0

File tree

7 files changed

+57
-2
lines changed

7 files changed

+57
-2
lines changed

package.json

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

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

+1
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ await testFile(
2525
);
2626

2727
await testFile("src/rules/might-throw/tests/import-ok.ts", [rule.name], []);
28+
await testFile("src/rules/might-throw/tests/module-ok.ts", [rule.name], []);

src/rules/might-throw/tests/module-ok.ts

Whitespace-only changes.

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

+1
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,4 @@ await testFile(
5050
},
5151
]
5252
);
53+
await testFile("src/rules/no-unhandled/tests/module-ok.ts", [rule.name], []);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { readFile } from "fs/promises";
2+
3+
async function a() {
4+
await readFile("./test.txt", "utf-8");
5+
}
6+
7+
a();
+46-1
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,61 @@
11
import { TSESTree } from "@typescript-eslint/utils";
22
import { RuleContext } from "@typescript-eslint/utils/ts-eslint";
3+
import { readFileSync } from "fs";
34
import path from "path";
45

6+
function cleanTsConfig(content: string) {
7+
return (
8+
content
9+
// Remove comments
10+
.replace(
11+
/(?:\r\n|\n|^)(?:[^'"])*?(?:'(?:[^\r\n\\']|\\'|[\\]{2})*'|"(?:[^\r\n\\"]|\\"|[\\]{2})*")*?(?:[^'"])*?(\/\*(?:[\s\S]*?)\*\/|\/\/.*)/g,
12+
""
13+
)
14+
// Remove trailing commas
15+
.replace(/,(\s+\])/gm, "$1")
16+
.replace(/,(\s+\})/gm, "$1")
17+
);
18+
}
19+
520
export function getImportDeclarationPath(
621
context: RuleContext<string, unknown[]>,
722
impt: TSESTree.ImportDeclaration
823
) {
924
const from = context.physicalFilename;
10-
const to = impt.source.value;
25+
let to = impt.source.value;
26+
1127
let ext = path.extname(to);
1228
if (ext === "") {
1329
ext = path.extname(from);
1430
}
31+
32+
if (!to.startsWith(".")) {
33+
const project = context.parserOptions.project;
34+
if (project) {
35+
const tsconfig = readFileSync(project.toString(), "utf-8");
36+
const aliases = JSON.parse(cleanTsConfig(tsconfig)).compilerOptions
37+
.paths as Record<string, string[]>;
38+
39+
let res = Object.entries(aliases)
40+
// sorting by longest - most qualified - alias
41+
.sort((a, b) => b[0].length - a[0].length)
42+
.find(
43+
([key]) => to.startsWith(key) || to.startsWith(key.replace(/\*$/, ""))
44+
);
45+
46+
if (res) {
47+
let [key, val] = res;
48+
key = key.replace(/\*$/, "");
49+
const firstVal = val[0].replace(/\*$/, "");
50+
to = to.replace(key, firstVal);
51+
return path.resolve(context.cwd, to + ext);
52+
}
53+
}
54+
55+
// no relative path and no TS alias,
56+
// considering it as a node_module
57+
return `./node_modules/${to}`;
58+
}
59+
1560
return path.resolve(path.dirname(from), to + ext);
1661
}

src/utils/resolve-imported-func.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export function resolveImportedFunc(
1515
impt: TSESTree.ImportDeclaration
1616
) {
1717
const importPath = getImportDeclarationPath(context, impt);
18+
if (importPath.startsWith("./node_modules")) return;
1819
const content = readFileSync(importPath, "utf-8");
1920
const parsed = parse(content, context);
2021
const identifierInParsed = findIdentifiersInChildren(

0 commit comments

Comments
 (0)