使用 eslint 让代码更规范

本文介绍了如何在Vue.js项目中利用vue-cli的eslint功能提高代码质量。通过配置vue.config.js启用lintOnSave实现保存时代码检查,使用vue-cli-service lint进行自动代码修复。讨论了eslint的规则设置,包括错误级别、通用规则及Vue特有规则,并展示了部分规则示例,如禁止console.log、禁用debugger、驼峰命名等。同时,强调了全局变量声明和解析器选项的配置。

由于开发人员经验,编码习惯不同,造成维护成本上升,容易产生bug

1. 现有的vue-cli 自带eslint代码规范控件,只需对其规则进行配置.通过修改vue.config.js 文件内的 lintOnSave 属性为true来设置是否在保存代码的时候进行代码检测,这个一个人编码习惯自行设置.
2. 通过eslint插件进行代码修复,在vue-cli的项目中只需运行vue-cli-service lint 指令即可进行自动代码修复,自动修复有一定风险.
3. 规则包括必须规则,vue推荐规则,通用规则,通过设置error 进行开启规则,设置off关闭规则,设置warn进行规则警告.
4. 以下是现有的规则列表
module.exports = {
  root: true,

  env: {
    node: true
  },

  rules: {
    'no-console':  process.env.NODE_ENV === 'production'?'error':'off',
    'no-debugger': process.env.NODE_ENV === 'production'?'error':'off',
    'vue/camelcase': 'off', // 驼峰命名
    'vue/space-infix-ops': 'off',
    'no-unused-vars':'off',
    'no-useless-escape':'off',
    'vue/no-use-v-if-with-v-for':'off',  //v-for,v-if 不能同时用在一个元件上
    'vue/no-parsing-error':'off',
    'vue/no-boolean-default': 'off', // 布尔值属性默认值必须是false
    'vue/max-attributes-per-line': 'off', // 一行一个属性
    'vue/eqeqeq': 'off', // 要用===
    'no-empty':'warn', // 不允许有空的代码段
    'vue/require-v-for-key':'warn', // v-for必须要有key
    'vue/require-default-prop': 'warn',// 组件prop 必须有默认值
    'vue/require-prop-types': 'warn',  //  组件prop 必须有类型
    'vue/valid-v-for':'error',  // v-for 有效检测
    'vue/no-confusing-v-for-v-if': 'error', // v-if 必须在v-for之后 因为v-for 优先级大于v-if
    'no-constant-condition':'error',
    'vue/attribute-hyphenation': 'error',
    'vue/html-closing-bracket-newline': 'error',
    'vue/html-closing-bracket-spacing': 'error',
    'vue/html-end-tags': 'error',
    'vue/html-indent': 'error',
    'vue/html-quotes': 'error',
    'vue/html-self-closing': 'error',
    'vue/multiline-html-element-content-newline': 'error',
    'vue/mustache-interpolation-spacing': 'error',
    'vue/name-property-casing': 'error',
    'vue/no-multi-spaces': 'error',
    'vue/no-spaces-around-equal-signs-in-attribute': 'error',
    'vue/no-template-shadow': 'error',
    'vue/prop-name-casing': 'error',
    'vue/singleline-html-element-content-newline': 'error',
    'vue/v-bind-style': 'error',
    'vue/v-on-style': 'error',
    'vue/attributes-order': 'error',
    'vue/no-v-html': 'error',
    'vue/order-in-components': 'error',
    'vue/this-in-template': 'error',
    'vue/array-bracket-spacing': 'error',
    'vue/arrow-spacing': 'error',
    'vue/block-spacing': 'error',
    'vue/brace-style': 'error',
    'vue/comma-dangle': 'error',
    'vue/component-name-in-template-casing': 'error',
    'vue/key-spacing': 'error',
    'vue/match-component-file-name': 'error',
    'vue/no-restricted-syntax': 'error',
    'vue/object-curly-spacing': 'error',
    'vue/require-direct-export': 'error',
    'vue/script-indent': 'error',
    'vue/space-unary-ops': 'error',
    'vue/v-on-function-call': 'error'
  },
  globals: { // 声明全局变量不需要检测
    "uni": "writable",
    "wx": "writable",
    "_hmt": "writable",
    "BMap": "writable",
  },

  parserOptions: {
    parser: 'babel-eslint'
  },

  'extends': [
    'plugin:vue/essential',
    'eslint:recommended'
  ]
}


### 如何使用 ESLint 格式化代码 在开发过程中,ESLint 是一个非常强大的工具,用于帮助开发者发现并修复 JavaScript 代码中的问题。为了使代码规范和一致,可以将 ESLint 配置为自动格式化代码。以下是详细的配置方法。 #### 在 VS Code 中配置 ESLint 格式化 1. **安装 ESLint 扩展** 首先需要确保在 VS Code 中安装了官方的 ESLint 扩展。可以通过扩展市场搜索“ESLint”并安装由 dbaeumer 提供的插件[^1]。 2. **启用 ESLint 格式化功能** 可以通过图形界面或直接编辑 `settings.json` 文件来完成配置。如果选择后者,可以在 `settings.json` 文件中添加以下内容: ```json { "eslint.format.enable": true, "editor.defaultFormatter": "dbaeumer.vscode-eslint", "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.fixAll.eslint": true }, "eslint.validate": ["javascript", "javascriptreact", "typescript", "vue"] } ``` 这段代码的作用是启用 ESLint 的格式化功能,并设置保存时自动格式化代码[^2]。 3. **针对特定语言的默认格式化工具** 如果项目中包含多种语言(如 VueJavaScript 等),可以为每种语言单独指定默认格式化工具。例如: ```json "[vue]": { "editor.defaultFormatter": "dbaeumer.vscode-eslint" }, "[javascript]": { "editor.defaultFormatter": "dbaeumer.vscode-eslint" } ``` 4. **其他可选配置** 如果需要详细的调试信息,可以添加以下配置项: ```json { "eslint.debug": true, "eslint.alwaysShowStatus": true, "editor.codeActionsOnSave": { "source.fixAll": true, "source.fixAll.eslint": true } } ``` 这些选项可以帮助开发者好地了解 ESLint 的运行状态[^3]。 #### 使用 ESLint 格式化代码的注意事项 - 确保项目根目录下存在 `.eslintrc.js` 或 `.eslintrc.json` 文件,用于定义代码的规则。 - 如果同时使用 Prettier,可能需要安装 `eslint-config-prettier` 和 `eslint-plugin-prettier` 插件,以避免两者之间的冲突[^3]。 ```javascript // 示例 .eslintrc.js 文件 module.exports = { extends: [ 'eslint:recommended', 'plugin:prettier/recommended' ], rules: { 'no-console': 'warn', 'indent': ['error', 2] } }; ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值