Sublime Forum

Missing commas in JSON arrays not detected

#1

When using JSON Syntax with a JSON file, if you remove a comma between two objects of an array no error will be displayed.

If I use Pretty JSON to validate the array it will fail, but this is an additional step that should not be required as those commas are required in a JSON file.

Also, I remove all the packages I had installed so nothing would cause an issue, but the problem persisted

I was not able to find any other topic regarding this. If this has already been reported, please redirect me to the thread

Kind regards

0 Likes

#2

Syntax definitions always need to find a good balance between highlighting syntax errors and keeping highlighting intact, even if code is not yet complete.

What you want seems like a task for a linter. You could try SublimeLinter or LSP & LSP-json to get even more intellisense features.

grafik

3 Likes

#3

Hi mate!

This is what I have right now because the out-of-the-box JSON syntax does not have it.

I understand, that it needs to find the balance, but what it looked weird to me is that whenever you remove a comma from another part of the JSON you get an alert. This is based on you breaking the JSON schema.
The same should occur with a comma in an array, as JSON does not accept spaces between objects or values (Eg.: “key”: 1 2)

image

Also, even when the file is saved, the error does not pop-up.

Anyway, Thanks for your quick reply mate! Really appreciate it

0 Likes

#4

I think it’s not fixed most likely because of historical reason. It will be a giant BC breaking if it’s fixed.

1 Like

#5

The main reason is probably a more (historical) technical one. You’d probably want to highlight the end of line after } illegal if a comma is missing. To do so, you’d need to know what’s the next non-whitespace character which may be found not before the next line. This hasn’t been possible in ST2 and ST3. Noone touched the syntax since then to fix that little short comming as there other more important/major things to get done before.

On the other hand, discussions arose to completely remove all those illegal highlightings as it

  1. often fails in incomplete code scenarios
  2. often requires a lot more effort to get done acceptable right
  3. is probably always behind the accuracy of linters, resulting in inconsistencies because some errors are highlighted by both the syntax and the linters, others only by linters.

There’s a huge PR to update JSON syntaxes by adding more variants and be much more eager with regards to illegal highlighting at https://github.com/sublimehq/Packages/pull/3097. I’ve used it for a while but honestly found it very annoying to see it highlighting code snippets illegal every here and then. So I tend turn into disliking all this kind of stuff.

As the screenshot shows, linters do a much better job here and they can be disabled if not needed.

2 Likes