|
| 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 | + |
| 6 | +const DOMGlobals = ['window', 'document'] |
| 7 | +const NodeGlobals = ['module', 'require'] |
| 8 | + |
| 9 | +const banConstEnum = { |
| 10 | + selector: 'TSEnumDeclaration[const=true]', |
| 11 | + message: |
| 12 | + 'Please use non-const enums. This project automatically inlines enums.', |
| 13 | +} |
| 14 | + |
| 15 | +export default tseslint.config( |
| 16 | + { |
| 17 | + files: ['**/*.js', '**/*.ts', '**/*.tsx'], |
| 18 | + extends: [tseslint.configs.base], |
| 19 | + plugins: { |
| 20 | + 'import-x': importX, |
| 21 | + }, |
| 22 | + rules: { |
| 23 | + 'no-debugger': 'error', |
| 24 | + 'no-console': ['error', { allow: ['warn', 'error', 'info'] }], |
| 25 | + // most of the codebase are expected to be env agnostic |
| 26 | + 'no-restricted-globals': ['error', ...DOMGlobals, ...NodeGlobals], |
| 27 | + |
| 28 | + 'no-restricted-syntax': [ |
| 29 | + 'error', |
| 30 | + banConstEnum, |
| 31 | + { |
| 32 | + selector: 'ObjectPattern > RestElement', |
| 33 | + message: |
| 34 | + 'Our output target is ES2016, and object rest spread results in ' + |
| 35 | + 'verbose helpers and should be avoided.', |
| 36 | + }, |
| 37 | + { |
| 38 | + selector: 'ObjectExpression > SpreadElement', |
| 39 | + message: |
| 40 | + 'esbuild transpiles object spread into very verbose inline helpers.\n' + |
| 41 | + 'Please use the `extend` helper from @vue/shared instead.', |
| 42 | + }, |
| 43 | + { |
| 44 | + selector: 'AwaitExpression', |
| 45 | + message: |
| 46 | + 'Our output target is ES2016, so async/await syntax should be avoided.', |
| 47 | + }, |
| 48 | + ], |
| 49 | + 'sort-imports': ['error', { ignoreDeclarationSort: true }], |
| 50 | + |
| 51 | + 'import-x/no-nodejs-modules': [ |
| 52 | + 'error', |
| 53 | + { allow: builtinModules.map(mod => `node:${mod}`) }, |
| 54 | + ], |
| 55 | + // This rule enforces the preference for using '@ts-expect-error' comments in TypeScript |
| 56 | + // code to indicate intentional type errors, improving code clarity and maintainability. |
| 57 | + '@typescript-eslint/prefer-ts-expect-error': 'error', |
| 58 | + // Enforce the use of 'import type' for importing types |
| 59 | + '@typescript-eslint/consistent-type-imports': [ |
| 60 | + 'error', |
| 61 | + { |
| 62 | + fixStyle: 'inline-type-imports', |
| 63 | + disallowTypeAnnotations: false, |
| 64 | + }, |
| 65 | + ], |
| 66 | + // Enforce the use of top-level import type qualifier when an import only has specifiers with inline type qualifiers |
| 67 | + '@typescript-eslint/no-import-type-side-effects': 'error', |
| 68 | + }, |
| 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 | + |
| 89 | + // shared, may be used in any env |
| 90 | + { |
| 91 | + files: ['packages/shared/**', 'eslint.config.js'], |
| 92 | + rules: { |
| 93 | + 'no-restricted-globals': 'off', |
| 94 | + }, |
| 95 | + }, |
| 96 | + |
| 97 | + // Packages targeting DOM |
| 98 | + { |
| 99 | + files: ['packages/{vue,vue-compat,runtime-dom}/**'], |
| 100 | + rules: { |
| 101 | + 'no-restricted-globals': ['error', ...NodeGlobals], |
| 102 | + }, |
| 103 | + }, |
| 104 | + |
| 105 | + // Packages targeting Node |
| 106 | + { |
| 107 | + files: ['packages/{compiler-sfc,compiler-ssr,server-renderer}/**'], |
| 108 | + rules: { |
| 109 | + 'no-restricted-globals': ['error', ...DOMGlobals], |
| 110 | + 'no-restricted-syntax': ['error', banConstEnum], |
| 111 | + }, |
| 112 | + }, |
| 113 | + |
| 114 | + // Private package, browser only + no syntax restrictions |
| 115 | + { |
| 116 | + files: ['packages/template-explorer/**', 'packages/sfc-playground/**'], |
| 117 | + rules: { |
| 118 | + 'no-restricted-globals': ['error', ...NodeGlobals], |
| 119 | + 'no-restricted-syntax': ['error', banConstEnum], |
| 120 | + 'no-console': 'off', |
| 121 | + }, |
| 122 | + }, |
| 123 | + |
| 124 | + // JavaScript files |
| 125 | + { |
| 126 | + files: ['*.js'], |
| 127 | + rules: { |
| 128 | + // We only do `no-unused-vars` checks for js files, TS files are checked by TypeScript itself. |
| 129 | + 'no-unused-vars': ['error', { vars: 'all', args: 'none' }], |
| 130 | + }, |
| 131 | + }, |
| 132 | + |
| 133 | + // Node scripts |
| 134 | + { |
| 135 | + files: [ |
| 136 | + 'eslint.config.js', |
| 137 | + 'rollup.config.js', |
| 138 | + 'scripts/**', |
| 139 | + './*.{js,ts}', |
| 140 | + 'packages/*/*.js', |
| 141 | + 'packages/vue/*/*.js', |
| 142 | + ], |
| 143 | + rules: { |
| 144 | + 'no-restricted-globals': 'off', |
| 145 | + 'no-restricted-syntax': ['error', banConstEnum], |
| 146 | + 'no-console': 'off', |
| 147 | + }, |
| 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 | + { |
| 159 | + ignores: [ |
| 160 | + '**/dist/', |
| 161 | + '**/temp/', |
| 162 | + '**/coverage/', |
| 163 | + '.idea/', |
| 164 | + 'explorations/', |
| 165 | + 'dts-build/packages', |
| 166 | + ], |
| 167 | + }, |
| 168 | +) |
0 commit comments