question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow trailing comma for "JSON with Comments" file type

See original GitHub issue

Background Certain config files, including VSCode’s config files, support a relaxed JSON syntax where:

  • comments are allowed
  • trailing commas are allowed
  • keys must still be quoted, just like in real JSON

(VSCode calls this syntax “JSON with Comments”.)

Example of issue I’m trying to have prettier-vscode format my VSCode settings.json and keybindings.json files. Sometimes these files are automatically modified by VSCode (for example, when changing UI zoom). Sometimes these files are modified directly by me.

When VSCode automatically modifies these files, it applies trailing commas, so I’d like have Prettier do the same. But this doesn’t seem possible: There is no way to force vscode-prettier to respect my trailingComma setting because #143 force-turns it off when the Prettier parser is json. I can edit prettierrc to change the parser to babylon or flow, but then Prettier will unquote keys, resulting in an invalid config.

Potential solutions

  • Add an exception to the #143 when the syntax is “JSON with Comments”, essentially narrowing the cases under which #143 to a more accurate set.
  • Escalate this issue to Prettier – propose that prettier supports two different JSON parsers, one which is strict and one which is the relaxed style. They can still use the same parser implementation – the only difference being that when using the strict JSON parser, trailing commas will always be removed. Then #143, which is sort of a hack, probably won’t be needed either.

I’m happy to PR a fix for this, please just let me either of the above solutions seems like a good idea. (Implementation tips would be appreciated too.)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
CiGitcommented, Nov 8, 2018

This should be done in prettier/prettier. Closing

0reactions
github-actions[bot]commented, Apr 13, 2020

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can you use a trailing comma in a JSON object?
No. The JSON spec, as maintained at http://json.org, does not allow trailing commas. From ...
Read more >
Trailing commas - JavaScript - MDN Web Docs
JavaScript allows trailing commas wherever a comma-separated list of values is accepted and more values may be expected after the last item.
Read more >
JSON with Commas and Comments - Hacker News
As a glue format, JSON feels just right, even if it's a bit picky sometimes (e.g. trailing commas). For anyone who wants extra...
Read more >
Why doesn't JSON allow trailing commas? Why? How hard is ...
So for certain types of configuration I really like it (usually bordering on being data but manually maintained). I thought it still didn't...
Read more >
System.Text.Json allow comments in JSON - TheCodeBuzz
System.Text.Json allows comments in JSON or trailing commas to serialize or deserialize using JsonSerializerOptions. Use JsonCommentHandling ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found