.eslintrc.js 10 KB

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