eslint学习

如何放在vue的项目中
1 在webpack.base.conf.js的rules中加上eslint-loader的加载模块
2 package.json中添加依赖包
3 index.js文件中启用eslint
4 在根目录下建立 .eslintrc.js文件(具体写规则)
5 设置忽略文件.eslintignore
配置
webpack.base.conf.js

const createLintingRule = () => ({
  test: /\.(js|vue)$/,
  loader: 'eslint-loader',
  enforce: 'pre',
  include: [resolve('src'), resolve('test')],
  options: {
    formatter: require('eslint-friendly-formatter'), //代表只发出警告,不会影响程序运行
    emitWarning: !config.dev.showEslintErrorsInOverlay
  }
})

在package.json中的devDependencies中添加

    "eslint": "^3.19.0",
    "eslint-config-standard": "^10.2.1",
    "eslint-config-vue": "^2.0.2",
    "eslint-friendly-formatter": "^3.0.0",
    "eslint-loader": "^1.7.1",
    "eslint-plugin-flow-vars": "^0.5.0",
    "eslint-plugin-html": "^3.0.0",
    "eslint-plugin-import": "^2.7.0",
    "eslint-plugin-node": "^5.2.0",
    "eslint-plugin-promise": "^3.4.0",
    "eslint-plugin-react": "^7.5.1",
    "eslint-plugin-standard": "^3.0.1",
    "eslint-plugin-vue": "^2.1.0",

在config中index.js文件里添加

module.exports = {
    useEslint: true,
    showEslintErrorsInOverlay: false,
}

剩下就在 .eslintrc.js文件中写入具体的规则

module.exports = {
  //此项是用来告诉eslint找当前配置文件不能往父级查找
  root: true,
  //此项是用来指定eslint解析器的,解析器必须符合规则,babel-eslint解析器是对babel解析器的包装使其与ESLint解析
  parser: 'babel-eslint',
  parserOptions: {
    // 设置 script(默认) 或 module,如果代码是在ECMASCRIPT中的模块
    sourceType: 'module',
    "ecmaVersion": 6, //es6标准
    "ecmaFeatures": {
      "jsx": true
    }
  },
  env: {
    "browser": true,//全局变量
  },
  plugins: [   //插件
    'html',
    "flow-vars", 
  ],
  /* 
   下面这些rules是用来设置从插件来的规范代码的规则,使用必须去掉前缀eslint-plugin-
    主要有如下的设置规则,可以设置字符串也可以设置数字,两者效果一致
    "off" -> 0 关闭规则
    "warn" -> 1 开启警告规则
    "error" -> 2 开启错误规则
  */
  rules: {
    // 不需要
    "space-before-function-paren": 0,  // 函数定义时括号前面要不要有空格
    "eqeqeq": 0, // 必须使用全等
    // 警告
    "no-extra-parens": 1, // 非必要的括号
    "no-empty": 1, // 块语句中的内容不能为空
    "no-use-before-define": [1, "nofunc"], // 未定义前不能使用
    "complexity": [1, 10], // 循环复杂度
    "no-unused-vars": 1, // 不能有声明后未被使用的变量或参数
    // vue
    "flow-vars/define-flow-type": 1,
    "flow-vars/use-flow-type": 1,
    // 错误
    "comma-dangle": [2, "never"], // 对象字面量项尾不能有逗号
    "no-debugger": 2, // 禁止使用debugger
    "no-constant-condition": 2, // 禁止在条件中使用常量表达式 if(true) if(1)
    "no-dupe-args": 2, // 函数参数不能重复
    "no-duplicate-case": 2, // switch中的case标签不能重复
    "no-empty-character-class": 2, // 正则表达式中的[]内容不能为空
    "no-func-assign": 2, // 禁止重复的函数声明
    // 代码风格
    "no-else-return": 1, // 如果if语句里面有return,后面不能跟else语句
    "no-multi-spaces": 1, // 不能用多余的空格
    "no-return-assign": [2, "always"], // return 语句中不能有赋值表达式
    "no-useless-call": 2, // 禁止不必要的call和apply
    "curly": 1, // 必须使用 if(){} 中的{}
  }
}

eslintignore里面写入不需要遵循eslint规则的文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值