Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/honest-flatmap-advice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"oxlint-plugin-react-doctor": patch
---

Stop recommending `flatMap` as a guaranteed performance improvement for `.map().filter(Boolean)`. The rule now suggests a single-pass `reduce` or `for...of` rewrite only for measured hot paths.
Original file line number Diff line number Diff line change
Expand Up @@ -1996,7 +1996,7 @@
"id": "js-flatmap-filter",
"title": ".map().filter(Boolean) loops twice",
"severity": "warn",
"recommendation": "Use `.flatMap(item => condition ? [value] : [])` to change and drop items in one pass, instead of building a throwaway array in between",
"recommendation": "If this is a measured hot path, combine the transform and truthy check with `.reduce()` or a `for...of` loop to avoid the intermediate array and second pass",
"category": "Performance",
"framework": "global",
"tags": ["test-noise"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,18 @@ describe("js-performance/js-flatmap-filter — regressions", () => {
expect(result.parseErrors).toEqual([]);
expect(result.diagnostics).toHaveLength(1);
});

it("recommends a direct single-pass rewrite instead of flatMap", () => {
const result = runRule(jsFlatmapFilter, `items.map((item) => item.value).filter(Boolean);`);
expect(result.parseErrors).toEqual([]);
expect(result.diagnostics).toHaveLength(1);
expect(result.diagnostics[0].message).toContain("measured hot path");
expect(result.diagnostics[0].message).toContain(".reduce()");
expect(result.diagnostics[0].message).toContain("for...of");
expect(result.diagnostics[0].message).not.toContain("flatMap");
expect(jsFlatmapFilter.recommendation).toContain("measured hot path");
expect(jsFlatmapFilter.recommendation).toContain("`.reduce()`");
expect(jsFlatmapFilter.recommendation).toContain("`for...of`");
expect(jsFlatmapFilter.recommendation).not.toContain("flatMap");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const jsFlatmapFilter = defineRule({
tags: ["test-noise"],
severity: "warn",
recommendation:
"Use `.flatMap(item => condition ? [value] : [])` to change and drop items in one pass, instead of building a throwaway array in between",
"If this is a measured hot path, combine the transform and truthy check with `.reduce()` or a `for...of` loop to avoid the intermediate array and second pass",
create: (context: RuleContext) => ({
CallExpression(node: EsTreeNodeOfType<"CallExpression">) {
if (
Expand Down Expand Up @@ -82,7 +82,7 @@ export const jsFlatmapFilter = defineRule({
context.report({
node,
message:
"This loops over your list twice because .map().filter(Boolean) makes two passes, so use .flatMap() to change & drop items in one pass",
"This .map().filter(Boolean) chain creates an intermediate array and traverses it again; if this is a measured hot path, combine the transform and truthy check with .reduce() or for...of",
});
},
}),
Expand Down
Loading