|
| 1 | +import { type ESLint } from 'eslint' |
| 2 | + |
| 3 | +const rules: ESLint.ConfigData['rules'] = { |
| 4 | + /* Prettier Overrides */ |
| 5 | + 'prettier/prettier': [ |
| 6 | + 'error', |
| 7 | + { |
| 8 | + printWidth: 100, |
| 9 | + tabWidth: 4, |
| 10 | + semi: false, |
| 11 | + trailingComma: 'all' /* Reduces git diff. */, |
| 12 | + singleQuote: true, |
| 13 | + arrowParens: 'always', // Reduces character diff when adding Typescript types. |
| 14 | + }, |
| 15 | + ], |
| 16 | + |
| 17 | + /* Variables */ |
| 18 | + 'no-var': 'error', |
| 19 | + 'prefer-const': 'error', |
| 20 | + 'no-unused-vars': 'off', // Prefer equivalent rule from @typescript-eslint */ |
| 21 | + '@typescript-eslint/no-unused-vars': [ |
| 22 | + 'error', |
| 23 | + { |
| 24 | + /* By ignoring rest siblings, we support using `...rest` as an alternative to |
| 25 | + * a 3rd party omit function, to omit keys from an object. */ |
| 26 | + ignoreRestSiblings: true, |
| 27 | + // The following allows us to maintain args for type purposes without needing to use them, |
| 28 | + // for example, a reducer in which the action is typed but we don't use the action in the |
| 29 | + // reducer |
| 30 | + argsIgnorePattern: '^_', |
| 31 | + // The alternative to this is to use empty array syntax, such as const [, , thing] = someArr |
| 32 | + // this syntax lets you at least name the unused parameters which reads a bit nicer, i.e. |
| 33 | + // const [_first, _second, third] = someArr |
| 34 | + destructuredArrayIgnorePattern: '^_', |
| 35 | + }, |
| 36 | + ], |
| 37 | + |
| 38 | + /* Strings */ |
| 39 | + quotes: ['error', 'single', { avoidEscape: true }], |
| 40 | + 'prefer-template': 'error', |
| 41 | + |
| 42 | + /* Naming Convention */ |
| 43 | + camelcase: 'off', // disabled in favour of @typescript-eslint's version |
| 44 | + '@typescript-eslint/naming-convention': 'off', // to enable this, we require a deeper exploration of our naming patterns |
| 45 | + |
| 46 | + /* Object Properties */ |
| 47 | + 'dot-notation': 'error', |
| 48 | + 'no-useless-computed-key': 'error', |
| 49 | + 'no-import-assign': 'warn', |
| 50 | + |
| 51 | + /* Conditionals */ |
| 52 | + eqeqeq: 'error', |
| 53 | + 'no-nested-ternary': 'error', |
| 54 | + |
| 55 | + /* Behaviours */ |
| 56 | + /* |
| 57 | + * Rule: Disallow empty function blocks. |
| 58 | + * Reason Disabled: Useful for try..catch{} where we don't care about failure. |
| 59 | + */ |
| 60 | + '@typescript-eslint/no-empty-function': 'off', |
| 61 | + 'no-empty': ['error', { allowEmptyCatch: true }], |
| 62 | + |
| 63 | + /* Broken Rules */ |
| 64 | + /* |
| 65 | + * Rule: Disallow assignments that can lead to race conditions. |
| 66 | + * Reason Disabled: The rule is broken and leads to many false positives. |
| 67 | + * See: https://github.com/eslint/eslint/issues/11899 |
| 68 | + */ |
| 69 | + 'require-atomic-updates': 'off', |
| 70 | + |
| 71 | + /* Imports */ |
| 72 | + 'no-duplicate-imports': 'error', |
| 73 | + 'sort-imports': [ |
| 74 | + 'error', |
| 75 | + { |
| 76 | + /* Prefer import/order declaration sort due to autofixer */ |
| 77 | + ignoreDeclarationSort: true, |
| 78 | + }, |
| 79 | + ], |
| 80 | + 'import/default': 'error', |
| 81 | + 'import/export': 'error', |
| 82 | + 'import/named': 'error', |
| 83 | + 'import/newline-after-import': 'error', |
| 84 | + 'import/no-absolute-path': 'error', |
| 85 | + 'import/no-duplicates': 'error', |
| 86 | + 'import/no-mutable-exports': 'error', |
| 87 | + 'import/no-self-import': 'error', |
| 88 | + 'import/no-useless-path-segments': 'error', |
| 89 | + 'import/no-unresolved': 'error', |
| 90 | + 'import/order': [ |
| 91 | + 'error', |
| 92 | + { |
| 93 | + alphabetize: { order: 'asc' }, |
| 94 | + 'newlines-between': 'always', |
| 95 | + groups: ['unknown', 'builtin', 'external', 'internal', 'parent', 'sibling', 'index'], |
| 96 | + }, |
| 97 | + ], |
| 98 | + |
| 99 | + /* Typescript Types */ |
| 100 | + '@typescript-eslint/no-non-null-assertion': 'off', |
| 101 | + '@typescript-eslint/no-explicit-any': 'off', |
| 102 | + '@typescript-eslint/explicit-function-return-type': 'off', |
| 103 | + '@typescript-eslint/ban-ts-comment': 'warn', // Discourage disabling static analysis. |
| 104 | + '@typescript-eslint/ban-types': 'warn', // Discourage disabling static analysis. |
| 105 | + '@typescript-eslint/consistent-type-imports': [ |
| 106 | + 'error', |
| 107 | + { prefer: 'type-imports', fixStyle: 'inline-type-imports' }, |
| 108 | + ], |
| 109 | + '@typescript-eslint/no-duplicate-enum-values': 'error', |
| 110 | + '@typescript-eslint/no-for-in-array': 'error', |
| 111 | + '@typescript-eslint/no-non-null-asserted-nullish-coalescing': 'error', |
| 112 | + '@typescript-eslint/prefer-includes': 'error', |
| 113 | + '@typescript-eslint/prefer-for-of': 'error', |
| 114 | + '@typescript-eslint/prefer-optional-chain': 'error', |
| 115 | + '@typescript-eslint/prefer-ts-expect-error': 'error', |
| 116 | + |
| 117 | + // no-use-before-define can cause errors with typescript concepts, like types or enums |
| 118 | + 'no-use-before-define': 'off', |
| 119 | + // functions can be called before they are defined because function declarations are hoisted |
| 120 | + '@typescript-eslint/no-use-before-define': ['error', { functions: false }], |
| 121 | +} |
| 122 | + |
| 123 | +const jsIncompatibleRules: ESLint.ConfigData['rules'] = { |
| 124 | + /* |
| 125 | + * Rule: Disallow require statements in favour of import statements. |
| 126 | + * Reason Disabled: We don't know if JS files are transpiled, so don't bother enforcing TS import syntax. |
| 127 | + */ |
| 128 | + '@typescript-eslint/no-var-requires': 'off', |
| 129 | +} |
| 130 | + |
| 131 | +const config: ESLint.ConfigData = { |
| 132 | + parser: '@typescript-eslint/parser', |
| 133 | + parserOptions: { |
| 134 | + ecmaVersion: 'latest', |
| 135 | + sourceType: 'module', |
| 136 | + }, |
| 137 | + plugins: ['import', 'prettier', '@typescript-eslint'], |
| 138 | + extends: [ |
| 139 | + 'eslint:recommended', |
| 140 | + 'plugin:@typescript-eslint/recommended', |
| 141 | + 'plugin:@typescript-eslint/stylistic', |
| 142 | + // prettier must be the last item in this list to prevent conflicts |
| 143 | + 'prettier', |
| 144 | + ], |
| 145 | + env: { |
| 146 | + node: true, |
| 147 | + es2024: true, |
| 148 | + }, |
| 149 | + rules, |
| 150 | + overrides: [ |
| 151 | + { |
| 152 | + files: ['**/*.js', '**/*.cjs', '**/*.mjs'], |
| 153 | + rules: jsIncompatibleRules, |
| 154 | + }, |
| 155 | + ], |
| 156 | + settings: { |
| 157 | + 'import/external-module-folders': ['node_modules', '.yarn'], |
| 158 | + 'import/parsers': { |
| 159 | + '@typescript-eslint/parser': [ |
| 160 | + '.ts', |
| 161 | + '.tsx', |
| 162 | + '.mts', |
| 163 | + '.cts', |
| 164 | + '.js', |
| 165 | + '.mjs', |
| 166 | + '.cjs', |
| 167 | + '.jsx', |
| 168 | + ], |
| 169 | + }, |
| 170 | + 'import/resolver': [ |
| 171 | + { |
| 172 | + [require.resolve('eslint-import-resolver-typescript')]: { |
| 173 | + alwaysTryTypes: true, |
| 174 | + }, |
| 175 | + }, |
| 176 | + { [require.resolve('@noahnu/eslint-import-resolver-require')]: {} }, |
| 177 | + ], |
| 178 | + }, |
| 179 | +} |
| 180 | + |
| 181 | +export = config |
0 commit comments