Skip to content
Source
Sign inSign up
TanStack/store

fix(store): guard shallow comparison against mismatched collection types and support invalid dates #375

Closedgrzdev(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/375/headopened

Summary

Fixes edge cases in the shallow equality helper:

  • Prevents false-positive equality when comparing distinct collection types (e.g. shallow(new Date(), {}), shallow(new Map(), {}), shallow(new Set(), {}), or shallow(new Map(), new Set()) previously evaluated to true because collection instances have zero own enumerable properties and fell through to empty object keys comparison).
  • Correctly treats invalid dates as equal using Object.is(objA.getTime(), objB.getTime()) (previously NaN !== NaN caused two invalid dates to be considered unequal).
  • Added comprehensive unit test suite in packages/store/tests/shallow.test.ts.
  • Added changeset declaring a patch release for @tanstack/store.

Summary by CodeRabbit

  • Bug Fixes
    • Shallow comparisons now return false when comparing a Map, Set, or Date with a different type.
    • Two invalid dates are now considered equal in shallow comparisons, while valid and invalid dates remain unequal.
    • Matching Maps and Sets continue to be compared by size and contents. Plain objects, arrays, and other values retain their existing comparison behavior.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Advanced

      Run ID: 06da56b3-1091-4473-ab12-4cdd30f00f24

      📥 Commits

      Reviewing files that changed from the base of the PR and between a9cf2324b4a24a8190ed7981c3be1594e0446872 and b8800606e58360f30ed87431ea4565cc5563a72c.

      📒 Files selected for processing (3)
      • .changeset/fix-shallow-mismatched-types-and-invalid-dates.md
      • packages/store/src/shallow.ts
      • packages/store/tests/shallow.test.ts

      Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.


      📝 Walkthrough

      Walkthrough

      The shallow comparison now returns false for mismatched Map, Set, or Date types. It compares Date timestamps with Object.is, so two invalid Dates compare equal. Tests cover these cases and other shallow comparisons. A patch changeset records the fixes.

      Changes

      Shallow comparison

      Layer / File(s)Summary
      Comparison behavior and coverage
      packages/store/src/shallow.ts, packages/store/tests/shallow.test.ts, .changeset/fix-shallow-mismatched-types-and-invalid-dates.md
      shallow checks for mismatched Map, Set, and Date types, and compares Date timestamps with Object.is. Tests cover these behaviors and primitive, reference, object, and array comparisons. A patch changeset records the fixes.

      Priority: ⬇️ Low

      Estimated code review effort: 2 (Simple) | ~10 minutes

      Change: Bug fix

      Merge Risk: ⚪ Minimal · up to b8800

      This change fixes edge cases in the shallow comparison helper and adds tests for them. No merge-blocking risk was found.

      Architecture Summary

      Architecture risk: 🔵 Low · up to b8800

      The change affects 1 system.

      Changed systems: packages/store

      Architecture concerns No architecture-level concerns identified.

      Review details

      Systems and components

      • observed — packages/store (library) was modified; 2 changed files map to changed impact.

      Before / after behavior

      • observed — Modified behavior in packages/store/src/shallow.ts: Map comparison now runs when either operand is a Map and returns false if only one is a Map; matching Maps retain the existing size and key/value checks.
      • observed — Modified behavior in packages/store/src/shallow.ts: Set comparison now runs when either operand is a Set and returns false if only one is a Set; matching Sets retain the existing size and membership checks.
      • observed — Modified behavior in packages/store/src/shallow.ts: Date comparison now runs when either operand is a Date and returns false if only one is a Date. Matching Dates compare timestamps with Object.is instead of !==, making two invalid Dates equal.
      • observed — Modified behavior in packages/store/tests/shallow.test.ts: Added tests asserting true for identical primitives (including NaN) and references, and false for differing primitives and nullish mismatches.
      🚥 Pre-merge checks | ✅ 4 | ❌ 1

      ❌ Failed checks (1 warning)

      Check nameStatusExplanationResolution
      Docstring Coverage⚠️ WarningDocstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (1 skipped: 1 …Write docstrings for the functions missing them to satisfy the coverage threshold.
      ✅ Passed checks (4 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly and concisely describes the main changes to mismatched collection types and invalid date handling in the shallow comparison.
      Description check✅ PassedThe description explains the changes, motivation, tests, and changeset. It does not use the template headings or confirm the checklist items, but it contains the main required information.
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Full details: Docstring Coverage

      Explanation

      Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (1 skipped: 1 unsupported.)

      • Fix all pre-merge checks with AI
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR

      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.
    2. TkDodo closed this

Sign in to join this conversation.