tsconfig icon indicating copy to clipboard operation
tsconfig copied to clipboard

parse error for trailing comma in tsconfig.json

Open dominikg opened this issue 4 years ago • 5 comments

According to this issue on typescript repo, trailing commas in tsconfig are legal (like comments) https://github.com/microsoft/TypeScript/issues/20384#issuecomment-348552936

Example:

{
  "extends": "@tsconfig/svelte/tsconfig.json",
  "include": ["src/**/*"],
  "exclude": ["node_modules"], // <- here
}

Comments are stripped here, but the comma is not, resulting in an error. https://github.com/TypeStrong/tsconfig/blob/3d0586d30a85e1098d4a966e6b563d58abc42620/src/tsconfig.ts#L187

dominikg avatar Aug 02 '21 21:08 dominikg

Makes sense, related to https://github.com/TypeStrong/tsconfig/issues/28. Happy to accept a PR here, but would likely mean trying to implement a custom JSON parser. I'd recommend using typescript directly where possible.

blakeembrey avatar Aug 04 '21 05:08 blakeembrey

I see 2 possible implementations

  1. after stripping comments with https://github.com/sindresorhus/strip-json-comments use a similar implementation to strip the dangling commas before passing it to JSON.parse ~~2) get nasty and use eval, trying to safeguard against nefarious input~~ edit: removed some code that looked like it could work but i don't want to leave it here as it is too dangerous

an optional peerdependency on typescript and using its native functions before resorting to a custom implementation would be better. In my case i ran into this in vite, which uses esbuild so there is no typescript by default.

dominikg avatar Aug 04 '21 15:08 dominikg

Have you thought about using the jsonc-parser package to read the config files ? https://www.npmjs.com/package/jsonc-parser

tmeindle avatar Jan 19 '22 17:01 tmeindle