eslint 的配置与使用

eslint 的配置与使用

格式

rules: {
    "规则名": [规则值, 规则配置]
}

规则值

"off"或者0    //关闭规则
"warn"或者1    //在打开的规则作为警告(不影响退出代码)
"error"或者2    //把规则作为一个错误(退出代码触发时为1)

.eslinrc.js 配置

官方参考

module.exports = {
  root: true, // 设置当前目录为根目录,停止在父级目录中寻找eslint配置
  env: {
    node: true, // Node.js 全局变量和 Node.js 作用域。
    es6: true, // 额外支持新的 ES6 全局变量
    browser: true // 在代码中可以放心使用宿主环境给你提供的全局变量
  },
  'extends': [
    'plugin:vue/essential',
    '@vue/standard'
  ],
  rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    // 自定义
    'indent': ['error', 4],
    'space-infix-ops': 'error', // 要求操作符周围有空格
    'comma-spacing': 'error', // 逗号后面加空格
    'arrow-spacing': 'error', // 使用 => 前后空格
    'spaced-comment': 'warn', // 注释风格,前后留一个空格
    'no-irregular-whitespace': 'error', // 不能有不规则的空格
    'no-mixed-spaces-and-tabs': 'error', // 不能混用空格和tab
    'no-multiple-empty-lines': 'error', // 不能有多余的空行
    'no-var': 'error', // 不能使用var
    'no-new': 'off',
    'new-cap': 'off', // 不要求构造函数首字母大写
    'eqeqeq': 'error', // 使用全等
    'quotes': [1, 'single'], // 引号类型,使用单引号
    'camelcase': 'off'
  },
  parserOptions: {
    parser: 'babel-eslint',
    ecmaVersion: 6, // 启用 ES6 语法支持
    sourceType: 'module'
  },
  globals: { // 声明在代码中自定义的全局变量
  }
}

配置可忽略文件.eslintignore

/build/
/config/
/dist/

使用 eslint 验证整个项目

vue项目配置方法:

"scripts": {
    "lint": "vue-cli-service lint",
},

使用:yarn lint 或 npm run lint

eslint自动修复

yarn lint --fix

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值