eslint.config.mjs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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-prototype-builtins': 'off',
  281. 'no-useless-escape': 'off',
  282. },
  283. },
  284. {
  285. // https://github.com/import-js/eslint-plugin-import/tree/main/docs/rules
  286. ...importPlugin.flatConfigs.recommended,
  287. name: 'plugin/import',
  288. rules: {
  289. 'import/newline-after-import': 'error', // https://prettier.io/docs/en/rationale.html#empty-lines
  290. 'import/no-absolute-path': 'error',
  291. 'import/no-amd': 'error',
  292. 'import/no-anonymous-default-export': 'error',
  293. 'import/no-duplicates': 'error',
  294. 'import/no-named-default': 'error',
  295. 'import/no-nodejs-modules': 'error',
  296. 'import/no-webpack-loader-syntax': 'error',
  297. // https://github.com/import-js/eslint-plugin-import/blob/main/config/recommended.js
  298. ...importPlugin.flatConfigs.recommended.rules,
  299. 'import/default': 'off', // Disabled in favor of typescript-eslint
  300. 'import/named': 'off', // Disabled in favor of typescript-eslint
  301. 'import/namespace': 'off', // Disabled in favor of typescript-eslint
  302. 'import/no-named-as-default-member': 'off', // Disabled in favor of typescript-eslint
  303. 'import/no-named-as-default': 'off', // TODO(ryan953): Fix violations and enable this rule
  304. 'import/no-unresolved': 'off', // Disabled in favor of typescript-eslint
  305. },
  306. },
  307. {
  308. name: 'plugin/react',
  309. // https://github.com/jsx-eslint/eslint-plugin-react/tree/master/docs/rules
  310. plugins: {
  311. ...react.configs.flat.recommended.plugins,
  312. ...react.configs.flat['jsx-runtime'].plugins,
  313. },
  314. rules: {
  315. 'react/function-component-definition': 'error',
  316. 'react/jsx-boolean-value': ['error', 'never'],
  317. 'react/jsx-fragments': ['error', 'element'],
  318. 'react/jsx-handler-names': 'off', // TODO(ryan953): Fix violations and enable this rule
  319. 'react/no-did-mount-set-state': 'error',
  320. 'react/no-did-update-set-state': 'error',
  321. 'react/no-redundant-should-component-update': 'error',
  322. 'react/no-typos': 'error',
  323. 'react/self-closing-comp': 'error',
  324. 'react/sort-comp': 'error',
  325. // https://github.com/jsx-eslint/eslint-plugin-react/blob/master/index.js
  326. ...react.configs.flat.recommended.rules,
  327. ...react.configs.flat['jsx-runtime'].rules,
  328. 'react/display-name': 'off', // TODO(ryan953): Fix violations and delete this line
  329. 'react/no-unescaped-entities': 'off',
  330. 'react/no-unknown-property': ['error', {ignore: ['css']}],
  331. 'react/prop-types': 'off', // TODO(ryan953): Fix violations and delete this line
  332. },
  333. },
  334. {
  335. name: 'plugin/react-hooks',
  336. // https://github.com/facebook/react/tree/main/packages/eslint-plugin-react-hooks
  337. plugins: {'react-hooks': reactHooks},
  338. rules: {
  339. 'react-hooks/exhaustive-deps': [
  340. 'error',
  341. {additionalHooks: '(useEffectAfterFirstRender|useMemoWithPrevious)'},
  342. ],
  343. 'react-hooks/rules-of-hooks': 'error',
  344. },
  345. },
  346. {
  347. name: 'plugin/typescript-eslint/custom',
  348. rules: {
  349. 'no-shadow': 'off', // Disabled in favor of @typescript-eslint/no-shadow
  350. 'no-use-before-define': 'off', // See also @typescript-eslint/no-use-before-define
  351. '@typescript-eslint/naming-convention': [
  352. 'error',
  353. {selector: 'typeLike', format: ['PascalCase'], leadingUnderscore: 'allow'},
  354. {selector: 'enumMember', format: ['UPPER_CASE']},
  355. ],
  356. '@typescript-eslint/no-restricted-types': [
  357. 'error',
  358. {
  359. types: {
  360. // TODO(scttcper): Turn object on to make our types more strict
  361. // object: {
  362. // message: 'The `object` type is hard to use. Use `Record<string, unknown>` instead. See: https://github.com/typescript-eslint/typescript-eslint/pull/848',
  363. // fixWith: 'Record<string, unknown>'
  364. // },
  365. Buffer: {
  366. message:
  367. 'Use Uint8Array instead. See: https://sindresorhus.com/blog/goodbye-nodejs-buffer',
  368. suggest: ['Uint8Array'],
  369. },
  370. '[]': "Don't use the empty array type `[]`. It only allows empty arrays. Use `SomeType[]` instead.",
  371. '[[]]':
  372. "Don't use `[[]]`. It only allows an array with a single element which is an empty array. Use `SomeType[][]` instead.",
  373. '[[[]]]': "Don't use `[[[]]]`. Use `SomeType[][][]` instead.",
  374. },
  375. },
  376. ],
  377. '@typescript-eslint/no-shadow': 'error',
  378. '@typescript-eslint/no-use-before-define': 'off', // Enabling this will cause a lot of thrash to the git history
  379. '@typescript-eslint/no-useless-empty-export': 'error',
  380. },
  381. },
  382. // https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/base.ts
  383. // https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/eslint-recommended-raw.ts
  384. // https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/recommended.ts
  385. // https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/strict.ts
  386. // https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/configs/stylistic.ts
  387. ...typescript.configs.strict.map(c => ({...c, name: `plugin/${c.name}`})),
  388. ...typescript.configs.stylistic.map(c => ({...c, name: `plugin/${c.name}`})),
  389. {
  390. name: 'plugin/typescript-eslint/overrides',
  391. // https://typescript-eslint.io/rules/
  392. plugins: {'@typescript-eslint': typescript.plugin},
  393. rules: {
  394. 'prefer-spread': 'off',
  395. '@typescript-eslint/prefer-enum-initializers': 'error',
  396. // Recommended overrides
  397. '@typescript-eslint/no-empty-object-type': 'off', // TODO(ryan953): Fix violations and delete this line
  398. '@typescript-eslint/no-explicit-any': 'off',
  399. '@typescript-eslint/no-namespace': 'off',
  400. '@typescript-eslint/no-non-null-asserted-optional-chain': 'off', // TODO(ryan953): Fix violations and delete this line
  401. '@typescript-eslint/no-require-imports': 'off', // TODO(ryan953): Fix violations and delete this line
  402. '@typescript-eslint/no-this-alias': 'off', // TODO(ryan953): Fix violations and delete this line
  403. '@typescript-eslint/no-unsafe-function-type': 'off', // TODO(ryan953): Fix violations and delete this line
  404. // Strict overrides
  405. '@typescript-eslint/no-dynamic-delete': 'off', // TODO(ryan953): Fix violations and delete this line
  406. '@typescript-eslint/no-invalid-void-type': 'off', // TODO(ryan953): Fix violations and delete this line
  407. '@typescript-eslint/no-non-null-assertion': 'off', // TODO(ryan953): Fix violations and delete this line
  408. '@typescript-eslint/unified-signatures': 'off',
  409. // Stylistic overrides
  410. '@typescript-eslint/array-type': ['error', {default: 'array-simple'}],
  411. '@typescript-eslint/class-literal-property-style': 'off', // TODO(ryan953): Fix violations and delete this line
  412. '@typescript-eslint/consistent-generic-constructors': 'off', // TODO(ryan953): Fix violations and delete this line
  413. '@typescript-eslint/consistent-indexed-object-style': 'off', // TODO(ryan953): Fix violations and delete this line
  414. '@typescript-eslint/consistent-type-definitions': 'off', // TODO(ryan953): Fix violations and delete this line
  415. '@typescript-eslint/no-empty-function': 'off', // TODO(ryan953): Fix violations and delete this line
  416. '@typescript-eslint/no-inferrable-types': 'off', // TODO(ryan953): Fix violations and delete this line
  417. '@typescript-eslint/prefer-for-of': '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. ['^(getsentry-images)(/.*|$)'],
  481. ['^(app|sentry)(/.*|$)'],
  482. // Getsentry packages.
  483. ['^(admin|getsentry)(/.*|$)'],
  484. // Style imports.
  485. [String.raw`^.+\.less$`],
  486. // Parent imports. Put `..` last.
  487. [String.raw`^\.\.(?!/?$)`, String.raw`^\.\./?$`],
  488. // Other relative imports. Put same-folder imports and `.` last.
  489. [String.raw`^\./(?=.*/)(?!/?$)`, String.raw`^\.(?!/?$)`, String.raw`^\./?$`],
  490. ],
  491. },
  492. ],
  493. },
  494. },
  495. {
  496. name: 'plugin/sentry',
  497. // https://github.com/getsentry/eslint-config-sentry/tree/master/packages/eslint-plugin-sentry/docs/rules
  498. plugins: {sentry},
  499. rules: {
  500. 'sentry/no-digits-in-tn': 'error',
  501. 'sentry/no-dynamic-translations': 'error', // TODO(ryan953): There are no docs for this rule
  502. 'sentry/no-styled-shortcut': 'error',
  503. },
  504. },
  505. {
  506. name: 'plugin/@emotion',
  507. // https://github.com/emotion-js/emotion/tree/main/packages/eslint-plugin/docs/rules
  508. plugins: {'@emotion': emotion},
  509. rules: {
  510. '@emotion/import-from-emotion': 'off', // Not needed, in v11 we import from @emotion/react
  511. '@emotion/jsx-import': 'off', // Not needed, handled by babel
  512. '@emotion/no-vanilla': 'error',
  513. '@emotion/pkg-renaming': 'off', // Not needed, we have migrated to v11 and the old package names cannot be used anymore
  514. '@emotion/styled-import': 'error',
  515. '@emotion/syntax-preference': ['error', 'string'],
  516. },
  517. },
  518. {
  519. name: 'plugin/unicorn',
  520. plugins: {unicorn},
  521. rules: {
  522. // The recommended rules are very opinionated. We don't need to enable them.
  523. 'unicorn/no-instanceof-array': 'error',
  524. 'unicorn/prefer-array-flat-map': 'error',
  525. 'unicorn/prefer-node-protocol': 'error',
  526. },
  527. },
  528. {
  529. name: 'plugin/jest',
  530. files: ['**/*.spec.{ts,js,tsx,jsx}', 'tests/js/**/*.{ts,js,tsx,jsx}'],
  531. // https://github.com/jest-community/eslint-plugin-jest/tree/main/docs/rules
  532. plugins: jest.configs['flat/recommended'].plugins,
  533. rules: {
  534. 'jest/max-nested-describe': 'error',
  535. 'jest/no-duplicate-hooks': 'error',
  536. 'jest/no-large-snapshots': ['error', {maxSize: 2000}], // We don't recommend snapshots, but if there are any keep it small
  537. // https://github.com/jest-community/eslint-plugin-jest/blob/main/src/index.ts
  538. ...jest.configs['flat/recommended'].rules,
  539. ...jest.configs['flat/style'].rules,
  540. 'jest/expect-expect': 'off', // Disabled as we have many tests which render as simple validations
  541. 'jest/no-conditional-expect': 'off', // TODO(ryan953): Fix violations then delete this line
  542. 'jest/no-disabled-tests': 'error', // `recommended` set this to warn, we've upgraded to error
  543. },
  544. },
  545. {
  546. name: 'plugin/jest-dom',
  547. files: ['**/*.spec.{ts,js,tsx,jsx}', 'tests/js/**/*.{ts,js,tsx,jsx}'],
  548. // https://github.com/testing-library/eslint-plugin-jest-dom/tree/main?tab=readme-ov-file#supported-rules
  549. ...jestDom.configs['flat/recommended'],
  550. },
  551. {
  552. name: 'plugin/testing-library',
  553. files: ['**/*.spec.{ts,js,tsx,jsx}', 'tests/js/**/*.{ts,js,tsx,jsx}'],
  554. // https://github.com/testing-library/eslint-plugin-testing-library/tree/main/docs/rules
  555. ...testingLibrary.configs['flat/react'],
  556. rules: {
  557. // https://github.com/testing-library/eslint-plugin-testing-library/blob/main/lib/configs/react.ts
  558. ...testingLibrary.configs['flat/react'].rules,
  559. 'testing-library/no-unnecessary-act': 'off',
  560. 'testing-library/render-result-naming-convention': 'off',
  561. },
  562. },
  563. {
  564. name: 'plugin/prettier',
  565. ...prettier,
  566. },
  567. {
  568. name: 'files/*.config.*',
  569. files: ['**/*.config.*'],
  570. languageOptions: {
  571. globals: {
  572. ...globals.commonjs,
  573. ...globals.node,
  574. },
  575. },
  576. rules: {
  577. 'import/no-nodejs-modules': 'off',
  578. },
  579. },
  580. {
  581. name: 'files/scripts',
  582. files: ['scripts/**/*.{js,ts}', 'tests/js/test-balancer/index.js'],
  583. languageOptions: {
  584. sourceType: 'commonjs',
  585. globals: {
  586. ...globals.commonjs,
  587. ...globals.node,
  588. },
  589. },
  590. rules: {
  591. 'no-console': 'off',
  592. 'import/no-nodejs-modules': 'off',
  593. },
  594. },
  595. {
  596. name: 'files/jest related',
  597. files: [
  598. 'tests/js/jest-pegjs-transform.js',
  599. 'tests/js/sentry-test/echartsMock.js',
  600. 'tests/js/sentry-test/importStyleMock.js',
  601. 'tests/js/sentry-test/loadFixtures.ts',
  602. 'tests/js/sentry-test/svgMock.js',
  603. 'tests/js/setup.ts',
  604. ],
  605. languageOptions: {
  606. sourceType: 'commonjs',
  607. globals: {
  608. ...globals.commonjs,
  609. },
  610. },
  611. rules: {
  612. 'import/no-nodejs-modules': 'off',
  613. },
  614. },
  615. {
  616. name: 'files/devtoolbar',
  617. files: ['static/app/components/devtoolbar/**/*.{ts,tsx}'],
  618. rules: {
  619. 'no-restricted-imports': [
  620. 'error',
  621. {
  622. paths: [
  623. ...restrictedImportPaths,
  624. {
  625. name: 'sentry/utils/queryClient',
  626. message:
  627. 'Import from `@tanstack/react-query` and `./hooks/useFetchApiData` or `./hooks/useFetchInfiniteApiData` instead.',
  628. },
  629. ],
  630. },
  631. ],
  632. },
  633. },
  634. {
  635. name: 'files/sentry-test',
  636. files: ['**/*.spec.{ts,js,tsx,jsx}', 'tests/js/**/*.{ts,js,tsx,jsx}'],
  637. rules: {
  638. 'no-loss-of-precision': 'off', // Sometimes we have wild numbers hard-coded in tests
  639. 'no-restricted-imports': [
  640. 'error',
  641. {
  642. patterns: restrictedImportPatterns,
  643. paths: [
  644. ...restrictedImportPaths,
  645. {
  646. name: 'sentry/locale',
  647. message: 'Translations are not needed in tests.',
  648. },
  649. ],
  650. },
  651. ],
  652. },
  653. },
  654. {
  655. name: 'files/sentry-stories',
  656. files: ['**/*.stories.tsx'],
  657. rules: {
  658. 'no-loss-of-precision': 'off', // Sometimes we have wild numbers hard-coded in stories
  659. },
  660. },
  661. {
  662. // We specify rules explicitly for the sdk-loader here so we do not have
  663. // eslint ignore comments included in the source file, which is consumed
  664. // by users.
  665. name: 'files/js-sdk-loader.ts',
  666. files: ['**/js-sdk-loader.ts'],
  667. rules: {
  668. 'no-console': 'off',
  669. },
  670. },
  671. ]);