Skip to content

fix: prevent reduceBy transducer throwing on nullish step result#3529

Open
ATOM00blue wants to merge 1 commit into
ramda:masterfrom
ATOM00blue:fix/reduceby-null-transducer
Open

fix: prevent reduceBy transducer throwing on nullish step result#3529
ATOM00blue wants to merge 1 commit into
ramda:masterfrom
ATOM00blue:fix/reduceby-null-transducer

Conversation

@ATOM00blue

Copy link
Copy Markdown

Fixes the TypeError raised when reduceBy is used as a transducer and the downstream reducer returns null/undefined.

XReduceBy's @@transducer/result handler read '@@transducer/reduced' off the step's return value without a null guard, so a reducer that legitimately returns null produced:
TypeError: Cannot read properties of null (reading '@@transducer/reduced').

This adds a result != null guard before the reduced-sentinel check, mirroring the existing guard in the non-transducer reduceBy path (source/reduceBy.js), so nullish accumulator results propagate correctly.

Added a regression test covering the transducer path (including the reproducer from the issue).

Closes #3238

When reduceBy is used as a transducer, the @@transducer/result handler
in _xreduceBy read '@@transducer/reduced' off the downstream step's
return value without guarding against null/undefined. A reducer that
legitimately returns null (or undefined) caused
"TypeError: Cannot read properties of null (reading '@@transducer/reduced')".

Guard the access with a null check, matching the existing guard in the
non-transducer reduceBy path, so nullish results propagate correctly.

Closes ramda#3238
Copilot AI review requested due to automatic review settings May 21, 2026 03:15
@github-actions

Copy link
Copy Markdown
Coverage Summary
> ramda@0.32.0 coverage:summary
> BABEL_ENV=cjs nyc --reporter=text-summary mocha -- --reporter=min --require @babel/register

�[2J�[1;3H
1209 passing (868ms)


=============================== Coverage summary ===============================
Statements   : 94.1% ( 2519/2677 )
Branches     : 85.9% ( 981/1142 )
Functions    : 93.37% ( 563/603 )
Lines        : 94.36% ( 2361/2502 )
================================================================================

@github-actions

Copy link
Copy Markdown
Coverage Summary
> ramda@0.32.0 coverage:summary
> BABEL_ENV=cjs nyc --reporter=text-summary mocha -- --reporter=min --require @babel/register

�[2J�[1;3H
1209 passing (986ms)


=============================== Coverage summary ===============================
Statements   : 94.1% ( 2519/2677 )
Branches     : 85.9% ( 981/1142 )
Functions    : 93.37% ( 563/603 )
Lines        : 94.36% ( 2361/2502 )
================================================================================

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a crash in the reduceBy transducer implementation when a downstream reducer returns a nullish accumulator (null/undefined), by guarding the reduced-sentinel check in XReduceBy’s @@transducer/result loop.

Changes:

  • Add a nullish guard in source/internal/_xreduceBy.js before reading @@transducer/reduced from the step result.
  • Add a regression test ensuring reduceBy used as a transducer does not throw when the downstream step returns null.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
source/internal/_xreduceBy.js Prevents TypeError by guarding reduced-sentinel access when the downstream step result is nullish.
test/reduceBy.js Adds a regression test covering the transducer path when the downstream step returns null.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/reduceBy.js
Comment on lines +89 to +93
it('does not throw when used as a transducer and the step returns null', function() {
var evenOddNaN = function(num) {
return !Number.isFinite(num) ? 'NaN' : num % 2 === 0 ? 'even' : 'odd';
};
var nullIfNotFiniteOp = function(op) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not concerned about this, but feel free to add a test case to cover it if you'd like

@kedashoe

Copy link
Copy Markdown
Contributor

Hi @ATOM00blue , thank you for the PR. I see the test is based on the linked issue. Can we also include a minimal test that shows the issue more clearly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong behaviour in reduceBy managing null or undefined accumulator values when used as transducer

3 participants