You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

eslint.config.mjs 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import {
  2. GLOB_TESTS, combine, javascript, node,
  3. stylistic, typescript, unicorn,
  4. } from '@antfu/eslint-config'
  5. import globals from 'globals'
  6. import storybook from 'eslint-plugin-storybook'
  7. // import { fixupConfigRules } from '@eslint/compat'
  8. import tailwind from 'eslint-plugin-tailwindcss'
  9. import reactHooks from 'eslint-plugin-react-hooks'
  10. import sonar from 'eslint-plugin-sonarjs'
  11. // import reactRefresh from 'eslint-plugin-react-refresh'
  12. export default combine(
  13. stylistic({
  14. lessOpinionated: true,
  15. // original @antfu/eslint-config does not support jsx
  16. jsx: false,
  17. semi: false,
  18. quotes: 'single',
  19. overrides: {
  20. // original config
  21. 'style/indent': ['error', 2],
  22. 'style/quotes': ['error', 'single'],
  23. 'curly': ['error', 'multi-or-nest', 'consistent'],
  24. 'style/comma-spacing': ['error', { before: false, after: true }],
  25. 'style/quote-props': ['warn', 'consistent-as-needed'],
  26. // these options does not exist in old version
  27. // maybe useless
  28. 'style/indent-binary-ops': 'off',
  29. 'style/multiline-ternary': 'off',
  30. 'antfu/top-level-function': 'off',
  31. 'antfu/curly': 'off',
  32. 'antfu/consistent-chaining': 'off',
  33. // copy from eslint-config-antfu 0.36.0
  34. 'style/brace-style': ['error', 'stroustrup', { allowSingleLine: true }],
  35. 'style/dot-location': ['error', 'property'],
  36. 'style/object-curly-newline': ['error', { consistent: true, multiline: true }],
  37. 'style/template-curly-spacing': ['error', 'never'],
  38. 'style/keyword-spacing': 'off',
  39. // not exist in old version, and big change
  40. 'style/member-delimiter-style': 'off',
  41. },
  42. }),
  43. javascript({
  44. overrides: {
  45. // handled by unused-imports/no-unused-vars
  46. 'no-unused-vars': 'off',
  47. },
  48. }),
  49. typescript({
  50. overrides: {
  51. // original config
  52. 'ts/consistent-type-definitions': ['warn', 'type'],
  53. // useful, but big change
  54. 'ts/no-empty-object-type': 'off',
  55. },
  56. }),
  57. unicorn(),
  58. node(),
  59. // use nextjs config will break @eslint/config-inspector
  60. // use `ESLINT_CONFIG_INSPECTOR=true pnpx @eslint/config-inspector` to check the config
  61. // ...process.env.ESLINT_CONFIG_INSPECTOR
  62. // ? []
  63. {
  64. rules: {
  65. // performance issue, and not used.
  66. '@next/next/no-html-link-for-pages': 'off',
  67. },
  68. },
  69. {
  70. ignores: [
  71. '**/node_modules/*',
  72. '**/dist/',
  73. '**/build/',
  74. '**/out/',
  75. '**/.next/',
  76. '**/public/*',
  77. '**/*.json',
  78. ],
  79. },
  80. {
  81. // orignal config
  82. rules: {
  83. // orignal ts/no-var-requires
  84. 'ts/no-require-imports': 'off',
  85. 'no-console': 'off',
  86. 'react-hooks/exhaustive-deps': 'warn',
  87. 'react/display-name': 'off',
  88. 'array-callback-return': ['error', {
  89. allowImplicit: false,
  90. checkForEach: false,
  91. }],
  92. // copy from eslint-config-antfu 0.36.0
  93. 'camelcase': 'off',
  94. 'default-case-last': 'error',
  95. // antfu use eslint-plugin-perfectionist to replace this
  96. // will cause big change, so keep the original sort-imports
  97. 'sort-imports': [
  98. 'error',
  99. {
  100. ignoreCase: false,
  101. ignoreDeclarationSort: true,
  102. ignoreMemberSort: false,
  103. memberSyntaxSortOrder: ['none', 'all', 'multiple', 'single'],
  104. allowSeparatedGroups: false,
  105. },
  106. ],
  107. // antfu migrate to eslint-plugin-unused-imports
  108. 'unused-imports/no-unused-vars': 'warn',
  109. 'unused-imports/no-unused-imports': 'warn',
  110. // We use `import { noop } from 'lodash-es'` across `web` project
  111. 'no-empty-function': 'error',
  112. },
  113. languageOptions: {
  114. globals: {
  115. ...globals.browser,
  116. ...globals.es2025,
  117. ...globals.node,
  118. React: 'readable',
  119. JSX: 'readable',
  120. },
  121. },
  122. },
  123. storybook.configs['flat/recommended'],
  124. // reactRefresh.configs.recommended,
  125. {
  126. rules: reactHooks.configs.recommended.rules,
  127. plugins: {
  128. 'react-hooks': reactHooks,
  129. },
  130. },
  131. // sonar
  132. {
  133. rules: {
  134. ...sonar.configs.recommended.rules,
  135. // code complexity
  136. 'sonarjs/cognitive-complexity': 'off',
  137. 'sonarjs/no-nested-functions': 'warn',
  138. 'sonarjs/no-nested-conditional': 'warn',
  139. 'sonarjs/nested-control-flow': 'warn', // 3 levels of nesting
  140. 'sonarjs/no-small-switch': 'off',
  141. 'sonarjs/no-nested-template-literals': 'warn',
  142. 'sonarjs/redundant-type-aliases': 'off',
  143. 'sonarjs/regex-complexity': 'warn',
  144. // maintainability
  145. 'sonarjs/no-ignored-exceptions': 'off',
  146. 'sonarjs/no-commented-code': 'warn',
  147. 'sonarjs/no-unused-vars': 'warn',
  148. 'sonarjs/prefer-single-boolean-return': 'warn',
  149. 'sonarjs/duplicates-in-character-class': 'off',
  150. 'sonarjs/single-char-in-character-classes': 'off',
  151. 'sonarjs/anchor-precedence': 'warn',
  152. 'sonarjs/updated-loop-counter': 'off',
  153. 'sonarjs/no-dead-store': 'warn',
  154. 'sonarjs/no-duplicated-branches': 'warn',
  155. 'sonarjs/max-lines': 'warn', // max 1000 lines
  156. 'sonarjs/no-variable-usage-before-declaration': 'error',
  157. // security
  158. 'sonarjs/no-hardcoded-passwords': 'off', // detect the wrong code that is not password.
  159. 'sonarjs/no-hardcoded-secrets': 'off',
  160. 'sonarjs/pseudo-random': 'off',
  161. // performance
  162. 'sonarjs/slow-regex': 'warn',
  163. // others
  164. 'sonarjs/todo-tag': 'warn',
  165. 'sonarjs/table-header': 'off',
  166. },
  167. plugins: {
  168. sonarjs: sonar,
  169. },
  170. },
  171. // need further research
  172. {
  173. rules: {
  174. // not exist in old version
  175. 'antfu/consistent-list-newline': 'off',
  176. 'node/prefer-global/process': 'off',
  177. 'node/prefer-global/buffer': 'off',
  178. 'node/no-callback-literal': 'off',
  179. // useful, but big change
  180. 'unicorn/prefer-number-properties': 'warn',
  181. 'unicorn/no-new-array': 'warn',
  182. 'style/indent': 'off',
  183. },
  184. },
  185. // suppress error for `no-undef` rule
  186. {
  187. files: GLOB_TESTS,
  188. languageOptions: {
  189. globals: {
  190. ...globals.browser,
  191. ...globals.es2021,
  192. ...globals.node,
  193. ...globals.jest,
  194. },
  195. },
  196. },
  197. tailwind.configs['flat/recommended'],
  198. {
  199. settings: {
  200. tailwindcss: {
  201. // These are the default values but feel free to customize
  202. callees: ['classnames', 'clsx', 'ctl', 'cn', 'classNames'],
  203. config: 'tailwind.config.js', // returned from `loadConfig()` utility if not provided
  204. cssFiles: [
  205. '**/*.css',
  206. '!**/node_modules',
  207. '!**/.*',
  208. '!**/dist',
  209. '!**/build',
  210. '!**/.storybook',
  211. '!**/.next',
  212. '!**/.public',
  213. ],
  214. cssFilesRefreshRate: 5_000,
  215. removeDuplicates: true,
  216. skipClassAttribute: false,
  217. whitelist: [],
  218. tags: [], // can be set to e.g. ['tw'] for use in tw`bg-blue`
  219. classRegex: '^class(Name)?$', // can be modified to support custom attributes. E.g. "^tw$" for `twin.macro`
  220. },
  221. },
  222. rules: {
  223. // due to 1k lines of tailwind config, these rule have performance issue
  224. 'tailwindcss/no-contradicting-classname': 'off',
  225. 'tailwindcss/enforces-shorthand': 'off',
  226. 'tailwindcss/no-custom-classname': 'off',
  227. 'tailwindcss/no-unnecessary-arbitrary-value': 'off',
  228. 'tailwindcss/no-arbitrary-value': 'off',
  229. 'tailwindcss/classnames-order': 'warn',
  230. 'tailwindcss/enforces-negative-arbitrary-values': 'warn',
  231. 'tailwindcss/migration-from-tailwind-2': 'warn',
  232. },
  233. },
  234. )