提问者:小点点

如何配置ESLint以允许fat-arrow类方法


ESLint正在抛出一个解析错误:当我尝试lint我的Es6类时,出现意外的token=错误。在eslint中启用fat arrow类方法时缺少什么配置参数?

样本类:

class App extends React.Component{
    ...
    handleClick = (evt) => {
        ...
    }
}

.埃斯林特

{
  "ecmaFeatures": {
    "jsx": true,
    "modules":true,
    "arrowFunctions":true,
    "classes":true,
    "spread":true,

  },
  "env": {
    "browser": true,
    "node": true,
    "es6": true
  },
  "rules": {
    "strict": 0,
    "no-underscore-dangle": 0,
    "quotes": [
      2,
      "single"
    ],
  }
}

共3个答案

匿名用户

如果您想使用实验特性(如箭头作为类方法),则需要使用babel eslint作为解析器。默认解析器(Espree)不支持实验特性。

匿名用户

首先安装babel eslint:

npm i -D babel-eslint

然后将以下内容添加到。eslintrc。json文件:

"parser": "babel-eslint"

匿名用户

首先安装以下插件:

npm i-D babel eslint eslint插件babel

然后将这些设置添加到您的eslint配置文件中:

.eslintrc。json

{
    "plugins": [ "babel" ],
    "parser": "babel-eslint",
    "rules": {
        "no-invalid-this": 0,
        "babel/no-invalid-this": 1,
    }
}

通过这种方式,您可以使用fat arrow类方法,并且您不会从eslint中获得任何no无效的This错误。

快乐海岸