【问题标题】:StandardJS linting returns parsing error for static variableStandardJS linting 返回静态变量的解析错误
【发布时间】:2019-04-14 20:39:23
【问题描述】:

我确实得到了一个Parsing error: Unexpected token linting 错误,这个line 使用standardJS。因此我的 CI 失败了。

我不明白这条线有什么问题。我该如何解决这个问题?

export default (App) => {
  return class Apollo extends React.Component {
    static displayName = 'withApollo(App)' // <--
    static async getInitialProps (ctx) {
    // ...
  }
}

【问题讨论】:

    标签: javascript eslint standardjs


    【解决方案1】:

    如果这是标准 javascript,那么错误是类只能包含函数,不能包含属性。

    正确的语法应该是:

    class Apollo extends React.Component {
      static async getInitialProps (ctx) {
      // ...
    }
    
    Apollo.displayName = 'withApollo(App)';
    
    export default (App) => {
      return Apollo;
    }
    

    如果您正在使用提议的(但尚未实现或批准)ES7/ES8 类属性,那么eslint 可能还不支持它。


    如果与原问题不同,需要使用App参数,只需在要导出的函数中进行即可:

    class Apollo extends React.Component {
      static async getInitialProps (ctx) {
      // ...
    }
    
    export default (App) => {
      Apollo.displayName = `withApollo(${App})`;
      return Apollo;
    }
    

    【讨论】:

    • 不幸的是,在课堂上可以使用App。请查看链接的原始文件。我该如何处理?
    • 不。原始文件根本没有使用App
    猜你喜欢
    • 1970-01-01
    • 2015-10-02
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 2015-08-17
    • 2020-04-10
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多