eslint.config.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. selector: 'ChainExpression',
  46. message:
  47. 'Our output target is ES2016, and optional chaining results in ' +
  48. 'verbose helpers and should be avoided.',
  49. },
  50. ],
  51. 'sort-imports': ['error', { ignoreDeclarationSort: true }],
  52. 'import-x/no-nodejs-modules': [
  53. 'error',
  54. { allow: builtinModules.map(mod => `node:${mod}`) },
  55. ],
  56. // This rule enforces the preference for using '@ts-expect-error' comments in TypeScript
  57. // code to indicate intentional type errors, improving code clarity and maintainability.
  58. '@typescript-eslint/prefer-ts-expect-error': 'error',
  59. // Enforce the use of 'import type' for importing types
  60. '@typescript-eslint/consistent-type-imports': [
  61. 'error',
  62. {
  63. fixStyle: 'inline-type-imports',
  64. disallowTypeAnnotations: false,
  65. },
  66. ],
  67. // Enforce the use of top-level import type qualifier when an import only has specifiers with inline type qualifiers
  68. '@typescript-eslint/no-import-type-side-effects': 'error',
  69. },
  70. },
  71. // tests, no restrictions (runs in Node / Vitest with jsdom)
  72. {
  73. files: ['**/__tests__/**', 'packages/dts-test/**'],
  74. plugins: { vitest },
  75. languageOptions: {
  76. globals: {
  77. ...vitest.environments.env.globals,
  78. },
  79. },
  80. rules: {
  81. 'no-console': 'off',
  82. 'no-restricted-globals': 'off',
  83. 'no-restricted-syntax': 'off',
  84. 'vitest/no-disabled-tests': 'error',
  85. 'vitest/no-focused-tests': 'error',
  86. },
  87. },
  88. // shared, may be used in any env
  89. {
  90. files: ['packages/shared/**', 'eslint.config.js'],
  91. rules: {
  92. 'no-restricted-globals': 'off',
  93. },
  94. },
  95. // Packages targeting DOM
  96. {
  97. files: ['packages/{vue,vue-compat,runtime-dom}/**'],
  98. rules: {
  99. 'no-restricted-globals': ['error', ...NodeGlobals],
  100. },
  101. },
  102. // Packages targeting Node
  103. {
  104. files: ['packages/{compiler-sfc,compiler-ssr,server-renderer}/**'],
  105. rules: {
  106. 'no-restricted-globals': ['error', ...DOMGlobals],
  107. 'no-restricted-syntax': ['error', banConstEnum],
  108. },
  109. },
  110. // Private package, browser only + no syntax restrictions
  111. {
  112. files: ['packages/template-explorer/**', 'packages/sfc-playground/**'],
  113. rules: {
  114. 'no-restricted-globals': ['error', ...NodeGlobals],
  115. 'no-restricted-syntax': ['error', banConstEnum],
  116. 'no-console': 'off',
  117. },
  118. },
  119. // JavaScript files
  120. {
  121. files: ['*.js'],
  122. rules: {
  123. // We only do `no-unused-vars` checks for js files, TS files are checked by TypeScript itself.
  124. 'no-unused-vars': ['error', { vars: 'all', args: 'none' }],
  125. },
  126. },
  127. // Node scripts
  128. {
  129. files: [
  130. 'eslint.config.js',
  131. 'rollup*.config.js',
  132. 'scripts/**',
  133. './*.{js,ts}',
  134. 'packages/*/*.js',
  135. 'packages/vue/*/*.js',
  136. ],
  137. rules: {
  138. 'no-restricted-globals': 'off',
  139. 'no-restricted-syntax': ['error', banConstEnum],
  140. 'no-console': 'off',
  141. },
  142. },
  143. // Import nodejs modules in compiler-sfc
  144. {
  145. files: ['packages/compiler-sfc/src/**'],
  146. rules: {
  147. 'import-x/no-nodejs-modules': ['error', { allow: builtinModules }],
  148. },
  149. },
  150. {
  151. ignores: [
  152. '**/dist/',
  153. '**/temp/',
  154. '**/coverage/',
  155. '.idea/',
  156. 'explorations/',
  157. 'dts-build/packages',
  158. ],
  159. },
  160. )