modify(): trailing inline comment is re-attached to the newly inserted property
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 72/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Quiet
- Tech stack
- typescript
- Domain
- tooling
Research direction
Start with the provided modify() and applyEdits() reproduction, then trace how modify() chooses the insertion point after the last property value and handles trailing inline comments. Done means the comment remains attached to port, the new debug property follows it, and the scoped cases continue to behave as described.
Written by the indexing model from the issue text.
Description
Summary
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).
Steps to reproduce
import { modify, applyEdits } from 'jsonc-parser';
const input = `{
"host": "localhost",
"port": 8080 // default port
}`;
const edits = modify(input, ['debug'], true, {
formattingOptions: { tabSize: 2, insertSpaces: true }
});
console.log(applyEdits(input, edits));
(The behavior is identical whether the package is loaded via import or require.)
Actual behavior
{
"host": "localhost",
"port": 8080,
"debug": true // default port
}
The // default port comment clearly belongs to port, but it is now attached to debug: true, where it makes no sense.
Expected behavior
{
"host": "localhost",
"port": 8080, // default port
"debug": true
}
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
- #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). - #51 — modify ... capture too much content: also about
modifyaffecting content beyond the targeted node.
This seems distinct enough from #76 to warrant its own issue, but I'm happy to consolidate if you prefer.
- Dominant language
- TypeScript
- Stars
- 759
- Forks
- 66
- Avg merge
- 5d 10h
- Merged PRs (30d)
- 7
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/node-jsonc-parser
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
microsoft/node-jsonc-parser#105 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
microsoft/node-jsonc-parser#103 · 2 comments · 8 reactions ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 48/100
microsoft/node-jsonc-parser#97 · 2 reactions ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
microsoft/node-jsonc-parser#95 · 1 reaction ·
-
Difficulty 1/5 Under an hour Newbie friendliness 35/100
All issues in microsoft/node-jsonc-parser
Similar issues
-
S: triage
Difficulty 1/5 Under an hour Newbie friendliness 85/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
fix(errors): EHOSTUNREACH from a happy-eyeballs connect is reported as a resolver error (STAMP-80) Open
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
snapshot-labs/stamp#666 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
GauravKarakoti/SecureFlow#1070 · 1 comment ·
-
feature:Languages/Translations good first issue ready Web
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
digitalfabrik/integreat-app#4394 ·