Repository navigation
Remove the trailing comma when removing the only property of an object - #147
Open
Alperen Kocakaplan (Pharex38) wants to merge 1 commit into
Open
Alperen Kocakaplan (Pharex38) wants to merge 1 commit into
Alperen Kocakaplan (Pharex38) wants to merge 1 commit into
Conversation
modify('{"a": 1,}', ['a'], undefined) produced '{ , }', which does not
parse even with allowTrailingComma. Arrays already handled this case.
Ammann Labs (agammann)
approved these changes
Nov 1, 2026
Ammann Labs (agammann)
left a comment
There was a problem hiding this comment.
I reviewed the sole-property deletion path and compiled the TypeScript. All 89 tests passed. I also checked removal with and without formatting for nested objects, CRLF input, and comments before and after the comma. In each case, the property was removed and the resulting JSONC parsed without errors. This looks correct to me.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Removing the only property of an object that has a trailing comma leaves the comma behind:
The same happens for nested objects, e.g. removing the last setting from a section of a
settings.jsonwritten with trailing commas. Arrays already handle it: the only-item case clears the brackets, so[1,]->[]. With two or more properties it works too, because then the edit spans to the next property, or starts at the previous one.When the removed property is the only one, the edit now also covers a comma that directly follows it (comments and whitespace in between are skipped by the scanner). A comment after the comma is kept:
{ "x": "y", // note }->{ // note }.Added
remove the only property with a trailing commatosrc/test/edit.test.ts. It fails without the change, andnpm testpasses (89 tests). I found it while fuzzingmodifyagainstparse; with the change, 20k random removals/inserts/sets all produce valid JSON with the expected value.