项目搭建02 项目配置ESlint

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"

6)执行校验

7)修复

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值