mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-04 11:16:53 +00:00
for pretty-format-json add option: --empty-object-with-newline
This commit is contained in:
parent
61b5f7e6f7
commit
496e23dea6
4 changed files with 82 additions and 1 deletions
|
|
@ -3,10 +3,15 @@ from __future__ import annotations
|
|||
import argparse
|
||||
import json
|
||||
import sys
|
||||
import re
|
||||
|
||||
from difflib import unified_diff
|
||||
from typing import Mapping
|
||||
from typing import Sequence
|
||||
|
||||
def _insert_linebreaks(json_str) -> str:
|
||||
# (?P<spaces>\s*) seems to capture the \n. Hence, there is no need for it in the substitution string
|
||||
return re.sub(r'\n(?P<spaces>\s*)(?P<json_key>.*): {}(?P<delim>,??)', '\n\g<spaces>\g<json_key>: {\n\g<spaces>}\g<delim>', json_str)
|
||||
|
||||
def _get_pretty_format(
|
||||
contents: str,
|
||||
|
|
@ -14,6 +19,7 @@ def _get_pretty_format(
|
|||
ensure_ascii: bool = True,
|
||||
sort_keys: bool = True,
|
||||
top_keys: Sequence[str] = (),
|
||||
empty_object_with_newline: bool = False,
|
||||
) -> str:
|
||||
def pairs_first(pairs: Sequence[tuple[str, str]]) -> Mapping[str, str]:
|
||||
before = [pair for pair in pairs if pair[0] in top_keys]
|
||||
|
|
@ -27,6 +33,8 @@ def _get_pretty_format(
|
|||
indent=indent,
|
||||
ensure_ascii=ensure_ascii,
|
||||
)
|
||||
if empty_object_with_newline:
|
||||
json_pretty = _insert_linebreaks(json_pretty)
|
||||
return f'{json_pretty}\n'
|
||||
|
||||
|
||||
|
|
@ -96,6 +104,13 @@ def main(argv: Sequence[str] | None = None) -> int:
|
|||
default=[],
|
||||
help='Ordered list of keys to keep at the top of JSON hashes',
|
||||
)
|
||||
parser.add_argument(
|
||||
'--empty-object-with-newline',
|
||||
action='store_true',
|
||||
dest='empty_object_with_newline',
|
||||
default=False,
|
||||
help='Format empty JSON objects to have a linebreak, also activates --no-sort-keys',
|
||||
)
|
||||
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
|
|
@ -106,9 +121,12 @@ def main(argv: Sequence[str] | None = None) -> int:
|
|||
contents = f.read()
|
||||
|
||||
try:
|
||||
if args.empty_object_with_newline:
|
||||
args.no_sort_keys = True
|
||||
pretty_contents = _get_pretty_format(
|
||||
contents, args.indent, ensure_ascii=not args.no_ensure_ascii,
|
||||
sort_keys=not args.no_sort_keys, top_keys=args.top_keys,
|
||||
empty_object_with_newline=args.empty_object_with_newline
|
||||
)
|
||||
except ValueError:
|
||||
print(
|
||||
|
|
@ -118,13 +136,15 @@ def main(argv: Sequence[str] | None = None) -> int:
|
|||
return 1
|
||||
|
||||
if contents != pretty_contents:
|
||||
status = 1
|
||||
if args.autofix:
|
||||
_autofix(json_file, pretty_contents)
|
||||
if args.empty_object_with_newline:
|
||||
status = 0
|
||||
else:
|
||||
diff_output = get_diff(contents, pretty_contents, json_file)
|
||||
sys.stdout.buffer.write(diff_output.encode())
|
||||
|
||||
status = 1
|
||||
|
||||
return status
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue