.eslintrc.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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/no-unknown-property': 'off',
  114. 'react/react-in-jsx-scope': 'off', // not needed with new JSX transform
  115. 'react/self-closing-comp': 'error',
  116. // recommended values found in https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/src/index.js
  117. 'jsx-a11y/accessible-emoji': 'warn',
  118. 'jsx-a11y/click-events-have-key-events': 'off',
  119. 'jsx-a11y/label-has-associated-control': 'off',
  120. 'jsx-a11y/media-has-caption': 'off',
  121. 'jsx-a11y/no-autofocus': 'off',
  122. // recommended rule is:
  123. // 'jsx-a11y/no-interactive-element-to-noninteractive-role': [
  124. // 'error',
  125. // {
  126. // tr: ['none', 'presentation'],
  127. // canvas: ['img'],
  128. // },
  129. // ],
  130. 'jsx-a11y/no-interactive-element-to-noninteractive-role': 'off',
  131. // recommended rule is:
  132. // 'jsx-a11y/no-noninteractive-element-interactions': [
  133. // 'error',
  134. // {
  135. // body: ['onError', 'onLoad'],
  136. // iframe: ['onError', 'onLoad'],
  137. // img: ['onError', 'onLoad'],
  138. // },
  139. // ],
  140. 'jsx-a11y/no-noninteractive-element-interactions': [
  141. 'warn',
  142. {
  143. handlers: [
  144. 'onClick',
  145. ],
  146. },
  147. ],
  148. // recommended rule is:
  149. // 'jsx-a11y/no-noninteractive-tabindex': [
  150. // 'error',
  151. // {
  152. // tags: [],
  153. // roles: ['tabpanel'],
  154. // allowExpressionValues: true,
  155. // },
  156. // ],
  157. 'jsx-a11y/no-noninteractive-tabindex': 'off',
  158. 'jsx-a11y/no-onchange': 'warn',
  159. // recommended is full 'error'
  160. 'jsx-a11y/no-static-element-interactions': [
  161. 'warn',
  162. {
  163. handlers: [
  164. 'onClick',
  165. ],
  166. },
  167. ],
  168. // See https://github.com/import-js/eslint-plugin-import/blob/main/config/recommended.js
  169. 'import/extensions': [
  170. 'error',
  171. 'always',
  172. {
  173. js: 'never',
  174. jsx: 'never',
  175. mjs: 'never',
  176. ts: 'never',
  177. tsx: 'never',
  178. },
  179. ],
  180. 'import/first': 'error',
  181. 'import/newline-after-import': 'error',
  182. 'import/no-anonymous-default-export': 'error',
  183. 'import/no-extraneous-dependencies': [
  184. 'error',
  185. {
  186. devDependencies: [
  187. '.eslintrc.js',
  188. 'config/webpack/**',
  189. 'app/javascript/mastodon/performance.js',
  190. 'app/javascript/mastodon/test_setup.js',
  191. 'app/javascript/**/__tests__/**',
  192. ],
  193. },
  194. ],
  195. 'import/no-amd': 'error',
  196. 'import/no-commonjs': 'error',
  197. 'import/no-import-module-exports': 'error',
  198. 'import/no-relative-packages': 'error',
  199. 'import/no-self-import': 'error',
  200. 'import/no-useless-path-segments': 'error',
  201. 'import/no-webpack-loader-syntax': 'error',
  202. 'import/order': [
  203. 'error',
  204. {
  205. alphabetize: { order: 'asc' },
  206. 'newlines-between': 'always',
  207. groups: [
  208. 'builtin',
  209. 'external',
  210. 'internal',
  211. 'parent',
  212. ['index', 'sibling'],
  213. 'object',
  214. ],
  215. pathGroups: [
  216. // React core packages
  217. {
  218. pattern: '{react,react-dom,react-dom/client,prop-types}',
  219. group: 'builtin',
  220. position: 'after',
  221. },
  222. // I18n
  223. {
  224. pattern: '{react-intl,intl-messageformat}',
  225. group: 'builtin',
  226. position: 'after',
  227. },
  228. // Common React utilities
  229. {
  230. pattern: '{classnames,react-helmet,react-router,react-router-dom}',
  231. group: 'external',
  232. position: 'before',
  233. },
  234. // Immutable / Redux / data store
  235. {
  236. pattern: '{immutable,react-redux,react-immutable-proptypes,react-immutable-pure-component,reselect}',
  237. group: 'external',
  238. position: 'before',
  239. },
  240. // Internal packages
  241. {
  242. pattern: '{mastodon/**}',
  243. group: 'internal',
  244. position: 'after',
  245. },
  246. ],
  247. pathGroupsExcludedImportTypes: [],
  248. },
  249. ],
  250. 'promise/always-return': 'off',
  251. 'promise/catch-or-return': [
  252. 'error',
  253. {
  254. allowFinally: true,
  255. },
  256. ],
  257. 'promise/no-callback-in-promise': 'off',
  258. 'promise/no-nesting': 'off',
  259. 'promise/no-promise-in-callback': 'off',
  260. 'formatjs/blocklist-elements': 'error',
  261. 'formatjs/enforce-default-message': ['error', 'literal'],
  262. 'formatjs/enforce-description': 'off', // description values not currently used
  263. 'formatjs/enforce-id': 'off', // Explicit IDs are used in the project
  264. 'formatjs/enforce-placeholders': 'off', // Issues in short_number.jsx
  265. 'formatjs/enforce-plural-rules': 'error',
  266. 'formatjs/no-camel-case': 'off', // disabledAccount is only non-conforming
  267. 'formatjs/no-complex-selectors': 'error',
  268. 'formatjs/no-emoji': 'error',
  269. 'formatjs/no-id': 'off', // IDs are used for translation keys
  270. 'formatjs/no-invalid-icu': 'error',
  271. 'formatjs/no-literal-string-in-jsx': 'off', // Should be looked at, but mainly flagging punctuation outside of strings
  272. 'formatjs/no-multiple-plurals': 'off', // Only used by hashtag.jsx
  273. 'formatjs/no-multiple-whitespaces': 'error',
  274. 'formatjs/no-offset': 'error',
  275. 'formatjs/no-useless-message': 'error',
  276. 'formatjs/prefer-formatted-message': 'error',
  277. 'formatjs/prefer-pound-in-plural': 'error',
  278. 'jsdoc/check-types': 'off',
  279. 'jsdoc/no-undefined-types': 'off',
  280. 'jsdoc/require-jsdoc': 'off',
  281. 'jsdoc/require-param-description': 'off',
  282. 'jsdoc/require-property-description': 'off',
  283. 'jsdoc/require-returns-description': 'off',
  284. 'jsdoc/require-returns': 'off',
  285. },
  286. overrides: [
  287. {
  288. files: [
  289. '.eslintrc.js',
  290. '*.config.js',
  291. '.*rc.js',
  292. 'ide-helper.js',
  293. 'config/webpack/**/*',
  294. 'config/formatjs-formatter.js',
  295. ],
  296. env: {
  297. commonjs: true,
  298. },
  299. parserOptions: {
  300. sourceType: 'script',
  301. },
  302. rules: {
  303. 'import/no-commonjs': 'off',
  304. },
  305. },
  306. {
  307. files: [
  308. '**/*.ts',
  309. '**/*.tsx',
  310. ],
  311. extends: [
  312. 'eslint:recommended',
  313. 'plugin:@typescript-eslint/strict-type-checked',
  314. 'plugin:@typescript-eslint/stylistic-type-checked',
  315. 'plugin:react/recommended',
  316. 'plugin:react-hooks/recommended',
  317. 'plugin:jsx-a11y/recommended',
  318. 'plugin:import/recommended',
  319. 'plugin:import/typescript',
  320. 'plugin:promise/recommended',
  321. 'plugin:jsdoc/recommended-typescript',
  322. 'plugin:prettier/recommended',
  323. ],
  324. parserOptions: {
  325. project: true,
  326. tsconfigRootDir: __dirname,
  327. },
  328. rules: {
  329. 'import/consistent-type-specifier-style': ['error', 'prefer-top-level'],
  330. '@typescript-eslint/consistent-type-definitions': ['warn', 'interface'],
  331. '@typescript-eslint/consistent-type-exports': 'error',
  332. '@typescript-eslint/consistent-type-imports': 'error',
  333. "@typescript-eslint/prefer-nullish-coalescing": ['error', {ignorePrimitives: {boolean: true}}],
  334. 'jsdoc/require-jsdoc': 'off',
  335. // Those rules set stricter rules for TS files
  336. // to enforce better practices when converting from JS
  337. 'import/no-default-export': 'warn',
  338. 'react/prefer-stateless-function': 'warn',
  339. 'react/function-component-definition': ['error', { namedComponents: 'arrow-function' }],
  340. 'react/jsx-uses-react': 'off', // not needed with new JSX transform
  341. 'react/react-in-jsx-scope': 'off', // not needed with new JSX transform
  342. 'react/prop-types': 'off',
  343. },
  344. },
  345. {
  346. files: [
  347. '**/__tests__/*.js',
  348. '**/__tests__/*.jsx',
  349. ],
  350. env: {
  351. jest: true,
  352. },
  353. }
  354. ],
  355. });