|
| 1 | +import { dirname } from 'node:path'; |
| 2 | +import { fileURLToPath } from 'node:url'; |
| 3 | + |
| 4 | +import tseslint from 'typescript-eslint'; |
| 5 | +import eslint from '@eslint/js'; |
| 6 | +import globals from 'globals'; |
| 7 | +import importPlugin from 'eslint-plugin-import-x'; |
| 8 | +import { createTypeScriptImportResolver } from 'eslint-import-resolver-typescript'; |
| 9 | +import jest from 'eslint-plugin-jest'; |
| 10 | + |
| 11 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 12 | + |
| 13 | +export default tseslint.config( |
| 14 | + // Global ignores (replaces .eslintignore + ignorePatterns) |
| 15 | + { |
| 16 | + ignores: [ |
| 17 | + '**/.yarn/**', |
| 18 | + '**/dist/**', |
| 19 | + '**/.next/**', |
| 20 | + '**/.vite/**', |
| 21 | + // NOTE: we are ignoring these examples because they were being ignored |
| 22 | + // before, we will need to isolate specific rules for examples and |
| 23 | + // remove these in the future. |
| 24 | + '**/vercel/examples/**', |
| 25 | + '**/react-native/example/**', |
| 26 | + '**/react-native/example-fdv2/**', |
| 27 | + '**/react/contract-tests/**', |
| 28 | + '**/react/examples/**', |
| 29 | + '**/jest/example/**', |
| 30 | + '**/electron/example/**', |
| 31 | + '**/svelte/.svelte-kit/**', |
| 32 | + '**/svelte/example/**', |
| 33 | + '**/fastly/example/build/**', |
| 34 | + '**/server-ai/examples/chat-judge/**', |
| 35 | + '**/server-ai/examples/direct-judge/**', |
| 36 | + '**/fromExternal/**', |
| 37 | + ], |
| 38 | + }, |
| 39 | + |
| 40 | + // Base config for all TypeScript files |
| 41 | + { |
| 42 | + files: ['**/*.ts', '**/*.tsx'], |
| 43 | + extends: [ |
| 44 | + eslint.configs.recommended, |
| 45 | + importPlugin.flatConfigs.recommended, |
| 46 | + tseslint.configs.recommended |
| 47 | + ], |
| 48 | + plugins: { |
| 49 | + // Alias as 'import' so existing eslint-disable comments with import/ prefix keep working |
| 50 | + import: importPlugin, |
| 51 | + jest, |
| 52 | + }, |
| 53 | + languageOptions: { |
| 54 | + parserOptions: { |
| 55 | + project: './tsconfig.eslint.json', |
| 56 | + tsconfigRootDir: __dirname, |
| 57 | + }, |
| 58 | + globals: { |
| 59 | + ...globals.node, |
| 60 | + BigInt: 'readonly', |
| 61 | + }, |
| 62 | + }, |
| 63 | + settings: { |
| 64 | + 'import-x/resolver-next': [ |
| 65 | + createTypeScriptImportResolver({ |
| 66 | + project: 'tsconfig.eslint.json', |
| 67 | + }), |
| 68 | + ], |
| 69 | + }, |
| 70 | + rules: { |
| 71 | + '@typescript-eslint/no-explicit-any': 'off', |
| 72 | + '@typescript-eslint/no-unused-vars': [ |
| 73 | + 'error', |
| 74 | + { |
| 75 | + ignoreRestSiblings: true, |
| 76 | + argsIgnorePattern: '^_', |
| 77 | + varsIgnorePattern: '^__', |
| 78 | + caughtErrors: 'none', |
| 79 | + }, |
| 80 | + ], |
| 81 | + // naming conventions |
| 82 | + '@typescript-eslint/naming-convention': [ |
| 83 | + 'error', |
| 84 | + { |
| 85 | + selector: ['method'], |
| 86 | + format: ['camelCase'], |
| 87 | + leadingUnderscore: 'forbid', |
| 88 | + }, |
| 89 | + { |
| 90 | + selector: ['method'], |
| 91 | + format: ['camelCase'], |
| 92 | + modifiers: ['private'], |
| 93 | + leadingUnderscore: 'require', |
| 94 | + }, |
| 95 | + { |
| 96 | + selector: ['classProperty', 'parameterProperty'], |
| 97 | + format: ['camelCase'], |
| 98 | + leadingUnderscore: 'forbid', |
| 99 | + }, |
| 100 | + { |
| 101 | + selector: ['classProperty', 'parameterProperty'], |
| 102 | + modifiers: ['static'], |
| 103 | + format: ['PascalCase'], |
| 104 | + leadingUnderscore: 'forbid', |
| 105 | + }, |
| 106 | + { |
| 107 | + selector: ['classProperty', 'parameterProperty'], |
| 108 | + modifiers: ['private'], |
| 109 | + format: ['camelCase'], |
| 110 | + leadingUnderscore: 'require', |
| 111 | + }, |
| 112 | + ], |
| 113 | + '@typescript-eslint/no-empty-object-type': [ |
| 114 | + 'error', |
| 115 | + { allowInterfaces: 'with-single-extends' }, |
| 116 | + ], |
| 117 | + 'no-restricted-syntax': [ |
| 118 | + 'error', |
| 119 | + { selector: 'ForInStatement', message: 'Use Object.{keys,values,entries} instead.' }, |
| 120 | + { selector: 'LabeledStatement', message: 'Labels are a form of GOTO.' }, |
| 121 | + { selector: 'WithStatement', message: '`with` is disallowed in strict mode.' }, |
| 122 | + ], |
| 123 | + 'import/no-extraneous-dependencies': [ |
| 124 | + 'error', |
| 125 | + { |
| 126 | + devDependencies: [ |
| 127 | + // NOTE: we should uncomment this in the future as config files typically |
| 128 | + // only read from devdependencies |
| 129 | + '**/jest*.ts', |
| 130 | + // '**/*.config.ts', |
| 131 | + '**/*.d.ts', |
| 132 | + '**/*{.,_}test.{ts,tsx}', |
| 133 | + // '**/*{.,_}spec.{ts,tsx}', |
| 134 | + ], |
| 135 | + }, |
| 136 | + ], |
| 137 | + // enable url-scheme imports |
| 138 | + 'import-x/no-unresolved': ['error', { ignore: ['^[a-z]+:'] }], |
| 139 | + 'no-underscore-dangle': ['error', { allowAfterThis: true }], |
| 140 | + 'no-await-in-loop': 'error', |
| 141 | + 'no-new': 'error', |
| 142 | + 'no-console': 'error', |
| 143 | + 'prefer-const': ['error', { ignoreReadBeforeAssign: true }], |
| 144 | + |
| 145 | + // NOTE: this will allow object fields to be reassigned. |
| 146 | + 'no-param-reassign': [ 'error' ], |
| 147 | + |
| 148 | + // TODO: we may want to re-enable this in the future. |
| 149 | + '@typescript-eslint/ban-ts-comment': 'off', |
| 150 | + |
| 151 | + // extension rules (https://typescript-eslint.io/rules#extension-rules) |
| 152 | + "no-use-before-define": "off", |
| 153 | + "@typescript-eslint/no-use-before-define": "error", |
| 154 | + |
| 155 | + // Keeping this as a reference, but I don't think we really need to enforce this rule. |
| 156 | + // "class-methods-use-this": "off", |
| 157 | + // "@typescript-eslint/class-methods-use-this": "error" |
| 158 | + }, |
| 159 | + }, |
| 160 | + |
| 161 | + // Test files: add jest globals and jest rules; relax test-specific rules |
| 162 | + { |
| 163 | + files: [ |
| 164 | + '**/*.test.ts', |
| 165 | + '**/*.test.tsx', |
| 166 | + '**/__tests__/**/*.ts', |
| 167 | + '**/__tests__/**/*.tsx' |
| 168 | + ], |
| 169 | + languageOptions: { |
| 170 | + globals: { |
| 171 | + ...globals.jest, |
| 172 | + }, |
| 173 | + }, |
| 174 | + rules: { |
| 175 | + 'jest/no-disabled-tests': 'warn', |
| 176 | + 'jest/no-focused-tests': 'error', |
| 177 | + 'jest/no-identical-title': 'error', |
| 178 | + 'jest/valid-expect': 'error', |
| 179 | + // 'no-console': 'off', |
| 180 | + '@typescript-eslint/no-require-imports': 'off', |
| 181 | + 'import-x/no-unresolved': 'off', |
| 182 | + }, |
| 183 | + }, |
| 184 | + |
| 185 | + // Examples, contract-tests, and tooling packages: allow console |
| 186 | + { |
| 187 | + files: [ |
| 188 | + '**/example/**', |
| 189 | + '**/examples/**', |
| 190 | + '**/example-*/**', |
| 191 | + '**/contract-tests/**', |
| 192 | + 'packages/tooling/**', |
| 193 | + ], |
| 194 | + rules: { |
| 195 | + // TODO re-enable later |
| 196 | + // 'no-console': 'off', |
| 197 | + // TODO: a lot of our examples fail this one, will need to check |
| 198 | + 'import-x/no-unresolved': 'off', |
| 199 | + }, |
| 200 | + }, |
| 201 | + |
| 202 | + // Override: contract-test-utils needs 'always' extensions (with ts exception) |
| 203 | + { |
| 204 | + files: ['packages/tooling/contract-test-utils/**/*.ts'], |
| 205 | + rules: { |
| 206 | + 'import/extensions': ['error', 'always', { ts: 'never' }], |
| 207 | + }, |
| 208 | + }, |
| 209 | +); |
0 commit comments