|
1 | 1 | import type { Linter } from "eslint";
|
2 | 2 |
|
3 |
| -const createConfig = ( |
4 |
| - type: "all" | "javascript" | "js_and_ts" | "jsx_and_tsx" | "typescript", |
5 |
| - config: Omit<Linter.Config, "files|overrides">, |
6 |
| -): Linter.Config => { |
7 |
| - let files = ["*.js", "*.mjs", "*.cjs"]; |
| 3 | +type FileType = "all" | "ava" | "javascript" | "jest" | "js_and_ts" | "jsx_and_tsx" | "tests" | "typescript" | "vitest"; |
8 | 4 |
|
9 |
| - // eslint-disable-next-line default-case |
| 5 | +const getType = (type: FileType) => { |
10 | 6 | switch (type) {
|
11 | 7 | case "typescript": {
|
12 | 8 | // @see https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#new-file-extensions
|
13 |
| - files = ["*.ts", "*.tsx", "*.mts", "*.cts"]; |
14 |
| - |
15 |
| - break; |
| 9 | + return ["*.ts", "*.tsx", "*.mts", "*.cts"]; |
16 | 10 | }
|
17 | 11 | case "jsx_and_tsx": {
|
18 |
| - files = ["*.jsx", "*.tsx"]; |
19 |
| - |
20 |
| - break; |
| 12 | + return ["*.jsx", "*.tsx"]; |
21 | 13 | }
|
22 | 14 | case "js_and_ts": {
|
23 |
| - files = ["*.js", "*.mjs", "*.cjs", "*.ts", "*.mts", "*.cts"]; |
24 |
| - |
25 |
| - break; |
| 15 | + return ["*.js", "*.mjs", "*.cjs", "*.ts", "*.mts", "*.cts"]; |
26 | 16 | }
|
27 | 17 | case "all": {
|
28 |
| - files = ["*.js", "*.jsx", "*.mjs", "*.cjs", "*.ts", "*.tsx", "*.mts", "*.cts"]; |
| 18 | + return ["*.js", "*.jsx", "*.mjs", "*.cjs", "*.ts", "*.tsx", "*.mts", "*.cts"]; |
| 19 | + } |
| 20 | + case "jest": { |
| 21 | + return [ |
| 22 | + // Test files |
| 23 | + "**/*.spec.{js,ts,tsx}", |
| 24 | + "**/*.test.{js,ts,tsx}", |
| 25 | + "**/test/*.{js,ts,tsx}", |
| 26 | + |
| 27 | + // Facebook convention |
| 28 | + "**/__mocks__/*.{js,ts,tsx}", |
| 29 | + "**/__tests__/*.{js,ts,tsx}", |
| 30 | + ]; |
| 31 | + } |
| 32 | + case "ava": { |
| 33 | + return [ |
| 34 | + "test.js", |
| 35 | + "src/test.js", |
| 36 | + "source/test.js", |
| 37 | + "**/test-*.js", |
| 38 | + "**/*.spec.js", |
| 39 | + "**/*.test.js", |
| 40 | + "**/test/**/*.js", |
| 41 | + "**/tests/**/*.js", |
| 42 | + "**/__tests__/**/*.js", |
| 43 | + ]; |
| 44 | + } |
| 45 | + case "vitest": { |
| 46 | + return ["**/__tests__/**/*.?(c|m)[jt]s?(x)", "**/?(*.){test,spec}.?(c|m)[jt]s?(x)"]; |
| 47 | + } |
| 48 | + case "tests": { |
| 49 | + return [ |
| 50 | + // ava |
| 51 | + "test.js", |
| 52 | + "src/test.js", |
| 53 | + "source/test.js", |
| 54 | + "**/test-*.js", |
| 55 | + "**/*.spec.js", |
| 56 | + "**/*.test.js", |
| 57 | + "**/test/**/*.js", |
| 58 | + "**/tests/**/*.js", |
| 59 | + "**/__tests__/**/*.js", |
29 | 60 |
|
30 |
| - break; |
| 61 | + // jest |
| 62 | + "**/*.spec.{js,ts,tsx}", |
| 63 | + "**/*.test.{js,ts,tsx}", |
| 64 | + "**/test/*.{js,ts,tsx}", |
| 65 | + "**/__mocks__/*.{js,ts,tsx}", |
| 66 | + "**/__tests__/*.{js,ts,tsx}", |
| 67 | + "**/__tests__/**/*.?(c|m)[jt]s?(x)", |
| 68 | + "**/?(*.){test,spec}.?(c|m)[jt]s?(x)", |
| 69 | + ]; |
| 70 | + } |
| 71 | + default: { |
| 72 | + throw new Error(`Unknown type: ${type}`); |
31 | 73 | }
|
32 |
| - // No default |
33 | 74 | }
|
| 75 | +}; |
34 | 76 |
|
| 77 | +export const createConfig = ( |
| 78 | + type: FileType, |
| 79 | + config: Omit<Linter.ConfigOverride<Linter.RulesRecord>, "files">, |
| 80 | + environment?: { [name: string]: boolean }, |
| 81 | +): Linter.Config => { |
35 | 82 | return {
|
| 83 | + env: environment, |
36 | 84 | overrides: [
|
37 | 85 | {
|
38 |
| - files, |
| 86 | + files: getType(type), |
39 | 87 | ...config,
|
40 | 88 | },
|
41 | 89 | ],
|
42 | 90 | };
|
43 | 91 | };
|
44 |
| - |
45 |
| -export default createConfig; |
| 92 | +export const createConfigs = ( |
| 93 | + rules: { |
| 94 | + config: Omit<Linter.ConfigOverride<Linter.RulesRecord>, "files">; |
| 95 | + type: FileType; |
| 96 | + }[], |
| 97 | +): Linter.Config => { |
| 98 | + return { |
| 99 | + overrides: rules.map(({ type, config }) => { |
| 100 | + return { |
| 101 | + files: getType(type), |
| 102 | + ...config, |
| 103 | + }; |
| 104 | + }), |
| 105 | + }; |
| 106 | +}; |
0 commit comments