eslint.config.mjs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. // @ts-check
  2. /**
  3. * To get started with this ESLint Configuration list be sure to read at least
  4. * these sections of the docs:
  5. * - https://eslint.org/docs/latest/use/configure/configuration-files#specifying-files-and-ignores
  6. * - https://eslint.org/docs/latest/use/configure/configuration-files#configuration-objects
  7. * - https://eslint.org/docs/latest/use/configure/configuration-files#cascading-configuration-objects
  8. *
  9. * This is your friend:
  10. * `npx eslint --inspect-config`
  11. */
  12. import * as emotion from '@emotion/eslint-plugin';
  13. import eslint from '@eslint/js';
  14. import {globalIgnores} from 'eslint/config';
  15. import prettier from 'eslint-config-prettier';
  16. // @ts-expect-error TS(7016): Could not find a declaration file
  17. import importPlugin from 'eslint-plugin-import';
  18. import jest from 'eslint-plugin-jest';
  19. import jestDom from 'eslint-plugin-jest-dom';
  20. import react from 'eslint-plugin-react';
  21. import reactHooks from 'eslint-plugin-react-hooks';
  22. // @ts-expect-error TS(7016): Could not find a declaration file
  23. import sentry from 'eslint-plugin-sentry';
  24. import simpleImportSort from 'eslint-plugin-simple-import-sort';
  25. import testingLibrary from 'eslint-plugin-testing-library';
  26. // @ts-expect-error TS (7016): Could not find a declaration file
  27. import typescriptSortKeys from 'eslint-plugin-typescript-sort-keys';
  28. import unicorn from 'eslint-plugin-unicorn';
  29. import globals from 'globals';
  30. import invariant from 'invariant';
  31. // biome-ignore lint/correctness/noNodejsModules: Need to get the list of things!
  32. import {builtinModules} from 'node:module';
  33. import typescript from 'typescript-eslint';
  34. invariant(react.configs.flat, 'For typescript');
  35. invariant(react.configs.flat.recommended, 'For typescript');
  36. invariant(react.configs.flat['jsx-runtime'], 'For typescript');
  37. const restrictedImportPatterns = [
  38. {
  39. group: ['sentry/components/devtoolbar/*'],
  40. message: 'Do not depend on toolbar internals',
  41. },
  42. ];
  43. const restrictedImportPaths = [
  44. {
  45. name: '@testing-library/react',
  46. message:
  47. 'Please import from `sentry-test/reactTestingLibrary` instead so that we can ensure consistency throughout the codebase',
  48. },
  49. {
  50. name: '@testing-library/react-hooks',
  51. message:
  52. 'Please import from `sentry-test/reactTestingLibrary` instead so that we can ensure consistency throughout the codebase',
  53. },
  54. {
  55. name: '@testing-library/user-event',
  56. message:
  57. 'Please import from `sentry-test/reactTestingLibrary` instead so that we can ensure consistency throughout the codebase',
  58. },
  59. {
  60. name: '@sentry/browser',
  61. message:
  62. 'Please import from `@sentry/react` to ensure consistency throughout the codebase.',
  63. },
  64. {
  65. name: 'marked',
  66. message:
  67. "Please import marked from 'app/utils/marked' so that we can ensure sanitation of marked output",
  68. },
  69. {
  70. name: 'lodash',
  71. message:
  72. "Please import lodash utilities individually. e.g. `import isEqual from 'lodash/isEqual';`. See https://github.com/getsentry/frontend-handbook#lodash from for information",
  73. },
  74. {
  75. name: 'lodash/get',
  76. message:
  77. 'Optional chaining `?.` and nullish coalescing operators `??` are available and preferred over using `lodash/get`. See https://github.com/getsentry/frontend-handbook#new-syntax for more information',
  78. },
  79. {
  80. name: 'sentry/utils/theme',
  81. importNames: ['lightColors', 'darkColors'],
  82. message:
  83. "'lightColors' and 'darkColors' exports intended for use in Storybook only. Instead, use theme prop from emotion or the useTheme hook.",
  84. },
  85. {
  86. name: 'react-router',
  87. importNames: ['withRouter'],
  88. message:
  89. "Use 'useLocation', 'useParams', 'useNavigate', 'useRoutes' from sentry/utils instead.",
  90. },
  91. {
  92. name: 'react-select',
  93. message: "Use 'sentry/components/forms/controls/reactSelectWrapper' instead.",
  94. },
  95. {
  96. name: 'sentry/utils/withSentryRouter',
  97. message:
  98. "Use 'useLocation', 'useParams', 'useNavigate', 'useRoutes' from sentry/utils instead.",
  99. },
  100. {
  101. name: 'qs',
  102. message: 'Please use query-string instead of qs',
  103. },
  104. {
  105. name: 'moment',
  106. message: 'Please import moment-timezone instead of moment',
  107. },
  108. ];
  109. // Used by both: `languageOptions` & `parserOptions`
  110. const ecmaVersion = 'latest';
  111. export default typescript.config([
  112. {
  113. // Main parser & linter options
  114. // Rules are defined below and inherit these properties
  115. // https://eslint.org/docs/latest/use/configure/configuration-files#configuration-objects
  116. name: 'eslint/global/languageOptions',
  117. languageOptions: {
  118. ecmaVersion,
  119. sourceType: 'module',
  120. globals: {
  121. // TODO(ryan953): globals.browser seems to have a bug with trailing whitespace
  122. ...Object.fromEntries(Object.keys(globals.browser).map(k => [k.trim(), false])),
  123. ...globals.jest,
  124. MockApiClient: true,
  125. tick: true,
  126. },
  127. parser: typescript.parser,
  128. parserOptions: {
  129. ecmaFeatures: {
  130. globalReturn: false,
  131. },
  132. ecmaVersion,
  133. // https://typescript-eslint.io/packages/parser/#emitdecoratormetadata
  134. emitDecoratorMetadata: undefined,
  135. // https://typescript-eslint.io/packages/parser/#experimentaldecorators
  136. experimentalDecorators: undefined,
  137. // https://typescript-eslint.io/packages/parser/#jsdocparsingmode
  138. jsDocParsingMode: process.env.SENTRY_DETECT_DEPRECATIONS ? 'all' : 'none',
  139. // https://typescript-eslint.io/packages/parser/#project
  140. project: process.env.SENTRY_DETECT_DEPRECATIONS ? './tsconfig.json' : false,
  141. // https://typescript-eslint.io/packages/parser/#projectservice
  142. // `projectService` is recommended, but slower, with our current tsconfig files.
  143. // projectService: true,
  144. // tsconfigRootDir: import.meta.dirname,
  145. },
  146. },
  147. linterOptions: {
  148. noInlineConfig: false,
  149. reportUnusedDisableDirectives: 'error',
  150. },
  151. settings: {
  152. react: {
  153. version: '18.2.0',
  154. defaultVersion: '18.2',
  155. },
  156. 'import/parsers': {'@typescript-eslint/parser': ['.ts', '.tsx']},
  157. 'import/resolver': {typescript: {}},
  158. 'import/extensions': ['.js', '.jsx'],
  159. },
  160. },
  161. {
  162. name: 'eslint/global/files',
  163. // Default file selection
  164. // https://eslint.org/docs/latest/use/configure/configuration-files#specifying-files-and-ignores
  165. files: ['**/*.js', '**/*.mjs', '**/*.ts', '**/*.jsx', '**/*.tsx'],
  166. },
  167. // Global ignores
  168. // https://eslint.org/docs/latest/use/configure/configuration-files#globally-ignoring-files-with-ignores
  169. globalIgnores([
  170. '.devenv/**/*',
  171. '.github/**/*',
  172. '.mypy_cache/**/*',
  173. '.pytest_cache/**/*',
  174. '.venv/**/*',
  175. '**/*.benchmark.ts',
  176. '**/*.d.ts',
  177. '**/dist/**/*',
  178. '**/tests/**/fixtures/**/*',
  179. '**/vendor/**/*',
  180. 'build-utils/**/*',
  181. 'config/chartcuterie/config.js',
  182. 'fixtures/artifact_bundle/**/*',
  183. 'fixtures/artifact_bundle_debug_ids/**/*',
  184. 'fixtures/artifact_bundle_duplicated_debug_ids/**/*',
  185. 'fixtures/profiles/embedded.js',
  186. 'jest.config.ts',
  187. 'api-docs/**/*',
  188. 'src/sentry/static/sentry/js/**/*',
  189. 'src/sentry/templates/sentry/**/*',
  190. 'stylelint.config.js',
  191. ]),
  192. /**
  193. * Rules are grouped by plugin. If you want to override a specific rule inside
  194. * the recommended set, then it's recommended to spread the new rule on top
  195. * of the predefined ones.
  196. *
  197. * For example: if you want to enable a new plugin in the codebase and their
  198. * recommended rules (or a new rule that's part of an existing plugin)
  199. *
  200. * 1. First you'd setup a configuration object for that plugin:
  201. * {
  202. * name: 'my-plugin/recommended',
  203. * ...myPlugin.configs.recommended,
  204. * },
  205. *
  206. * 2. Second you'd override the rule you want to deal with, maybe making it a
  207. * warning to start:
  208. * {
  209. * name: 'my-plugin/recommended',
  210. * ...myPlugin.configs.recommended,
  211. * rules: {
  212. * ['a-rule-outside-the-recommended-list']: 'error',
  213. *
  214. * ...myPlugin.configs.recommended.rules,
  215. * ['a-recommended-rule']: 'warn',
  216. * }
  217. * },
  218. *
  219. * 3. Finally, once all warnings are fixed, update from 'warning' to 'error',
  220. * or remove the override and rely on the recommended rules again.
  221. */
  222. {
  223. name: 'eslint/rules',
  224. // https://eslint.org/docs/latest/rules/
  225. rules: {
  226. 'array-callback-return': 'error',
  227. 'block-scoped-var': 'error',
  228. 'consistent-return': 'error',
  229. 'default-case': 'error',
  230. 'dot-notation': 'error',
  231. eqeqeq: 'error',
  232. 'guard-for-in': 'off', // TODO(ryan953): Fix violations and enable this rule
  233. 'multiline-comment-style': ['error', 'separate-lines'],
  234. 'no-alert': 'error',
  235. 'no-caller': 'error',
  236. 'no-console': 'error',
  237. 'no-else-return': ['error', {allowElseIf: false}],
  238. 'no-eval': 'error',
  239. 'no-extend-native': 'error',
  240. 'no-extra-bind': 'error',
  241. 'no-floating-decimal': 'error',
  242. 'no-implied-eval': 'error',
  243. 'no-inner-declarations': 'error',
  244. 'no-lone-blocks': 'error',
  245. 'no-loop-func': 'error',
  246. 'no-multi-str': 'error',
  247. 'no-native-reassign': 'error',
  248. 'no-new-func': 'error',
  249. 'no-new-wrappers': 'error',
  250. 'no-new': 'error',
  251. 'no-octal-escape': 'error',
  252. 'no-param-reassign': 'off', // TODO(ryan953): Fix violations and enable this rule
  253. 'no-proto': 'error',
  254. 'no-restricted-imports': [
  255. 'error',
  256. {patterns: restrictedImportPatterns, paths: restrictedImportPaths},
  257. ],
  258. 'no-return-assign': 'error',
  259. 'no-script-url': 'error',
  260. 'no-self-compare': 'error',
  261. 'no-sequences': 'error',
  262. 'no-throw-literal': 'error',
  263. 'object-shorthand': ['error', 'properties'],
  264. radix: 'error',
  265. 'require-await': 'error', // Enabled in favor of @typescript-eslint/require-await, which requires type info
  266. 'spaced-comment': [
  267. 'error',
  268. 'always',
  269. {
  270. line: {markers: ['/'], exceptions: ['-', '+']},
  271. block: {exceptions: ['*'], balanced: true},
  272. },
  273. ],
  274. strict: 'error',
  275. 'vars-on-top': 'off',
  276. 'wrap-iife': ['error', 'any'],
  277. yoda: 'error',
  278. // https://github.com/eslint/eslint/blob/main/packages/js/src/configs/eslint-recommended.js
  279. ...eslint.configs.recommended.rules,
  280. 'no-cond-assign': ['error', 'always'],
  281. 'no-prototype-builtins': 'off',
  282. 'no-useless-escape': 'off',
  283. },
  284. },
  285. {
  286. // https://github.com/import-js/eslint-plugin-import/tree/main/docs/rules
  287. ...importPlugin.flatConfigs.recommended,
  288. name: 'plugin/import',
  289. rules: {
  290. // https://github.com/import-js/eslint-plugin-import/blob/main/config/recommended.js
  291. ...importPlugin.flatConfigs.recommended.rules,
  292. 'import/newline-after-import': 'error', // https://prettier.io/docs/en/rationale.html#empty-lines
  293. 'import/no-absolute-path': 'error',
  294. 'import/no-amd': 'error',
  295. 'import/no-anonymous-default-export': 'error',
  296. 'import/no-duplicates': 'error',
  297. 'import/no-named-default': 'error',
  298. 'import/no-nodejs-modules': 'error',
  299. 'import/no-webpack-loader-syntax': 'error',
  300. 'import/default': 'off', // Disabled in favor of typescript-eslint
  301. 'import/named': 'off', // Disabled in favor of typescript-eslint
  302. 'import/namespace': 'off', // Disabled in favor of typescript-eslint
  303. 'import/no-named-as-default-member': 'off', // Disabled in favor of typescript-eslint
  304. 'import/no-named-as-default': 'off', // TODO(ryan953): Fix violations and enable this rule
  305. 'import/no-unresolved': 'off', // Disabled in favor of typescript-eslint
  306. },
  307. },
  308. {
  309. name: 'plugin/react',
  310. // https://github.com/jsx-eslint/eslint-plugin-react/tree/master/docs/rules
  311. plugins: {
  312. ...react.configs.flat.recommended.plugins,
  313. ...react.configs.flat['jsx-runtime'].plugins,
  314. },
  315. rules: {
  316. 'react/function-component-definition': 'error',
  317. 'react/jsx-boolean-value': ['error', 'never'],
  318. 'react/jsx-fragments': ['error', 'element'],
  319. 'react/jsx-handler-names': 'off', // TODO(ryan953): Fix violations and enable this rule
  320. 'react/no-did-mount-set-state': 'error',
  321. 'react/no-did-update-set-state': 'error',
  322. 'react/no-redundant-should-component-update': 'error',
  323. 'react/no-typos': 'error',
  324. 'react/self-closing-comp': 'error',
  325. 'react/sort-comp': 'error',
  326. // https://github.com/jsx-eslint/eslint-plugin-react/blob/master/index.js
  327. ...react.configs.flat.recommended.rules,
  328. ...react.configs.flat['jsx-runtime'].rules,
  329. 'react/display-name': 'off', // TODO(ryan953): Fix violations and delete this line
  330. 'react/no-unescaped-entities': 'off',
  331. 'react/no-unknown-property': ['error', {ignore: ['css']}],
  332. 'react/prop-types': 'off', // TODO(ryan953): Fix violations and delete this line
  333. },
  334. },
  335. {
  336. name: 'plugin/react-hooks',
  337. // https://github.com/facebook/react/tree/main/packages/eslint-plugin-react-hooks
  338. plugins: {'react-hooks': reactHooks},
  339. rules: {
  340. 'react-hooks/exhaustive-deps': [
  341. 'error',
  342. {additionalHooks: '(useEffectAfterFirstRender|useMemoWithPrevious)'},
  343. ],
  344. 'react-hooks/rules-of-hooks': 'error',
  345. },
  346. },
  347. {
  348. name: 'plugin/typescript-eslint/custom',
  349. rules: {
  350. 'no-shadow': 'off', // Disabled in favor of @typescript-eslint/no-shadow
  351. 'no-use-before-define': 'off', // See also @typescript-eslint/no-use-before-define
  352. '@typescript-eslint/naming-convention': [
  353. 'error',
  354. {selector: 'typeLike', format: ['PascalCase'], leadingUnderscore: 'allow'},
  355. {selector: 'enumMember', format: ['UPPER_CASE']},
  356. ],
  357. '@typescript-eslint/no-restricted-types': [
  358. 'error',
  359. {
  360. types: {
  361. object: {
  362. message:
  363. 'The `object` type is hard to use. Use `Record<PropertyKey, unknown>` instead. See: https://github.com/typescript-eslint/typescript-eslint/pull/848',
  364. fixWith: 'Record<PropertyKey, unknown>',
  365. },
  366. Buffer: {
  367. message:
  368. 'Use Uint8Array instead. See: https://sindresorhus.com/blog/goodbye-nodejs-buffer',
  369. suggest: ['Uint8Array'],
  370. },
  371. '[]': "Don't use the empty array type `[]`. It only allows empty arrays. Use `SomeType[]` instead.",
  372. '[[]]':
  373. "Don't use `[[]]`. It only allows an array with a single element which is an empty array. Use `SomeType[][]` instead.",
  374. '[[[]]]': "Don't use `[[[]]]`. Use `SomeType[][][]` instead.",
  375. },
  376. },
  377. ],
  378. '@typescript-eslint/no-shadow': 'error',
  379. '@typescript-eslint/no-use-before-define': 'off', // Enabling this will cause a lot of thrash to the git history
  380. '@typescript-eslint/no-useless-empty-export': 'error',
  381. },
  382. },
  383. // https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/base.ts
  384. // https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/eslint-recommended-raw.ts
  385. // https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/recommended.ts
  386. // https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/strict.ts
  387. // https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/stylistic.ts
  388. ...typescript.configs.strict.map(c => ({...c, name: `plugin/${c.name}`})),
  389. ...typescript.configs.stylistic.map(c => ({...c, name: `plugin/${c.name}`})),
  390. {
  391. name: 'plugin/typescript-eslint/overrides',
  392. // https://typescript-eslint.io/rules/
  393. plugins: {'@typescript-eslint': typescript.plugin},
  394. rules: {
  395. 'prefer-spread': 'off',
  396. '@typescript-eslint/prefer-enum-initializers': 'error',
  397. 'no-unused-expressions': 'off', // Disabled in favor of @typescript-eslint/no-unused-expressions
  398. '@typescript-eslint/no-unused-expressions': ['error', {allowTernary: true}],
  399. // Recommended overrides
  400. '@typescript-eslint/no-empty-object-type': ['error', {allowInterfaces: 'always'}],
  401. '@typescript-eslint/no-explicit-any': 'off',
  402. '@typescript-eslint/no-namespace': 'off',
  403. '@typescript-eslint/no-non-null-asserted-optional-chain': 'off', // TODO(ryan953): Fix violations and delete this line
  404. '@typescript-eslint/no-require-imports': 'off', // TODO(ryan953): Fix violations and delete this line
  405. '@typescript-eslint/no-this-alias': 'off', // TODO(ryan953): Fix violations and delete this line
  406. // Strict overrides
  407. '@typescript-eslint/no-dynamic-delete': 'off', // TODO(ryan953): Fix violations and delete this line
  408. '@typescript-eslint/no-invalid-void-type': 'off', // TODO(ryan953): Fix violations and delete this line
  409. '@typescript-eslint/no-non-null-assertion': 'off', // TODO(ryan953): Fix violations and delete this line
  410. '@typescript-eslint/unified-signatures': 'off',
  411. // Stylistic overrides
  412. '@typescript-eslint/array-type': ['error', {default: 'array-simple'}],
  413. '@typescript-eslint/class-literal-property-style': 'off', // TODO(ryan953): Fix violations and delete this line
  414. '@typescript-eslint/consistent-generic-constructors': 'off', // TODO(ryan953): Fix violations and delete this line
  415. '@typescript-eslint/consistent-indexed-object-style': 'off', // TODO(ryan953): Fix violations and delete this line
  416. '@typescript-eslint/consistent-type-definitions': 'off', // TODO(ryan953): Fix violations and delete this line
  417. '@typescript-eslint/no-empty-function': 'off', // TODO(ryan953): Fix violations and delete this line
  418. // Customization
  419. '@typescript-eslint/no-unused-vars': [
  420. 'error',
  421. {
  422. vars: 'all',
  423. args: 'all',
  424. // TODO(scttcper): We could enable this to enforce catch (error)
  425. // https://eslint.org/docs/latest/rules/no-unused-vars#caughterrors
  426. caughtErrors: 'none',
  427. // Ignore vars that start with an underscore
  428. // e.g. if you want to omit a property using object spread:
  429. //
  430. // const {name: _name, ...props} = this.props;
  431. //
  432. varsIgnorePattern: '^_',
  433. argsIgnorePattern: '^_',
  434. destructuredArrayIgnorePattern: '^_',
  435. },
  436. ],
  437. },
  438. },
  439. {
  440. name: 'plugin/typescript-eslint/process.env.SENTRY_DETECT_DEPRECATIONS=1',
  441. rules: {
  442. '@typescript-eslint/no-deprecated': process.env.SENTRY_DETECT_DEPRECATIONS
  443. ? 'error'
  444. : 'off',
  445. },
  446. },
  447. {
  448. name: 'plugin/typescript-sort-keys',
  449. // https://github.com/infctr/eslint-plugin-typescript-sort-keys
  450. plugins: {'typescript-sort-keys': typescriptSortKeys},
  451. rules: {
  452. 'typescript-sort-keys/interface': [
  453. 'error',
  454. 'asc',
  455. {caseSensitive: true, natural: false, requiredFirst: true},
  456. ],
  457. },
  458. },
  459. {
  460. name: 'plugin/simple-import-sort',
  461. // https://github.com/lydell/eslint-plugin-simple-import-sort
  462. plugins: {'simple-import-sort': simpleImportSort},
  463. rules: {
  464. 'import/order': 'off',
  465. 'sort-imports': 'off',
  466. 'simple-import-sort/imports': [
  467. 'error',
  468. {
  469. groups: [
  470. // Side effect imports.
  471. [String.raw`^\u0000`],
  472. // Node.js builtins.
  473. [`^(${builtinModules.join('|')})(/|$)`],
  474. // Packages. `react` related packages come first.
  475. ['^react', String.raw`^@?\w`],
  476. // Test should be separate from the app
  477. ['^(sentry-test|getsentry-test)(/.*|$)'],
  478. // Internal packages.
  479. ['^(sentry-locale|sentry-images)(/.*|$)'],
  480. ['^(app|sentry)(/.*|$)'],
  481. // Getsentry packages.
  482. ['^(admin|getsentry)(/.*|$)'],
  483. // Style imports.
  484. [String.raw`^.+\.less$`],
  485. // Parent imports. Put `..` last.
  486. [String.raw`^\.\.(?!/?$)`, String.raw`^\.\./?$`],
  487. // Other relative imports. Put same-folder imports and `.` last.
  488. [String.raw`^\./(?=.*/)(?!/?$)`, String.raw`^\.(?!/?$)`, String.raw`^\./?$`],
  489. ],
  490. },
  491. ],
  492. },
  493. },
  494. {
  495. name: 'plugin/sentry',
  496. // https://github.com/getsentry/eslint-config-sentry/tree/master/packages/eslint-plugin-sentry/docs/rules
  497. plugins: {sentry},
  498. rules: {
  499. 'sentry/no-digits-in-tn': 'error',
  500. 'sentry/no-dynamic-translations': 'error', // TODO(ryan953): There are no docs for this rule
  501. 'sentry/no-styled-shortcut': 'error',
  502. },
  503. },
  504. {
  505. name: 'plugin/@emotion',
  506. // https://github.com/emotion-js/emotion/tree/main/packages/eslint-plugin/docs/rules
  507. plugins: {'@emotion': emotion},
  508. rules: {
  509. '@emotion/import-from-emotion': 'off', // Not needed, in v11 we import from @emotion/react
  510. '@emotion/jsx-import': 'off', // Not needed, handled by babel
  511. '@emotion/no-vanilla': 'error',
  512. '@emotion/pkg-renaming': 'off', // Not needed, we have migrated to v11 and the old package names cannot be used anymore
  513. '@emotion/styled-import': 'error',
  514. '@emotion/syntax-preference': ['error', 'string'],
  515. },
  516. },
  517. {
  518. name: 'plugin/unicorn',
  519. plugins: {unicorn},
  520. rules: {
  521. // The recommended rules are very opinionated. We don't need to enable them.
  522. 'unicorn/no-instanceof-array': 'error',
  523. 'unicorn/prefer-array-flat-map': 'error',
  524. 'unicorn/prefer-node-protocol': 'error',
  525. },
  526. },
  527. {
  528. name: 'plugin/jest',
  529. files: ['**/*.spec.{ts,js,tsx,jsx}', 'tests/js/**/*.{ts,js,tsx,jsx}'],
  530. // https://github.com/jest-community/eslint-plugin-jest/tree/main/docs/rules
  531. plugins: jest.configs['flat/recommended'].plugins,
  532. rules: {
  533. 'jest/max-nested-describe': 'error',
  534. 'jest/no-duplicate-hooks': 'error',
  535. 'jest/no-large-snapshots': ['error', {maxSize: 2000}], // We don't recommend snapshots, but if there are any keep it small
  536. // https://github.com/jest-community/eslint-plugin-jest/blob/main/src/index.ts
  537. ...jest.configs['flat/recommended'].rules,
  538. ...jest.configs['flat/style'].rules,
  539. 'jest/expect-expect': 'off', // Disabled as we have many tests which render as simple validations
  540. 'jest/no-conditional-expect': 'off', // TODO(ryan953): Fix violations then delete this line
  541. 'jest/no-disabled-tests': 'error', // `recommended` set this to warn, we've upgraded to error
  542. },
  543. },
  544. {
  545. name: 'plugin/jest-dom',
  546. files: ['**/*.spec.{ts,js,tsx,jsx}', 'tests/js/**/*.{ts,js,tsx,jsx}'],
  547. // https://github.com/testing-library/eslint-plugin-jest-dom/tree/main?tab=readme-ov-file#supported-rules
  548. ...jestDom.configs['flat/recommended'],
  549. },
  550. {
  551. name: 'plugin/testing-library',
  552. files: ['**/*.spec.{ts,js,tsx,jsx}', 'tests/js/**/*.{ts,js,tsx,jsx}'],
  553. // https://github.com/testing-library/eslint-plugin-testing-library/tree/main/docs/rules
  554. ...testingLibrary.configs['flat/react'],
  555. rules: {
  556. // https://github.com/testing-library/eslint-plugin-testing-library/blob/main/lib/configs/react.ts
  557. ...testingLibrary.configs['flat/react'].rules,
  558. 'testing-library/no-unnecessary-act': 'off',
  559. 'testing-library/render-result-naming-convention': 'off',
  560. },
  561. },
  562. {
  563. name: 'plugin/prettier',
  564. ...prettier,
  565. },
  566. {
  567. name: 'files/*.config.*',
  568. files: ['**/*.config.*'],
  569. languageOptions: {
  570. globals: {
  571. ...globals.commonjs,
  572. ...globals.node,
  573. },
  574. },
  575. rules: {
  576. 'import/no-nodejs-modules': 'off',
  577. },
  578. },
  579. {
  580. name: 'files/scripts',
  581. files: ['scripts/**/*.{js,ts}', 'tests/js/test-balancer/index.js'],
  582. languageOptions: {
  583. sourceType: 'commonjs',
  584. globals: {
  585. ...globals.commonjs,
  586. ...globals.node,
  587. },
  588. },
  589. rules: {
  590. 'no-console': 'off',
  591. 'import/no-nodejs-modules': 'off',
  592. },
  593. },
  594. {
  595. name: 'files/jest related',
  596. files: [
  597. 'tests/js/jest-pegjs-transform.js',
  598. 'tests/js/sentry-test/echartsMock.js',
  599. 'tests/js/sentry-test/importStyleMock.js',
  600. 'tests/js/sentry-test/loadFixtures.ts',
  601. 'tests/js/sentry-test/svgMock.js',
  602. 'tests/js/setup.ts',
  603. ],
  604. languageOptions: {
  605. sourceType: 'commonjs',
  606. globals: {
  607. ...globals.commonjs,
  608. },
  609. },
  610. rules: {
  611. 'import/no-nodejs-modules': 'off',
  612. },
  613. },
  614. {
  615. name: 'files/devtoolbar',
  616. files: ['static/app/components/devtoolbar/**/*.{ts,tsx}'],
  617. rules: {
  618. 'no-restricted-imports': [
  619. 'error',
  620. {
  621. paths: [
  622. ...restrictedImportPaths,
  623. {
  624. name: 'sentry/components/button',
  625. message:
  626. "Cannot depend on Button from inside the toolbar. Button depends on analytics tracking which isn't avaialble in the toolbar context",
  627. },
  628. {
  629. name: 'sentry/utils/queryClient',
  630. message:
  631. 'Import from `@tanstack/react-query` and `./hooks/useFetchApiData` or `./hooks/useFetchInfiniteApiData` instead.',
  632. },
  633. ],
  634. },
  635. ],
  636. },
  637. },
  638. {
  639. name: 'files/sentry-test',
  640. files: ['**/*.spec.{ts,js,tsx,jsx}', 'tests/js/**/*.{ts,js,tsx,jsx}'],
  641. rules: {
  642. 'no-loss-of-precision': 'off', // Sometimes we have wild numbers hard-coded in tests
  643. 'no-restricted-imports': [
  644. 'error',
  645. {
  646. patterns: restrictedImportPatterns,
  647. paths: [
  648. ...restrictedImportPaths,
  649. {
  650. name: 'sentry/locale',
  651. message: 'Translations are not needed in tests.',
  652. },
  653. ],
  654. },
  655. ],
  656. },
  657. },
  658. {
  659. name: 'files/sentry-stories',
  660. files: ['**/*.stories.tsx'],
  661. rules: {
  662. 'no-loss-of-precision': 'off', // Sometimes we have wild numbers hard-coded in stories
  663. },
  664. },
  665. {
  666. // We specify rules explicitly for the sdk-loader here so we do not have
  667. // eslint ignore comments included in the source file, which is consumed
  668. // by users.
  669. name: 'files/js-sdk-loader.ts',
  670. files: ['**/js-sdk-loader.ts'],
  671. rules: {
  672. 'no-console': 'off',
  673. },
  674. },
  675. ]);