Skip to content

Commit 922e708

Browse files
fix: Empty json cannot be fixed (#43)
Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
1 parent b636d9a commit 922e708

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/nitpick/blender.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,8 @@ def load(self) -> bool:
673673
if self.path is not None:
674674
self._string = Path(self.path).read_text(encoding="UTF-8")
675675
if self._string is not None:
676-
self._object = flatten_quotes(json.loads(self._string))
676+
s = self._string.strip() or "{}" # preventive measure for empty file
677+
self._object = flatten_quotes(json.loads(s))
677678
if self._object is not None:
678679
# Every file should end with a blank line
679680
self._reformatted = json.dumps(self._object, sort_keys=True, indent=2) + "\n"

src/nitpick/plugins/json.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def enforce_rules(self) -> Iterator[Fuss]:
5959
self.report(SharedViolations.MISSING_VALUES, blender, comparison.missing),
6060
)
6161

62-
if self.autofix and self.dirty and blender:
62+
if self.autofix and self.dirty and blender is not None:
6363
self.file_path.write_text(JsonDoc(obj=unflatten_quotes(blender)).reformatted)
6464

6565
def expected_dict_from_contains_keys(self):
@@ -86,7 +86,7 @@ def report(self, violation: ViolationEnum, blender: JsonDict, change: BaseDoc |
8686
"""Report a violation while optionally modifying the JSON dict."""
8787
if not change:
8888
return
89-
if blender:
89+
if blender is not None:
9090
if violation == SharedViolations.MISSING_VALUES:
9191
self._update_missing_values(blender, change)
9292
else:

tests/test_json.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,41 @@ def test_jsonfile_deprecated(tmp_path):
356356
)
357357
assert len(filtered) == 1
358358
assert issubclass(filtered[0].category, DeprecationWarning)
359+
360+
361+
def test_missing_value_should_be_added_to_empty_json_file_with_no_keys(tmp_path):
362+
"""Missing value should be added to empty json file with just "{}"."""
363+
ProjectMock(tmp_path).style(
364+
"""
365+
["my.json".contains_json]
366+
"some_key" = \"\"\"
367+
"some_value"
368+
\"\"\"
369+
"""
370+
).save_file("my.json", """{}""").api_fix().assert_file_contents(
371+
"my.json",
372+
"""
373+
{
374+
"some_key": "some_value"
375+
}
376+
""",
377+
)
378+
379+
380+
def test_missing_value_should_be_added_to_completely_empty_json_file(tmp_path):
381+
"""Missing value should be added to completely empty json file."""
382+
ProjectMock(tmp_path).style(
383+
"""
384+
["my.json".contains_json]
385+
"some_key" = \"\"\"
386+
"some_value"
387+
\"\"\"
388+
"""
389+
).save_file("my.json", "\n").api_fix().assert_file_contents(
390+
"my.json",
391+
"""
392+
{
393+
"some_key": "some_value"
394+
}
395+
""",
396+
)

0 commit comments

Comments
 (0)