eslint.config.mjs 29 KB

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