Skip to content

Commit 7f3ff77

Browse files
authored
Merge pull request #13 from Simbiat/main
ESM/CJS support and type correction
2 parents f608a74 + 81d79bf commit 7f3ff77

2 files changed

Lines changed: 27 additions & 18 deletions

File tree

src/index.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
import { RuleModule } from "@typescript-eslint/utils/ts-eslint";
21
import { ESLint } from "eslint";
32
import { rules } from "./rules";
43
import noUnhandled from "./rules/no-unhandled/no-unhandled";
54
import mightThrow from "./rules/might-throw/might-throw";
65
import useErrorCause from "./rules/use-error-cause/use-error-cause";
76

8-
type RuleKey = keyof typeof rules;
9-
10-
interface Plugin extends Omit<ESLint.Plugin, "rules"> {
11-
rules: Record<RuleKey, RuleModule<string>>;
12-
}
13-
147
export const name = "eslint-plugin-exception-handling";
15-
export const plugin: Plugin = {
8+
9+
export const plugin: ESLint.Plugin = {
1610
meta: {
1711
name,
1812
version: "1.0.0",
@@ -27,7 +21,7 @@ export const plugin: Plugin = {
2721
},
2822
},
2923
},
30-
rules,
24+
rules: rules as unknown as ESLint.Plugin["rules"],
3125
};
3226

3327
export { rules };

src/utils/get-import-declaration.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,25 @@ function resolveTSAlias(tsconfigpath: string, to: string, cwd: string) {
4141
return to;
4242
}
4343

44-
const codeExt = [".ts", ".js", ".tsx", ".jsx"];
44+
const codeExt = [".ts", ".js", ".tsx", ".jsx", ".mjs", ".mjsx", ".mts", ".mtsx", ".cjs", ".cjsx", ".cts", ".ctsx"];
45+
const extAlts = {
46+
// Base family
47+
".js": [".jsx", ".ts", ".tsx"],
48+
".jsx": [".js", ".tsx", ".ts"],
49+
".ts": [".tsx", ".js", ".jsx"],
50+
".tsx": [".ts", ".jsx", ".js"],
51+
// ESM-explicit family (m*)
52+
".mjs": [".mts", ".mjsx", ".mtsx"],
53+
".mjsx": [".mjs", ".mtsx", ".mts"],
54+
".mts": [".mtsx", ".mjs", ".mjsx"],
55+
".mtsx": [".mts", ".mjsx", ".mjs"],
56+
// CJS-explicit family (c*)
57+
".cjs": [".cts", ".cjsx", ".ctsx"],
58+
".cjsx": [".cjs", ".ctsx", ".cts"],
59+
".cts": [".ctsx", ".cjs", ".cjsx"],
60+
".ctsx": [".cts", ".cjsx", ".cjs"],
61+
};
62+
4563

4664
function endsWithAny(str: string, arr: string[]) {
4765
return arr.some((ext) => str.endsWith(ext));
@@ -92,14 +110,11 @@ export function getImportDeclaration(
92110
}
93111

94112
if (!existsSync(res)) {
95-
if (res.endsWith(".js")) {
96-
res = findFileExtension(res.replace(".js", ""), [".jsx", ".ts", ".tsx"]);
97-
} else if (res.endsWith(".jsx")) {
98-
res = findFileExtension(res.replace(".jsx", ""), [".js", ".tsx", ".ts"]);
99-
} else if (res.endsWith(".ts")) {
100-
res = findFileExtension(res.replace(".ts", ""), [".tsx", ".js", ".jsx"]);
101-
} else if (res.endsWith(".tsx")) {
102-
res = findFileExtension(res.replace(".tsx", ""), [".ts", ".jsx", ".js"]);
113+
for (const [ext, alternatives] of Object.entries(extAlts)) {
114+
if (res.endsWith(ext)) {
115+
res = findFileExtension(res.slice(0, -ext.length), alternatives);
116+
break;
117+
}
103118
}
104119
}
105120

0 commit comments

Comments
 (0)