指尖上的记忆指尖上的记忆
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub
vue3下eslint自动修复的问题
vue3有如下代码:
watch(eventInSeries.value, (value) => {
  if (value?.value) {
    const isExist = selectedItems.value.some(
      (item) => item.value === value.value,
    );
    if (!isExist) {
      selectedItems.value.push(value);
    }
  }
});

通过如下命令:
npx eslint path/to/file.vue --fix

$ npx eslint ./components/UserForm.vue --fix

会自动把上面的
if (value?.value) {}
修改为
if (value.value) {}

导致git提交一直报:
⚠ lint-staged prevented an empty git commit.
  Use the --allow-empty option to continue, or check your task configuration
husky - pre-commit script failed (code 1)


但是实际上我们需要判断value是否为null,否则value.value会报错

解决办法:
watch(eventInSeries.value, (value) => {
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  if (value?.value) {
    const isExist = selectedItems.value.some(
      (item) => item.value === value.value,
    );
    if (!isExist) {
      selectedItems.value.push(value);
    }
  }
});

局部加上
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
说明就可以了

全局配置:
在eslint.config.mjs里( ESLint 的 Flat Config 模式(即新版配置方式),不是传统的 .eslintrc.js。)
rules: {
      '@typescript-eslint/no-unnecessary-condition': 'off', // ✅ 关闭它!本来设置的是 '@typescript-eslint/no-unnecessary-condition': 'error',
    },