dprint / dprint-plugin-json

JSON code formatting plugin for dprint.
https://dprint.dev/plugins/json
MIT License
18 stars 5 forks source link

Allow having trailing commas for config files like tsconfig.json #14

Open davidbludlow opened 1 year ago

davidbludlow commented 1 year ago

I like having trailing commas in files like:

Even though these files have .json in their name, it is common enough and acceptable to sometimes have trailing commas in these files. But dprint always removes them.

dsherret commented 1 year ago

Similar to #10. Maybe this could be a setting where you can specify the files you want trailing commas in or there could be a list of defaults.

davidbludlow commented 1 year ago

Yes!

I propose that we either enforce trailing commas in these types of files or that we make a new setting to allow this. I imagine it looking something like

"filesWithTrailingCommas": [
  "**/.vscode/extensions.json",
  "**/.vscode/launch.json",
  "**/.vscode/settings.json",
  "**/.vscode/tasks.json",
  "**/tsconfig*.json",
  "**/jsconfig*.json"
]

and it could default to that value that I said right there

You may have a better idea of what to call the setting or how to structure it. I am just saying an idea.

davidbludlow commented 1 year ago

In a perfect world, Microsoft would have decided to name those files .jsonc instead of .json, but I don't think that is going to happen :)

davidbludlow commented 1 year ago
You may skip reading this. It isn't that important. Click to expand this section: This was also a problem, back when I used prettier, until I found [this hacky workarround](https://github.com/prettier/prettier/issues/5708#issuecomment-491997511). I tried to do that same hacky workaround with dprint but it didn't work because https://github.com/dprint/dprint-plugin-prettier/issues/39 is still broken. The hack I tried was to have dprint.json say ```json { "json": { "associations": [ "**/*.json", "!**/.vscode/extensions.json", "!**/.vscode/launch.json", "!**/.vscode/settings.json", "!**/tsconfig*.json", "!**/jsconfig.json" ] }, "prettier": { "overrides": [ { "files": [ "**/.vscode/extensions.json", "**/.vscode/launch.json", "**/.vscode/settings.json", "**/tsconfig*.json", "**/jsconfig*.json" ], "options": { "parser": "json5", "quoteProps": "preserve", "singleQuote": false, "trailingComma": "all" } } ] }, "includes": [ "**/*.{ts,tsx,js,jsx,cjs,mjs,json,yaml,md,mdx,toml,vue,html,css,scss,sass,less,hbs,graphql,gql}" ], "excludes": [ "**/node_modules", "**/*-lock.json" ], "plugins": [ "https://plugins.dprint.dev/typescript-0.84.0.wasm", "https://plugins.dprint.dev/json-0.17.1.wasm", "https://plugins.dprint.dev/markdown-0.15.2.wasm", "https://plugins.dprint.dev/toml-0.5.4.wasm", "https://plugins.dprint.dev/prettier-0.24.0.json@9a57d0d8e440ad90d07a503166af47e7a6a886abd46767933f9c279f72468596" ] } ``` But that hack is ugly! I would much prefer we do something different. And, like I said, it doesn't work anyway.
Zamiell commented 1 year ago

Hi there. I'm investigating moving my monorepo from prettier to dprint, and this issue is one of the pain points for me. An option for this would be great!

I would even go so as far to say that files like "tsconfig.json" and "./.vscode/settings.json" should be parsed as JSONC by default without the end-user having to specify anything in their config. (Because this is what VSCode reports in the bottom-right-hand-corner as what the file actually is.)