Lint Rules

useFlatMap (since v10.0.0)

Promotes the use of .flatMap() when map().flat() are used together.

Examples

Invalid

const array = ["split", "the text", "into words"];
array.map(sentence => sentence.split(' ')).flat();
nursery/useFlatMap.js:2:1 lint/nursery/useFlatMap  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The call chain .map().flat() can be replaced with a single .flatMap() call.
  
    1 │ const array = ["split", "the text", "into words"];
  > 2 │ array.map(sentence => sentence.split(' ')).flat();
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    3 │ 
  
   Safe fix: Replace the chain with .flatMap().
  
    1 1  const array = ["split", "the text", "into words"];
    2  - array.map(sentence·=>·sentence.split('·')).flat();
      2+ array.flatMap(sentence·=>·sentence.split('·'));
    3 3  
  
const array = ["split", "the text", "into words"];
array.map(sentence => sentence.split(' ')).flat(1);
nursery/useFlatMap.js:2:1 lint/nursery/useFlatMap  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   The call chain .map().flat() can be replaced with a single .flatMap() call.
  
    1 │ const array = ["split", "the text", "into words"];
  > 2 │ array.map(sentence => sentence.split(' ')).flat(1);
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    3 │ 
  
   Safe fix: Replace the chain with .flatMap().
  
    1 1  const array = ["split", "the text", "into words"];
    2  - array.map(sentence·=>·sentence.split('·')).flat(1);
      2+ array.flatMap(sentence·=>·sentence.split('·'));
    3 3  
  

Valid

const array = ["split", "the text", "into words"];
array.map(sentence => sentence.split(' ')).flat(2);