Repository navigation
Remove the trailing comma when removing the only property of an object - #147
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)
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.
|
I independently reproduced this on the released jsonc-parser 3.3.1. With I applied the comma-scan branch from this PR in memory to the released JavaScript implementation. That combined nested-object/comment fixture produced valid JSONC, retained |
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.