You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
modify(): trailing inline comment is re-attached to the newly inserted property #125
When a new property is appended with modify(), a same-line (inline) trailing comment on the object's last property is re-attached to the newly inserted property. The comment then documents a different key, so the edit changes the meaning of the file, not just its formatting.
Environment
jsonc-parser: 3.3.1 Also reproduces on 4.0.0-next.2 see the first comment with updated attachment
node: v22
Reproducible on any OS (pure string manipulation, no I/O involved).
The comment should stay with port, and the new property should be inserted after it.
Scope
Triggers when: the last property of an object has an inline (same-line) trailing comment and a new property is appended.
Does NOT trigger when:
the comment is on its own line, or
the inline comment is on a non-last property.
So only the inline comment of the last property is affected.
Likely cause
The insertion point appears to be computed immediately after the last property's value token. The original trailing comma and the inline comment that follow it are left in place, so the inserted text lands before them — which visually re-associates the comment with the newly inserted property.
Related issues
Option to keep spaces before inline comments #76 — Option to keep spaces before inline comments: same area (inline comments handled by modify), but that report is about lost spacing/alignment, whereas this is about the comment being moved onto the wrong key (a semantic change).
Follow-up: the same underlying cause also affects deletion (removeProperty, i.e. modify(text, path, undefined, ...)), and there it can be worse — deleting one property can silently drop the trailing comment of a neighbouring property.
Environment — jsonc-parser: 3.3.1 · node: v22 Also reproduces on 4.0.0-next.2
The // keep me comment belonged to a, a property that was not touched, yet deleting b removed it.
Expected output
{
"a": 1// keep me
}
Why
Same root cause as the insertion case: the deletion range starts at previous.offset + previous.length (the end of the previous property's value), which is before that property's trailing comment — comments are trivia and live outside the property node, so the comment falls inside the removed range.
Related variant
When the deleted property is the one carrying the comment, the comment instead survives as an orphan on the previous property:
Both behaviours — the insertion from the original report and the deletion above — also reproduce on the current pre-release (jsonc-parser@next, 4.0.0-next.2), so this is not specific to 3.3.1:
// after inserting "debug": the comment moves onto the inserted property
{
"host": "localhost",
"port": 8080,
"debug": true// default port
}
// after deleting "b": the "// keep me" comment of "a" is gone
{
"a": 1
}
So the trailing-comment handling problem spans both insertion and deletion; a complete fix should cover removeProperty as well.
Activity
italopaesano commentedon Jun 30, 2026
Follow-up: the same underlying cause also affects deletion (
removeProperty, i.e.modify(text, path, undefined, ...)), and there it can be worse — deleting one property can silently drop the trailing comment of a neighbouring property.Environment —
jsonc-parser: 3.3.1 · node: v22 Also reproduces on 4.0.0-next.2Reproduction
Actual output
{ "a": 1 }The
// keep mecomment belonged toa, a property that was not touched, yet deletingbremoved it.Expected output
{ "a": 1 // keep me }Why
Same root cause as the insertion case: the deletion range starts at
previous.offset + previous.length(the end of the previous property's value), which is before that property's trailing comment — comments are trivia and live outside the property node, so the comment falls inside the removed range.Related variant
When the deleted property is the one carrying the comment, the comment instead survives as an orphan on the previous property:
Also reproduces on the 4.0.0 pre-release
Both behaviours — the insertion from the original report and the deletion above — also reproduce on the current pre-release (
jsonc-parser@next, 4.0.0-next.2), so this is not specific to 3.3.1:So the trailing-comment handling problem spans both insertion and deletion; a complete fix should cover
removePropertyas well.jsonccommentdemo.zip