eslint-plugin-esm 0.0.0 → 0.1.1

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.
Files changed (76) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +60 -0
  3. package/dist/common.d.ts +24 -0
  4. package/dist/common.d.ts.map +1 -0
  5. package/dist/common.js +65 -0
  6. package/dist/index.d.ts +3 -1
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +23 -2
  9. package/dist/rules/nearest-relative-path.d.ts +5 -0
  10. package/dist/rules/nearest-relative-path.d.ts.map +1 -0
  11. package/dist/rules/nearest-relative-path.js +25 -0
  12. package/dist/rules/no-directory-imports.d.ts +5 -0
  13. package/dist/rules/no-directory-imports.d.ts.map +1 -0
  14. package/dist/rules/no-directory-imports.js +31 -0
  15. package/dist/rules/no-dynamic-imports.d.ts +5 -0
  16. package/dist/rules/no-dynamic-imports.d.ts.map +1 -0
  17. package/dist/rules/no-dynamic-imports.js +14 -0
  18. package/dist/rules/no-git-ignored-imports.d.ts +5 -0
  19. package/dist/rules/no-git-ignored-imports.d.ts.map +1 -0
  20. package/dist/rules/no-git-ignored-imports.js +46 -0
  21. package/dist/rules/no-phantom-dep-imports.d.ts +5 -0
  22. package/dist/rules/no-phantom-dep-imports.d.ts.map +1 -0
  23. package/dist/rules/no-phantom-dep-imports.js +73 -0
  24. package/dist/rules/no-relative-parent-imports.d.ts +5 -0
  25. package/dist/rules/no-relative-parent-imports.d.ts.map +1 -0
  26. package/dist/rules/no-relative-parent-imports.js +11 -0
  27. package/dist/rules/no-rename-exports.d.ts +5 -0
  28. package/dist/rules/no-rename-exports.d.ts.map +1 -0
  29. package/dist/rules/no-rename-exports.js +15 -0
  30. package/dist/rules/no-rename-imports.d.ts +5 -0
  31. package/dist/rules/no-rename-imports.d.ts.map +1 -0
  32. package/dist/rules/no-rename-imports.js +14 -0
  33. package/dist/rules/no-side-effect-imports.d.ts +5 -0
  34. package/dist/rules/no-side-effect-imports.d.ts.map +1 -0
  35. package/dist/rules/no-side-effect-imports.js +33 -0
  36. package/dist/rules/no-ts-file-imports.d.ts +5 -0
  37. package/dist/rules/no-ts-file-imports.d.ts.map +1 -0
  38. package/dist/rules/no-ts-file-imports.js +18 -0
  39. package/dist/utils.d.ts +5 -0
  40. package/dist/utils.d.ts.map +1 -0
  41. package/dist/utils.js +16 -0
  42. package/doc/rules/nearest-relative-path.md +48 -0
  43. package/doc/rules/no-directory-imports.md +30 -0
  44. package/doc/rules/no-dynamic-imports.md +30 -0
  45. package/doc/rules/no-git-ignored-imports.md +36 -0
  46. package/doc/rules/no-phantom-dep-imports.md +26 -0
  47. package/doc/rules/no-relative-parent-imports.md +33 -0
  48. package/doc/rules/no-rename-exports.md +29 -0
  49. package/doc/rules/no-rename-imports.md +28 -0
  50. package/doc/rules/no-side-effect-imports.md +31 -0
  51. package/doc/rules/no-ts-file-imports.md +119 -0
  52. package/package.json +7 -5
  53. package/src/common.ts +96 -0
  54. package/src/index.ts +23 -0
  55. package/src/rules/nearest-relative-path.spec.ts +44 -0
  56. package/src/rules/nearest-relative-path.ts +28 -0
  57. package/src/rules/no-directory-imports.spec.ts +31 -0
  58. package/src/rules/no-directory-imports.ts +34 -0
  59. package/src/rules/no-dynamic-imports.spec.ts +27 -0
  60. package/src/rules/no-dynamic-imports.ts +15 -0
  61. package/src/rules/no-git-ignored-imports.spec.ts +40 -0
  62. package/src/rules/no-git-ignored-imports.ts +53 -0
  63. package/src/rules/no-phantom-dep-imports.spec.ts +28 -0
  64. package/src/rules/no-phantom-dep-imports.ts +90 -0
  65. package/src/rules/no-relative-parent-imports.spec.ts +28 -0
  66. package/src/rules/no-relative-parent-imports.ts +13 -0
  67. package/src/rules/no-rename-exports.spec.ts +22 -0
  68. package/src/rules/no-rename-exports.ts +17 -0
  69. package/src/rules/no-rename-imports.spec.ts +22 -0
  70. package/src/rules/no-rename-imports.ts +16 -0
  71. package/src/rules/no-side-effect-imports.spec.ts +25 -0
  72. package/src/rules/no-side-effect-imports.ts +44 -0
  73. package/src/rules/no-ts-file-imports.spec.ts +39 -0
  74. package/src/rules/no-ts-file-imports.ts +23 -0
  75. package/src/test.spec.ts +110 -0
  76. package/src/utils.ts +15 -0
@@ -0,0 +1,23 @@
1
+ import { create, createRule, getRuleName } from "../common.js";
2
+
3
+ export const noTsFileImports = createRule({
4
+ name: getRuleName(import.meta.url),
5
+ message: "Disallow importing from a declaration style file or a ts file",
6
+ create: (context) => create(context, check),
7
+ });
8
+
9
+ function check(filename: string, source: string) {
10
+ // disabled this rule in declaration files
11
+ if (
12
+ [".d.ts", ".d.cts", ".d.mts", ".d.tsx"].some((ext) =>
13
+ filename.endsWith(ext),
14
+ )
15
+ ) {
16
+ return false;
17
+ }
18
+ const file = source.split("/").at(-1);
19
+ if (!file || file.includes(".d.")) {
20
+ return true;
21
+ }
22
+ return [".ts", ".cts", ".mts", ".tsx"].some((ext) => file.endsWith(ext));
23
+ }
@@ -0,0 +1,110 @@
1
+ import fs from "node:fs/promises";
2
+ import { createRequire } from "node:module";
3
+ import path from "node:path";
4
+ import process from "node:process";
5
+ import { describe, it } from "node:test";
6
+ import { fileURLToPath } from "node:url";
7
+ import { RuleTester, type Rule } from "eslint";
8
+ import { outdent } from "outdent";
9
+
10
+ export type TestCase = string | { code: string; filename?: string };
11
+
12
+ const tester = new RuleTester({
13
+ parser: createRequire(import.meta.url).resolve("@typescript-eslint/parser"),
14
+ parserOptions: { ecmaVersion: "latest", sourceType: "module" },
15
+ });
16
+
17
+ export async function test({
18
+ name,
19
+ rule,
20
+ valid,
21
+ invalid,
22
+ errors = 1,
23
+ }: {
24
+ name: string;
25
+ rule: Rule.RuleModule;
26
+ valid: TestCase[];
27
+ invalid: TestCase[];
28
+ errors?: number;
29
+ }) {
30
+ await describe(name, async () => {
31
+ await Promise.all(
32
+ valid.map(async (testCase) => {
33
+ await it(JSON.stringify(testCase), () => {
34
+ tester.run(name, rule, {
35
+ valid: [testCase],
36
+ invalid: [],
37
+ });
38
+ });
39
+ }),
40
+ );
41
+
42
+ await Promise.all(
43
+ invalid.map(async (testCase) => {
44
+ await it(JSON.stringify(testCase), () => {
45
+ const code = typeof testCase === "string" ? testCase : testCase.code;
46
+ const filename =
47
+ typeof testCase === "string" ? undefined : testCase.filename;
48
+ tester.run(name, rule, {
49
+ valid: [],
50
+ invalid: [{ code, errors, filename }],
51
+ });
52
+ });
53
+ }),
54
+ );
55
+
56
+ await genDoc({ name, rule, valid, invalid });
57
+ });
58
+ }
59
+
60
+ async function genDoc({
61
+ name,
62
+ rule,
63
+ valid,
64
+ invalid,
65
+ }: {
66
+ name: string;
67
+ rule: Rule.RuleModule;
68
+ valid: TestCase[];
69
+ invalid: TestCase[];
70
+ }) {
71
+ const handle = (testCases: TestCase[]) =>
72
+ testCases
73
+ .map((testCase) =>
74
+ typeof testCase === "string" ? { code: testCase } : testCase,
75
+ )
76
+ .map((testCase) =>
77
+ testCase.filename
78
+ ? `${testCase.code} // filename: ${testCase.filename}`
79
+ : testCase.code,
80
+ )
81
+ .join("\n");
82
+ const mdContent = outdent`
83
+ <!-- prettier-ignore-start -->
84
+ # ${name}
85
+
86
+ ${rule.meta?.docs?.description}
87
+
88
+ ## Rule Details
89
+
90
+ ### Fail
91
+
92
+ \`\`\`ts
93
+ ${handle(invalid)}
94
+ \`\`\`
95
+
96
+ ### Pass
97
+
98
+ \`\`\`ts
99
+ ${handle(valid)}
100
+ \`\`\`
101
+ <!-- prettier-ignore-end -->
102
+
103
+ `.replaceAll(process.cwd(), "/foo");
104
+
105
+ const currentDir = path.dirname(fileURLToPath(import.meta.url));
106
+ await fs.writeFile(
107
+ path.join(currentDir, "..", "doc", "rules", `${name}.md`),
108
+ mdContent,
109
+ );
110
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Like [lodash.memoize](https://lodash.com/docs/4.17.15#memoize)
3
+ */
4
+ export function memoize<Arg, Res>(fn: (arg: Arg) => Res): (arg: Arg) => Res {
5
+ const cache = new Map<Arg, Res>(); // memory leak
6
+ return (arg: Arg) => {
7
+ if (cache.has(arg)) {
8
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
9
+ return cache.get(arg)!;
10
+ }
11
+ const result = fn(arg);
12
+ cache.set(arg, result);
13
+ return result;
14
+ };
15
+ }