eslint.config.mjs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import stylistic from '@stylistic/eslint-plugin'
  2. import sonarjs from 'eslint-plugin-sonarjs'
  3. import tseslint from 'typescript-eslint'
  4. // Strict type-aware correctness rules plus repository formatting. Tests/examples relax deliberate
  5. // mock unsafety; vendored sources retain upstream style and receive only selected safety checks.
  6. export default tseslint.config(
  7. {
  8. ignores: [
  9. '**/lib/**',
  10. '**/node_modules/**',
  11. '**/.sessions/**',
  12. '.claude/**', // harness-local state (worktrees, skills) — other checkouts, not this one's sources
  13. '**/.doc-typecheck-*/**',
  14. 'website/.generated/**',
  15. 'vendor/**', // vendored source keeps upstream style and idioms
  16. 'native/**', // imported landlock-run subtree: self-contained workspace with its own gates (native/README.md)
  17. '**/*.js',
  18. '**/*.mjs',
  19. '*.config.ts', // root tool configs (vitest, tsdown) — no project service
  20. ],
  21. },
  22. // --- our packages: full strictness -------------------------------------
  23. {
  24. files: ['packages/*/*/src/**/*.ts', 'examples/**/*.ts', 'scripts/**/*.ts', 'website/**/*.ts'],
  25. extends: [
  26. ...tseslint.configs.strictTypeChecked,
  27. ],
  28. languageOptions: {
  29. parserOptions: {
  30. // One project service resolves each file to its owning tsconfig and shares dependency
  31. // graphs. Per-package programs duplicated path-mapped and Cordis closures, reaching ~5 GB.
  32. projectService: true,
  33. tsconfigRootDir: import.meta.dirname,
  34. },
  35. },
  36. rules: {
  37. // The bug class this repo cares most about: lost promises in the loop.
  38. '@typescript-eslint/no-floating-promises': 'error',
  39. '@typescript-eslint/no-misused-promises': 'error',
  40. '@typescript-eslint/require-await': 'error',
  41. '@typescript-eslint/switch-exhaustiveness-check': ['error', {
  42. considerDefaultExhaustiveForUnions: true,
  43. }],
  44. '@typescript-eslint/no-unnecessary-condition': ['error', {
  45. allowConstantLoopConditions: true,
  46. }],
  47. // `any` requires a justification comment — enforced as: no bare casts.
  48. '@typescript-eslint/no-explicit-any': 'error',
  49. // Style points where the codebase intentionally diverges from preset:
  50. '@typescript-eslint/no-namespace': 'off', // Cordis Config-namespace idiom
  51. '@typescript-eslint/no-empty-object-type': 'off', // merge-extensible maps
  52. '@typescript-eslint/no-invalid-void-type': 'off', // event signatures
  53. '@typescript-eslint/restrict-template-expressions': ['error', {
  54. allowNumber: true,
  55. allowBoolean: true,
  56. }],
  57. // `void foo()` in arrow listeners is our idiom for intentional fire-and-forget
  58. 'no-void': 'off',
  59. '@typescript-eslint/no-unused-vars': ['error', {
  60. argsIgnorePattern: '^_',
  61. varsIgnorePattern: '^_',
  62. caughtErrorsIgnorePattern: '^_',
  63. }],
  64. },
  65. },
  66. // --- examples: demo code conforms to async interfaces without awaiting ---
  67. {
  68. files: ['examples/**/*.ts'],
  69. rules: {
  70. '@typescript-eslint/require-await': 'off',
  71. },
  72. },
  73. // --- tests: same rules, minus the friction that fights test ergonomics --
  74. {
  75. files: ['packages/*/*/tests/**/*.ts', 'examples/*/tests/**/*.ts', 'scripts/**/*.spec.ts'],
  76. extends: [
  77. ...tseslint.configs.strictTypeChecked,
  78. ],
  79. languageOptions: {
  80. parserOptions: {
  81. // Same shared project service as the src block: test files resolve
  82. // through the root tsconfig (its include covers every tests/ tree).
  83. projectService: true,
  84. tsconfigRootDir: import.meta.dirname,
  85. },
  86. },
  87. rules: {
  88. '@typescript-eslint/no-floating-promises': 'error',
  89. '@typescript-eslint/no-misused-promises': 'error',
  90. '@typescript-eslint/no-explicit-any': 'error',
  91. '@typescript-eslint/no-non-null-assertion': 'off', // assertions follow expect()s
  92. '@typescript-eslint/no-unnecessary-condition': 'off',
  93. '@typescript-eslint/require-await': 'off', // mock execute() signatures
  94. '@typescript-eslint/no-empty-function': 'off', // stub agents
  95. '@typescript-eslint/only-throw-error': 'off', // testing non-Error throws
  96. '@typescript-eslint/no-namespace': 'off',
  97. '@typescript-eslint/no-empty-object-type': 'off',
  98. '@typescript-eslint/restrict-template-expressions': 'off',
  99. '@typescript-eslint/no-unused-vars': ['error', {
  100. argsIgnorePattern: '^_',
  101. varsIgnorePattern: '^_',
  102. caughtErrorsIgnorePattern: '^_',
  103. }],
  104. },
  105. },
  106. // --- file-local duplication (all owned TypeScript) ---------------------
  107. {
  108. files: ['packages/**/*.ts', 'examples/**/*.ts', 'scripts/**/*.ts', 'website/**/*.ts'],
  109. plugins: { sonarjs },
  110. rules: {
  111. // Cross-file clones are covered separately by jscpd.
  112. 'sonarjs/duplicates-in-character-class': 'error',
  113. 'sonarjs/no-all-duplicated-branches': 'error',
  114. 'sonarjs/no-duplicate-in-composite': 'error',
  115. 'sonarjs/no-duplicate-test-title': 'error',
  116. 'sonarjs/no-identical-conditions': 'error',
  117. 'sonarjs/no-identical-expressions': 'error',
  118. 'sonarjs/no-identical-functions': 'error',
  119. 'sonarjs/no-duplicated-branches': 'error',
  120. },
  121. },
  122. // --- formatting (everything we own) -------------------------------------
  123. {
  124. files: ['packages/**/*.ts', 'examples/**/*.ts', 'scripts/**/*.ts', 'website/**/*.ts', 'eslint.config.mjs'],
  125. plugins: { '@stylistic': stylistic },
  126. rules: {
  127. '@stylistic/indent': ['error', 2],
  128. '@stylistic/semi': ['error', 'never'],
  129. '@stylistic/quotes': ['error', 'single', { avoidEscape: true }],
  130. '@stylistic/comma-dangle': ['error', 'always-multiline'],
  131. '@stylistic/eol-last': ['error', 'always'],
  132. '@stylistic/no-trailing-spaces': 'error',
  133. '@stylistic/object-curly-spacing': ['error', 'always'],
  134. '@stylistic/arrow-parens': ['error', 'as-needed', { requireForBlockBody: true }],
  135. '@stylistic/member-delimiter-style': ['error', {
  136. multiline: { delimiter: 'none' },
  137. singleline: { delimiter: 'semi', requireLast: false },
  138. }],
  139. '@stylistic/max-len': ['error', { code: 140, ignoreUrls: true, ignoreStrings: true, ignoreTemplateLiterals: true }],
  140. },
  141. },
  142. )