eslint.config.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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: [
  107. 'packages/template-explorer/**',
  108. 'packages/sfc-playground/**',
  109. 'playground/**',
  110. ],
  111. rules: {
  112. 'no-restricted-globals': ['error', ...NodeGlobals],
  113. 'no-restricted-syntax': ['error', banConstEnum],
  114. 'no-console': 'off',
  115. },
  116. },
  117. // JavaScript files
  118. {
  119. files: ['*.js'],
  120. rules: {
  121. // We only do `no-unused-vars` checks for js files, TS files are checked by TypeScript itself.
  122. 'no-unused-vars': ['error', { vars: 'all', args: 'none' }],
  123. },
  124. },
  125. // Node scripts
  126. {
  127. files: [
  128. 'eslint.config.js',
  129. 'rollup.config.js',
  130. 'scripts/**',
  131. './*.{js,ts}',
  132. 'packages/*/*.js',
  133. 'packages/vue/*/*.js',
  134. ],
  135. rules: {
  136. 'no-restricted-globals': 'off',
  137. 'no-restricted-syntax': ['error', banConstEnum],
  138. 'no-console': 'off',
  139. },
  140. },
  141. // Import nodejs modules in compiler-sfc
  142. {
  143. files: ['packages/compiler-sfc/src/**'],
  144. rules: {
  145. 'import-x/no-nodejs-modules': ['error', { allow: builtinModules }],
  146. },
  147. },
  148. {
  149. ignores: [
  150. '**/dist/',
  151. '**/temp/',
  152. '**/coverage/',
  153. '.idea/',
  154. 'explorations/',
  155. 'dts-build/packages',
  156. ],
  157. },
  158. )