.eslintrc.js 10 KB

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