eslint.config.mjs 27 KB

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