1
0

.eslintrc.js 10 KB

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