Skip to content
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

parse error for trailing comma in tsconfig.json #30

Open
dominikg opened this issue Aug 2, 2021 · 3 comments
Open

parse error for trailing comma in tsconfig.json #30

dominikg opened this issue Aug 2, 2021 · 3 comments

Comments

@dominikg
Copy link

dominikg commented Aug 2, 2021

According to this issue on typescript repo, trailing commas in tsconfig are legal (like comments)
microsoft/TypeScript#20384 (comment)

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.

export function parse (contents: string, filename: string) {

@blakeembrey
Copy link
Member

Makes sense, related to #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.

@dominikg
Copy link
Author

dominikg commented Aug 4, 2021

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.

@tmeindle
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants