【问题标题】:Unexpected block statement surrounding arrow body ESlint围绕箭头体 ESlint 的意外块语句
【发布时间】:2016-12-20 11:54:42
【问题描述】:

为什么这段代码会给我 lint 警告?解决方法是什么?任何帮助将不胜感激。

 const configureStore = () => {
  return createStore(
      rootReducer,
      middleware
    );
};

这是我的 eslint 配置

{
  "parser": "babel-eslint",
  "extends": "airbnb",
  "plugins": [
    "react"
  ],
  "rules": {
    "comma-dangle": 0,
    "object-curly-spacing": 0,
    "no-multiple-empty-lines": [
      "error",
      {
        "max": 1
      }
    ],
    "arrow-body-style": 1,
    "newline-per-chained-call": 1
  },
  "env": {
    "browser": true,
    "node": true,
    "mocha": true
  }
}

【问题讨论】:

    标签: javascript reactjs ecmascript-6 eslint arrow-functions


    【解决方案1】:
    const configureStore = () => {
      return createStore(
          rootReducer,
          middleware
        );
    };
    

    把箭头后面的大括号改成大括号

    const configureStore = () => (
           createStore(
              rootReducer,
              middleware
            );
        );
    

    它对我有用。 这是关于箭头体的话题--stylehttps://eslint.org/docs/rules/arrow-body-style.html

    在箭头函数体中需要大括号(箭头体样式) 选项 该规则有一个或两个选项。第一个是字符串,可以是:

    • “always”在函数体周围强制使用大括号
    • “as-needed”不强制使用可以省略的大括号(默认)
    • “never”在函数体周围不强制使用大括号(将箭头函数限制为返回表达式的角色) 当您根据需要进行配置时
    //this is a bad style    
    let foo = () => {
            return {
               bar: {
                    foo: 1,
                    bar: 2,
                }
            };
        };
    // This is good
    let foo = () => ({
        bar: {
            foo: 1,
            bar: 2,
        }
    });
    

    您可以从https://eslint.org/docs/rules/arrow-body-style.html#as-needed找到更多详细信息

    【讨论】:

      【解决方案2】:

      在不知道您的特定 lint 配置的情况下进行调试有点困难。

      我唯一的猜测是,如果您在箭头主体上使用仅包含单个表达式的块,您的 linter 的配置方式会报错

      试试

      const configureStore = () =>
        createStore(rootReducer, middleware);
      

      如果这不起作用,请提供评论

      --

      ps,如果您将确切的代码粘贴到 repl.it,则不会出现 lint 警告

      【讨论】:

      • 它删除了 lint 警告,但应用程序停止工作。它没有得到createStore
      • “它没有得到createStore”——这到底是什么意思? createStore 在你的模块范围内吗?
      • Could not find "store" in either the context or props of "Connect(Login)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(Login)".
      • 是的。我正在从 redux 导入 createStore
      • 好的,这是一个完全独立的问题。该错误与configureStorecreateStorerootReducermiddleware 无关。
      最近更新 更多