Skip to content

refactor: split parser into focused methods - #23

Closed
CNSeniorious000 wants to merge 1 commit into
mainfrom
refactor/parser-internals
Closed

CNSeniorious000 wants to merge 1 commit into
mainfrom
refactor/parser-internals

Conversation

@CNSeniorious000

Copy link
Copy Markdown
Member

Summary

  • split the parser closures into focused class methods to reduce function size and cognitive complexity
  • use table-driven literal parsing with a first-character fast path
  • preserve existing parsing and error behavior

Verification

  • bun run build
  • bun run test --run — 6 tests passed
  • bunx fallow — 0 complexity findings; maintainability 85.9 (good)
  • 2,688 old/new differential comparisons passed
  • Bun and Node microbenchmarks showed no overall throughput regression

@bolt-new-by-stackblitz

Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@vercel

vercel Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Project Deployment Actions Updated
partial-json-coverage Ready Ready Preview Oct 28, 2026 3:13am UTC

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T03:14:50.601325Z 8271012 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Oct 28, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/partial-json@23

commit: 8271012

@sourcery-ai sourcery-ai Bot 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.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/index.ts" line_range="123" />
<code_context>
+        this.skipBlank();
+        this.index++; // skip colon
+        try {
+            obj[key] = this.parseAny();
+        } catch (e) {
+            if (Allow.OBJ & this.allow) return true;
</code_context>
<issue_to_address>
**Deep objects exhaust the stack sooner**

When input contains objects nested close to the runtime’s call-stack limit, each nested object keeps a `parseObjEntry` frame alive while `this.parseAny()` recursively parses its value, adding one stack frame per object level compared with the previous parser. Deep but otherwise valid object input therefore reaches a `RangeError` at a shallower depth.

Avoid keeping a separate `parseObjEntry` frame active during recursive value parsing, or use an iterative traversal for nested objects.
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: src/index.ts:123


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/index.ts
this.skipBlank();
this.index++; // skip colon
try {
obj[key] = this.parseAny();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Deep objects exhaust the stack sooner

When input contains objects nested close to the runtime’s call-stack limit, each nested object keeps a parseObjEntry frame alive while this.parseAny() recursively parses its value, adding one stack frame per object level compared with the previous parser. Deep but otherwise valid object input therefore reaches a RangeError at a shallower depth.

Avoid keeping a separate parseObjEntry frame active during recursive value parsing, or use an iterative traversal for nested objects.

Prompt for AI agents
In `src/index.ts` at line 123:

**Deep objects exhaust the stack sooner**

When input contains objects nested close to the runtime’s call-stack limit, each nested object keeps a `parseObjEntry` frame alive while `this.parseAny()` recursively parses its value, adding one stack frame per object level compared with the previous parser. Deep but otherwise valid object input therefore reaches a `RangeError` at a shallower depth.

Avoid keeping a separate `parseObjEntry` frame active during recursive value parsing, or use an iterative traversal for nested objects.

@coderabbitai

coderabbitai Bot commented Oct 28, 2026

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2cb34c5b-5288-4500-b55f-183f489e4f17
Commits

Reviewing files that changed from the base of the PR and between 86fd4bc and 8271012.

Files selected for processing: 1
  • src/index.ts

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

📝 Summary

Summary

  • Refactored the parser into focused methods on a stateful Parser class.
  • Added table-driven parsing for literals, including partial-input rules and a first-character fast path.
  • Preserved partial-input handling and malformed or incomplete input errors.

Verification

The author reports 6 passing tests, 2,688 passing old/new differential comparisons, and no overall throughput regression in Bun and Node microbenchmarks.

The inline parser is replaced by a stateful Parser class with methods for value dispatch, literals, strings, objects, arrays, numbers, whitespace, and positioned errors. Literal definitions now use a table with values, Allow flags, and minimum partial-prefix lengths. Object-entry parsing is separated into a helper. Partial-input handling remains controlled by the corresponding Allow flags.

Priority: Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Merge Risk: Minimal · up to 82710

No merge-blocking risk was identified. Run the normal build and test checks before merging.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly describes the main parser refactor into focused methods.
Description check Passed The description accurately summarizes the parser refactor, literal parsing changes, preserved behavior, and verification results.
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 1 files.

Finishing Touches: 💡 1
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Copy link
Copy Markdown
Member Author

Benchmark

Compared main (86fd4bc) with the PR head (8271012) over 25 alternating rounds. Values are median throughput in ops/s.

Case Node 26.12 Change Bun 1.4.4 Change
Complete object 1.65M → 2.25M +36.5% 1.82M → 2.61M +43.2%
Complete nested 46.5K → 57.2K +22.9% 52.8K → 62.7K +18.8%
Partial object 493K → 514K +4.4% 1.38M → 1.56M +13.3%
Partial nested 38.1K → 43.7K +14.7% 50.5K → 58.4K +15.8%
Partial string 723K → 698K -3.5% 3.19M → 3.32M +4.3%
Partial number 728K → 703K -3.3% 3.47M → 3.77M +8.5%
Partial literal 32.9M → 40.5M +23.1% 27.9M → 37.2M +33.3%

Overall throughput improves, especially for objects and nested structures. Isolated partial string and number cases show a small (~3%) regression under Node.

@CNSeniorious000
CNSeniorious000 deleted the refactor/parser-internals branch October 28, 2026 03:51

This branch was successfully deployed

1 active deployment
Preview — 8271012c Deployed Oct 28, 2026 by vercel[bot]
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