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

Cannot parse or format JSON files in VScode, or CLI #154

Open
thany opened this issue Feb 17, 2022 · 5 comments
Open

Cannot parse or format JSON files in VScode, or CLI #154

thany opened this issue Feb 17, 2022 · 5 comments
Labels
documentation Improvements or additions to documentation help wanted Extra attention is needed

Comments

@thany
Copy link

thany commented Feb 17, 2022

So I've done the following:

1. Install the package:

npm i -D eslint-plugin-jsonc`

2. Added only to the extends section in my .eslintrc.json, like so:

  "extends": [
    "eslint:recommended",
    "plugin:jsonc/recommended-with-jsonc",
    "next"
  ],

I did not configure anything else there. Just this!
And yes, it's a Next.js project. Shouldn't matter, but there you go.

3. Added the following configuration to my workspace settings: (in ./.vscode/settings.json)

  "eslint.enable": true,
  "eslint.useESLintClass": true,
  "eslint.alwaysShowStatus": true,
  "editor.codeActionsOnSave": {
    "source.fixAll": false,
    "source.fixAll.eslint": true
  },
  "[json]": {
    "editor.formatOnSave": true,
    "editor.defaultFormatter": "dbaeumer.vscode-eslint"
  },
  "[jsonc]": {
    "editor.formatOnSave": true,
    "editor.defaultFormatter": "dbaeumer.vscode-eslint"
  },
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    "typescript",
    "typescriptreact",
    "json",
    "jsonc"
  ],

And restarted VSCode.
(It doesn't actually matter where (workspace/user settings) or if at all this is added, as you'll see further on. It'll break on the commandline before anything else, so I might as well leave this whole step out, frankly. I'm going to leave it in for completeness though.)

4. Time to test it out!

So I've added a dead simple file, called test.json:

{
  "foo": "bar"
}

Results

On the commandline

First I installed eslint globally:

npm i -g eslint

When executing eslint test.json I get the following error:

D:\[redacted]\test.json
  2:7  error  Parsing error: D:\[redacted]\test.json: Missing semicolon. (2:7)

  1 | {
> 2 |   "foo": "bar"
    |        ^
  3 | }
  4 |

✖ 1 problem (1 error, 0 warnings)

Semicolons in JSON 🤨 It doesn't feel quite right to me...

In VSCode

Opening that marvellously simple test file again, I get the same error on the problems panel.

When pressing Ctrl+S, I get a message on the status bar that says:

Extension 'ESLint' is configured as formatter but it cannot format `JSON`-files

This makes sense given that ESLint cannot even seem to parse a super simple json file... If it can't parse them properly, how could it ever format them, right?

What's going on? It's probably some stupid setting or a tiny little thing I've missed, because I can't believe this package being utterly broken. It doesn't feel like that kind of package 😊 It feels professional 😎 So maybe something is borking things up 🤔

@ota-meshi
Copy link
Owner

Thank you for posting issue.
I don't know the cause of your problem. Can you share your repository that can reproduce the problem?

@ota-meshi ota-meshi added the needs repro Need a repository that can reproduce the problem label Feb 17, 2022
@thany
Copy link
Author

thany commented Feb 17, 2022

It's not on github yet, I'll push it within a few days when I've set up some things.

@thany
Copy link
Author

thany commented Feb 17, 2022

And here's a simplified reproduction repo:
https://github.com/thany/jsonc-error-repro

If I did it right, here's how to reproduce:

  1. Checkout the repo
  2. Run npm i on it
  3. Install eslint globally: npm i -g eslint
  4. Run it against the test.json: eslint test.json

Unfortunately, Next.js's built-in lint command doesn't test that file. Don't know why, and it doesn't matter. But that the only reason you'll need eslint available as a CLI command.


I did some further testing myself, and it seems removing next from the extend array makes the error go away. But I really need that one in there, because of the nature of the project.

Also when I move plugin:jsonc/recommended-with-jsonc to the end of the extend array, the error also goes away. But that too seems only a temporary workaround: let's say I wanted to include another plugin, which also needs to be last in the list. There can be only one last.

@ota-meshi
Copy link
Owner

Thank you for sharing the repository. I looked it up.
It seems that the parser configured with eslint-config-next is being used.
As you did, if you put plugin: jsonc/recommended-with-jsoncat the end of extends will cause the .json file to select the json parser.
If you don't want to change the order of extends, you need to add parser settings.
https://ota-meshi.github.io/eslint-plugin-jsonc/user-guide/#parser-configuration

@thany
Copy link
Author

thany commented Feb 22, 2022

Thanks for looking into it.

I think I'd rather add parser configuration. Messing with plugin order theoretically shouldn't matter, but in practice it sometimes does matter. It might be helpful for future users to add a paragraph about Next.js to the documentation describing how to prevent this issue.

@ota-meshi ota-meshi added documentation Improvements or additions to documentation help wanted Extra attention is needed and removed needs repro Need a repository that can reproduce the problem labels Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants