【问题标题】:eslint arrow-body-style issueeslint 箭体式问题
【发布时间】:2018-01-05 04:26:56
【问题描述】:

我配置了我的 eslint,因此它根据需要使用箭头体样式:

arrow-body-style: ["error", "as-needed"]

但由于某种原因,我在下面收到错误消息。

router.get('/', (req, res, next) => {
  Product.find({})
    .select('name price _id')
    .then(items => {
      const response = {
        count: items.length,
        products: items.map(item => { // eslint points the error here
          return {
            name: item.name,
            price: item.price,
            _id: item._id,
            request: {
              type: 'GET',
              url: `http://localhost:3000/products/${item._id}`
            }
          };
        })
      };
      res.status(200).json(response);
    })
    .catch(error => {
      console.log(error);
      res.status(500).json({ message: 'Server error' });
    });
});

我应该如何重写我的代码?

【问题讨论】:

    标签: javascript eslint


    【解决方案1】:

    使用arrow-body-style: ["error", "as-needed"] 配置是多余的,因为它是默认行为。您不需要再次设置它,因为它已经设置为默认表单。

    按需

    带有默认“按需”选项的此规则的错误代码示例:

    /*eslint arrow-body-style: ["error", "as-needed"]*/
    /*eslint-env es6*/
    
    let foo = () => {
        return 0;
    };
    let foo = () => {
        return {
           bar: {
                foo: 1,
                bar: 2,
            }
        };
    };
    

    带有默认“按需”选项的此规则的正确代码示例:

    /*eslint arrow-body-style: ["error", "as-needed"]*/
    /*eslint-env es6*/
    
    let foo = () => 0;
    let foo = (retv, name) => {
        retv[name] = true;
        return retv;
    };
    let foo = () => ({
        bar: {
            foo: 1,
            bar: 2,
        }
    });
    let foo = () => { bar(); };
    let foo = () => {};
    let foo = () => { /* do nothing */ };
    let foo = () => {
        // do nothing.
    };
    let foo = () => ({ bar: 0 });
    

    ESLint Docs on arrow-body

    在您的代码示例中应该是这样的:

    router.get('/', (req, res, next) => {
      Product.find({})
        .select('name price _id')
        .then(items => {
          const response = {
            count: items.length,
            products: items.map(item => ({ // no more errors
                 name: item.name,
                 price: item.price,
                 _id: item._id,
                 request: {
                   type: 'GET',
                   url: `http://localhost:3000/products/${item._id}`
                });
            })
          };
          res.status(200).json(response);
        })
        .catch(error => {
          console.log(error);
          res.status(500).json({ message: 'Server error' });
        });
    });
    

    由于您只是返回一个普通对象,因此不需要额外的大括号和return。将对象括在括号 ({ ... }) 中,因为它是隐式返回的。

    【讨论】:

      【解决方案2】:

      尽量省略return关键字并将结果用括号括起来:

      products: items.map(item => ({
        name: item.name,
        price: item.price,
        _id: item._id,
        request: {
          type: 'GET',
          url: `http://localhost:3000/products/${item._id}`
        }
      }))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-02
        • 2019-05-22
        • 2017-09-13
        • 2016-12-20
        • 2017-12-21
        • 2018-04-10
        • 1970-01-01
        • 2016-06-04
        相关资源
        最近更新 更多