|
| 1 | +import { fixupPluginRules } from '@eslint/compat'; |
| 2 | +import { FlatCompat } from '@eslint/eslintrc'; |
| 3 | +import js from '@eslint/js'; |
| 4 | +import importX from 'eslint-plugin-import-x'; |
| 5 | +import * as mdx from 'eslint-plugin-mdx'; |
| 6 | +import noRelativeImportPaths from 'eslint-plugin-no-relative-import-paths'; |
| 7 | +import react from 'eslint-plugin-react'; |
| 8 | +import storybook from 'eslint-plugin-storybook'; |
| 9 | +import tseslint from 'typescript-eslint'; |
| 10 | + |
| 11 | +const compat = new FlatCompat(); |
| 12 | +const pluginToPatch = '@next/next'; |
| 13 | + |
| 14 | +const compatConfig = compat |
| 15 | + .config({ |
| 16 | + extends: [ |
| 17 | + // https://github.com/vercel/next.js/discussions/49337 |
| 18 | + 'plugin:@next/eslint-plugin-next/core-web-vitals', |
| 19 | + |
| 20 | + // https://github.com/facebook/react/issues/28313 |
| 21 | + 'plugin:react-hooks/recommended', |
| 22 | + ], |
| 23 | + }) |
| 24 | + .map(entry => { |
| 25 | + if (Object.hasOwn(entry.plugins, pluginToPatch)) { |
| 26 | + entry.plugins[pluginToPatch] = fixupPluginRules( |
| 27 | + entry.plugins[pluginToPatch] |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + return entry; |
| 32 | + }); |
| 33 | + |
| 34 | +export default tseslint.config( |
| 35 | + { |
| 36 | + ignores: [ |
| 37 | + '.next', |
| 38 | + '.swc', |
| 39 | + '.turbo', |
| 40 | + 'build', |
| 41 | + 'coverage', |
| 42 | + 'global.d.ts', |
| 43 | + 'junit.xml', |
| 44 | + 'storybook-static/**', |
| 45 | + ], |
| 46 | + }, |
| 47 | + { |
| 48 | + extends: [ |
| 49 | + js.configs.recommended, |
| 50 | + importX.flatConfigs.recommended, |
| 51 | + importX.flatConfigs.typescript, |
| 52 | + react.configs.flat['jsx-runtime'], |
| 53 | + ...tseslint.configs.recommended, |
| 54 | + ...compatConfig, |
| 55 | + ], |
| 56 | + files: ['**/*.{js,md,mdx,mjs,ts,tsx}'], |
| 57 | + plugins: { |
| 58 | + 'no-relative-import-paths': noRelativeImportPaths, |
| 59 | + }, |
| 60 | + rules: { |
| 61 | + '@typescript-eslint/array-type': ['error', { default: 'generic' }], |
| 62 | + '@typescript-eslint/consistent-type-imports': 'error', |
| 63 | + '@typescript-eslint/no-require-imports': 'off', |
| 64 | + 'import-x/namespace': 'off', |
| 65 | + 'import-x/no-named-as-default-member': 'off', |
| 66 | + 'import-x/no-unresolved': 'off', |
| 67 | + 'import-x/order': [ |
| 68 | + 'error', |
| 69 | + { |
| 70 | + groups: [ |
| 71 | + 'builtin', |
| 72 | + 'external', |
| 73 | + 'internal', |
| 74 | + ['sibling', 'parent'], |
| 75 | + 'index', |
| 76 | + 'unknown', |
| 77 | + ], |
| 78 | + 'newlines-between': 'always', |
| 79 | + alphabetize: { |
| 80 | + order: 'asc', |
| 81 | + caseInsensitive: true, |
| 82 | + }, |
| 83 | + }, |
| 84 | + ], |
| 85 | + 'no-relative-import-paths/no-relative-import-paths': [ |
| 86 | + 'warn', |
| 87 | + { allowSameFolder: true, prefix: '@' }, |
| 88 | + ], |
| 89 | + }, |
| 90 | + settings: { |
| 91 | + react: { |
| 92 | + version: 'detect', |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + { |
| 97 | + files: ['**/*.{md,mdx}'], |
| 98 | + extends: [mdx.configs.flat], |
| 99 | + rules: { |
| 100 | + 'no-irregular-whitespace': 'off', |
| 101 | + '@next/next/no-img-element': 'off', |
| 102 | + |
| 103 | + // https://github.com/typescript-eslint/typescript-eslint/issues/9860 |
| 104 | + '@typescript-eslint/consistent-type-imports': 'off', |
| 105 | + }, |
| 106 | + }, |
| 107 | + { |
| 108 | + files: ['**/*.{mdx,tsx}'], |
| 109 | + rules: { |
| 110 | + '@typescript-eslint/consistent-type-definitions': ['error', 'type'], |
| 111 | + 'react/no-unescaped-entities': 'off', |
| 112 | + 'react/function-component-definition': [ |
| 113 | + 'error', |
| 114 | + { |
| 115 | + namedComponents: 'arrow-function', |
| 116 | + unnamedComponents: 'arrow-function', |
| 117 | + }, |
| 118 | + ], |
| 119 | + 'no-restricted-syntax': [ |
| 120 | + 'error', |
| 121 | + { |
| 122 | + selector: |
| 123 | + "ImportDeclaration[source.value='react'][specifiers.0.type='ImportDefaultSpecifier']", |
| 124 | + message: |
| 125 | + 'Default React import not allowed since we use the TypeScript jsx-transform. If you need a global type that collides with a React named export (such as `MouseEvent`), try using `globalThis.MouseHandler`', |
| 126 | + }, |
| 127 | + { |
| 128 | + selector: |
| 129 | + "ImportDeclaration[source.value='react'] :matches(ImportNamespaceSpecifier)", |
| 130 | + message: |
| 131 | + 'Named * React import is not allowed. Please import what you need from React with Named Imports', |
| 132 | + }, |
| 133 | + ], |
| 134 | + }, |
| 135 | + }, |
| 136 | + { |
| 137 | + files: ['.storybook/**', '**/*.mjs', '**/*.test.*'], |
| 138 | + rules: { |
| 139 | + 'no-relative-import-paths/no-relative-import-paths': 'off', |
| 140 | + }, |
| 141 | + }, |
| 142 | + { |
| 143 | + files: ['components/**/*.stories.tsx'], |
| 144 | + extends: [...storybook.configs['flat/recommended']], |
| 145 | + } |
| 146 | +); |
0 commit comments