2
0

eslint.config.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import importX from 'eslint-plugin-import-x'
  2. import tseslint from 'typescript-eslint'
  3. import vitest from '@vitest/eslint-plugin'
  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: [
  74. '**/__tests__/**',
  75. 'packages-private/dts-test/**',
  76. 'packages-private/dts-build-test/**',
  77. ],
  78. plugins: { vitest },
  79. languageOptions: {
  80. globals: {
  81. ...vitest.environments.env.globals,
  82. },
  83. },
  84. rules: {
  85. 'no-console': 'off',
  86. 'no-restricted-globals': 'off',
  87. 'no-restricted-syntax': 'off',
  88. 'vitest/no-disabled-tests': 'error',
  89. 'vitest/no-focused-tests': 'error',
  90. },
  91. },
  92. // shared, may be used in any env
  93. {
  94. files: ['packages/shared/**', 'eslint.config.js'],
  95. rules: {
  96. 'no-restricted-globals': 'off',
  97. },
  98. },
  99. // Packages targeting DOM
  100. {
  101. files: ['packages/{vue,vue-compat,runtime-dom}/**'],
  102. rules: {
  103. 'no-restricted-globals': ['error', ...NodeGlobals],
  104. },
  105. },
  106. // Packages targeting Node
  107. {
  108. files: ['packages/{compiler-sfc,compiler-ssr,server-renderer}/**'],
  109. rules: {
  110. 'no-restricted-globals': ['error', ...DOMGlobals],
  111. 'no-restricted-syntax': ['error', banConstEnum],
  112. },
  113. },
  114. // Private package, browser only + no syntax restrictions
  115. {
  116. files: [
  117. 'packages-private/template-explorer/**',
  118. 'packages-private/sfc-playground/**',
  119. ],
  120. rules: {
  121. 'no-restricted-globals': ['error', ...NodeGlobals],
  122. 'no-restricted-syntax': ['error', banConstEnum],
  123. 'no-console': 'off',
  124. },
  125. },
  126. // JavaScript files
  127. {
  128. files: ['*.js'],
  129. rules: {
  130. // We only do `no-unused-vars` checks for js files, TS files are checked by TypeScript itself.
  131. 'no-unused-vars': ['error', { vars: 'all', args: 'none' }],
  132. },
  133. },
  134. // Node scripts
  135. {
  136. files: [
  137. 'eslint.config.js',
  138. 'rollup*.config.js',
  139. 'scripts/**',
  140. './*.{js,ts}',
  141. 'packages/*/*.js',
  142. 'packages/vue/*/*.js',
  143. ],
  144. rules: {
  145. 'no-restricted-globals': 'off',
  146. 'no-restricted-syntax': ['error', banConstEnum],
  147. 'no-console': 'off',
  148. },
  149. },
  150. // Import nodejs modules in compiler-sfc
  151. {
  152. files: ['packages/compiler-sfc/src/**'],
  153. rules: {
  154. 'import-x/no-nodejs-modules': ['error', { allow: builtinModules }],
  155. },
  156. },
  157. {
  158. ignores: [
  159. '**/dist/',
  160. '**/temp/',
  161. '**/coverage/',
  162. '.idea/',
  163. 'explorations/',
  164. 'dts-build/packages',
  165. ],
  166. },
  167. )