eslint抱怨getInitialProps

这是我的.eslintrc

{
  "plugins": ["react"],
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "env": {
    "es6": true,
    "browser": true,
    "node": true,
    "mocha": true
  },
  "extends": ["eslint:recommended", "plugin:react/recommended", "standard"],
  "rules": {}
}

这是我的组件:

class Index extends React.Component {
  static async getInitialProps({ req }) {
    ....
  }
}

Eslint抱怨getInitialProps:

Parsing error: Unexpected token getInitialProps

除了添加抑制注释之外,还有什么方法可以使eslint接受getInitialProps声明?

小宇宙LEY2020/03/24 15:47:59

正如@Scott在评论中指出的那样,解决方案是添加babel-eslint,我的最终.eslintrc是以下内容:

{
  "plugins": ["react"],
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "env": {
    "es6": true,
    "browser": true,
    "node": true,
    "mocha": true
  },
  "extends": ["eslint:recommended", "plugin:react/recommended", "standard"],
  "rules": {}
}