eslint.config.mjs 26 KB

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