4

这是我的 .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 声明?

4

1 回答 1

5

正如@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": {}
}
于 2017-10-20T10:10:08.120 回答