.eslintrc.js 10 KB

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