项目添加ESLint


1. 安装 

yarn add eslint -D

2. 生成配置文件

npx eslint --init

√ How would you like to use ESLint? · problems
√ What type of modules does your project use? · esm
√ Which framework does your project use? · none
√ Does your project use TypeScript? · No / Yes
√ Where does your code run? · browser, node
√ What format do you want your config file to be in? · JavaScript

3. 检查代码

npx eslint ./

其他

plugins: 新增一些规则,然后在rules中使用

extends集成: 使用别人的配置, 如果有冲突的话就rules自己定义

常见错误修改

  1. !xxx.hasOwnProperty(key);    =>  !Object.prototype.hasOwnProperty.call(xxx, key); 切忌不要把 !xxx 直接复制过来,因为是取反,永远bool值
  2. const abc = useSelector(xxx) || [];    然后abc在 useCallback中的依赖参数中使用了,  因为存在[] 所以永远会重新渲染
    修改方法:const abc = useSelector(xxx)   然后在使用abc的地方 (abc || []).map .....
  3. const abc = () => async () => {
      new Promise(async (res, rej) => {
      if() return xxx
      if() throw xxx
    }) 
    }

     ===>

    const abc = () => async () => {
      if() return xxx
      if() throw xxx
    }