prantlf / jsonlint

JSON/CJSON/JSON5 parser, syntax & schema validator and pretty-printer with a command-line client, written in pure JavaScript.
http://prantlf.github.io/jsonlint/
MIT License
37 stars 9 forks source link

Empty objects get split by newline with `--pretty-print`, then fails without #19

Open swantzter opened 1 year ago

swantzter commented 1 year ago

Reproduction: https://github.com/swantzter/jsonlint-repro

Hi, we're having an issue where there's inconsistent behaviours between --pretty-print and "just" linting when it comes to empty objects.

When pretty-print serialises an object it outputs {\n} whereas the lint want it to become just {}

Given the above reproduction and test.json:

{
  "obj1": {},
  "obj2": {
  }
}

running npm run lint:fix (jsonlint --in-place --pretty-print) changes the file and outputs the following

{
  "obj1": {
  },
  "obj2": {
  }
}

Running npm run lint (jsonlint) after that makes it complain:

test.json: 1 hunk differs
===================================================================
--- test.json.orig
+++ test.json
@@ -1,6 +1,4 @@
 {
-  "obj1": {
-  },
-  "obj2": {
-  }
+  "obj1": {},
+  "obj2": {}
 }
prantlf commented 2 months ago

Yes, --pretty-print will insert line breaks into empty objects, otherwise they'll be formatted only as two braces - {}.

This file can be checked by jsonlint --check:

{
  "obj1": {},
  "obj2": {}
}

And this file can be checked by jsonlint --check --pretty-print:

{
  "obj1": {
  },
  "obj2": {
  }
}

It's a clumsy workaround. I'll try to come up with some fix.