New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prettier should format JSONC with trailing commas #15553
Comments
Maybe we should add a jsonc parser? |
Yes, that's what I'm proposing in the final section of the OP. 🙏 |
I also have needs for this, @sosukesuzuki what do you think? |
I think adding |
PR welcome. |
To make sure I understand the 1, It should always use double quote. |
Yes sir! |
Is there a way to disable the addition of trailing commas in jsonc files? My |
@Cielquan You could try something like this in your Prettier config: overrides: [
// Revert JSONC parsing:
// https://github.com/prettier/prettier/issues/15553
{
files: ["**/*.jsonc"],
options: {
parser: "json",
},
},
], But what you described sounds hacky as hell and really terrible! You should definitely submit an issue/PR to |
Thanks for the response. I will try the config snippet on monday. Yeah, when I looked up the stacktrace and found out how the parsing was done I thought that too, that there has to be a better way to parse jsonc. |
… doesn't like trailing commas! prettier/prettier#15553
You could frame this problem as either:
Either way, this should be changed.
Steps to Reproduce
Observe that the formatted JSONC file does not have trailing commas.
Current Hacky Workaround
Use the following custom Prettier config:
Since JSONC is a subset of JSON5, this works properly. However, it is important that we also specify
quoteProps: "preserve"
, as the default value forquoteProps
for JSON5 is "as-needed", which would unquote JSON properties, making it non-valid JSONC.Obviously, having to use a custom Prettier config is really unfortunate, as one of the main ideas of using Prettier is that you shouldn't use your own custom config, you should just use all the default values. But this bug / bad default behavior prevents that.
Also, as a semi-related note, it was proposed that the default value for
quoteProps
in JSON5 would be changed in #4639. If a PR for that was accepted, then we could simplify the workaround slightly, although the ideal solution is to not to have to do a workaround to begin with.Summarizing Prior Discussion
This issue continues #5708.
The core issue is that Prettier does not have a separate parser for JSONC files, it just uses the JSON parser. Since the JSON parser works with comments, this "just works" some of the time, but crashes and burns when trailing commas are involved.
ikatyang says here that they considered making a JSONC parser, but this was confusing, as it was really just a trailing commas parser and not a comment parser. Which is definitely a valid concern!
However, five years have passed since that comment, and JSONC files have only become more prevalent in the ecosystem. For example, popular tools like CSpell (code spell checker), Knip (dead code finder), and TypeDoc (documentation generator) all support JSONC files by default ("cspell.jsonc", "knip.jsonc", and "typedoc.jsonc" respectively).
Thus, there is a more compelling reason for Prettier to support the JSONC format more robustly. To @ikatyang, and the rest of the Prettier team, can we revisit this decision? A dedicated parser for JSONC means that we can finally format JSONC files properly out of the box without having to use the dirty JSON5 hack specified above.
The text was updated successfully, but these errors were encountered: