简体   繁体   English

ESLint在课堂上使用airbnb规则的胖箭头

[英]ESLint in class with fat arrow using airbnb rules

The following code located in a class that extends React.Component 以下代码位于扩展React.Component的类中

  nextState = () => {
    this.setState({
      state : this.state.state + 1
    });
  };

However ESLint with airbnb rules catches this and throws the following error: error Parsing error: Unexpected token = 但是,使用airbnb规则的ESLint会捕获此信息并引发以下错误: error Parsing error: Unexpected token =

I would like to keep this syntax as it allows me to avoid binding this in the constructor. 我想保留这种语法,因为它允许我避免在构造函数中绑定this

I struggled with this problem for quite a while. 我在这个问题上挣扎了很长一段时间。 I found that this configuration of .eslintrc works for your problem. 我发现.eslintrc这种配置适用于您的问题。

{
  "extends": "airbnb",
  "parser": "babel-eslint"
}

It works nicely with Sublime Text 3 with SublimeLinter-contrib-eslint. 它与Sublime Text 3和SublimeLinter-contrib-eslint很好地配合使用。

Note that you need to npm install -g eslint babel-eslint 请注意,您需要npm install -g eslint babel-eslint

put .eslintrc in ~/ for global config, put .eslintrc in app folder to overwrite global config. .eslintrc放在~/ for global config中,将.eslintrc放在app文件夹中覆盖全局配置。

also note that: Assignment operation inside class is not part of es6, see this link for discussion 还要注意:类内的赋值操作不是es6的一部分,请参阅此链接进行讨论

You need to specify Language options. 您需要指定语言选项。 For ref: http://eslint.org/docs/user-guide/configuring#specifying-language-options 对于参考: http//eslint.org/docs/user-guide/configuring#specifying-language-options

You can do this with a single command also inside your .eslintrc file. 您也可以在.eslintrc文件中使用单个命令执行此操作。

{
    "env": {
        "es6": true,
        "node": true
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM