Eslint

教程参考:https://www.jianshu.com/p/ad1e46faaea2

Eslint官方规则文档:http://eslint.cn/docs/rules/

Eslint官方自定义规则文档:http://eslint.cn/docs/developer-guide/shareable-configs

Eslint官方忽略文件和目录:https://eslint.org/docs/user-guide/configuring#ignoring-files-and-directories

创建Eslint:

1、npm init 。创建package.js。

2、npm install eslint --save-dev。创建Eslint。

3、修改package.js。

"scripts": { "test": "react-scripts test --env=jsdom", "lint": "eslint src", // 校验与.eslint.js同级的src "lint:create": "eslint --init"//创建Eslint文件 }

 

错误处理:

1、创建Eslint文件时,遇到

Which file(s), path(s), or glob(s) should be examined? 若是要校验path,需要写上相对路径。例如要检验同级别的src文件夹。则需要./src。若是其他,则上述的第三点需要进行相应的更改。

2、.eslint.js中的rule为空的时候,在执行校验的时候会出错。

 

部分需要记住的:

1、绝大部分的规则遵循以下要求。

  • "off" 或 0 - 关闭规则
  • "warn" 或 1 - 开启规则,使用警告级别的错误:warn (不会导致程序退出)
  • "error" 或 2 - 开启规则,使用错误级别的错误:error (当被触发的时候,程序会退出)

2、单行跳过校验。在代码末尾加上 // eslint-disable-line

 

 

module.exports = {
    "env": {
        "browser": true,
        "es6": true
    },
    "extends": "eslint:recommended",
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly"
    },
    "parserOptions": {
        "ecmaVersion": 2018
    },
    "rules": {
        "no-console": 1, // 禁止console。1禁止,0允许
        "no-unused-vars": 1, // 禁止变量定义但未使用,
        "no-dupe-args": 1,// 禁止函数定义中出现重名参数
        "no-trailing-spaces": 1,//禁止行尾空格
        "no-shadow": 1,//禁止变量声明与外层作用域的变量同名
        // "no-param-reassign": ['error', { 'props': true, 'ignorePropertyModificationsFor': ['res'] }],//禁止对函数的参数重新赋值,除了ajax回调的res
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值