dprint-plugin-json icon indicating copy to clipboard operation
dprint-plugin-json copied to clipboard

Allow having trailing commas for config files like tsconfig.json

Open davidbludlow opened this issue 2 years ago • 5 comments

I like having trailing commas in files like:

  • tsconfig.json
  • jsconfig.json
  • .vscode/settings.json
  • .vscode/extensions.json

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.

davidbludlow avatar Mar 30 '23 01:03 davidbludlow

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.

dsherret avatar Mar 30 '23 02:03 dsherret

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 avatar Mar 30 '23 02:03 davidbludlow

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 avatar Mar 30 '23 02:03 davidbludlow

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. 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": {
    "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.

davidbludlow avatar Mar 30 '23 02:03 davidbludlow

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.)

Zamiell avatar Aug 24 '23 21:08 Zamiell