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

Support --fix to fix JSON issues

Open aviranco opened this issue 8 years ago • 9 comments

Currently there is no support for fixing the json errors using --fix flag.

aviranco avatar Sep 26 '16 08:09 aviranco

That's a great idea! Will tag as help wanted in case anyone wants to contribute

azeemba avatar Jan 15 '17 01:01 azeemba

@aviranco That is because this library is not eslint. It's a thin wrapper around jshint and uses that library's default settings. Check the source (and package.json) files of this module.

sirlancelot avatar Jul 02 '17 06:07 sirlancelot

Man listen, I know you are upset that this plugin uses jshint but you need to stop spreading misinformation.

"This library is not eslint" makes no sense. It's an eslint plugin. Most plugins do linting without using eslint (because otherwise there would be no reason for them to exist). Additionally, being able to support --fix is again independent of how the plugin implements linting. jshint doesn't make it easy to add the --fix support but we could still implement it manually or by switching to something like https://github.com/zaach/jsonlint

azeemba avatar Jul 02 '17 15:07 azeemba

@azeemba

  1. I think "I know you are upset" and the "spreading misinformation" bit are both really inappropriate here. @sirlancelot's statement is factual.
  2. Just to clarify - this plugin doesn't apply (or attempt to apply) any ESLint rules to JSON source, right? It literally just invokes a separate library. So if I have a certain setup for indentation, or use eslint-plugin-prettier, this plugin won't feed the JSON into that pipeline but would do its own thing. Correct?

motiz88 avatar Nov 02 '17 13:11 motiz88

This plugin does not attempt to apply general ESLint rules because it should not do so. JSON has stronger requirements than JavaScript.

If we deferred linting to ESLint then a lot of incorrect JSON will be marked as valid (since ESLint is simply following JS rules). Its also possible that if some rules like quote-props: "as-needed"are enabled then valid JSON will be marked as invalid.

For this reason, the JSON parser must be separate from ESLint (unless of course ESLint starts adding native JSON support).

Regarding my tone from the previous post, there is some additional context in #13.

azeemba avatar Nov 04 '17 16:11 azeemba

Hello people. I found a hacky way to help with this issue, but it's still kind of manual. First, you add var x = at the beginning of the .json file you want to fix. (making this a javascript code..) Second, you run eslint -c ./.eslintrc.json --fix your.json and you ensure the rules of the .eslintrc.json has "quote-props": "error" (at least that's my intended purpose, to fix key quotes in json) Voila, it works.. I wish there is another way, I've tried a lot of tools and this is only one I found working. All other tools complain like "expecting STRING but found blabla.." Hope it helps. (don't forget to remove var x = ..)

wang0109 avatar Nov 25 '17 05:11 wang0109

The above will fix { "rests": [ { host: "127.0.0.1" } ] } to { "rests": [ { "host": "127.0.0.1" } ] }

wang0109 avatar Nov 25 '17 05:11 wang0109

Now that we have switched implementation away from JSHint and using VSCode's implementation, doing this might be feasible since the library provides a "format" method out of the box.

eslint's plugin interface doesn't make it easy for the plugins themselves to do the fixes but I believe it should be feasible. If anyone is interested in working on this, let me know and I can work with them to figure it out!

azeemba avatar Nov 26 '18 07:11 azeemba

Are they any plans to add --fix? I started using eslint-plugin-json-format but it is less popular

anthony0030 avatar Mar 26 '20 08:03 anthony0030