eslint-plugin-lodash-fp: `prefer-flat-map` being erroneously triggered

The rule prefer-flat-map is being trigger with the following line and I don’t know why.

    const filterOut = _.flow(_.get('foods'), _.reject('tomato'), _.reject('strawberry'), _.reject({ id: 'SUGARID' }))

Thanks 🎈

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Landed in 4473a0e

Ok cool, let’s go with that then 😃, will do that tonight

That’s perfect. Thank you!

Ha! Found it!

It’s not actually the prefer-flat-map rule that has an error, but rather the prefer-composition-grouping rule that is triggered, and that it currently gives the same error message as prefer-flat-map, and the same problem exists for no-extraneous-iteratee-args. Ugh… shameful… That’s what you guess when you develop the logic first and then the error message and have no review 😄.

I’ll fix that tonight so that both of the rules return a proper error message.

Then, to come back to your error, you have multiple of the same operations (among map, filter and reject) next to each other, which can be improved upon.

_.flow(
  _.get('foods'),
  _.reject('tomato'),
  _.reject('strawberry'),
  _.reject({ id: 'SUGARID' })
)

// -->

_.flow(
  _.get('foods'),
  _.reject(v => v.tomato || v.strawberry || v.id === 'SUGARID')
)

I know that when you use some compiled functional languages, the former is compiled into the latter, as it is more efficient for processing, and it may in some cases be easier to read, especially if you give the function a nice name. That’s why I proposed this rule. I did not know/think of the property shorthand though.

_.flow(
  _.map('a'),
  _.map('b'),
  _.map('c'),
  _.filter('foo')
);
// vs
_.flow(
  _.map('a.b.c'),
  _.filter('foo')
);
// Much simpler and faster in this case for instance

See https://github.com/jfmengels/eslint-plugin-lodash-fp/blob/master/docs/rules/prefer-composition-grouping.md for more details.