.eslintrc.cjs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. const { builtinModules } = require('node:module')
  2. const DOMGlobals = ['window', 'document']
  3. const NodeGlobals = ['module', 'require']
  4. const banConstEnum = {
  5. selector: 'TSEnumDeclaration[const=true]',
  6. message:
  7. 'Please use non-const enums. This project automatically inlines enums.',
  8. }
  9. /**
  10. * @type {import('eslint-define-config').ESLintConfig}
  11. */
  12. module.exports = {
  13. parser: '@typescript-eslint/parser',
  14. parserOptions: {
  15. sourceType: 'module',
  16. },
  17. plugins: ['jest', 'import', '@typescript-eslint'],
  18. rules: {
  19. 'no-debugger': 'error',
  20. // most of the codebase are expected to be env agnostic
  21. 'no-restricted-globals': ['error', ...DOMGlobals, ...NodeGlobals],
  22. 'no-restricted-syntax': [
  23. 'error',
  24. banConstEnum,
  25. // since we target ES2015 for baseline support, we need to forbid object
  26. // rest spread usage in destructure as it compiles into a verbose helper.
  27. 'ObjectPattern > RestElement',
  28. // tsc compiles assignment spread into Object.assign() calls, but esbuild
  29. // still generates verbose helpers, so spread assignment is also prohiboted
  30. 'ObjectExpression > SpreadElement',
  31. 'AwaitExpression',
  32. ],
  33. 'sort-imports': ['error', { ignoreDeclarationSort: true }],
  34. 'import/no-nodejs-modules': [
  35. 'error',
  36. { allow: builtinModules.map(mod => `node:${mod}`) },
  37. ],
  38. // This rule enforces the preference for using '@ts-expect-error' comments in TypeScript
  39. // code to indicate intentional type errors, improving code clarity and maintainability.
  40. '@typescript-eslint/prefer-ts-expect-error': 'error',
  41. // Enforce the use of 'import type' for importing types
  42. '@typescript-eslint/consistent-type-imports': [
  43. 'error',
  44. {
  45. fixStyle: 'inline-type-imports',
  46. disallowTypeAnnotations: false,
  47. },
  48. ],
  49. // Enforce the use of top-level import type qualifier when an import only has specifiers with inline type qualifiers
  50. '@typescript-eslint/no-import-type-side-effects': 'error',
  51. },
  52. overrides: [
  53. // tests, no restrictions (runs in Node / jest with jsdom)
  54. {
  55. files: ['**/__tests__/**', 'packages/dts-test/**'],
  56. rules: {
  57. 'no-restricted-globals': 'off',
  58. 'no-restricted-syntax': 'off',
  59. 'jest/no-disabled-tests': 'error',
  60. 'jest/no-focused-tests': 'error',
  61. },
  62. },
  63. // shared, may be used in any env
  64. {
  65. files: ['packages/shared/**', '.eslintrc.cjs'],
  66. rules: {
  67. 'no-restricted-globals': 'off',
  68. },
  69. },
  70. // Packages targeting DOM
  71. {
  72. files: ['packages/{vue,vue-compat,runtime-dom}/**'],
  73. rules: {
  74. 'no-restricted-globals': ['error', ...NodeGlobals],
  75. },
  76. },
  77. // Packages targeting Node
  78. {
  79. files: ['packages/{compiler-sfc,compiler-ssr,server-renderer}/**'],
  80. rules: {
  81. 'no-restricted-globals': ['error', ...DOMGlobals],
  82. 'no-restricted-syntax': ['error', banConstEnum],
  83. },
  84. },
  85. // Private package, browser only + no syntax restrictions
  86. {
  87. files: ['packages/template-explorer/**', 'packages/sfc-playground/**'],
  88. rules: {
  89. 'no-restricted-globals': ['error', ...NodeGlobals],
  90. 'no-restricted-syntax': ['error', banConstEnum],
  91. },
  92. },
  93. // JavaScript files
  94. {
  95. files: ['*.js', '*.cjs'],
  96. rules: {
  97. // We only do `no-unused-vars` checks for js files, TS files are checked by TypeScript itself.
  98. 'no-unused-vars': ['error', { vars: 'all', args: 'none' }],
  99. },
  100. },
  101. // Node scripts
  102. {
  103. files: [
  104. 'scripts/**',
  105. './*.{js,ts}',
  106. 'packages/*/*.js',
  107. 'packages/vue/*/*.js',
  108. ],
  109. rules: {
  110. 'no-restricted-globals': 'off',
  111. 'no-restricted-syntax': ['error', banConstEnum],
  112. },
  113. },
  114. // Import nodejs modules in compiler-sfc
  115. {
  116. files: ['packages/compiler-sfc/src/**'],
  117. rules: {
  118. 'import/no-nodejs-modules': ['error', { allow: builtinModules }],
  119. },
  120. },
  121. ],
  122. }