You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.3 KiB

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