user7496931
user7496931

Reputation:

eslint arrow-body-style issue

I configured my eslint so it uses the arrow-body-style as needed:

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

But I'm getting an error below for some reason.

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' });
    });
});

How exactly am I supposed to re-write my code?

Upvotes: 7

Views: 5126

Answers (2)

codejockie
codejockie

Reputation: 10864

Using arrow-body-style: ["error", "as-needed"] configuration is redundant as it is the default behaviour. You don't need setting it again as it is already set as the default form.

as-needed

Examples of incorrect code for this rule with the default "as-needed" option:

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

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

Examples of correct code for this rule with the default "as-needed" option:

/*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

In your code sample it should be this way:

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' });
    });
});

Since you're simply returning a plain object, there's no need for the extra pair of braces and return. Wrapping the object in parentheses ({ ... }), works as that is implicitly returned.

Upvotes: 7

Igor Soloydenko
Igor Soloydenko

Reputation: 11825

Try to omit return keyword and wrap the result into parenthesis:

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

Upvotes: 2

Related Questions