eslint.config.js 4.9 KB

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