.eslintrc.cjs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // eslint-disable-next-line @typescript-eslint/no-var-requires
  2. const { builtinModules } = require('module');
  3. /** @type {import("@types/eslint").Linter.Config} */
  4. module.exports = {
  5. extends: [
  6. 'plugin:@typescript-eslint/recommended-type-checked',
  7. 'plugin:@typescript-eslint/stylistic-type-checked',
  8. 'prettier',
  9. 'plugin:regexp/recommended',
  10. ],
  11. parser: '@typescript-eslint/parser',
  12. parserOptions: {
  13. project: ['./packages/*/tsconfig.json', './tsconfig.eslint.json'],
  14. tsconfigRootDir: __dirname,
  15. },
  16. plugins: ['@typescript-eslint', 'prettier', 'no-only-tests', 'regexp'],
  17. rules: {
  18. // These off/configured-differently-by-default rules fit well for us
  19. '@typescript-eslint/switch-exhaustiveness-check': 'error',
  20. '@typescript-eslint/array-type': ['error', { default: 'array-simple' }],
  21. '@typescript-eslint/no-unused-vars': [
  22. 'warn',
  23. {
  24. argsIgnorePattern: '^_',
  25. varsIgnorePattern: '^_',
  26. caughtErrorsIgnorePattern: '^_',
  27. ignoreRestSiblings: true,
  28. },
  29. ],
  30. 'no-only-tests/no-only-tests': 'error',
  31. '@typescript-eslint/no-shadow': ['error'],
  32. 'no-console': 'warn',
  33. // Todo: do we want these?
  34. '@typescript-eslint/array-type': 'off',
  35. '@typescript-eslint/ban-ts-comment': 'off',
  36. '@typescript-eslint/class-literal-property-style': 'off',
  37. '@typescript-eslint/consistent-indexed-object-style': 'off',
  38. '@typescript-eslint/consistent-type-definitions': 'off',
  39. '@typescript-eslint/dot-notation': 'off',
  40. '@typescript-eslint/no-base-to-string': 'off',
  41. '@typescript-eslint/no-empty-function': 'off',
  42. '@typescript-eslint/no-floating-promises': 'off',
  43. '@typescript-eslint/no-misused-promises': 'off',
  44. '@typescript-eslint/no-redundant-type-constituents': 'off',
  45. '@typescript-eslint/no-this-alias': 'off',
  46. '@typescript-eslint/no-unsafe-argument': 'off',
  47. '@typescript-eslint/no-unsafe-assignment': 'off',
  48. '@typescript-eslint/no-unsafe-call': 'off',
  49. '@typescript-eslint/no-unsafe-member-access': 'off',
  50. '@typescript-eslint/no-unsafe-return': 'off',
  51. '@typescript-eslint/prefer-nullish-coalescing': 'off',
  52. '@typescript-eslint/prefer-optional-chain': 'off',
  53. '@typescript-eslint/prefer-string-starts-ends-with': 'off',
  54. '@typescript-eslint/require-await': 'off',
  55. '@typescript-eslint/restrict-plus-operands': 'off',
  56. '@typescript-eslint/restrict-template-expressions': 'off',
  57. '@typescript-eslint/sort-type-constituents': 'off',
  58. '@typescript-eslint/unbound-method': 'off',
  59. '@typescript-eslint/no-explicit-any': 'off',
  60. // Enforce separate type imports for type-only imports to avoid bundling unneeded code
  61. '@typescript-eslint/consistent-type-imports': [
  62. 'error',
  63. {
  64. prefer: 'type-imports',
  65. fixStyle: 'separate-type-imports',
  66. disallowTypeAnnotations: false,
  67. },
  68. ],
  69. // These rules enabled by the preset configs don't work well for us
  70. '@typescript-eslint/await-thenable': 'off',
  71. 'prefer-const': 'off',
  72. // In some cases, using explicit letter-casing is more performant than the `i` flag
  73. 'regexp/use-ignore-case': 'off',
  74. },
  75. overrides: [
  76. {
  77. // Ensure Node builtins aren't included in Astro's server runtime
  78. files: ['packages/astro/src/runtime/**/*.ts'],
  79. rules: {
  80. 'no-restricted-imports': [
  81. 'error',
  82. {
  83. paths: [...builtinModules],
  84. patterns: ['node:*'],
  85. },
  86. ],
  87. },
  88. },
  89. {
  90. files: ['packages/astro/src/runtime/client/**/*.ts'],
  91. env: {
  92. browser: true,
  93. },
  94. },
  95. {
  96. files: ['packages/**/test/*.js', 'packages/**/*.js'],
  97. env: {
  98. mocha: true,
  99. },
  100. globals: {
  101. globalThis: false, // false means read-only
  102. },
  103. rules: {
  104. 'no-console': 'off',
  105. },
  106. },
  107. {
  108. files: ['packages/integrations/**/*.ts'],
  109. rules: {
  110. 'no-console': ['error', { allow: ['warn', 'error', 'info', 'debug'] }],
  111. },
  112. },
  113. {
  114. files: ['benchmark/**/*.js'],
  115. rules: {
  116. '@typescript-eslint/no-unused-vars': 'off',
  117. 'no-console': 'off',
  118. },
  119. },
  120. {
  121. files: ['packages/astro/src/core/errors/errors-data.ts'],
  122. rules: {
  123. // This file is used for docs generation, as such the code need to be in a certain format, we can somewhat ensure this with these rules
  124. 'object-shorthand': ['error', 'methods', { avoidExplicitReturnArrows: true }],
  125. 'arrow-body-style': ['error', 'never'],
  126. },
  127. },
  128. ],
  129. };