|
| 1 | +import js from "@eslint/js"; |
| 2 | +import stylistic from "@stylistic/eslint-plugin"; |
| 3 | +import ts from "@typescript-eslint/eslint-plugin"; |
| 4 | +import tsParser from "@typescript-eslint/parser"; |
| 5 | +import { resolve as tsResolver } from "eslint-import-resolver-typescript"; |
| 6 | +import importPlugin from "eslint-plugin-import"; |
| 7 | +import jsdoc from "eslint-plugin-jsdoc"; |
| 8 | +import perfectionist from "eslint-plugin-perfectionist"; |
| 9 | +import explicitGenerics from "eslint-plugin-require-explicit-generics"; |
| 10 | +import sonarjs from "eslint-plugin-sonarjs"; |
| 11 | +import unicorn from "eslint-plugin-unicorn"; |
| 12 | +import globals from "globals"; |
| 13 | + |
| 14 | +const JS_MAX_PARAMS_ALLOWED = 3; |
| 15 | + |
| 16 | +/** @typedef {import("eslint").Linter.Config} */ |
| 17 | +let Config; |
| 18 | +/** @typedef {import("eslint").Linter.ParserModule} */ |
| 19 | +let ParserModule; |
| 20 | + |
| 21 | +/** @type {Config} */ |
| 22 | +const filesConfig = { |
| 23 | + files: ["./src/**/*.{js,ts,mjs}"] |
| 24 | +}; |
| 25 | + |
| 26 | +/** @type {Config} */ |
| 27 | +const ignoresConfig = { |
| 28 | + ignores: ["public", "build"] |
| 29 | +}; |
| 30 | + |
| 31 | +/** @type {Config} */ |
| 32 | +const jsConfig = { |
| 33 | + languageOptions: { |
| 34 | + globals: globals.node, |
| 35 | + parserOptions: { |
| 36 | + ecmaVersion: 14, |
| 37 | + sourceType: "module" |
| 38 | + } |
| 39 | + }, |
| 40 | + rules: { |
| 41 | + ...js.configs.recommended.rules, |
| 42 | + "arrow-parens": ["error", "always"], |
| 43 | + curly: ["error", "all"], |
| 44 | + "max-params": ["error", JS_MAX_PARAMS_ALLOWED], |
| 45 | + "no-console": ["error"], |
| 46 | + "no-multiple-empty-lines": [ |
| 47 | + "error", |
| 48 | + { |
| 49 | + max: 1 |
| 50 | + } |
| 51 | + ], |
| 52 | + "no-restricted-syntax": [ |
| 53 | + "error", |
| 54 | + { |
| 55 | + message: "Export/Import all (*) is forbidden.", |
| 56 | + selector: "ExportAllDeclaration,ImportAllDeclaration" |
| 57 | + }, |
| 58 | + { |
| 59 | + message: "Exports should be at the end of the file.", |
| 60 | + selector: "ExportNamedDeclaration[declaration!=null]" |
| 61 | + }, |
| 62 | + { |
| 63 | + message: "TS features are forbidden.", |
| 64 | + selector: "TSEnumDeclaration,ClassDeclaration[abstract=true]" |
| 65 | + }, |
| 66 | + { |
| 67 | + message: |
| 68 | + "Avoid import/export type { Type } from './module'. Prefer import/export { type Type } from './module'.", |
| 69 | + selector: "ImportDeclaration[importKind=type],ExportNamedDeclaration[exportKind=type]" |
| 70 | + } |
| 71 | + ], |
| 72 | + "no-unused-vars": [ |
| 73 | + "error", |
| 74 | + { |
| 75 | + "argsIgnorePattern": "^_", |
| 76 | + "caughtErrorsIgnorePattern": "^_", |
| 77 | + "varsIgnorePattern": "^_" |
| 78 | + } |
| 79 | + ], |
| 80 | + "object-shorthand": ["error"], |
| 81 | + "prefer-destructuring": ["error"], |
| 82 | + quotes: ["error", "double"] |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +/** @type {Config} */ |
| 87 | +const importConfig = { |
| 88 | + plugins: { |
| 89 | + import: importPlugin |
| 90 | + }, |
| 91 | + rules: { |
| 92 | + ...importPlugin.configs.recommended.rules, |
| 93 | + "import/exports-last": ["error"], |
| 94 | + "import/extensions": [ |
| 95 | + "error", |
| 96 | + { |
| 97 | + js: "always" |
| 98 | + } |
| 99 | + ], |
| 100 | + "import/newline-after-import": ["error"], |
| 101 | + "import/no-default-export": ["error"], |
| 102 | + "import/no-duplicates": ["error"] |
| 103 | + }, |
| 104 | + settings: { |
| 105 | + "import/parsers": { |
| 106 | + espree: [".js", ".cjs"] |
| 107 | + }, |
| 108 | + "import/resolver": { |
| 109 | + typescript: tsResolver |
| 110 | + } |
| 111 | + } |
| 112 | +}; |
| 113 | + |
| 114 | +/** @type {Config} */ |
| 115 | +const sonarConfig = { |
| 116 | + plugins: { |
| 117 | + sonarjs |
| 118 | + }, |
| 119 | + rules: { |
| 120 | + ...sonarjs.configs.recommended.rules, |
| 121 | + "sonarjs/no-duplicate-string": ["off"], |
| 122 | + "sonarjs/no-unused-vars": ["off"] |
| 123 | + } |
| 124 | +}; |
| 125 | + |
| 126 | +/** @type {Config} */ |
| 127 | +const unicornConfig = { |
| 128 | + plugins: { |
| 129 | + unicorn |
| 130 | + }, |
| 131 | + rules: { |
| 132 | + ...unicorn.configs.recommended.rules, |
| 133 | + "unicorn/no-null": ["off"] |
| 134 | + } |
| 135 | +}; |
| 136 | + |
| 137 | +/** @type {Config} */ |
| 138 | +const perfectionistConfig = { |
| 139 | + plugins: { |
| 140 | + perfectionist |
| 141 | + }, |
| 142 | + rules: perfectionist.configs["recommended-natural"].rules |
| 143 | +}; |
| 144 | + |
| 145 | +/** @type {Config} */ |
| 146 | +const typescriptConfig = { |
| 147 | + ignores: ["eslint.config.js", "lint-staged.config.js", "stylelint.config.js"], |
| 148 | + languageOptions: { |
| 149 | + parser: /** @type {ParserModule} */ (tsParser), |
| 150 | + parserOptions: { |
| 151 | + project: "./tsconfig.json" |
| 152 | + } |
| 153 | + }, |
| 154 | + plugins: { |
| 155 | + "@typescript-eslint": ts |
| 156 | + }, |
| 157 | + rules: { |
| 158 | + ...ts.configs["strict-type-checked"].rules, |
| 159 | + "@typescript-eslint/consistent-type-exports": ["error"], |
| 160 | + "@typescript-eslint/consistent-type-imports": [ |
| 161 | + "error", |
| 162 | + { |
| 163 | + fixStyle: "inline-type-imports" |
| 164 | + } |
| 165 | + ], |
| 166 | + "@typescript-eslint/explicit-function-return-type": [ |
| 167 | + "error", |
| 168 | + { |
| 169 | + allowTypedFunctionExpressions: true |
| 170 | + } |
| 171 | + ], |
| 172 | + "@typescript-eslint/no-magic-numbers": [ |
| 173 | + "error", |
| 174 | + { |
| 175 | + ignoreEnums: true, |
| 176 | + ignoreReadonlyClassProperties: true |
| 177 | + } |
| 178 | + ], |
| 179 | + "@typescript-eslint/no-misused-promises": [ |
| 180 | + "error", |
| 181 | + { |
| 182 | + "checksVoidReturn": false |
| 183 | + } |
| 184 | + ], |
| 185 | + "@typescript-eslint/no-unused-vars": ["off"], |
| 186 | + "@typescript-eslint/return-await": ["error", "always"] |
| 187 | + } |
| 188 | +}; |
| 189 | + |
| 190 | +/** @type {Config} */ |
| 191 | +const jsdocConfig = { |
| 192 | + files: ["eslint.config.js", "lint-staged.config.js"], |
| 193 | + plugins: { |
| 194 | + jsdoc |
| 195 | + }, |
| 196 | + rules: { |
| 197 | + ...jsdoc.configs["recommended-typescript-flavor-error"].rules, |
| 198 | + "jsdoc/no-undefined-types": ["error"] |
| 199 | + } |
| 200 | +}; |
| 201 | + |
| 202 | +/** @type {Config} */ |
| 203 | +const stylisticConfig = { |
| 204 | + plugins: { |
| 205 | + "@stylistic": stylistic |
| 206 | + }, |
| 207 | + rules: { |
| 208 | + "@stylistic/padding-line-between-statements": [ |
| 209 | + "error", |
| 210 | + { |
| 211 | + blankLine: "never", |
| 212 | + next: "export", |
| 213 | + prev: "export" |
| 214 | + }, |
| 215 | + { |
| 216 | + blankLine: "always", |
| 217 | + next: "*", |
| 218 | + prev: ["block-like", "throw"] |
| 219 | + }, |
| 220 | + { |
| 221 | + blankLine: "always", |
| 222 | + next: ["return", "block-like", "throw"], |
| 223 | + prev: "*" |
| 224 | + } |
| 225 | + ] |
| 226 | + } |
| 227 | +}; |
| 228 | + |
| 229 | +/** @type {Config} */ |
| 230 | +const explicitGenericsConfig = { |
| 231 | + plugins: { |
| 232 | + "require-explicit-generics": explicitGenerics |
| 233 | + } |
| 234 | +}; |
| 235 | + |
| 236 | +/** @type {Config[]} */ |
| 237 | +const overridesConfigs = [ |
| 238 | + { |
| 239 | + files: ["packages.d.ts", "lint-staged.config.js", "eslint.config.js"], |
| 240 | + rules: { |
| 241 | + "import/no-default-export": ["off"] |
| 242 | + } |
| 243 | + }, |
| 244 | + { |
| 245 | + files: ["*.js"], |
| 246 | + rules: { |
| 247 | + "@typescript-eslint/explicit-function-return-type": ["off"] |
| 248 | + } |
| 249 | + }, |
| 250 | + { |
| 251 | + files: ["./src/server.ts"], |
| 252 | + rules: { |
| 253 | + "import/no-default-export": ["off"], |
| 254 | + "no-console": ["off"] |
| 255 | + } |
| 256 | + } |
| 257 | +]; |
| 258 | + |
| 259 | +/** @type {Config[]} */ |
| 260 | +const config = [ |
| 261 | + filesConfig, |
| 262 | + ignoresConfig, |
| 263 | + jsConfig, |
| 264 | + importConfig, |
| 265 | + sonarConfig, |
| 266 | + unicornConfig, |
| 267 | + perfectionistConfig, |
| 268 | + typescriptConfig, |
| 269 | + jsdocConfig, |
| 270 | + stylisticConfig, |
| 271 | + explicitGenericsConfig, |
| 272 | + ...overridesConfigs |
| 273 | +]; |
| 274 | + |
| 275 | +export default config; |
0 commit comments