New
Introducing React Doctor for Enterprise

react-doctor/js-flatmap-filter

Use `.flatMap(item => condition ? [value] : [])` — transforms and filters in a single pass instead of creating an intermediate array

  • Category: Performance
  • Severity: warn
  • Source: oxlint-plugin-react-doctor
  • Framework: global
  • Enabled when: always

Validation prompt

Use this to decide whether a fired diagnostic is real or a false positive.

The rule fires only on exactly arr.map(fn).filter(Boolean) or arr.map(fn).filter(x => x) (identity arrow). False positive if fn legitimately returns 0, '', NaN, null, or undefined that you want to keep — filter(Boolean) strips those, so the developer may have been relying on that to drop falsy results.

Fix prompt

Use this once validation confirms the diagnostic is real.

Rewrite as arr.flatMap(item => { const result = transform(item); return result ? [result] : []; }) so the transform-and-filter happens in one pass with no intermediate array. Preserve the original filter(Boolean) falsy-stripping semantics in your conditional (e.g. don't accidentally keep 0 or empty strings if they should be dropped). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap

More Performance rules from the rules reference:

  • react-doctor/js-hoist-intl: Hoist `new Intl.NumberFormat(...)` to module scope or wrap in `useMemo` — Intl constructors allocate dozens of objects per locale lookup
  • react-doctor/js-hoist-regexp: Hoist `new RegExp(...)` (or large regex literals) to a module-level constant so it isn't recompiled on every loop iteration
  • react-doctor/js-index-maps: Build an index `Map` once outside the loop instead of `array.find(...)` inside it
  • react-doctor/js-length-check-first: Short-circuit with `a.length === b.length && a.every((x, i) => x === b[i])` — unequal-length arrays exit immediately
  • react-doctor/js-min-max-loop: Use `Math.min(...array)` / `Math.max(...array)` instead of sorting just to read the first or last element