|
| 1 | +import js from "@eslint/js"; |
| 2 | +import typescriptParser from "@typescript-eslint/parser"; |
| 3 | +import typescriptPlugin from "@typescript-eslint/eslint-plugin"; |
| 4 | +import reactPlugin from "eslint-plugin-react"; |
| 5 | +import reactHooks from "eslint-plugin-react-hooks"; |
| 6 | +import jsxA11y from "eslint-plugin-jsx-a11y"; |
| 7 | + |
| 8 | +// Manually defined globals to avoid issues with the 'globals' package |
| 9 | +const browserGlobals = { |
| 10 | + window: "readonly", |
| 11 | + document: "readonly", |
| 12 | + navigator: "readonly", |
| 13 | + console: "readonly", |
| 14 | + fetch: "readonly", |
| 15 | + setTimeout: "readonly", |
| 16 | + HTMLInputElement: "readonly", |
| 17 | + HTMLElement: "readonly", |
| 18 | +}; |
| 19 | +const nodeGlobals = { |
| 20 | + process: "readonly", |
| 21 | + require: "readonly", |
| 22 | + module: "readonly", |
| 23 | + exports: "writable", |
| 24 | + __dirname: "readonly", |
| 25 | + __filename: "readonly", |
| 26 | + global: "readonly", |
| 27 | + console: "readonly", |
| 28 | +}; |
| 29 | +const jestGlobals = { |
| 30 | + jest: "readonly", |
| 31 | + describe: "readonly", |
| 32 | + it: "readonly", |
| 33 | + expect: "readonly", |
| 34 | + afterEach: "readonly", |
| 35 | + beforeEach: "readonly", |
| 36 | + test: "readonly", |
| 37 | + beforeAll: "readonly", |
| 38 | + afterAll: "readonly", |
| 39 | +}; |
| 40 | + |
| 41 | +export default [ |
| 42 | + // Base config for all JS/TS files (can be overridden) |
| 43 | + { |
| 44 | + ignores: [ |
| 45 | + "node_modules/**", |
| 46 | + "dist/**", |
| 47 | + "coverage/**", |
| 48 | + "*.min.js", |
| 49 | + "examples/**", |
| 50 | + ], |
| 51 | + }, |
| 52 | + js.configs.recommended, // Apply ESLint recommended rules globally (respecting ignores) |
| 53 | + |
| 54 | + // Configuration for TypeScript files in src/ |
| 55 | + { |
| 56 | + files: ["src/**/*.{ts,tsx}"], |
| 57 | + ignores: ["**/*.test.ts", "**/*.test.tsx"], |
| 58 | + languageOptions: { |
| 59 | + parser: typescriptParser, |
| 60 | + parserOptions: { |
| 61 | + ecmaFeatures: { jsx: true }, |
| 62 | + ecmaVersion: "latest", |
| 63 | + sourceType: "module", |
| 64 | + project: "./tsconfig.json", // Project-aware linting for src files |
| 65 | + }, |
| 66 | + globals: { |
| 67 | + ...browserGlobals, |
| 68 | + ...nodeGlobals, |
| 69 | + ...jestGlobals, |
| 70 | + es2021: true, |
| 71 | + }, |
| 72 | + }, |
| 73 | + plugins: { |
| 74 | + "@typescript-eslint": typescriptPlugin, |
| 75 | + react: reactPlugin, |
| 76 | + "react-hooks": reactHooks, |
| 77 | + "jsx-a11y": jsxA11y, |
| 78 | + }, |
| 79 | + rules: { |
| 80 | + ...typescriptPlugin.configs.recommended.rules, |
| 81 | + ...reactPlugin.configs.recommended.rules, |
| 82 | + "react/react-in-jsx-scope": "off", |
| 83 | + "react/jsx-uses-react": "off", |
| 84 | + "jsx-a11y/href-no-hash": "off", |
| 85 | + "react-hooks/rules-of-hooks": "error", |
| 86 | + "react-hooks/exhaustive-deps": "warn", |
| 87 | + "@typescript-eslint/no-unused-vars": [ |
| 88 | + "warn", |
| 89 | + { argsIgnorePattern: "^_", varsIgnorePattern: "^_" }, |
| 90 | + ], |
| 91 | + "@typescript-eslint/no-explicit-any": "off", |
| 92 | + "@typescript-eslint/no-non-null-assertion": "off", |
| 93 | + "@typescript-eslint/explicit-module-boundary-types": "off", |
| 94 | + "@typescript-eslint/no-unsafe-function-type": "off", |
| 95 | + "react/no-children-prop": "off", |
| 96 | + "@typescript-eslint/no-unused-expressions": "off", |
| 97 | + "no-undef": "off", // TypeScript handles this for .ts/.tsx |
| 98 | + }, |
| 99 | + settings: { |
| 100 | + react: { version: "detect" }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + |
| 104 | + // Configuration for TypeScript test files in typescript/ (for dtslint, no project parsing) |
| 105 | + { |
| 106 | + files: ["typescript/**/*.ts", "typescript/**/*.tsx"], |
| 107 | + languageOptions: { |
| 108 | + parser: typescriptParser, |
| 109 | + parserOptions: { |
| 110 | + ecmaFeatures: { jsx: true }, // Allow JSX in .tsx test files |
| 111 | + ecmaVersion: "latest", |
| 112 | + sourceType: "module", |
| 113 | + }, |
| 114 | + globals: { |
| 115 | + ...browserGlobals, |
| 116 | + ...nodeGlobals, |
| 117 | + ...jestGlobals, |
| 118 | + es2021: true, |
| 119 | + }, // General globals for TS test files |
| 120 | + }, |
| 121 | + plugins: { |
| 122 | + "@typescript-eslint": typescriptPlugin, |
| 123 | + // Add other plugins if relevant for these test files, e.g., react if they use React |
| 124 | + }, |
| 125 | + rules: { |
| 126 | + // Lighter ruleset for .d.ts test files or general TS syntax checking |
| 127 | + "@typescript-eslint/no-explicit-any": "off", |
| 128 | + "@typescript-eslint/no-unused-vars": "off", |
| 129 | + "no-unused-vars": "off", |
| 130 | + "no-undef": "off", // Disable no-undef for TypeScript files |
| 131 | + }, |
| 132 | + }, |
| 133 | + |
| 134 | + // Configuration for JavaScript files (e.g., .js test files in src/, config files) |
| 135 | + { |
| 136 | + files: ["**/*.js", "**/*.jsx"], |
| 137 | + ignores: ["examples/**"], |
| 138 | + languageOptions: { |
| 139 | + ecmaVersion: "latest", |
| 140 | + sourceType: "module", |
| 141 | + parserOptions: { |
| 142 | + ecmaFeatures: { |
| 143 | + jsx: true, |
| 144 | + }, |
| 145 | + }, |
| 146 | + globals: { |
| 147 | + ...browserGlobals, |
| 148 | + ...nodeGlobals, |
| 149 | + ...jestGlobals, |
| 150 | + es2021: true, |
| 151 | + }, |
| 152 | + }, |
| 153 | + plugins: { |
| 154 | + react: reactPlugin, |
| 155 | + }, |
| 156 | + rules: { |
| 157 | + "no-undef": "error", |
| 158 | + "react/jsx-uses-vars": "warn", |
| 159 | + "react/react-in-jsx-scope": "off", |
| 160 | + "no-unused-vars": ["warn", { argsIgnorePattern: "^_" }], // Enforce _ prefix for unused vars in JS files |
| 161 | + }, |
| 162 | + }, |
| 163 | + |
| 164 | + // Specific overrides for ALL test files (JS and TS) |
| 165 | + { |
| 166 | + files: ["**/*.test.js", "**/*.test.jsx", "**/*.test.ts", "**/*.test.tsx"], |
| 167 | + rules: { |
| 168 | + "no-unused-vars": "off", |
| 169 | + "@typescript-eslint/no-unused-vars": "off", |
| 170 | + }, |
| 171 | + }, |
| 172 | +]; |
0 commit comments