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
Allow JSON with trailing commas #5708
Comments
|
wow, they made their own JSON parser in the first commit, and still use it. |
@lydell the problem isn't the prettier Also, this particular syntax isn't VSCode-specific, it's a pretty common JSON superset that's used in config files. Sublime Text's config files accept comments and trailing commas as well. |
Have you considered suggesting to vscode (and whoever else) that they adopt JSON5 for their config, rather than this other intermediate thing? I don't think it would be a breaking change. |
See also #4639 (linked from the prettier-vscode issue). |
Personally, I think I was thinking of adding a |
Since |
@ikatyang yeah I think json5 is a little too over-the-top, at that point they might as well be using yaml… |
We did not have
They are the same in theory, but accepting #4639 does not look like the same to me since we don't want to add more options (same as respecting original text). |
I am suspecting that the most seamless solution might not be to change the Some other suggestions:
|
#4639 wouldn't require a new option, would it? It could just always output anything parsed as |
True, but would it be possible for future changes to json5 output to be non-jsonc-compatible? |
It's hard to predict what changes could "break" the output but I think it's safe to assume that if the source is jsonc-compatible, the output would be too. For example, let's imagine the multiline feature didn't exist in JSON5 and the feature was just added The following would continue to be JSONC-compatible: {
"foo": "bar\nbaz",
} But if you change your source code to use this new feature, then it woud no longer be compatible: {
"foo": "bar\
baz",
} |
the |
Would also like this. It's very annoying when commenting out the last line of an array or object that prettier will remove the comma of the line before. So then when you comment the line back in, it's a syntax error because the comma is missing. |
@felixfbecker That's indeed a very unfortunate side effect. |
.prettierrc
:) |
@difr thanks 👍 |
@NewFuture commapeople should be tight together :), |
This override works for me mostly: overrides: {
files: '{**/.vscode/*.json,**/tsconfig.json,**/tsconfig.*.json}',
options: {
parser: 'json5',
quoteProps: 'preserve',
singleQuote: false,
trailingComma: 'all',
},
}, Except that it fails if a string contains an escaped double quote
Also, if for some reason you remove quotes around a key, Prettier won't add them back because there is no It would be great if there was a |
@felixfbecker Those aren't unreasonable requests. If you're interested, would you open separate issues for them? Thanks |
The json5 override doesn't work when you have strings that contain double quote characters -- to avoid escaping them, Prettier will use single quotes. See #973 Workaround: Use |
Hi, I'm the maintainer for JSON5.
I don't understand your question since JSON5 isn't compatible with JSON with Comments. The converse is true though. JSON with Comments is a subset of JSON5. Maybe mean future changes to the output of Prettier for JSON5 documents?
There will be no future changes to JSON5 (as long as I'm still maintaining the project). The spec is frozen as there aren't really too many more ES5 features to implement. (Native Date and RegExp support is not going to happen, and neither is numeric keys.)
Hey, I resent that. 😄 I do get where you're coming from though. There is quite a bit of overlap between JSON5 and YAML, and YAML has better community support. I think JSON5 is even a subset of YAML, but that was never a target. However, it's harder to write a YAML parser because it allows for so many different ways to say the same thing, which is the antithesis of JSON. |
Hi @jordanbtucker thanks for chiming in. I'm going to preface the below by saying that although JSON5 has been mentioned a lot in this issue, I think this issue is not actually related to JSON5, and the solution might not need to involve JSON5. JSON5 is simply one of the two closest Prettier syntaxes to JSON with Comments that we have (with the other being JSON). So please know that the discussion about JSON5 here is generally not about how good JSON5 is itself but rather about its effectiveness as a solution for this issue.
I meant that even if we "supported" JSON with Comments by using JSON5 and adding options to tell Prettier to not use each JSON5 feature that isn't compatible with JSON with Comments (e.g., by adding the "key keys quoted property" as noted in the comment above mine), it's possible that a future version of JSON5 adds even more leniency and we'll be in this state again until we add an option to turn off that new feature. But, as you've said below, new features are probably not going to be added, so this isn't a big concern.
This is good to hear, although I still think that it feels like a hack to support one lenient JSON syntax with another. Of course this is through no fault of JSON5.
Sorry for some of the assumptions I made and thanks for clarifying -- now I understand why JSON5 was created even when YAML already exists. |
I'm looking for a way to support the following style:
This would typically be not a problem -- I should be able to set
parser: json
andtrailingComma: all
. However, #2308* disables this specific combination and I can't get around it.Here's my use case: I want Prettier to format my VSCode settings files, which I keep in a git repo.
Note: I originally filed this bug in another repo prettier/prettier-vscode#589
* I am aware that #2308 was created because trailing commas broke VSCode. However, times have changed and VSCode settings files now support trailing commas.
Thanks!
Prettier 1.15.3
Playground link
Input:
Output:
Expected behavior:
I'd like a set of input options that doesn't make changes to the input; i.e., I want the output to also be
The text was updated successfully, but these errors were encountered: