Skip to content

fix(utils): handle null in isJSONSerializable#575

Open
terminalchai wants to merge 1 commit into
unjs:mainfrom
terminalchai:fix/isjson-serializable-null
Open

fix(utils): handle null in isJSONSerializable#575
terminalchai wants to merge 1 commit into
unjs:mainfrom
terminalchai:fix/isjson-serializable-null

Conversation

@terminalchai
Copy link
Copy Markdown

@terminalchai terminalchai commented May 10, 2026

Fixes #571

Two bugs in isJSONSerializable:

  1. The comparison t === null is dead code � typeof never returns the string 'null', so this condition can never be true.
  2. When value is null, it bypasses the early returns, reaches if (value.buffer), and throws TypeError: Cannot read properties of null (reading 'buffer').

Fix: add an explicit value === null guard immediately after the undefined check and return true (null is valid JSON). Remove the dead t === null from the typeof comparison.

Summary by CodeRabbit

  • Refactor
    • Improved internal code organization and consistency for better maintainability.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 10, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9539d427-0ac4-40d9-acfc-834c6a14d323

📥 Commits

Reviewing files that changed from the base of the PR and between dfbe3ca and ad86bec.

📒 Files selected for processing (1)
  • src/utils.ts

📝 Walkthrough

Walkthrough

The isJSONSerializable utility function in src/utils.ts has been refactored to handle null explicitly. An early value === null check now returns true before evaluating the typeof condition, correctly identifying null as JSON-serializable and preventing a prior type error.

Changes

null handling bug fix

Layer / File(s) Summary
null type handling
src/utils.ts
Explicit value === null check added before typeof evaluation, fixing null detection to return true instead of incorrectly attempting to access properties on null.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A null that was lost in the type-checking wild,
Now caught by an early return, reconciled.
No more shall it throw when we check if it's there,
JSON-safe and serene, with explicit care!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(utils): handle null in isJSONSerializable' directly and specifically describes the main change—fixing null handling in the isJSONSerializable function.
Linked Issues check ✅ Passed The code changes directly address all three coding requirements from issue #571: removing dead code (t === null comparison), explicitly handling null as JSON-serializable, and preventing the TypeError when null is passed.
Out of Scope Changes check ✅ Passed All changes in src/utils.ts are directly related to fixing null handling in isJSONSerializable as specified in issue #571, with no out-of-scope modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 and usage tips.

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.

isJSONSerializable bug

1 participant