eslint.config.mjs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. // Shared ESLint flat config for the Overleaf web service.
  2. //
  3. // Mirrors what services/web/.eslintrc.js used to provide before the
  4. // flat-config migration.
  5. import { defineConfig, globalIgnores } from 'eslint/config'
  6. import tsParser from '@typescript-eslint/parser'
  7. import overleaf from '@overleaf/eslint-plugin'
  8. import globals from 'globals'
  9. import mocha from 'eslint-plugin-mocha'
  10. import chaiExpect from 'eslint-plugin-chai-expect'
  11. import chaiFriendly from 'eslint-plugin-chai-friendly'
  12. import vitest from '@vitest/eslint-plugin'
  13. import unicorn from 'eslint-plugin-unicorn'
  14. import cypress from 'eslint-plugin-cypress/flat'
  15. import testingLibrary from 'eslint-plugin-testing-library'
  16. import jsxA11Y from 'eslint-plugin-jsx-a11y'
  17. import reactPlugin from 'eslint-plugin-react'
  18. import reactHooks from 'eslint-plugin-react-hooks'
  19. import storybook from 'eslint-plugin-storybook'
  20. import importPlugin from 'eslint-plugin-import'
  21. import n from 'eslint-plugin-n'
  22. import promise from 'eslint-plugin-promise'
  23. import typescriptEslint from '@typescript-eslint/eslint-plugin'
  24. import js from '@eslint/js'
  25. import prettier from 'eslint-config-prettier/flat'
  26. import { fixupPluginRules } from '@eslint/compat'
  27. // eslint-plugin-import ships its configs in eslintrc shape; wrap with
  28. // fixupPluginRules so its rules work in flat config.
  29. const importPluginFixed = fixupPluginRules(importPlugin)
  30. // eslint-plugin-react 7.x still calls the removed `context.getFilename()`
  31. // API; wrap with fixupPluginRules so the compat shim translates it under
  32. // ESLint v10. Build a derivative flat-recommended config that references
  33. // the wrapped plugin instead of the original.
  34. const react = fixupPluginRules(reactPlugin)
  35. const reactFlatRecommended = {
  36. ...reactPlugin.configs.flat.recommended,
  37. plugins: { react },
  38. }
  39. import _ from 'lodash'
  40. import confusingBrowserGlobals from 'confusing-browser-globals'
  41. // Type-aware linting is expensive: setting `parserOptions.project` makes
  42. // @typescript-eslint build a full TypeScript program from tsconfig.backend.json
  43. // on every ESLint invocation, even when linting a single file.
  44. // Set ESLINT_FAST=1 to skip type-aware checks; the full lint still runs in CI.
  45. const TYPE_AWARE = process.env.ESLINT_FAST !== '1'
  46. export default defineConfig([
  47. // Declare which file extensions ESLint should consider in this workspace.
  48. // Replaces the previous `--ext .js,.jsx,.mjs,.ts,.tsx` CLI flag, which is
  49. // silently ignored under ESLint v9 + flat config.
  50. {
  51. files: ['**/*.{js,jsx,mjs,cjs,ts,tsx}'],
  52. },
  53. {
  54. languageOptions: {
  55. parser: tsParser,
  56. // Default to node globals for all backend-style files; the
  57. // frontend block below replaces with browser globals where
  58. // appropriate. Matches what the old monorepo-root .eslintrc
  59. // used to provide via env: { node: true }, plus the three
  60. // browser globals eslint-config-standard@17 added universally.
  61. globals: {
  62. ...globals.node,
  63. document: 'readonly',
  64. navigator: 'readonly',
  65. window: 'readonly',
  66. },
  67. },
  68. // Match v8 default: don't flag stale `eslint-disable` directives.
  69. linterOptions: {
  70. reportUnusedDisableDirectives: 'off',
  71. },
  72. extends: [
  73. js.configs.recommended,
  74. typescriptEslint.configs['flat/recommended'],
  75. storybook.configs['flat/recommended'],
  76. ],
  77. plugins: {
  78. '@overleaf': overleaf,
  79. '@typescript-eslint': typescriptEslint,
  80. import: importPluginFixed,
  81. // Registered (no rules enabled) so that pre-existing
  82. // `eslint-disable n/handle-callback-err` (etc.) directives in
  83. // the source still resolve to a real rule name. Under v8 these
  84. // came in transitively via eslint-config-standard.
  85. n,
  86. promise,
  87. react,
  88. 'react-hooks': reactHooks,
  89. 'jsx-a11y': jsxA11Y,
  90. 'testing-library': testingLibrary,
  91. cypress,
  92. },
  93. settings: {
  94. // Tell eslint-plugin-react to detect which version of React we are using
  95. react: {
  96. version: 'detect',
  97. },
  98. },
  99. rules: {
  100. 'no-constant-binary-expression': 'error',
  101. 'no-restricted-globals': ['error', ...confusingBrowserGlobals],
  102. // do not allow importing of implicit dependencies.
  103. 'import/no-extraneous-dependencies': 'error',
  104. '@overleaf/prefer-kebab-url': 'error',
  105. '@overleaf/require-cio-snake-case-properties': 'error',
  106. // disable some TypeScript rules
  107. '@typescript-eslint/no-var-requires': 'off',
  108. '@typescript-eslint/no-unused-vars': 'off',
  109. '@typescript-eslint/no-empty-function': 'off',
  110. '@typescript-eslint/no-explicit-any': 'off',
  111. '@typescript-eslint/no-this-alias': 'off',
  112. '@typescript-eslint/no-non-null-assertion': 'off',
  113. '@typescript-eslint/ban-ts-comment': 'off',
  114. 'no-use-before-define': 'off',
  115. '@typescript-eslint/no-use-before-define': [
  116. 'error',
  117. {
  118. functions: false,
  119. classes: false,
  120. variables: false,
  121. },
  122. ],
  123. // The following three overrides preserve v8 behaviour the source
  124. // tree inherited from eslint-config-standard@17. See the matching
  125. // block in libraries/eslint-config/index.mjs for context.
  126. 'no-unused-vars': [
  127. 'error',
  128. {
  129. args: 'none',
  130. caughtErrors: 'none',
  131. ignoreRestSiblings: true,
  132. vars: 'all',
  133. },
  134. ],
  135. 'no-redeclare': ['error', { builtinGlobals: false }],
  136. 'no-empty': ['error', { allowEmptyCatch: true }],
  137. // ESLint v10's eslint:recommended added these three rules. Disable
  138. // to preserve the v9 zero-error baseline; revisit in a follow-up
  139. // cleanup PR.
  140. 'no-unassigned-vars': 'off',
  141. 'no-useless-assignment': 'off',
  142. 'preserve-caught-error': 'off',
  143. },
  144. },
  145. // NOTE: changing paths may require updating them in the Makefile too.
  146. {
  147. // Node
  148. files: [
  149. '**/app/src/**/*.{js,mjs}',
  150. 'app.{js,mjs}',
  151. 'i18next-scanner.config.js',
  152. 'scripts/**/*.{js,mjs}',
  153. 'webpack.config*.js',
  154. ],
  155. languageOptions: {
  156. globals: {
  157. ...globals.node,
  158. },
  159. },
  160. },
  161. {
  162. // Test specific rules
  163. files: ['**/test/**/*.{js,jsx,mjs,cjs,ts,tsx}'],
  164. ignores: ['**/test/unit/src/**/*.test.mjs', 'test/unit/bootstrap.mjs'], // exclude vitest files
  165. plugins: {
  166. mocha,
  167. 'chai-expect': chaiExpect,
  168. 'chai-friendly': chaiFriendly,
  169. },
  170. languageOptions: {
  171. globals: {
  172. ...globals.mocha,
  173. },
  174. },
  175. rules: {
  176. // mocha-specific rules
  177. 'mocha/handle-done-callback': 'error',
  178. 'mocha/no-exclusive-tests': 'error',
  179. 'mocha/no-global-tests': 'error',
  180. 'mocha/no-identical-title': 'error',
  181. 'mocha/no-nested-tests': 'error',
  182. 'mocha/no-pending-tests': 'error',
  183. 'mocha/no-mocha-arrows': 'error',
  184. // Swap the no-unused-expressions rule with a more chai-friendly one
  185. 'no-unused-expressions': 'off',
  186. 'chai-friendly/no-unused-expressions': 'error',
  187. // chai-specific rules
  188. 'chai-expect/missing-assertion': 'error',
  189. 'chai-expect/terminating-properties': 'error',
  190. // prefer-arrow-callback applies to all callbacks, not just ones in mocha tests.
  191. // we don't enforce this at the top-level - just in tests to manage `this` scope
  192. // based on mocha's context mechanism
  193. 'mocha/prefer-arrow-callback': 'error',
  194. '@typescript-eslint/no-unused-expressions': 'off',
  195. },
  196. },
  197. {
  198. files: ['**/test/unit/src/**/*.test.mjs', 'test/unit/bootstrap.mjs'],
  199. languageOptions: {
  200. globals: {
  201. ...globals.jest, // best match for vitest API etc.
  202. },
  203. },
  204. plugins: {
  205. '@vitest': vitest,
  206. 'chai-expect': chaiExpect,
  207. 'chai-friendly': chaiFriendly, // still using chai for now
  208. },
  209. rules: {
  210. // vitest-specific rules
  211. '@vitest/no-focused-tests': 'error',
  212. '@vitest/no-disabled-tests': 'error',
  213. // Swap the no-unused-expressions rule with a more chai-friendly one
  214. 'no-unused-expressions': 'off',
  215. 'chai-friendly/no-unused-expressions': 'error',
  216. // chai-specific rules
  217. 'chai-expect/missing-assertion': 'error',
  218. 'chai-expect/terminating-properties': 'error',
  219. '@typescript-eslint/no-unused-expressions': 'off',
  220. '@overleaf/require-vi-doMock-valid-path': 'error',
  221. },
  222. },
  223. {
  224. // ES specific rules
  225. files: [
  226. '**/app/src/**/*.mjs',
  227. 'modules/*/index.mjs',
  228. 'app.mjs',
  229. 'scripts/**/*.mjs',
  230. 'migrations/**/*.mjs',
  231. '**/test/acceptance/src/**/*.mjs',
  232. '**/test/unit/src/**/*.mjs',
  233. ],
  234. ignores: [
  235. // migration template file
  236. 'migrations/lib/template.mjs',
  237. ],
  238. languageOptions: {
  239. sourceType: 'module',
  240. parserOptions: {},
  241. },
  242. plugins: {
  243. unicorn,
  244. },
  245. rules: {
  246. 'import/no-unresolved': [
  247. 'error',
  248. {
  249. // eslint-plugin-import does not support exports directive in package.json
  250. // https://github.com/import-js/eslint-plugin-import/issues/1810
  251. ignore: ['^p-queue$'],
  252. },
  253. ],
  254. 'import/named': 'error',
  255. 'import/default': 'error',
  256. 'import/extensions': [
  257. 'error',
  258. 'ignorePackages',
  259. {
  260. js: 'always',
  261. mjs: 'always',
  262. },
  263. ],
  264. 'unicorn/prefer-module': 'error',
  265. 'unicorn/prefer-node-protocol': 'error',
  266. },
  267. },
  268. {
  269. // Backend specific rules
  270. files: ['**/app/src/**/*.{js,mjs}', 'app.{js,mjs}'],
  271. languageOptions: {
  272. parserOptions: TYPE_AWARE
  273. ? {
  274. tsconfigRootDir: import.meta.dirname,
  275. project: './tsconfig.backend.json',
  276. }
  277. : {},
  278. },
  279. rules: {
  280. // do not allow importing of implicit dependencies.
  281. 'import/no-extraneous-dependencies': [
  282. 'error',
  283. {
  284. // do not allow importing of devDependencies.
  285. devDependencies: false,
  286. },
  287. ],
  288. 'no-restricted-syntax': [
  289. 'error',
  290. // do not allow node-fetch in backend code
  291. {
  292. selector:
  293. "CallExpression[callee.name='require'] > .arguments[value='node-fetch']",
  294. message:
  295. 'Requiring node-fetch is not allowed in production services, please use fetch-utils.',
  296. },
  297. // mongoose populate must set fields to populate
  298. {
  299. selector:
  300. "CallExpression[callee.property.name='populate'][arguments.length<2]",
  301. message:
  302. "Populate without a second argument returns the whole document. Use populate('field',['prop1','prop2']) instead",
  303. },
  304. // Require `new` when constructing ObjectId (For mongo + mongoose upgrade)
  305. {
  306. selector:
  307. "CallExpression[callee.name='ObjectId'], CallExpression[callee.property.name='ObjectId']",
  308. message:
  309. 'Construct ObjectId with `new ObjectId()` instead of `ObjectId()`',
  310. },
  311. // Require `new` when mapping a list of ids to a list of ObjectId (For mongo + mongoose upgrade)
  312. {
  313. selector:
  314. "CallExpression[callee.property.name='map'] Identifier[name='ObjectId']:first-child, CallExpression[callee.property.name='map'] MemberExpression[property.name='ObjectId']:first-child",
  315. message:
  316. "Don't map ObjectId directly. Use `id => new ObjectId(id)` instead",
  317. },
  318. // Catch incorrect usage of `await db.collection.find()`
  319. {
  320. selector:
  321. "AwaitExpression > CallExpression > MemberExpression[property.name='find'][object.object.name='db']",
  322. message:
  323. 'Mongo find returns a cursor not a promise, use `for await (const result of cursor)` or `.toArray()` instead.',
  324. },
  325. ],
  326. '@typescript-eslint/no-floating-promises': TYPE_AWARE
  327. ? [
  328. 'error',
  329. {
  330. checkThenables: true,
  331. },
  332. ]
  333. : 'off',
  334. },
  335. },
  336. {
  337. // Backend scripts specific rules
  338. files: ['**/scripts/**/*.{js,mjs}'],
  339. rules: {
  340. 'no-restricted-syntax': [
  341. 'error',
  342. // Require `new` when constructing ObjectId (For mongo + mongoose upgrade)
  343. {
  344. selector:
  345. "CallExpression[callee.name='ObjectId'], CallExpression[callee.property.name='ObjectId']",
  346. message:
  347. 'Construct ObjectId with `new ObjectId()` instead of `ObjectId()`',
  348. },
  349. // Require `new` when mapping a list of ids to a list of ObjectId (For mongo + mongoose upgrade)
  350. {
  351. selector:
  352. "CallExpression[callee.property.name='map'] Identifier[name='ObjectId']:first-child, CallExpression[callee.property.name='map'] MemberExpression[property.name='ObjectId']:first-child",
  353. message:
  354. "Don't map ObjectId directly. Use `id => new ObjectId(id)` instead",
  355. },
  356. // Catch incorrect usage of `await db.collection.find()`
  357. {
  358. selector:
  359. "AwaitExpression > CallExpression > MemberExpression[property.name='find'][object.object.name='db']",
  360. message:
  361. 'Mongo find returns a cursor not a promise, use `for await (const result of cursor)` or `.toArray()` instead.',
  362. },
  363. ],
  364. },
  365. },
  366. {
  367. // Insist on using Script Runner for new scripts. Old scripts should be
  368. // converted to use Script Runner in future, but are excluded for now.
  369. rules: {
  370. '@overleaf/require-script-runner': 'error',
  371. },
  372. files: ['**/scripts/**/*.mjs'], // ESM only
  373. ignores: [
  374. 'modules/admin-roles/scripts/import_admin_role_assignments.mjs',
  375. 'modules/admin-roles/scripts/remove_admin_role_from_user.mjs',
  376. 'modules/admin-roles/scripts/remove_admin_roles_from_non_admins.mjs',
  377. 'modules/admin-roles/scripts/utils.mjs',
  378. 'modules/institutions/scripts/apply_policy_to_institution.mjs',
  379. 'modules/server-ce-scripts/scripts/change-compile-timeout.mjs',
  380. 'modules/server-ce-scripts/scripts/check-mongodb.mjs',
  381. 'modules/server-ce-scripts/scripts/check-redis.mjs',
  382. 'modules/server-ce-scripts/scripts/check-texlive-images.mjs',
  383. 'modules/server-ce-scripts/scripts/create-user.mjs',
  384. 'modules/server-ce-scripts/scripts/delete-user.mjs',
  385. 'modules/server-ce-scripts/scripts/export-user-projects.mjs',
  386. 'modules/server-ce-scripts/scripts/migrate-user-emails.mjs',
  387. 'modules/server-ce-scripts/scripts/rename-tag.mjs',
  388. 'modules/server-ce-scripts/scripts/transfer-all-projects-to-user.mjs',
  389. 'modules/server-ce-scripts/scripts/upgrade-user-features.mjs',
  390. 'modules/subscriptions/scripts/backfill_user_last_trial.mjs',
  391. 'scripts/add_feature_override.mjs',
  392. 'scripts/add_subscription_members_csv.mjs',
  393. 'scripts/analytics/helpers/GoogleBigQueryHelper.mjs',
  394. 'scripts/attach_dangling_comments_to_doc.mjs',
  395. 'scripts/backfill_mixpanel_user_properties.mjs',
  396. 'scripts/backfill_project_image_name.mjs',
  397. 'scripts/backfill_user_properties.mjs',
  398. 'scripts/backfill_users_sso_attribute.mjs',
  399. 'scripts/bench_bcrypt.mjs',
  400. 'scripts/check_institution_users.mjs',
  401. 'scripts/check_overleafModuleImports.mjs',
  402. 'scripts/check_saml_emails.mjs',
  403. 'scripts/clear_feedback_collection.mjs',
  404. 'scripts/clear_sessions_set_must_reconfirm.mjs',
  405. 'scripts/count_files_in_projects.mjs',
  406. 'scripts/count_project_size.mjs',
  407. 'scripts/create_oauth_personal_access_token.mjs',
  408. 'scripts/create_project.mjs',
  409. 'scripts/deactivate_projects.mjs',
  410. 'scripts/delete-duplicate-splittest-versions/delete_test_dupes.mjs',
  411. 'scripts/delete-orphaned-docs/delete-orphaned-docs.mjs',
  412. 'scripts/delete_dangling_comments.mjs',
  413. 'scripts/delete_orphaned_chat_threads.mjs',
  414. 'scripts/delete_orphaned_data_helper.mjs',
  415. 'scripts/delete_subscriptions.mjs',
  416. 'scripts/devcontainer_setup.mjs',
  417. 'scripts/e2e_test_setup.mjs',
  418. 'scripts/ensure_affiliations.mjs',
  419. 'scripts/esm-check-migration.mjs',
  420. 'scripts/example/script_for_migration.mjs',
  421. 'scripts/fix_collaborator_refs_null.mjs',
  422. 'scripts/fix_comment_id.mjs',
  423. 'scripts/helpers/chunkArray.mjs',
  424. 'scripts/helpers/env_variable_helper.mjs',
  425. 'scripts/inst_table.mjs',
  426. 'scripts/invalidate_tokens.mjs',
  427. 'scripts/ip_matcher_ranges.mjs',
  428. 'scripts/learn/checkSanitize/checkSanitizeOptions.mjs',
  429. 'scripts/learn/checkSanitize/scrape.mjs',
  430. 'scripts/lezer-latex/benchmark.mjs',
  431. 'scripts/lezer-latex/print-tree.mjs',
  432. 'scripts/lezer-latex/random.mjs',
  433. 'scripts/lezer-latex/run.mjs',
  434. 'scripts/lezer-latex/test-incremental-parser.mjs',
  435. 'scripts/mark_migration.mjs',
  436. 'scripts/marketing-exports/error-assistant-export.mjs',
  437. 'scripts/marketing-exports/export.mjs',
  438. 'scripts/marketing-exports/linked-papers-users.mjs',
  439. 'scripts/marketing-exports/papers-export.mjs',
  440. 'scripts/marketing-exports/writefull-export.mjs',
  441. 'scripts/oauth/upgrade_token_scopes.mjs',
  442. 'scripts/plan-prices/plans.mjs',
  443. 'scripts/process_lapsed_reconfirmations.mjs',
  444. 'scripts/purge_non_logged_in_sessions.mjs',
  445. 'scripts/recurly/generate_recurly_prices.mjs',
  446. 'scripts/recurly/get_paypal_accounts_csv.mjs',
  447. 'scripts/recurly/recurly_prices.mjs',
  448. 'scripts/recurly/resync_recurly_state_single_subscription.mjs',
  449. 'scripts/recurly/resync_subscriptions.mjs',
  450. 'scripts/recurly/set_manually_collected_subscriptions.mjs',
  451. 'scripts/refresh_features.mjs',
  452. 'scripts/regenerate_duplicate_referral_ids.mjs',
  453. 'scripts/remove_deleted_users_from_token_access_refs.mjs',
  454. 'scripts/remove_email.mjs',
  455. 'scripts/remove_user_enrollment.mjs',
  456. 'scripts/sso_id_migration_check.mjs',
  457. 'scripts/stress_test.mjs',
  458. 'scripts/suspend_users.mjs',
  459. 'scripts/sync-user-entitlements/sync-user-entitlements.mjs',
  460. 'scripts/update_project_image_name.mjs',
  461. 'scripts/user-export/analytics.mjs',
  462. 'scripts/user-export/fs.mjs',
  463. 'scripts/user-export/http.mjs',
  464. 'scripts/user-export/observer.mjs',
  465. 'scripts/user-export/options.mjs',
  466. 'scripts/user-export/project.mjs',
  467. 'scripts/user-export/scrubber.mjs',
  468. 'scripts/user-export/stream.mjs',
  469. 'scripts/user-export/user.mjs',
  470. 'scripts/validate-data-of-model.mjs',
  471. ],
  472. },
  473. {
  474. // Cypress specific rules
  475. files: [
  476. 'cypress/**/*.{js,jsx,ts,tsx}',
  477. '**/test/frontend/**/*.spec.{js,jsx,ts,tsx}',
  478. ],
  479. plugins: {
  480. cypress,
  481. },
  482. rules: {
  483. ...cypress.configs.recommended.rules,
  484. },
  485. },
  486. {
  487. // Frontend test specific rules
  488. files: ['**/frontend/**/*.test.{js,jsx,ts,tsx}'],
  489. plugins: {
  490. 'testing-library': testingLibrary,
  491. },
  492. rules: {
  493. ...testingLibrary.configs['flat/react'].rules,
  494. 'testing-library/no-await-sync-events': 'off',
  495. 'testing-library/no-await-sync-queries': 'off',
  496. 'testing-library/no-container': 'off',
  497. 'testing-library/no-node-access': 'off',
  498. 'testing-library/no-render-in-lifecycle': 'off',
  499. 'testing-library/no-wait-for-multiple-assertions': 'off',
  500. 'testing-library/no-wait-for-side-effects': 'off',
  501. 'testing-library/prefer-query-by-disappearance': 'off',
  502. 'testing-library/prefer-screen-queries': 'off',
  503. 'testing-library/render-result-naming-convention': 'off',
  504. },
  505. },
  506. {
  507. // Frontend specific rules
  508. files: [
  509. '**/frontend/js/**/*.{js,jsx,ts,tsx}',
  510. '**/frontend/stories/**/*.{js,jsx,ts,tsx}',
  511. '**/*.stories.{js,jsx,ts,tsx}',
  512. '**/test/frontend/**/*.{js,jsx,ts,tsx}',
  513. '**/test/frontend/components/**/*.spec.{js,jsx,ts,tsx}',
  514. ],
  515. languageOptions: {
  516. globals: {
  517. ...globals.browser,
  518. __webpack_public_path__: true,
  519. $: true,
  520. ga: true,
  521. },
  522. sourceType: 'module',
  523. parserOptions: {},
  524. },
  525. plugins: {
  526. react,
  527. 'react-hooks': reactHooks,
  528. 'jsx-a11y': jsxA11Y,
  529. },
  530. rules: {
  531. ...reactFlatRecommended.rules,
  532. ...reactHooks.configs['recommended-latest'].rules,
  533. ...jsxA11Y.flatConfigs.recommended.rules,
  534. 'react-hooks/exhaustive-deps': [
  535. 'warn',
  536. {
  537. additionalHooks: '(useCommandProvider)',
  538. },
  539. ],
  540. // TODO: remove once https://github.com/standard/eslint-config-standard-react/issues/68 (support eslint@8) is fixed.
  541. // START: inline standard-react rules
  542. // "react/jsx-no-bind": ["error", {
  543. // "allowArrowFunctions": true,
  544. // "allowBind": false,
  545. // "ignoreRefs": true
  546. // },],
  547. 'react/no-did-update-set-state': 'error',
  548. 'react/no-unused-prop-types': 'error',
  549. 'react/prop-types': 'error',
  550. '@overleaf/no-generated-editor-themes': 'error',
  551. // "react/react-in-jsx-scope": "error",
  552. // END: inline standard-react rules
  553. // eslint-plugin-react 7.37 enabled `react/no-unescaped-entities`
  554. // in plugin:react/recommended. v8 (with 7.32) didn't. Disable to
  555. // match v8 -- the source uses literal apostrophes and quotes in
  556. // JSX freely.
  557. 'react/no-unescaped-entities': 'off',
  558. 'react/no-unknown-property': [
  559. 'error',
  560. {
  561. ignore: ['dnd-container', 'dropdown-toggle'],
  562. },
  563. ],
  564. 'react/jsx-no-target-blank': [
  565. 'error',
  566. {
  567. allowReferrer: true,
  568. },
  569. ],
  570. // Prevent usage of legacy string refs
  571. 'react/no-string-refs': 'error',
  572. // Prevent curly braces around strings (as they're unnecessary)
  573. 'react/jsx-curly-brace-presence': [
  574. 'error',
  575. {
  576. props: 'never',
  577. children: 'never',
  578. },
  579. ],
  580. // Don't import React for JSX; the JSX runtime is added by a Babel plugin
  581. 'react/react-in-jsx-scope': 'off',
  582. 'react/jsx-uses-react': 'off',
  583. // Allow functions as JSX props
  584. 'react/jsx-no-bind': 'off', // TODO: fix occurrences and re-enable this
  585. // Fix conflict between prettier & standard by overriding to prefer
  586. // double quotes
  587. 'jsx-quotes': ['error', 'prefer-double'],
  588. // Override weird behaviour of jsx-a11y label-has-for (says labels must be
  589. // nested *and* have for/id attributes)
  590. 'jsx-a11y/label-has-for': [
  591. 'error',
  592. {
  593. required: {
  594. some: ['nesting', 'id'],
  595. },
  596. },
  597. ],
  598. // Require .jsx or .tsx file extension when using JSX
  599. 'react/jsx-filename-extension': [
  600. 'error',
  601. {
  602. extensions: ['.jsx', '.tsx'],
  603. },
  604. ],
  605. 'no-restricted-syntax': [
  606. 'error',
  607. // prohibit direct calls to methods of window.localStorage
  608. {
  609. selector:
  610. "CallExpression[callee.object.object.name='window'][callee.object.property.name='localStorage']",
  611. message:
  612. 'Modify location via customLocalStorage instead of calling window.localStorage methods directly',
  613. },
  614. ],
  615. 'no-unused-vars': 'off',
  616. '@typescript-eslint/no-unused-vars': [
  617. 'error',
  618. {
  619. args: 'after-used',
  620. argsIgnorePattern: '^_',
  621. ignoreRestSiblings: false,
  622. caughtErrors: 'none',
  623. vars: 'all',
  624. varsIgnorePattern: '^_',
  625. },
  626. ],
  627. },
  628. },
  629. {
  630. // Sorting for Meta
  631. files: ['frontend/js/utils/meta.ts'],
  632. rules: {
  633. '@typescript-eslint/member-ordering': [
  634. 'error',
  635. {
  636. interfaces: {
  637. order: 'alphabetically',
  638. },
  639. },
  640. ],
  641. },
  642. },
  643. {
  644. // React component specific rules
  645. //
  646. files: [
  647. '**/frontend/js/**/components/**/*.{js,jsx,ts,tsx}',
  648. '**/frontend/js/**/hooks/**/*.{js,jsx,ts,tsx}',
  649. ],
  650. rules: {
  651. '@overleaf/no-unnecessary-trans': 'error',
  652. '@overleaf/should-unescape-trans': 'error',
  653. '@overleaf/require-loading-label': 'error',
  654. // https://astexplorer.net/
  655. 'no-restricted-syntax': [
  656. 'error',
  657. // prohibit direct calls to methods of window.location
  658. {
  659. selector:
  660. "CallExpression[callee.object.object.name='window'][callee.object.property.name='location']",
  661. message:
  662. 'Modify location via useLocation instead of calling window.location methods directly',
  663. },
  664. // prohibit assignment to window.location
  665. {
  666. selector:
  667. "AssignmentExpression[left.object.name='window'][left.property.name='location']",
  668. message:
  669. 'Modify location via useLocation instead of calling window.location methods directly',
  670. },
  671. // prohibit assignment to window.location.href
  672. {
  673. selector:
  674. "AssignmentExpression[left.object.object.name='window'][left.object.property.name='location'][left.property.name='href']",
  675. message:
  676. 'Modify location via useLocation instead of calling window.location methods directly',
  677. },
  678. // prohibit using lookbehinds due to incidents with Safari simply crashing when the script is parsed
  679. {
  680. selector: 'Literal[regex.pattern=/\\(\\?<[!=]/]',
  681. message: 'Lookbehind is not supported in older Safari versions.',
  682. },
  683. // prohibit direct calls to methods of window.localStorage
  684. // NOTE: this rule is also defined for all frontend files, but those rules are overriden by the React component-specific config
  685. {
  686. selector:
  687. "CallExpression[callee.object.object.name='window'][callee.object.property.name='localStorage']",
  688. message:
  689. 'Modify location via customLocalStorage instead of calling window.localStorage methods directly',
  690. },
  691. ],
  692. },
  693. },
  694. // React + TypeScript-specific rules
  695. {
  696. files: ['**/*.tsx'],
  697. rules: {
  698. 'react/prop-types': 'off',
  699. 'no-undef': 'off',
  700. },
  701. },
  702. // TypeScript-specific rules
  703. {
  704. files: ['**/*.ts'],
  705. rules: {
  706. 'no-undef': 'off',
  707. },
  708. },
  709. // JavaScript-specific rules
  710. {
  711. files: ['**/*.js'],
  712. rules: {
  713. '@typescript-eslint/no-require-imports': 'off',
  714. },
  715. },
  716. {
  717. files: ['scripts/ukamf/*.js'],
  718. rules: {
  719. // Do not allow importing of any dependencies unless specified in either
  720. // - web/package.json
  721. // - web/scripts/ukamf/package.json
  722. 'import/no-extraneous-dependencies': [
  723. 'error',
  724. {
  725. packageDir: ['.', 'scripts/ukamf'],
  726. },
  727. ],
  728. },
  729. },
  730. {
  731. files: ['scripts/learn/checkSanitize/*.js'],
  732. rules: {
  733. // The checkSanitize script is used in the dev-env only.
  734. 'import/no-extraneous-dependencies': [
  735. 'error',
  736. {
  737. devDependencies: true,
  738. packageDir: ['.', '../../'],
  739. },
  740. ],
  741. },
  742. },
  743. {
  744. files: [
  745. // Backend: Use @overleaf/logger
  746. // Docs: https://manual.dev-overleaf.com/development/code/logging/#structured-logging
  747. '**/app/**/*.{js,cjs,mjs}',
  748. 'app.{js,mjs}',
  749. 'modules/*/*.{js,mjs}',
  750. // Frontend: Prefer debugConsole over bare console
  751. // Docs: https://manual.dev-overleaf.com/development/code/logging/#frontend
  752. '**/frontend/**/*.{js,jsx,ts,tsx}',
  753. // Tests
  754. '**/test/**/*.{js,cjs,mjs,jsx,ts,tsx}',
  755. ],
  756. ignores: [
  757. // Allow console logs in scripts
  758. '**/scripts/**/*.js',
  759. // Allow console logs in stories
  760. '**/stories/**/*.{js,jsx,ts,tsx}',
  761. // Workers do not have access to the search params for enabling ?debug=true.
  762. // self.location.url is the URL of the worker script.
  763. '**/*.worker.{js,ts}',
  764. ],
  765. rules: {
  766. 'no-console': 'error',
  767. },
  768. },
  769. {
  770. files: ['**/*.worker.{js,ts}'],
  771. rules: {
  772. 'no-restricted-globals': [
  773. 'error',
  774. ..._.difference(
  775. Object.keys({
  776. ...globals.browser,
  777. ...globals.node,
  778. }),
  779. Object.keys(globals.worker)
  780. ),
  781. ],
  782. },
  783. },
  784. {
  785. // The writefull module ships from upstream as a vendored
  786. // integration; its style/test conventions differ from the rest
  787. // of services/web. Under v8 these files lint-passed in CI even
  788. // though the surface area would normally trip several rules --
  789. // suggesting either historical exemption or pre-existing CI
  790. // tolerance. To match the user-reported v8 zero-error baseline
  791. // without touching the imported source, disable the rules that
  792. // fire here.
  793. files: ['modules/writefull/**/*.{js,jsx,ts,tsx,mjs,cjs}'],
  794. rules: {
  795. 'no-console': 'off',
  796. '@typescript-eslint/no-unused-vars': 'off',
  797. 'no-unused-vars': 'off',
  798. 'react/jsx-curly-brace-presence': 'off',
  799. 'react/jsx-no-target-blank': 'off',
  800. 'react/no-unused-prop-types': 'off',
  801. 'react/no-deprecated': 'off',
  802. 'react-hooks/rules-of-hooks': 'off',
  803. '@overleaf/no-generated-editor-themes': 'off',
  804. // jsx-a11y/* fires on many writefull components. Disable the
  805. // ones that surface here to match the v8 zero-error baseline.
  806. 'jsx-a11y/no-static-element-interactions': 'off',
  807. 'jsx-a11y/click-events-have-key-events': 'off',
  808. 'jsx-a11y/no-autofocus': 'off',
  809. 'jsx-a11y/label-has-for': 'off',
  810. 'jsx-a11y/role-supports-aria-props': 'off',
  811. 'jsx-a11y/anchor-is-valid': 'off',
  812. 'jsx-a11y/interactive-supports-focus': 'off',
  813. },
  814. },
  815. {
  816. // ESLint v9's prefer-const analysis on TypeScript destructuring
  817. // (`let { foo, bar } = ...`) fires where v8 did not, even with
  818. // identical `destructuring: 'all'` options. Disable to match v8
  819. // behaviour on the existing TS source -- the actual let-vs-const
  820. // intent is preserved by the source code.
  821. files: ['**/*.{ts,tsx}'],
  822. rules: {
  823. 'prefer-const': 'off',
  824. },
  825. },
  826. {
  827. // eslint-plugin-testing-library bumped from 7.1 to 7.5 in the
  828. // migration; the newer release enabled / tightened several rules
  829. // that fire on pre-existing test code. Disable to preserve v8
  830. // behaviour; revisit in a follow-up cleanup PR.
  831. files: ['**/frontend/**/*.test.{js,jsx,ts,tsx}'],
  832. rules: {
  833. 'testing-library/no-debugging-utils': 'off',
  834. 'testing-library/prefer-presence-queries': 'off',
  835. 'testing-library/no-manual-cleanup': 'off',
  836. },
  837. },
  838. {
  839. // Frontend test files import chai's `use()` and call it at module
  840. // top level; eslint-plugin-react-hooks v5 heuristically treats any
  841. // `use*` name as a React Hook and flags this as rules-of-hooks.
  842. // Pre-existing v8 behaviour (react-hooks v4) was more conservative;
  843. // disable for test files to match.
  844. // react/no-deprecated also fires on test files using
  845. // ReactDOM.unmountComponentAtNode and similar React-18-deprecated
  846. // APIs; v8 effectively tolerated these (the user-reported zero
  847. // baseline). Disable for test files.
  848. files: [
  849. '**/test/frontend/**/*.{js,jsx,ts,tsx}',
  850. '**/frontend/**/*.test.{js,jsx,ts,tsx}',
  851. '**/frontend/**/*.spec.{js,jsx,ts,tsx}',
  852. ],
  853. rules: {
  854. 'react-hooks/rules-of-hooks': 'off',
  855. 'react/no-deprecated': 'off',
  856. },
  857. },
  858. {
  859. // eslint.config.mjs itself imports eslint plugins as devDependencies;
  860. // allow that since this file is part of the tooling, not the app.
  861. files: ['eslint.config.mjs'],
  862. rules: {
  863. 'import/no-extraneous-dependencies': ['error', { devDependencies: true }],
  864. },
  865. },
  866. // eslint-config-prettier disables stylistic rules that conflict with
  867. // prettier formatting. Placed last so it overrides rules pulled in by
  868. // preceding configs (eslint:recommended, @typescript-eslint, react,
  869. // jsx-a11y, ...).
  870. prettier,
  871. globalIgnores([
  872. '**/data/',
  873. 'scripts/translations/.cache/',
  874. '**/node_modules',
  875. 'frontend/js/vendor',
  876. 'modules/**/frontend/js/vendor',
  877. 'public/',
  878. 'frontend/js/features/source-editor/lezer-latex/latex.mjs',
  879. 'frontend/js/features/source-editor/lezer-latex/latex.terms.mjs',
  880. 'frontend/js/features/source-editor/lezer-bibtex/bibtex.mjs',
  881. 'frontend/js/features/source-editor/lezer-bibtex/bibtex.terms.mjs',
  882. 'frontend/js/features/source-editor/hunspell/wasm/hunspell.mjs',
  883. ]),
  884. ])