项目开发前配置ESLint

修改.eslintrc.cjs文件,补充配置项

rules: {
    'prettier/prettier': [
      'warn',
      {
        singleQuote: true,
        semi: false,
        printWidth: 180,
        trailingComma: 'none',
        endOfLine: 'auto'
      }
    ],

    'vue/multi-word-component-names': [
      'warn',
      {
        ignores: ['index']
      }
    ],
    'vue/no-setup-props-destructure': ['off']
  }
  • 格式:单引号,没有分号,行宽度 180 字符,省略最后一个逗号,换行字符串自动(系统不一样换行符号不一样)。
  • Vue 组件需要大驼峰命名,除去 index 之外,App 是默认支持的。
  • 允许对 props 进行解构,因为我们会开启解构保持响应式的语法糖。

 VSCode 开启 ESLint 自动修复

按照下图操作,点击(在settings.json中编辑)

然后自动在.vscode目录下自动生成settin.json文件

在settin.json加入一下代码

"editor.codeActionsOnSave": {
        "source.fixAll": true,
    }

把鼠标放在单词上会自动显示解释

代码检查工作流

husky 配置

初始化与安装:Getting started | 🐶 husky

pnpm dlx husky-init && pnpm install

上面的两句命令,如果不能成功,就一句一句运行

修改 .husky/pre-commit 文件

#!/usr/bin/env sh
pnpm lint

测试效果

  1. 故意调整代码格式导致不符合eslint规则,例如多添加一些空行
  2. git add . git commit -m "xxx" 。会发现,提交时,会自动运行pnpm lint。上图的就会自动取消

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
首先,你需要安装 `eslint-config-standard` 和 `eslint-plugin-standard`。 ``` npm install eslint-config-standard eslint-plugin-standard --save-dev ``` 然后,在你的webpack配置文件中添加eslint配置。 ```javascript module.exports = { // ... other configurations module: { rules: [ { test: /\.js$/, loader: 'eslint-loader', enforce: 'pre', include: [path.resolve(__dirname, 'src')], // 指定检查的目录 options: { formatter: require('eslint-friendly-formatter'), // 指定错误报告的格式规范 eslintPath: require.resolve('eslint'), configFile: '.eslintrc.json', // 指定eslint配置文件路径 plugins: ['standard'], // 配置eslint插件 globals: [], // 声明全局变量 rules: { 'no-console': 'error', // 禁止使用console 'no-debugger': 'error', // 禁止使用debugger 'no-alert': 'error', // 禁止使用alert 'no-unused-vars': 'error', // 禁止未使用的变量 'standard/no-callback-literal': 'error' // 禁止在回调函数中直接使用字面量 } } }, // ... other loaders ] } } ``` 最后,你需要在项目根目录下创建 `.eslintrc.json` 文件,并进行以下配置: ```json { "extends": [ "standard" ], "env": { "browser": true, "node": true, "es6": true }, "parserOptions": { "ecmaVersion": 2020, "sourceType": "module" }, "rules": { "semi": ["error", "always"], "quotes": ["error", "single"] } } ``` 上述配置表示,我们使用 `standard` 规则集进行代码检查,支持浏览器和 node.js 环境,使用 ECMAScript 2020 版本,强制使用分号和单引号。 这样,你就可以在开发过程中使用 `eslint-loader` 进行代码检查了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值