|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const path = require('node:path') |
| 4 | + |
| 5 | +const { |
| 6 | + convertIgnorePatternToMinimatch, |
| 7 | + includeIgnoreFile, |
| 8 | +} = require('@eslint/compat') |
| 9 | +const js = require('@eslint/js') |
| 10 | +const tsParser = require('@typescript-eslint/parser') |
| 11 | +const { |
| 12 | + createTypeScriptImportResolver, |
| 13 | +} = require('eslint-import-resolver-typescript') |
| 14 | +const importXPlugin = require('eslint-plugin-import-x') |
| 15 | +const nodePlugin = require('eslint-plugin-n') |
| 16 | +const sortDestructureKeysPlugin = require('eslint-plugin-sort-destructure-keys') |
| 17 | +const unicornPlugin = require('eslint-plugin-unicorn') |
| 18 | +const globals = require('globals') |
| 19 | +const tsEslint = require('typescript-eslint') |
| 20 | + |
| 21 | +const constants = require('@socketsecurity/registry/lib/constants') |
| 22 | +const { BIOME_JSON, GITIGNORE, LATEST, TSCONFIG_JSON } = constants |
| 23 | + |
| 24 | +const { flatConfigs: origImportXFlatConfigs } = importXPlugin |
| 25 | + |
| 26 | +const rootPath = __dirname |
| 27 | +const rootTsConfigPath = path.join(rootPath, TSCONFIG_JSON) |
| 28 | + |
| 29 | +const nodeGlobalsConfig = Object.fromEntries( |
| 30 | + Object.entries(globals.node).map(([k]) => [k, 'readonly']), |
| 31 | +) |
| 32 | + |
| 33 | +const biomeConfigPath = path.join(rootPath, BIOME_JSON) |
| 34 | +const biomeConfig = require(biomeConfigPath) |
| 35 | +const biomeIgnores = { |
| 36 | + name: 'Imported biome.json ignore patterns', |
| 37 | + ignores: biomeConfig.files.includes |
| 38 | + .filter(p => p.startsWith('!')) |
| 39 | + .map(p => convertIgnorePatternToMinimatch(p.slice(1))), |
| 40 | +} |
| 41 | + |
| 42 | +const gitignorePath = path.join(rootPath, GITIGNORE) |
| 43 | +const gitIgnores = includeIgnoreFile(gitignorePath) |
| 44 | + |
| 45 | +if (process.env.LINT_DIST) { |
| 46 | + const isNotDistGlobPattern = p => !/(?:^|[\\/])dist/.test(p) |
| 47 | + biomeIgnores.ignores = biomeIgnores.ignores?.filter(isNotDistGlobPattern) |
| 48 | + gitIgnores.ignores = gitIgnores.ignores?.filter(isNotDistGlobPattern) |
| 49 | +} |
| 50 | + |
| 51 | +if (process.env.LINT_EXTERNAL) { |
| 52 | + const isNotExternalGlobPattern = p => !/(?:^|[\\/])external/.test(p) |
| 53 | + biomeIgnores.ignores = biomeIgnores.ignores?.filter(isNotExternalGlobPattern) |
| 54 | + gitIgnores.ignores = gitIgnores.ignores?.filter(isNotExternalGlobPattern) |
| 55 | +} |
| 56 | + |
| 57 | +const sharedPlugins = { |
| 58 | + 'sort-destructure-keys': sortDestructureKeysPlugin, |
| 59 | + unicorn: unicornPlugin, |
| 60 | +} |
| 61 | + |
| 62 | +const sharedRules = { |
| 63 | + 'unicorn/consistent-function-scoping': 'error', |
| 64 | + curly: 'error', |
| 65 | + 'line-comment-position': ['error', { position: 'above' }], |
| 66 | + 'no-await-in-loop': 'error', |
| 67 | + 'no-control-regex': 'error', |
| 68 | + 'no-empty': ['error', { allowEmptyCatch: true }], |
| 69 | + 'no-new': 'error', |
| 70 | + 'no-proto': 'error', |
| 71 | + 'no-undef': 'error', |
| 72 | + 'no-unused-vars': [ |
| 73 | + 'error', |
| 74 | + { |
| 75 | + argsIgnorePattern: '^_|^this$', |
| 76 | + ignoreRestSiblings: true, |
| 77 | + varsIgnorePattern: '^_', |
| 78 | + }, |
| 79 | + ], |
| 80 | + 'no-var': 'error', |
| 81 | + 'no-warning-comments': ['warn', { terms: ['fixme'] }], |
| 82 | + 'prefer-const': 'error', |
| 83 | + 'sort-destructure-keys/sort-destructure-keys': 'error', |
| 84 | + 'sort-imports': ['error', { ignoreDeclarationSort: true }], |
| 85 | +} |
| 86 | + |
| 87 | +const sharedRulesForImportX = { |
| 88 | + ...origImportXFlatConfigs.recommended.rules, |
| 89 | + 'import-x/extensions': [ |
| 90 | + 'error', |
| 91 | + 'never', |
| 92 | + { |
| 93 | + cjs: 'ignorePackages', |
| 94 | + js: 'ignorePackages', |
| 95 | + json: 'always', |
| 96 | + mjs: 'ignorePackages', |
| 97 | + mts: 'ignorePackages', |
| 98 | + ts: 'ignorePackages', |
| 99 | + }, |
| 100 | + ], |
| 101 | + 'import-x/order': [ |
| 102 | + 'warn', |
| 103 | + { |
| 104 | + groups: [ |
| 105 | + 'builtin', |
| 106 | + 'external', |
| 107 | + 'internal', |
| 108 | + ['parent', 'sibling', 'index'], |
| 109 | + 'type', |
| 110 | + ], |
| 111 | + pathGroups: [ |
| 112 | + { |
| 113 | + pattern: '@socket{registry,security}/**', |
| 114 | + group: 'internal', |
| 115 | + }, |
| 116 | + ], |
| 117 | + pathGroupsExcludedImportTypes: ['type'], |
| 118 | + 'newlines-between': 'always', |
| 119 | + alphabetize: { |
| 120 | + order: 'asc', |
| 121 | + }, |
| 122 | + }, |
| 123 | + ], |
| 124 | +} |
| 125 | + |
| 126 | +const sharedRulesForNode = { |
| 127 | + 'n/exports-style': ['error', 'module.exports'], |
| 128 | + 'n/no-missing-require': ['off'], |
| 129 | + // The n/no-unpublished-bin rule does does not support non-trivial glob |
| 130 | + // patterns used in package.json "files" fields. In those cases we simplify |
| 131 | + // the glob patterns used. |
| 132 | + 'n/no-unpublished-bin': 'error', |
| 133 | + 'n/no-unsupported-features/es-builtins': 'error', |
| 134 | + 'n/no-unsupported-features/es-syntax': 'error', |
| 135 | + 'n/no-unsupported-features/node-builtins': [ |
| 136 | + 'error', |
| 137 | + { |
| 138 | + ignores: [ |
| 139 | + 'fetch', |
| 140 | + 'fs.promises.cp', |
| 141 | + 'module.enableCompileCache', |
| 142 | + 'readline/promises', |
| 143 | + 'test', |
| 144 | + 'test.describe', |
| 145 | + ], |
| 146 | + version: constants.maintainedNodeVersions.current, |
| 147 | + }, |
| 148 | + ], |
| 149 | + 'n/prefer-node-protocol': 'error', |
| 150 | +} |
| 151 | + |
| 152 | +function getImportXFlatConfigs(isEsm) { |
| 153 | + return { |
| 154 | + recommended: { |
| 155 | + ...origImportXFlatConfigs.recommended, |
| 156 | + languageOptions: { |
| 157 | + ...origImportXFlatConfigs.recommended.languageOptions, |
| 158 | + ecmaVersion: LATEST, |
| 159 | + sourceType: isEsm ? 'module' : 'script', |
| 160 | + }, |
| 161 | + rules: { |
| 162 | + ...sharedRulesForImportX, |
| 163 | + 'import-x/no-named-as-default-member': 'off', |
| 164 | + }, |
| 165 | + }, |
| 166 | + typescript: { |
| 167 | + ...origImportXFlatConfigs.typescript, |
| 168 | + plugins: origImportXFlatConfigs.recommended.plugins, |
| 169 | + settings: { |
| 170 | + ...origImportXFlatConfigs.typescript.settings, |
| 171 | + 'import-x/resolver-next': [ |
| 172 | + createTypeScriptImportResolver({ |
| 173 | + project: rootTsConfigPath, |
| 174 | + }), |
| 175 | + ], |
| 176 | + }, |
| 177 | + rules: { |
| 178 | + ...sharedRulesForImportX, |
| 179 | + // TypeScript compilation already ensures that named imports exist in |
| 180 | + // the referenced module. |
| 181 | + 'import-x/named': 'off', |
| 182 | + 'import-x/no-named-as-default-member': 'off', |
| 183 | + 'import-x/no-unresolved': 'off', |
| 184 | + }, |
| 185 | + }, |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +const importFlatConfigsForScript = getImportXFlatConfigs(false) |
| 190 | +const importFlatConfigsForModule = getImportXFlatConfigs(true) |
| 191 | + |
| 192 | +module.exports = [ |
| 193 | + gitIgnores, |
| 194 | + biomeIgnores, |
| 195 | + { |
| 196 | + files: ['**/*.{cts,mts,ts}'], |
| 197 | + ...js.configs.recommended, |
| 198 | + ...importFlatConfigsForModule.typescript, |
| 199 | + languageOptions: { |
| 200 | + ...js.configs.recommended.languageOptions, |
| 201 | + ...importFlatConfigsForModule.typescript.languageOptions, |
| 202 | + globals: { |
| 203 | + ...js.configs.recommended.languageOptions?.globals, |
| 204 | + ...importFlatConfigsForModule.typescript.languageOptions?.globals, |
| 205 | + ...nodeGlobalsConfig, |
| 206 | + BufferConstructor: 'readonly', |
| 207 | + BufferEncoding: 'readonly', |
| 208 | + NodeJS: 'readonly', |
| 209 | + }, |
| 210 | + parser: tsParser, |
| 211 | + parserOptions: { |
| 212 | + ...js.configs.recommended.languageOptions?.parserOptions, |
| 213 | + ...importFlatConfigsForModule.typescript.languageOptions?.parserOptions, |
| 214 | + projectService: { |
| 215 | + ...importFlatConfigsForModule.typescript.languageOptions |
| 216 | + ?.parserOptions?.projectService, |
| 217 | + allowDefaultProject: [ |
| 218 | + // Allow configs. |
| 219 | + '*.config.mts', |
| 220 | + // Allow paths like src/utils/*.test.mts. |
| 221 | + 'src/*/*.test.mts', |
| 222 | + // Allow paths like src/commands/optimize/*.test.mts. |
| 223 | + 'src/*/*/*.test.mts', |
| 224 | + 'test/*.mts', |
| 225 | + ], |
| 226 | + defaultProject: 'tsconfig.json', |
| 227 | + tsconfigRootDir: rootPath, |
| 228 | + // Need this to glob the test files in /src. Otherwise it won't work. |
| 229 | + maximumDefaultProjectFileMatchCount_THIS_WILL_SLOW_DOWN_LINTING: 1_000_000, |
| 230 | + }, |
| 231 | + }, |
| 232 | + }, |
| 233 | + linterOptions: { |
| 234 | + ...js.configs.recommended.linterOptions, |
| 235 | + ...importFlatConfigsForModule.typescript.linterOptions, |
| 236 | + reportUnusedDisableDirectives: 'off', |
| 237 | + }, |
| 238 | + plugins: { |
| 239 | + ...js.configs.recommended.plugins, |
| 240 | + ...importFlatConfigsForModule.typescript.plugins, |
| 241 | + ...nodePlugin.configs['flat/recommended-module'].plugins, |
| 242 | + ...sharedPlugins, |
| 243 | + '@typescript-eslint': tsEslint.plugin, |
| 244 | + }, |
| 245 | + rules: { |
| 246 | + ...js.configs.recommended.rules, |
| 247 | + ...importFlatConfigsForModule.typescript.rules, |
| 248 | + ...nodePlugin.configs['flat/recommended-module'].rules, |
| 249 | + ...sharedRulesForNode, |
| 250 | + ...sharedRules, |
| 251 | + '@typescript-eslint/array-type': ['error', { default: 'array-simple' }], |
| 252 | + '@typescript-eslint/consistent-type-assertions': [ |
| 253 | + 'error', |
| 254 | + { assertionStyle: 'as' }, |
| 255 | + ], |
| 256 | + '@typescript-eslint/no-misused-new': 'error', |
| 257 | + '@typescript-eslint/no-this-alias': [ |
| 258 | + 'error', |
| 259 | + { allowDestructuring: true }, |
| 260 | + ], |
| 261 | + // Returning unawaited promises in a try/catch/finally is dangerous |
| 262 | + // (the `catch` won't catch if the promise is rejected, and the `finally` |
| 263 | + // won't wait for the promise to resolve). Returning unawaited promises |
| 264 | + // elsewhere is probably fine, but this lint rule doesn't have a way |
| 265 | + // to only apply to try/catch/finally (the 'in-try-catch' option *enforces* |
| 266 | + // not awaiting promises *outside* of try/catch/finally, which is not what |
| 267 | + // we want), and it's nice to await before returning anyways, since you get |
| 268 | + // a slightly more comprehensive stack trace upon promise rejection. |
| 269 | + '@typescript-eslint/return-await': ['error', 'always'], |
| 270 | + // Disable the following rules because they don't play well with TypeScript. |
| 271 | + 'n/hashbang': 'off', |
| 272 | + 'n/no-extraneous-import': 'off', |
| 273 | + 'n/no-missing-import': 'off', |
| 274 | + 'no-redeclare': 'off', |
| 275 | + 'no-unused-vars': 'off', |
| 276 | + }, |
| 277 | + }, |
| 278 | + { |
| 279 | + files: ['**/*.{cjs,js}'], |
| 280 | + ...js.configs.recommended, |
| 281 | + ...importFlatConfigsForScript.recommended, |
| 282 | + ...nodePlugin.configs['flat/recommended-script'], |
| 283 | + languageOptions: { |
| 284 | + ...js.configs.recommended.languageOptions, |
| 285 | + ...importFlatConfigsForModule.recommended.languageOptions, |
| 286 | + ...nodePlugin.configs['flat/recommended-script'].languageOptions, |
| 287 | + globals: { |
| 288 | + ...js.configs.recommended.languageOptions?.globals, |
| 289 | + ...importFlatConfigsForModule.recommended.languageOptions?.globals, |
| 290 | + ...nodePlugin.configs['flat/recommended-script'].languageOptions |
| 291 | + ?.globals, |
| 292 | + ...nodeGlobalsConfig, |
| 293 | + }, |
| 294 | + }, |
| 295 | + plugins: { |
| 296 | + ...js.configs.recommended.plugins, |
| 297 | + ...importFlatConfigsForScript.recommended.plugins, |
| 298 | + ...nodePlugin.configs['flat/recommended-script'].plugins, |
| 299 | + ...sharedPlugins, |
| 300 | + }, |
| 301 | + rules: { |
| 302 | + ...js.configs.recommended.rules, |
| 303 | + ...importFlatConfigsForScript.recommended.rules, |
| 304 | + ...nodePlugin.configs['flat/recommended-script'].rules, |
| 305 | + ...sharedRulesForNode, |
| 306 | + ...sharedRules, |
| 307 | + }, |
| 308 | + }, |
| 309 | + { |
| 310 | + files: ['**/*.mjs'], |
| 311 | + ...js.configs.recommended, |
| 312 | + ...importFlatConfigsForModule.recommended, |
| 313 | + ...nodePlugin.configs['flat/recommended-module'], |
| 314 | + languageOptions: { |
| 315 | + ...js.configs.recommended.languageOptions, |
| 316 | + ...importFlatConfigsForModule.recommended.languageOptions, |
| 317 | + ...nodePlugin.configs['flat/recommended-module'].languageOptions, |
| 318 | + globals: { |
| 319 | + ...js.configs.recommended.languageOptions?.globals, |
| 320 | + ...importFlatConfigsForModule.recommended.languageOptions?.globals, |
| 321 | + ...nodePlugin.configs['flat/recommended-module'].languageOptions |
| 322 | + ?.globals, |
| 323 | + ...nodeGlobalsConfig, |
| 324 | + }, |
| 325 | + }, |
| 326 | + plugins: { |
| 327 | + ...js.configs.recommended.plugins, |
| 328 | + ...importFlatConfigsForModule.recommended.plugins, |
| 329 | + ...nodePlugin.configs['flat/recommended-module'].plugins, |
| 330 | + ...sharedPlugins, |
| 331 | + }, |
| 332 | + rules: { |
| 333 | + ...js.configs.recommended.rules, |
| 334 | + ...importFlatConfigsForModule.recommended.rules, |
| 335 | + ...nodePlugin.configs['flat/recommended-module'].rules, |
| 336 | + ...sharedRulesForNode, |
| 337 | + ...sharedRules, |
| 338 | + }, |
| 339 | + }, |
| 340 | +] |
0 commit comments