eslint.config.mjs 33 KB

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