.eslintrc.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // @ts-check
  2. const { defineConfig } = require('eslint-define-config');
  3. module.exports = defineConfig({
  4. root: true,
  5. extends: [
  6. 'eslint:recommended',
  7. 'plugin:react/recommended',
  8. 'plugin:react-hooks/recommended',
  9. 'plugin:jsx-a11y/recommended',
  10. 'plugin:import/recommended',
  11. 'plugin:promise/recommended',
  12. 'plugin:jsdoc/recommended',
  13. ],
  14. env: {
  15. browser: true,
  16. node: true,
  17. es6: true,
  18. },
  19. parser: '@typescript-eslint/parser',
  20. plugins: [
  21. 'react',
  22. 'jsx-a11y',
  23. 'import',
  24. 'promise',
  25. '@typescript-eslint',
  26. 'formatjs',
  27. ],
  28. parserOptions: {
  29. sourceType: 'module',
  30. ecmaFeatures: {
  31. jsx: true,
  32. },
  33. ecmaVersion: 2021,
  34. requireConfigFile: false,
  35. babelOptions: {
  36. configFile: false,
  37. presets: ['@babel/react', '@babel/env'],
  38. },
  39. },
  40. settings: {
  41. react: {
  42. version: 'detect',
  43. },
  44. 'import/ignore': [
  45. 'node_modules',
  46. '\\.(css|scss|json)$',
  47. ],
  48. 'import/resolver': {
  49. typescript: {},
  50. },
  51. },
  52. rules: {
  53. 'consistent-return': 'error',
  54. 'dot-notation': 'error',
  55. eqeqeq: ['error', 'always', { 'null': 'ignore' }],
  56. 'indent': ['error', 2],
  57. 'jsx-quotes': ['error', 'prefer-single'],
  58. 'semi': ['error', 'always'],
  59. 'no-case-declarations': 'off',
  60. 'no-catch-shadow': 'error',
  61. 'no-console': [
  62. 'warn',
  63. {
  64. allow: [
  65. 'error',
  66. 'warn',
  67. ],
  68. },
  69. ],
  70. 'no-empty': ['error', { "allowEmptyCatch": true }],
  71. 'no-restricted-properties': [
  72. 'error',
  73. { property: 'substring', message: 'Use .slice instead of .substring.' },
  74. { property: 'substr', message: 'Use .slice instead of .substr.' },
  75. ],
  76. 'no-restricted-syntax': [
  77. 'error',
  78. {
  79. // eslint-disable-next-line no-restricted-syntax
  80. selector: 'Literal[value=/•/], JSXText[value=/•/]',
  81. // eslint-disable-next-line no-restricted-syntax
  82. message: "Use '·' (middle dot) instead of '•' (bullet)",
  83. },
  84. ],
  85. 'no-unused-expressions': 'error',
  86. 'no-unused-vars': 'off',
  87. '@typescript-eslint/no-unused-vars': [
  88. 'error',
  89. {
  90. vars: 'all',
  91. args: 'after-used',
  92. destructuredArrayIgnorePattern: '^_',
  93. ignoreRestSiblings: true,
  94. },
  95. ],
  96. 'valid-typeof': 'error',
  97. 'react/jsx-filename-extension': ['error', { extensions: ['.jsx', 'tsx'] }],
  98. 'react/jsx-boolean-value': 'error',
  99. 'react/display-name': 'off',
  100. 'react/jsx-fragments': ['error', 'syntax'],
  101. 'react/jsx-equals-spacing': 'error',
  102. 'react/jsx-no-bind': 'error',
  103. 'react/jsx-no-useless-fragment': 'error',
  104. 'react/jsx-no-target-blank': 'off',
  105. 'react/jsx-tag-spacing': 'error',
  106. 'react/jsx-uses-react': 'off', // not needed with new JSX transform
  107. 'react/jsx-wrap-multilines': 'error',
  108. 'react/react-in-jsx-scope': 'off', // not needed with new JSX transform
  109. 'react/self-closing-comp': 'error',
  110. // recommended values found in https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/v6.8.0/src/index.js#L46
  111. 'jsx-a11y/click-events-have-key-events': 'off',
  112. 'jsx-a11y/label-has-associated-control': 'off',
  113. 'jsx-a11y/media-has-caption': 'off',
  114. 'jsx-a11y/no-autofocus': 'off',
  115. // recommended rule is:
  116. // 'jsx-a11y/no-interactive-element-to-noninteractive-role': [
  117. // 'error',
  118. // {
  119. // tr: ['none', 'presentation'],
  120. // canvas: ['img'],
  121. // },
  122. // ],
  123. 'jsx-a11y/no-interactive-element-to-noninteractive-role': 'off',
  124. // recommended rule is:
  125. // 'jsx-a11y/no-noninteractive-tabindex': [
  126. // 'error',
  127. // {
  128. // tags: [],
  129. // roles: ['tabpanel'],
  130. // allowExpressionValues: true,
  131. // },
  132. // ],
  133. 'jsx-a11y/no-noninteractive-tabindex': 'off',
  134. // recommended is full 'error'
  135. 'jsx-a11y/no-static-element-interactions': [
  136. 'warn',
  137. {
  138. handlers: [
  139. 'onClick',
  140. ],
  141. },
  142. ],
  143. // See https://github.com/import-js/eslint-plugin-import/blob/v2.29.1/config/recommended.js
  144. 'import/extensions': [
  145. 'error',
  146. 'always',
  147. {
  148. js: 'never',
  149. jsx: 'never',
  150. mjs: 'never',
  151. ts: 'never',
  152. tsx: 'never',
  153. },
  154. ],
  155. 'import/first': 'error',
  156. 'import/newline-after-import': 'error',
  157. 'import/no-anonymous-default-export': 'error',
  158. 'import/no-extraneous-dependencies': [
  159. 'error',
  160. {
  161. devDependencies: [
  162. '.eslintrc.js',
  163. 'config/webpack/**',
  164. 'app/javascript/mastodon/performance.js',
  165. 'app/javascript/mastodon/test_setup.js',
  166. 'app/javascript/**/__tests__/**',
  167. ],
  168. },
  169. ],
  170. 'import/no-amd': 'error',
  171. 'import/no-commonjs': 'error',
  172. 'import/no-import-module-exports': 'error',
  173. 'import/no-relative-packages': 'error',
  174. 'import/no-self-import': 'error',
  175. 'import/no-useless-path-segments': 'error',
  176. 'import/no-webpack-loader-syntax': 'error',
  177. 'import/order': [
  178. 'error',
  179. {
  180. alphabetize: { order: 'asc' },
  181. 'newlines-between': 'always',
  182. groups: [
  183. 'builtin',
  184. 'external',
  185. 'internal',
  186. 'parent',
  187. ['index', 'sibling'],
  188. 'object',
  189. ],
  190. pathGroups: [
  191. // React core packages
  192. {
  193. pattern: '{react,react-dom,react-dom/client,prop-types}',
  194. group: 'builtin',
  195. position: 'after',
  196. },
  197. // I18n
  198. {
  199. pattern: '{react-intl,intl-messageformat}',
  200. group: 'builtin',
  201. position: 'after',
  202. },
  203. // Common React utilities
  204. {
  205. pattern: '{classnames,react-helmet,react-router,react-router-dom}',
  206. group: 'external',
  207. position: 'before',
  208. },
  209. // Immutable / Redux / data store
  210. {
  211. pattern: '{immutable,@reduxjs/toolkit,react-redux,react-immutable-proptypes,react-immutable-pure-component}',
  212. group: 'external',
  213. position: 'before',
  214. },
  215. // Internal packages
  216. {
  217. pattern: '{mastodon/**}',
  218. group: 'internal',
  219. position: 'after',
  220. },
  221. ],
  222. pathGroupsExcludedImportTypes: [],
  223. },
  224. ],
  225. 'promise/always-return': 'off',
  226. 'promise/catch-or-return': [
  227. 'error',
  228. {
  229. allowFinally: true,
  230. },
  231. ],
  232. 'promise/no-callback-in-promise': 'off',
  233. 'promise/no-nesting': 'off',
  234. 'promise/no-promise-in-callback': 'off',
  235. 'formatjs/blocklist-elements': 'error',
  236. 'formatjs/enforce-default-message': ['error', 'literal'],
  237. 'formatjs/enforce-description': 'off', // description values not currently used
  238. 'formatjs/enforce-id': 'off', // Explicit IDs are used in the project
  239. 'formatjs/enforce-placeholders': 'off', // Issues in short_number.jsx
  240. 'formatjs/enforce-plural-rules': 'error',
  241. 'formatjs/no-camel-case': 'off', // disabledAccount is only non-conforming
  242. 'formatjs/no-complex-selectors': 'error',
  243. 'formatjs/no-emoji': 'error',
  244. 'formatjs/no-id': 'off', // IDs are used for translation keys
  245. 'formatjs/no-invalid-icu': 'error',
  246. 'formatjs/no-literal-string-in-jsx': 'off', // Should be looked at, but mainly flagging punctuation outside of strings
  247. 'formatjs/no-multiple-whitespaces': 'error',
  248. 'formatjs/no-offset': 'error',
  249. 'formatjs/no-useless-message': 'error',
  250. 'formatjs/prefer-formatted-message': 'error',
  251. 'formatjs/prefer-pound-in-plural': 'error',
  252. 'jsdoc/check-types': 'off',
  253. 'jsdoc/no-undefined-types': 'off',
  254. 'jsdoc/require-jsdoc': 'off',
  255. 'jsdoc/require-param-description': 'off',
  256. 'jsdoc/require-property-description': 'off',
  257. 'jsdoc/require-returns-description': 'off',
  258. 'jsdoc/require-returns': 'off',
  259. },
  260. overrides: [
  261. {
  262. files: [
  263. '.eslintrc.js',
  264. '*.config.js',
  265. '.*rc.js',
  266. 'ide-helper.js',
  267. 'config/webpack/**/*',
  268. 'config/formatjs-formatter.js',
  269. ],
  270. env: {
  271. commonjs: true,
  272. },
  273. parserOptions: {
  274. sourceType: 'script',
  275. },
  276. rules: {
  277. 'import/no-commonjs': 'off',
  278. },
  279. },
  280. {
  281. files: [
  282. '**/*.ts',
  283. '**/*.tsx',
  284. ],
  285. extends: [
  286. 'eslint:recommended',
  287. 'plugin:@typescript-eslint/strict-type-checked',
  288. 'plugin:@typescript-eslint/stylistic-type-checked',
  289. 'plugin:react/recommended',
  290. 'plugin:react-hooks/recommended',
  291. 'plugin:jsx-a11y/recommended',
  292. 'plugin:import/recommended',
  293. 'plugin:import/typescript',
  294. 'plugin:promise/recommended',
  295. 'plugin:jsdoc/recommended-typescript',
  296. ],
  297. parserOptions: {
  298. projectService: true,
  299. tsconfigRootDir: __dirname,
  300. },
  301. rules: {
  302. // Disable formatting rules that have been enabled in the base config
  303. 'indent': 'off',
  304. // This is not needed as we use noImplicitReturns, which handles this in addition to understanding types
  305. 'consistent-return': 'off',
  306. 'import/consistent-type-specifier-style': ['error', 'prefer-top-level'],
  307. '@typescript-eslint/consistent-type-definitions': ['warn', 'interface'],
  308. '@typescript-eslint/consistent-type-exports': 'error',
  309. '@typescript-eslint/consistent-type-imports': 'error',
  310. "@typescript-eslint/prefer-nullish-coalescing": ['error', { ignorePrimitives: { boolean: true } }],
  311. "@typescript-eslint/no-restricted-imports": [
  312. "warn",
  313. {
  314. "name": "react-redux",
  315. "importNames": ["useSelector", "useDispatch"],
  316. "message": "Use typed hooks `useAppDispatch` and `useAppSelector` instead."
  317. }
  318. ],
  319. "@typescript-eslint/restrict-template-expressions": ['warn', { allowNumber: true }],
  320. 'jsdoc/require-jsdoc': 'off',
  321. // Those rules set stricter rules for TS files
  322. // to enforce better practices when converting from JS
  323. 'import/no-default-export': 'warn',
  324. 'react/prefer-stateless-function': 'warn',
  325. 'react/function-component-definition': ['error', { namedComponents: 'arrow-function' }],
  326. 'react/jsx-uses-react': 'off', // not needed with new JSX transform
  327. 'react/react-in-jsx-scope': 'off', // not needed with new JSX transform
  328. 'react/prop-types': 'off',
  329. },
  330. },
  331. {
  332. files: [
  333. '**/__tests__/*.js',
  334. '**/__tests__/*.jsx',
  335. ],
  336. env: {
  337. jest: true,
  338. },
  339. }
  340. ],
  341. });