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.

130 lines
4.1 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. // since we target ES2015 for baseline support, we need to forbid object
  27. // rest spread usage in destructure as it compiles into a verbose helper.
  28. 'ObjectPattern > RestElement',
  29. // tsc compiles assignment spread into Object.assign() calls, but esbuild
  30. // still generates verbose helpers, so spread assignment is also prohiboted
  31. 'ObjectExpression > SpreadElement',
  32. 'AwaitExpression',
  33. ],
  34. 'sort-imports': ['error', { ignoreDeclarationSort: true }],
  35. 'import/no-nodejs-modules': [
  36. 'error',
  37. { allow: builtinModules.map(mod => `node:${mod}`) },
  38. ],
  39. // This rule enforces the preference for using '@ts-expect-error' comments in TypeScript
  40. // code to indicate intentional type errors, improving code clarity and maintainability.
  41. '@typescript-eslint/prefer-ts-expect-error': 'error',
  42. // Enforce the use of 'import type' for importing types
  43. '@typescript-eslint/consistent-type-imports': [
  44. 'error',
  45. {
  46. fixStyle: 'inline-type-imports',
  47. disallowTypeAnnotations: false,
  48. },
  49. ],
  50. // Enforce the use of top-level import type qualifier when an import only has specifiers with inline type qualifiers
  51. '@typescript-eslint/no-import-type-side-effects': 'error',
  52. },
  53. overrides: [
  54. // tests, no restrictions (runs in Node / jest with jsdom)
  55. {
  56. files: ['**/__tests__/**', 'packages/dts-test/**'],
  57. rules: {
  58. 'no-console': 'off',
  59. 'no-restricted-globals': 'off',
  60. 'no-restricted-syntax': 'off',
  61. 'jest/no-disabled-tests': 'error',
  62. 'jest/no-focused-tests': 'error',
  63. },
  64. },
  65. // shared, may be used in any env
  66. {
  67. files: ['packages/shared/**', '.eslintrc.cjs'],
  68. rules: {
  69. 'no-restricted-globals': 'off',
  70. },
  71. },
  72. // Packages targeting DOM
  73. {
  74. files: ['packages/{vue,vue-compat,runtime-dom}/**'],
  75. rules: {
  76. 'no-restricted-globals': ['error', ...NodeGlobals],
  77. },
  78. },
  79. // Packages targeting Node
  80. {
  81. files: ['packages/{compiler-sfc,compiler-ssr,server-renderer}/**'],
  82. rules: {
  83. 'no-restricted-globals': ['error', ...DOMGlobals],
  84. 'no-restricted-syntax': ['error', banConstEnum],
  85. },
  86. },
  87. // Private package, browser only + no syntax restrictions
  88. {
  89. files: ['packages/template-explorer/**', 'packages/sfc-playground/**'],
  90. rules: {
  91. 'no-restricted-globals': ['error', ...NodeGlobals],
  92. 'no-restricted-syntax': ['error', banConstEnum],
  93. 'no-console': 'off',
  94. },
  95. },
  96. // JavaScript files
  97. {
  98. files: ['*.js', '*.cjs'],
  99. rules: {
  100. // We only do `no-unused-vars` checks for js files, TS files are checked by TypeScript itself.
  101. 'no-unused-vars': ['error', { vars: 'all', args: 'none' }],
  102. },
  103. },
  104. // Node scripts
  105. {
  106. files: [
  107. 'scripts/**',
  108. './*.{js,ts}',
  109. 'packages/*/*.js',
  110. 'packages/vue/*/*.js',
  111. ],
  112. rules: {
  113. 'no-restricted-globals': 'off',
  114. 'no-restricted-syntax': ['error', banConstEnum],
  115. 'no-console': 'off',
  116. },
  117. },
  118. // Import nodejs modules in compiler-sfc
  119. {
  120. files: ['packages/compiler-sfc/src/**'],
  121. rules: {
  122. 'import/no-nodejs-modules': ['error', { allow: builtinModules }],
  123. },
  124. },
  125. ],
  126. }