eslint.config.mjs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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',
  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/no-array-constructor': 'off', // TODO(ryan953): Fix violations and delete this line
  400. '@typescript-eslint/no-empty-object-type': 'off', // TODO(ryan953): Fix violations and delete this line
  401. '@typescript-eslint/no-explicit-any': 'off',
  402. '@typescript-eslint/no-namespace': 'off', // TODO(ryan953): Fix violations and delete this line
  403. '@typescript-eslint/no-non-null-asserted-optional-chain': 'off', // TODO(ryan953): Fix violations and delete this line
  404. '@typescript-eslint/no-require-imports': 'off', // TODO(ryan953): Fix violations and delete this line
  405. '@typescript-eslint/no-this-alias': 'off', // TODO(ryan953): Fix violations and delete this line
  406. '@typescript-eslint/no-unsafe-function-type': 'off', // TODO(ryan953): Fix violations and delete this line
  407. // Strict overrides
  408. '@typescript-eslint/no-dynamic-delete': 'off', // TODO(ryan953): Fix violations and delete this line
  409. '@typescript-eslint/no-extraneous-class': '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', // TODO(ryan953): Fix violations and delete this line
  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. '@typescript-eslint/prefer-for-of': 'off', // TODO(ryan953): Fix violations and delete this line
  422. // Customization
  423. '@typescript-eslint/no-unused-vars': [
  424. 'error',
  425. {
  426. vars: 'all',
  427. args: 'all',
  428. // TODO(scttcper): We could enable this to enforce catch (error)
  429. // https://eslint.org/docs/latest/rules/no-unused-vars#caughterrors
  430. caughtErrors: 'none',
  431. // Ignore vars that start with an underscore
  432. // e.g. if you want to omit a property using object spread:
  433. //
  434. // const {name: _name, ...props} = this.props;
  435. //
  436. varsIgnorePattern: '^_',
  437. argsIgnorePattern: '^_',
  438. destructuredArrayIgnorePattern: '^_',
  439. },
  440. ],
  441. },
  442. },
  443. {
  444. name: 'plugin/typescript-eslint/process.env.SENTRY_DETECT_DEPRECATIONS=1',
  445. rules: {
  446. '@typescript-eslint/no-deprecated': process.env.SENTRY_DETECT_DEPRECATIONS
  447. ? 'error'
  448. : 'off',
  449. },
  450. },
  451. {
  452. name: 'plugin/typescript-sort-keys',
  453. // https://github.com/infctr/eslint-plugin-typescript-sort-keys
  454. plugins: {'typescript-sort-keys': typescriptSortKeys},
  455. rules: {
  456. 'typescript-sort-keys/interface': [
  457. 'error',
  458. 'asc',
  459. {caseSensitive: true, natural: false, requiredFirst: true},
  460. ],
  461. },
  462. },
  463. {
  464. name: 'plugin/simple-import-sort',
  465. // https://github.com/lydell/eslint-plugin-simple-import-sort
  466. plugins: {'simple-import-sort': simpleImportSort},
  467. rules: {
  468. 'import/order': 'off',
  469. 'sort-imports': 'off',
  470. 'simple-import-sort/imports': [
  471. 'error',
  472. {
  473. groups: [
  474. // Side effect imports.
  475. [String.raw`^\u0000`],
  476. // Node.js builtins.
  477. [`^(${builtinModules.join('|')})(/|$)`],
  478. // Packages. `react` related packages come first.
  479. ['^react', String.raw`^@?\w`],
  480. // Test should be separate from the app
  481. ['^(sentry-test|getsentry-test)(/.*|$)'],
  482. // Internal packages.
  483. ['^(sentry-locale|sentry-images)(/.*|$)'],
  484. ['^(getsentry-images)(/.*|$)'],
  485. ['^(app|sentry)(/.*|$)'],
  486. // Getsentry packages.
  487. ['^(admin|getsentry)(/.*|$)'],
  488. // Style imports.
  489. [String.raw`^.+\.less$`],
  490. // Parent imports. Put `..` last.
  491. [String.raw`^\.\.(?!/?$)`, String.raw`^\.\./?$`],
  492. // Other relative imports. Put same-folder imports and `.` last.
  493. [String.raw`^\./(?=.*/)(?!/?$)`, String.raw`^\.(?!/?$)`, String.raw`^\./?$`],
  494. ],
  495. },
  496. ],
  497. },
  498. },
  499. {
  500. name: 'plugin/sentry',
  501. // https://github.com/getsentry/eslint-config-sentry/tree/master/packages/eslint-plugin-sentry/docs/rules
  502. plugins: {sentry},
  503. rules: {
  504. 'sentry/no-digits-in-tn': 'error',
  505. 'sentry/no-dynamic-translations': 'error', // TODO(ryan953): There are no docs for this rule
  506. 'sentry/no-styled-shortcut': 'error',
  507. },
  508. },
  509. {
  510. name: 'plugin/@emotion',
  511. // https://github.com/emotion-js/emotion/tree/main/packages/eslint-plugin/docs/rules
  512. plugins: {'@emotion': emotion},
  513. rules: {
  514. '@emotion/import-from-emotion': 'off', // Not needed, in v11 we import from @emotion/react
  515. '@emotion/jsx-import': 'off', // Not needed, handled by babel
  516. '@emotion/no-vanilla': 'error',
  517. '@emotion/pkg-renaming': 'off', // Not needed, we have migrated to v11 and the old package names cannot be used anymore
  518. '@emotion/styled-import': 'error',
  519. '@emotion/syntax-preference': ['error', 'string'],
  520. },
  521. },
  522. {
  523. name: 'plugin/unicorn',
  524. plugins: {unicorn},
  525. rules: {
  526. // The recommended rules are very opinionated. We don't need to enable them.
  527. 'unicorn/no-instanceof-array': 'error',
  528. 'unicorn/prefer-array-flat-map': 'error',
  529. 'unicorn/prefer-node-protocol': 'error',
  530. },
  531. },
  532. {
  533. name: 'plugin/jest',
  534. files: ['**/*.spec.{ts,js,tsx,jsx}', 'tests/js/**/*.{ts,js,tsx,jsx}'],
  535. // https://github.com/jest-community/eslint-plugin-jest/tree/main/docs/rules
  536. plugins: jest.configs['flat/recommended'].plugins,
  537. rules: {
  538. 'jest/max-nested-describe': 'error',
  539. 'jest/no-duplicate-hooks': 'error',
  540. 'jest/no-large-snapshots': ['error', {maxSize: 2000}], // We don't recommend snapshots, but if there are any keep it small
  541. // https://github.com/jest-community/eslint-plugin-jest/blob/main/src/index.ts
  542. ...jest.configs['flat/recommended'].rules,
  543. ...jest.configs['flat/style'].rules,
  544. 'jest/expect-expect': 'off', // Disabled as we have many tests which render as simple validations
  545. 'jest/no-conditional-expect': 'off', // TODO(ryan953): Fix violations then delete this line
  546. 'jest/no-disabled-tests': 'error', // `recommended` set this to warn, we've upgraded to error
  547. },
  548. },
  549. {
  550. name: 'plugin/jest-dom',
  551. files: ['**/*.spec.{ts,js,tsx,jsx}', 'tests/js/**/*.{ts,js,tsx,jsx}'],
  552. // https://github.com/testing-library/eslint-plugin-jest-dom/tree/main?tab=readme-ov-file#supported-rules
  553. ...jestDom.configs['flat/recommended'],
  554. },
  555. {
  556. name: 'plugin/testing-library',
  557. files: ['**/*.spec.{ts,js,tsx,jsx}', 'tests/js/**/*.{ts,js,tsx,jsx}'],
  558. // https://github.com/testing-library/eslint-plugin-testing-library/tree/main/docs/rules
  559. ...testingLibrary.configs['flat/react'],
  560. rules: {
  561. // https://github.com/testing-library/eslint-plugin-testing-library/blob/main/lib/configs/react.ts
  562. ...testingLibrary.configs['flat/react'].rules,
  563. 'testing-library/no-unnecessary-act': 'off',
  564. 'testing-library/render-result-naming-convention': 'off',
  565. },
  566. },
  567. {
  568. name: 'plugin/prettier',
  569. ...prettier,
  570. },
  571. {
  572. name: 'files/*.config.*',
  573. files: ['**/*.config.*'],
  574. languageOptions: {
  575. globals: {
  576. ...globals.commonjs,
  577. ...globals.node,
  578. },
  579. },
  580. rules: {
  581. 'import/no-nodejs-modules': 'off',
  582. },
  583. },
  584. {
  585. name: 'files/scripts',
  586. files: ['scripts/**/*.{js,ts}', 'tests/js/test-balancer/index.js'],
  587. languageOptions: {
  588. sourceType: 'commonjs',
  589. globals: {
  590. ...globals.commonjs,
  591. ...globals.node,
  592. },
  593. },
  594. rules: {
  595. 'no-console': 'off',
  596. 'import/no-nodejs-modules': 'off',
  597. },
  598. },
  599. {
  600. name: 'files/jest related',
  601. files: [
  602. 'tests/js/jest-pegjs-transform.js',
  603. 'tests/js/sentry-test/echartsMock.js',
  604. 'tests/js/sentry-test/importStyleMock.js',
  605. 'tests/js/sentry-test/loadFixtures.ts',
  606. 'tests/js/sentry-test/svgMock.js',
  607. 'tests/js/setup.ts',
  608. ],
  609. languageOptions: {
  610. sourceType: 'commonjs',
  611. globals: {
  612. ...globals.commonjs,
  613. },
  614. },
  615. rules: {
  616. 'import/no-nodejs-modules': 'off',
  617. },
  618. },
  619. {
  620. name: 'files/devtoolbar',
  621. files: ['static/app/components/devtoolbar/**/*.{ts,tsx}'],
  622. rules: {
  623. 'no-restricted-imports': [
  624. 'error',
  625. {
  626. paths: [
  627. ...restrictedImportPaths,
  628. {
  629. name: 'sentry/utils/queryClient',
  630. message:
  631. 'Import from `@tanstack/react-query` and `./hooks/useFetchApiData` or `./hooks/useFetchInfiniteApiData` instead.',
  632. },
  633. ],
  634. },
  635. ],
  636. },
  637. },
  638. {
  639. name: 'files/sentry-test',
  640. files: ['**/*.spec.{ts,js,tsx,jsx}', 'tests/js/**/*.{ts,js,tsx,jsx}'],
  641. rules: {
  642. 'no-loss-of-precision': 'off', // Sometimes we have wild numbers hard-coded in tests
  643. 'no-restricted-imports': [
  644. 'error',
  645. {
  646. patterns: restrictedImportPatterns,
  647. paths: [
  648. ...restrictedImportPaths,
  649. {
  650. name: 'sentry/locale',
  651. message: 'Translations are not needed in tests.',
  652. },
  653. ],
  654. },
  655. ],
  656. },
  657. },
  658. {
  659. name: 'files/sentry-stories',
  660. files: ['**/*.stories.tsx'],
  661. rules: {
  662. 'no-loss-of-precision': 'off', // Sometimes we have wild numbers hard-coded in stories
  663. },
  664. },
  665. {
  666. // We specify rules explicitly for the sdk-loader here so we do not have
  667. // eslint ignore comments included in the source file, which is consumed
  668. // by users.
  669. name: 'files/js-sdk-loader.ts',
  670. files: ['**/js-sdk-loader.ts'],
  671. rules: {
  672. 'no-console': 'off',
  673. },
  674. },
  675. ]);