Repository navigation
feat: add no-spread-in-reduce rule - #109
Merged
Merged
Conversation
…allback
`arr.reduce((acc, x) => [...acc, x], [])` rebuilds the accumulator on
every step, copying all N entries each time — O(N²) in total. Same
for `({...acc, [k]: v})`. Mutating (`acc.push(x); return acc`) or
producing the structure once (`Object.fromEntries`, `flatMap`) keeps
it linear.
Flags `reduce` and `reduceRight` callbacks (arrow or function) where
the returned expression is an array or object literal containing a
spread of the accumulator parameter — at any position, not just first
(`[x, ...acc]` and `{k: v, ...acc}` have the same cost). Looks at the
expression body or the last `return` of a block body. Out of scope:
destructured params, aliased accumulators, conditional returns. Not
in `recommended` — opt-in only.
…liased accumulators
The original rule only inspected the last `return` of a callback whose
first param was a plain Identifier. That missed three real shapes of
the same O(N²) bug:
- conditional returns: `if (cond) return [...acc, x]; return acc;`
examines the `if` branch too.
- destructured accumulator: `({list}, x) => ({list: [...list, x]})`
treats every name bound by the param pattern (with renames and
defaults) as an accumulator name.
- aliased accumulator: `{ const a = acc; return [...a, x]; }` and
`{ const {list} = acc; return [...list, x]; }` hoist names declared
at the top of the body that resolve to the accumulator.
Walks the whole returned expression for any spread of an accumulator-
bound name (using the parser's visitor keys) so the destructured case
`{list: [...list, x]}` — where the spread sits inside a rebuilt
shape — is caught. Skips nested functions; their returns belong to
themselves.
Surfaces two additional real hits on heimdall2 (a fingerprint reducer
and a vuln-merging reducer), both conditional-branch shapes the prior
implementation missed.
webpro
force-pushed
the
rule-no-spread-in-reduce
branch
from
May 4, 2026 08:01
61cc988 to
c416937
Compare
43081j
previously approved these changes
May 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
New
no-spread-in-reducerule: spreading the accumulator inside.reduce()rebuilds it every step, which is O(N²). The rule flags it and points at the spread; the user mutates (acc.push(x); return acc) or restructures (flatMap,Object.fromEntries) instead.A real fix this catches (heimdall2 vuln-merging reducer):
Commits
This PR is two commits that you can land together or independently:
feat: flag spread of accumulator inside reduce callback— the core rule. Flags the direct shape(acc, x) => [...acc, x](and object-spread,reduceRight, function-expression callbacks,[x, ...acc]/{k: v, ...acc}— position doesn't change the perf cost).feat: also flag conditional, destructured, and aliased accumulators— broadens detection to:if (cond) return [...acc, x]; return acc;walks both arms.({list}, x) => ({list: [...list, x]})treats every name bound by the param pattern (with renames) as an accumulator name.const a = acc;/const {list} = acc;hoist into the accumulator name set.If you'd rather keep the rule small for the first cut, the second commit can be dropped — the first commit lands a useful rule on its own. The second commit catches 2 additional real heimdall2 hits the first commit misses.
Design notes
recommended— opt-in only.[]/{}initial). Happy to add it as asuggestin a follow-up if you want it.acc.concat(x)andObject.assign({}, acc, x)(same perf, but a different rule's name);[...acc.slice(...), x](false-positive risk too high without semantic info).Test plan
npm test— 697 tests pass (28 in this rule, including spread position, conditional branches, destructured params, aliased acc, nested-function skip)npm run buildnpm run lint[...acc, x]/{...acc, k: v}accumulators. No false positives. The 2 heimdall2 hits are unique to the second commit (conditional-branch shapes the first commit doesn't reach).