eslint.config.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import importX from 'eslint-plugin-import-x'
  2. import tseslint from 'typescript-eslint'
  3. import vitest from 'eslint-plugin-vitest'
  4. import { builtinModules } from 'node:module'
  5. const DOMGlobals = ['window', 'document']
  6. const NodeGlobals = ['module', 'require']
  7. const banConstEnum = {
  8. selector: 'TSEnumDeclaration[const=true]',
  9. message:
  10. 'Please use non-const enums. This project automatically inlines enums.',
  11. }
  12. export default tseslint.config(
  13. {
  14. files: ['**/*.js', '**/*.ts', '**/*.tsx'],
  15. extends: [tseslint.configs.base],
  16. plugins: {
  17. 'import-x': importX,
  18. },
  19. rules: {
  20. 'no-debugger': 'error',
  21. 'no-console': ['error', { allow: ['warn', 'error', 'info'] }],
  22. // most of the codebase are expected to be env agnostic
  23. 'no-restricted-globals': ['error', ...DOMGlobals, ...NodeGlobals],
  24. 'no-restricted-syntax': [
  25. 'error',
  26. banConstEnum,
  27. {
  28. selector: 'ObjectPattern > RestElement',
  29. message:
  30. 'Our output target is ES2016, and object rest spread results in ' +
  31. 'verbose helpers and should be avoided.',
  32. },
  33. {
  34. selector: 'ObjectExpression > SpreadElement',
  35. message:
  36. 'esbuild transpiles object spread into very verbose inline helpers.\n' +
  37. 'Please use the `extend` helper from @vue/shared instead.',
  38. },
  39. {
  40. selector: 'AwaitExpression',
  41. message:
  42. 'Our output target is ES2016, so async/await syntax should be avoided.',
  43. },
  44. ],
  45. 'sort-imports': ['error', { ignoreDeclarationSort: true }],
  46. 'import-x/no-nodejs-modules': [
  47. 'error',
  48. { allow: builtinModules.map(mod => `node:${mod}`) },
  49. ],
  50. // This rule enforces the preference for using '@ts-expect-error' comments in TypeScript
  51. // code to indicate intentional type errors, improving code clarity and maintainability.
  52. '@typescript-eslint/prefer-ts-expect-error': 'error',
  53. // Enforce the use of 'import type' for importing types
  54. '@typescript-eslint/consistent-type-imports': [
  55. 'error',
  56. {
  57. fixStyle: 'inline-type-imports',
  58. disallowTypeAnnotations: false,
  59. },
  60. ],
  61. // Enforce the use of top-level import type qualifier when an import only has specifiers with inline type qualifiers
  62. '@typescript-eslint/no-import-type-side-effects': 'error',
  63. },
  64. },
  65. // tests, no restrictions (runs in Node / Vitest with jsdom)
  66. {
  67. files: ['**/__tests__/**', 'packages/dts-test/**'],
  68. plugins: { vitest },
  69. languageOptions: {
  70. globals: {
  71. ...vitest.environments.env.globals,
  72. },
  73. },
  74. rules: {
  75. 'no-console': 'off',
  76. 'no-restricted-globals': 'off',
  77. 'no-restricted-syntax': 'off',
  78. 'vitest/no-disabled-tests': 'error',
  79. 'vitest/no-focused-tests': 'error',
  80. },
  81. },
  82. // shared, may be used in any env
  83. {
  84. files: ['packages/shared/**', 'eslint.config.js'],
  85. rules: {
  86. 'no-restricted-globals': 'off',
  87. },
  88. },
  89. // Packages targeting DOM
  90. {
  91. files: ['packages/{vue,vue-compat,runtime-dom}/**'],
  92. rules: {
  93. 'no-restricted-globals': ['error', ...NodeGlobals],
  94. },
  95. },
  96. // Packages targeting Node
  97. {
  98. files: ['packages/{compiler-sfc,compiler-ssr,server-renderer}/**'],
  99. rules: {
  100. 'no-restricted-globals': ['error', ...DOMGlobals],
  101. 'no-restricted-syntax': ['error', banConstEnum],
  102. },
  103. },
  104. // Private package, browser only + no syntax restrictions
  105. {
  106. files: ['packages/template-explorer/**', 'packages/sfc-playground/**'],
  107. rules: {
  108. 'no-restricted-globals': ['error', ...NodeGlobals],
  109. 'no-restricted-syntax': ['error', banConstEnum],
  110. 'no-console': 'off',
  111. },
  112. },
  113. // JavaScript files
  114. {
  115. files: ['*.js'],
  116. rules: {
  117. // We only do `no-unused-vars` checks for js files, TS files are checked by TypeScript itself.
  118. 'no-unused-vars': ['error', { vars: 'all', args: 'none' }],
  119. },
  120. },
  121. // Node scripts
  122. {
  123. files: [
  124. 'eslint.config.js',
  125. 'rollup.config.js',
  126. 'scripts/**',
  127. './*.{js,ts}',
  128. 'packages/*/*.js',
  129. 'packages/vue/*/*.js',
  130. ],
  131. rules: {
  132. 'no-restricted-globals': 'off',
  133. 'no-restricted-syntax': ['error', banConstEnum],
  134. 'no-console': 'off',
  135. },
  136. },
  137. // Import nodejs modules in compiler-sfc
  138. {
  139. files: ['packages/compiler-sfc/src/**'],
  140. rules: {
  141. 'import-x/no-nodejs-modules': ['error', { allow: builtinModules }],
  142. },
  143. },
  144. {
  145. ignores: [
  146. '**/dist/',
  147. '**/temp/',
  148. '**/coverage/',
  149. '.idea/',
  150. 'explorations/',
  151. 'dts-build/packages',
  152. ],
  153. },
  154. )