Skip to content

fix(table-core): guard Array.isArray before index access in range filter autoRemove#6394

Open
sanjibani wants to merge 1 commit into
TanStack:betafrom
sanjibani:fix/issue-6353-filterFns-range-autoRemove
Open

fix(table-core): guard Array.isArray before index access in range filter autoRemove#6394
sanjibani wants to merge 1 commit into
TanStack:betafrom
sanjibani:fix/issue-6353-filterFns-range-autoRemove

Conversation

@sanjibani

@sanjibani sanjibani commented Jul 9, 2026

Copy link
Copy Markdown

Fixes #6353.

Bug

filterFn_between, filterFn_betweenInclusive, and filterFn_inNumberRange share an autoRemove:

autoRemove: (val: any) =>
  testFalsy(val) || (testFalsy(val[0]) && testFalsy(val[1]))

When a scalar value such as 99 is passed instead of a [min, max] tuple, val[0] and val[1] are both undefined, so the condition evaluates to true && true and the filter is silently auto-removed.

Numeric columns default to inNumberRange via column_getAutoFilterFn, so col.setColumnFilter(99) on a numeric column discards the filter before the row scan even runs.

Fix

Wrap the index checks in Array.isArray(val) &&:

autoRemove: (val: any) =>
  testFalsy(val) ||
  (Array.isArray(val) && testFalsy(val[0]) && testFalsy(val[1]))

The leading testFalsy(val) still handles undefined, null, 0, and ''. Behavior of the existing array-tuple cases is unchanged because Array.isArray is true and short-circuits to the same boolean.

Tests

  • New filterFns.inNumberRange.autoRemove describe block (the function had no direct unit tests previously).
  • One regression test added to each of between.autoRemove and betweenInclusive.autoRemove covering autoRemove(99).
  • Existing array-tuple cases unchanged.

Local trace of all relevant cases:

val before after
undefined true true
[undefined, undefined] true true
[null, null] true true
['', ''] true true
[5, 10] false false
[0, 10] false false
[undefined, 10] false false
99 true (BUG) false
'' true true

2 files changed, +49/-3.

Summary by CodeRabbit

  • Bug Fixes
    • Improved range filter handling so single values are no longer mistaken for empty ranges.
    • Fixed auto-removal behavior for number range filters when non-array inputs are provided.
    • Added regression coverage to protect against similar issues in future updates.

…ter autoRemove

`filterFn_between`, `filterFn_betweenInclusive`, and
`filterFn_inNumberRange` shared an `autoRemove` that did
`testFalsy(val[0]) && testFalsy(val[1])`. When a scalar value such as
`99` was passed instead of a `[min, max]` tuple, `val[0]` and
`val[1]` were both `undefined`, so the condition evaluated to
`true && true` and the filter was silently auto-removed.

Numeric columns default to `inNumberRange` via
`column_getAutoFilterFn`, so calling `col.setColumnFilter(99)` on a
numeric column discarded the filter before the row scan even ran.

Wrap the index checks in `Array.isArray(val) &&`. The leading
`testFalsy(val)` still handles `undefined`, `null`, `0`, and
`''`. Behavior of the existing array-tuple cases is unchanged because
`Array.isArray` is true and short-circuits to the same boolean.

Closes TanStack#6353
@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The autoRemove predicates for the between, betweenInclusive, and inNumberRange range filter functions now guard with Array.isArray(val) before indexing val[0] and val[1], preventing scalar values from being incorrectly auto-removed. Regression tests were added covering scalar inputs for all three filter functions.

Changes

Range filter autoRemove fix

Layer / File(s) Summary
Array.isArray guard in autoRemove predicates
packages/table-core/src/fns/filterFns.ts
filterFn_between, filterFn_betweenInclusive, and filterFn_inNumberRange autoRemove logic now checks Array.isArray(val) before evaluating val[0]/val[1] falsiness.
Regression tests for scalar autoRemove inputs
packages/table-core/tests/unit/fns/filterFns.test.ts
New tests assert autoRemove returns false for scalar (non-array) inputs passed to between, betweenInclusive, and inNumberRange filter functions.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Possibly related PRs

  • TanStack/table#6332: Extends the same autoRemove logic for between/betweenInclusive (and adds inNumberRange) by adding an Array.isArray guard before checking val[0]/val[1].
  • TanStack/table#6371: Modifies range-based filter function logic in the same filterFns.ts file, including between/betweenInclusive endpoint handling.

Suggested reviewers: KevinVandy

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main fix to guard array indexing in range filter autoRemove.
Linked Issues check ✅ Passed The code and tests implement the linked fix across all three range filter functions as requested.
Out of Scope Changes check ✅ Passed The PR stays focused on the range filter bug fix and related regression tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/table-core/tests/unit/fns/filterFns.test.ts`:
- Around line 599-606: The scalar-number regression test in filterFns.test.ts is
incorrectly asserting that autoRemove('') should be false, but testFalsy('')
already makes autoRemove short-circuit to true. Update the test case around
autoRemove to keep only scalar number assertions (for example, the autoRemove
checks for 99 and 0) and remove the empty-string assertion so the test matches
the intended behavior and the PR’s handling of falsy strings.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3ba58bf1-9358-4668-8e91-b0aa1143805e

📥 Commits

Reviewing files that changed from the base of the PR and between 9db621f and 0041efb.

📒 Files selected for processing (2)
  • packages/table-core/src/fns/filterFns.ts
  • packages/table-core/tests/unit/fns/filterFns.test.ts

Comment on lines +599 to +606
it('should NOT auto-remove a scalar number passed instead of a range tuple', () => {
// Regression test for #6353 - `val[0]` and `val[1]` were both
// undefined for a scalar, so the previous index checks produced
// true && true and the filter was discarded.
expect(autoRemove(99 as any)).toBe(false)
expect(autoRemove(0 as any)).toBe(false)
expect(autoRemove('' as any)).toBe(false)
})

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.

🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win

Test expects false for '' but autoRemove('') returns true — this test will fail.

testFalsy('') returns true (line 390: val === ''), so autoRemove('') short-circuits to true. The test at line 605 asserts .toBe(false), which will fail. This also contradicts the PR objectives, which state that '' should be auto-removed.

The '' case is also miscategorized in this test — it's titled "should NOT auto-remove a scalar number" but '' is an empty string, not a number. Remove the '' assertion from this test; '' is already correctly handled by testFalsy(val) and should remain auto-removed.

🐛 Proposed fix: remove the incorrect `''` assertion
       it('should NOT auto-remove a scalar number passed instead of a range tuple', () => {
         // Regression test for `#6353` - `val[0]` and `val[1]` were both
         // undefined for a scalar, so the previous index checks produced
         // true && true and the filter was discarded.
         expect(autoRemove(99 as any)).toBe(false)
         expect(autoRemove(0 as any)).toBe(false)
-        expect(autoRemove('' as any)).toBe(false)
       })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it('should NOT auto-remove a scalar number passed instead of a range tuple', () => {
// Regression test for #6353 - `val[0]` and `val[1]` were both
// undefined for a scalar, so the previous index checks produced
// true && true and the filter was discarded.
expect(autoRemove(99 as any)).toBe(false)
expect(autoRemove(0 as any)).toBe(false)
expect(autoRemove('' as any)).toBe(false)
})
it('should NOT auto-remove a scalar number passed instead of a range tuple', () => {
// Regression test for `#6353` - `val[0]` and `val[1]` were both
// undefined for a scalar, so the previous index checks produced
// true && true and the filter was discarded.
expect(autoRemove(99 as any)).toBe(false)
expect(autoRemove(0 as any)).toBe(false)
})
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/table-core/tests/unit/fns/filterFns.test.ts` around lines 599 - 606,
The scalar-number regression test in filterFns.test.ts is incorrectly asserting
that autoRemove('') should be false, but testFalsy('') already makes autoRemove
short-circuit to true. Update the test case around autoRemove to keep only
scalar number assertions (for example, the autoRemove checks for 99 and 0) and
remove the empty-string assertion so the test matches the intended behavior and
the PR’s handling of falsy strings.

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.

1 participant