[PR #5634] fix: detect trailing comma and report error at correct position #5293

Open
opened 2026-03-17 02:45:16 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/hoppscotch/hoppscotch/pull/5634
Author: @tlawjddnjs
Created: 11/30/2025
Status: 🔄 Open

Base: mainHead: fix/5502-detect-json-trailing-comma


📝 Commits (2)

  • afb66fd fix: detect trailing comma and report error at correct position
  • ecd614e Update packages/hoppscotch-common/src/helpers/jsoncParse.ts

📊 Changes

1 file changed (+30 additions, -23 deletions)

View changed files

📝 packages/hoppscotch-common/src/helpers/jsoncParse.ts (+30 -23)

📄 Description

Closes #5502

This PR fixes an issue where trailing commas in JSON objects or arrays were not detected as invalid JSON in the request body editor. The server rejected such bodies, but the editor did not highlight the error. This change updates the parser to properly detect and report trailing commas with accurate error positions.

What's changed

  • Added explicit trailing comma detection in parseObj and parseArr.
  • Store the comma token range before consuming it with expect(",").
  • When the next token after the comma is a closing } or ], report a SyntaxError pointing to the comma token rather than the closing bracket/brace.
  • Ensures that invalid JSON with trailing commas is now surfaced correctly in the UI.

Notes to reviewers

  • The fix is strictly scoped to JSON trailing comma handling.
  • No existing behavior for valid JSON structures is affected.
  • The error reporting position now aligns with VSCode-like behavior, improving the editor’s feedback accuracy.

Summary by cubic

Detects trailing commas in JSON objects/arrays and highlights the comma as the error in the request body editor. Aligns UI validation with server behavior for clearer feedback.

  • Bug Fixes
    • Added trailing comma checks in parseObj/parseArr and throw with the comma token range.
    • Store comma start/end before consuming "," to report the exact error position.
    • Unified root parsing via parseVal + EOF, replacing the object/array try-catch flow.

Written for commit ecd614e875. Summary will update automatically on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/hoppscotch/hoppscotch/pull/5634 **Author:** [@tlawjddnjs](https://github.com/tlawjddnjs) **Created:** 11/30/2025 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/5502-detect-json-trailing-comma` --- ### 📝 Commits (2) - [`afb66fd`](https://github.com/hoppscotch/hoppscotch/commit/afb66fd4dbf2f8088e3ac4faea513049ffd30337) fix: detect trailing comma and report error at correct position - [`ecd614e`](https://github.com/hoppscotch/hoppscotch/commit/ecd614e875bc69703e8e0aa47b26e225cb450fa1) Update packages/hoppscotch-common/src/helpers/jsoncParse.ts ### 📊 Changes **1 file changed** (+30 additions, -23 deletions) <details> <summary>View changed files</summary> 📝 `packages/hoppscotch-common/src/helpers/jsoncParse.ts` (+30 -23) </details> ### 📄 Description <!-- Thanks for creating this pull request 🤗 Please make sure that the pull request is limited to one type (docs, feature, etc.) and keep it as small as possible. You can open multiple prs instead of opening a huge one. --> <!-- If this pull request closes an issue, please mention the issue number below --> Closes #5502 <!-- Add an introduction into what this PR tries to solve in a couple of sentences --> This PR fixes an issue where trailing commas in JSON objects or arrays were not detected as invalid JSON in the request body editor. The server rejected such bodies, but the editor did not highlight the error. This change updates the parser to properly detect and report trailing commas with accurate error positions. ### What's changed <!-- Describe point by point the different things you have changed in this PR --> - Added explicit trailing comma detection in `parseObj` and `parseArr`. - Store the comma token range before consuming it with `expect(",")`. - When the next token after the comma is a closing `}` or `]`, report a `SyntaxError` pointing to the comma token rather than the closing bracket/brace. - Ensures that invalid JSON with trailing commas is now surfaced correctly in the UI. ### Notes to reviewers <!-- Any information you feel the reviewer should know about when reviewing your PR --> - The fix is strictly scoped to JSON trailing comma handling. - No existing behavior for valid JSON structures is affected. - The error reporting position now aligns with VSCode-like behavior, improving the editor’s feedback accuracy. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Detects trailing commas in JSON objects/arrays and highlights the comma as the error in the request body editor. Aligns UI validation with server behavior for clearer feedback. - **Bug Fixes** - Added trailing comma checks in parseObj/parseArr and throw with the comma token range. - Store comma start/end before consuming "," to report the exact error position. - Unified root parsing via parseVal + EOF, replacing the object/array try-catch flow. <sup>Written for commit ecd614e875bc69703e8e0aa47b26e225cb450fa1. Summary will update automatically on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/hoppscotch#5293
No description provided.