eslint arrow-body-style issue

时间:2018-01-04 19:58:41

标签: javascript eslint

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

let backgroundImage = UIImageView(frame: UIScreen.main.bounds) backgroundImage.image = UIImage(named: "bg_name.png") backgroundImage.contentMode = UIViewContentMode.scaleAspectFill self.view.insertSubview(backgroundImage, at: 0)

但我出于某种原因在下面收到错误。

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

我应该如何重新编写代码?

2 个答案:

答案 0 :(得分:1)

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

  

根据需要

此规则的错误代码示例,默认情况下为" as-needed"选项:

/*eslint arrow-body-style: ["error", "as-needed"]*/
/*eslint-env es6*/

let foo = () => {
    return 0;
};
let foo = () => {
    return {
       bar: {
            foo: 1,
            bar: 2,
        }
    };
};

此规则的正确代码示例,默认情况下为" as-needed"选项:

/*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。将对象括在括号({ ... })中,可以隐式返回。

答案 1 :(得分:0)

尝试省略return关键字并将结果包装成括号:

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