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

Error! Unexpected token ] in "tsconfig.json" at position 666 #28

Closed
raphael10-collab opened this issue Mar 11, 2021 · 18 comments · Fixed by #37
Closed

Error! Unexpected token ] in "tsconfig.json" at position 666 #28

raphael10-collab opened this issue Mar 11, 2021 · 18 comments · Fixed by #37

Comments

@raphael10-collab
Copy link

My tsconfig.json is :

{
  "compilerOptions": {
    "target": "esnext",
    "module": "commonjs",
    "lib": ["dom", "es2015", "esnext"],
    "outDir": "dist",
    "jsx": "react",
    "baseUrl": "./src",
    "paths": {
      "@src/*": ["src/*"],
      "@sections/*": ["src/app/sections/*"],
      "@app/*": ["src/app/*"]
    },
    "strict": true,
    "sourceMap": true,
    "skipLibCheck": true,
    "noImplicitAny": false,
    "noImplicitThis": false,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "resolveJsonModule": true,
    "allowJs": true
  },
  "include": ["src/**/*"],
  "exclude": [
    "src/index.js",
    "dist",
  ]
}

Executing alias-hq CLI :

./node_modules/.bin/alias-hq 

  == Alias HQ ==

  Error! Unexpected token ] in "tsconfig.json" at position 666

  Edit the file to fix this, then try again

Why? How to solve the problem?

typescript : v.4.1.5
@davestewart
Copy link
Owner

"exclude": [
    "src/index.js",
    "dist", <-- here
  ]

@IanVS
Copy link
Contributor

IanVS commented Nov 24, 2021

tsconfig files aren't technically treated like pure json files, and typically allow trailing commas and comments. Perhaps you could consider using https://github.com/dominikg/tsconfck to parse it?

@davestewart
Copy link
Owner

Seems like a fair request! Want to PR?

@davestewart
Copy link
Owner

The other option would be to replace with a regex...

@IanVS
Copy link
Contributor

IanVS commented Nov 24, 2021

I can take a look at making a PR, sure.

Seems safer and less buggy to use a parser that's built for the purpose, but if you'd rather do it with a regex that's fine too, but I'm not sure I'm up for writing it. ;)

@davestewart
Copy link
Owner

Actually, the fact that tsconfck resolves extends would also fix #30...

@IanVS
Copy link
Contributor

IanVS commented Nov 24, 2021

FWIW, tsconfck.parse is an async function, so it's going to mean that the rest of the functions using it will need to become async as well. So I guess it means it'll probably need a major version bump.

@IanVS
Copy link
Contributor

IanVS commented Nov 24, 2021

I opened #36 if you want to take a quick look. I don't have time to finish it up at the moment, but I'll come back to it when I can.

@davestewart
Copy link
Owner

davestewart commented Nov 24, 2021

FWIW, tsconfck.parse is an async function

Crap... just seen the PR; yup lots of file changes.

Not sure how that's going to work if they bubble up to the CLI level. Would also affect the Alias CLI.

Not sure this is a good tradeoff that being the case...

Thoughts?

@IanVS
Copy link
Contributor

IanVS commented Nov 24, 2021

I think it shouldn't effect the cli, but I'll check it later. What is the alias cli? I can take a look at that too.

@davestewart
Copy link
Owner

What is the alias cli?

It's a major component of this library:

I can take a look at that too.

Sure. But I don't want to rewrite the whole thing and force users to go async, just to solve some trailing commas and merging a few paths...

I've posted the question here:

@davestewart
Copy link
Owner

davestewart commented Nov 24, 2021

It seems the asynchronous nature of the lib is due to using the async version of Node's Fs functions:

https://github.com/dominikg/tsconfck/blob/60f63d821a5aefdaa8f81ab22cc7e00fbc3e973d/src/util.ts#L33

Not sure why the author chose not to use the sync version of Node's filesystem functions which I think would make it more flexible?

EDIT: looks like he is doing some funky stuff with importing TS files:

https://github.com/dominikg/tsconfck/blob/60f63d821a5aefdaa8f81ab22cc7e00fbc3e973d/src/util.ts#L17

@IanVS
Copy link
Contributor

IanVS commented Nov 25, 2021

Yeah I think you're right, it's probably not worth a breaking change to the api.

Another option is to use typescript's own parseConfigFileTextToJson() function, but I'm not sure if you want to add typescript as a dependency, either. microsoft/TypeScript#44573 (comment)

It's a bummer, because typescript by default adds comments to the tsconfig.json in a new project.

@davestewart
Copy link
Owner

Thanks for taking a look anyway. Food for thought if nothing else!

And yeah, I think best to keep this sync + JS compatible.

I'll reopen so this can be solved anyway.

Thanks for bringing the config issue to my attention!

@davestewart davestewart reopened this Nov 25, 2021
@IanVS
Copy link
Contributor

IanVS commented Nov 25, 2021

Bringing in typescript wouldn't mean that it's no longer js compatible, but it would add a somewhat large dependency. But, since alias hq itself is a dev dependency, maybe it's not really a big deal.

@davestewart
Copy link
Owner

davestewart commented Nov 25, 2021

Re the trailing comma, this rx seems to handle it:

json = json.replace(/,+(\s+[}\]])/g, '$1')

Comments could be handled like so:

json = json.replace(/\/\/[^"]+$/gm, '')

This is just some mucking about in a scratchpad, but might be a quick win for this issue.

#30 would be a bit more work.

@IanVS
Copy link
Contributor

IanVS commented Nov 25, 2021

Maybe take a look at #37 and see if that might be a good solution?

@davestewart
Copy link
Owner

davestewart commented Nov 25, 2021

I just noticed you have 🦆, + 🐝 in your profile. Two of my 3 favourite creatures along with 🐙 !

Actually, add crows to that list!

davestewart pushed a commit that referenced this issue Nov 25, 2021
* Use typescript to parse tsconfig.json

* Handle extends in config files

* Fix linting errors

* Bump version

Co-authored-by: Dave Stewart <dave@davestewart.co.uk>

Closes #28 
Closes #30
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