Skip to content

Allow trailing comma for "JSON with Comments" file type #589

@szhu

Description

@szhu

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 Fix json and trailing comma #143 when the syntax is "JSON with Comments", essentially narrowing the cases under which Fix json and trailing comma #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 Fix json and trailing comma #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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    lockedLocked due to inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions