1)安装
pnpm i eslint -D
2)生成配置文件
npx eslint --init
第二个选择模块开发 第一项
3)添加配置规则
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";
import pluginVue from "eslint-plugin-vue";
/** @type {import('eslint').Linter.Config[]} */
export default [
{
files: ["**/*.{js,mjs,cjs,ts,vue}"],
languageOptions: {
globals: {
...globals.browser,
...globals.es2021,
...globals.node,
},
},
},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
...pluginVue.configs["flat/essential"],
{
files: ["**/*.vue"],
languageOptions: {
parser: pluginVue.parser,
parserOptions: {
parser: tseslint.parser,
extraFileExtensions: [".vue"],
ecmaFeatures: {
jsx: true,
},
},
},
},
{
rules: {
'no-var': 'error', //ES 6 中不允许使用 var
'no-multiple-empty-lines': ['warn', { max: 1 }], // 空行
'no-console': 'warn', // 改为警告,而不是基于环境
'no-debugger': 'warn', // 改为警告,而不是基于环境
'no-unexpected-multiline': 'error', // 禁止出现意外的多行
'no-useless-escape': 'off', // 禁用不必要的转义字符
'@typescript-eslint/no-unused-vars': 'error', // 未使用的变量
'@typescript-eslint/prefer-ts-expect-error': 'error', // 优先使用 ts-expect-error
'@typescript-eslint/no-explicit-any': 'off', // 不允许使用 any
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-namespace': 'off',
'@typescript-eslint/semi': 'off',
'vue/multi-word-component-names': 'off', // vue文件命名不限制
'vue/script-setup-uses-vars': 'error', // 防止 script setup 中使用的变量被标记为未使用
'vue/no-mutating-props': 'off', // 禁止修改 props
'vue/attribute-hyphenation': 'off', // 禁用属性名的连字符
},
},
];
4)创建 .eslintignore 文件,添加校验忽略
dist
node_modules
5)package.json 中添加校验
"lint": "eslint src",
"fix": "eslint src --fix"