New option: quoteObjectProps - to support JSON transformation
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 5.3k
- Forks
- 364
- Avg merge
- 3d 8h
- Merged PRs (30d)
- 3
Description
Feature request: recast option that wraps object properties in double-quotes, so that the emitted object literal can be valid JSON.
I'm trying to implement a JSON transformer that preserves comments. Tools such as VSCode and TypeScript support JSON with JavaScript comments. Writing tooling to manipulate these configurations is a bit more complicated than merely JSON.stringify(manipulate(JSON.parse(jsonText))) Ideally comments are preserved even as the JSON structure is manipulated.
I can use recast to accomplish this. I'll wrap the JSON in parentheses to make it valid JS, then parse it with recast. From the recast AST I can generate a plain JS object, using Symbols or Maps to link the object's structure back to the recast AST.
Then JS code can manipulate the JS object freely.
Finally, I can traverse the JS object, modify the recast AST to match the object's new structure, and use recast's printer to emit JSON that preserves the original formatting and comments as much as possible.
The only missing piece is that recast won't wrap object properties in double-quotes.
EDIT: I think this can be implemented here: https://github.com/benjamn/recast/blob/master/lib/printer.js#L718-L723
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.
Research direction
Read lib/printer.js around lines 718-723, where the issue suggests implementing the option, and trace how object properties are emitted. Add the quoteObjectProps option so emitted object properties use double quotes, then verify that the output is valid JSON while preserving comments and formatting as much as possible.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100