Skip to content

Commit 6394898

Browse files
committed
fix formatting issues
1 parent 496e23d commit 6394898

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

pre_commit_hooks/pretty_format_json.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
from typing import Mapping
1010
from typing import Sequence
1111

12-
def _insert_linebreaks(json_str) -> str:
13-
# (?P<spaces>\s*) seems to capture the \n. Hence, there is no need for it in the substitution string
14-
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)
12+
def _insert_linebreaks(json_str: str) -> str:
13+
return re.sub(
14+
r'\n(?P<spaces>\s*)(?P<json_key>.*): {}(?P<delim>,??)',
15+
'\n\g<spaces>\g<json_key>: {\n\g<spaces>}\g<delim>',
16+
json_str)
1517

1618
def _get_pretty_format(
1719
contents: str,
@@ -109,7 +111,8 @@ def main(argv: Sequence[str] | None = None) -> int:
109111
action='store_true',
110112
dest='empty_object_with_newline',
111113
default=False,
112-
help='Format empty JSON objects to have a linebreak, also activates --no-sort-keys',
114+
help='Format empty JSON objects to have a linebreak, ' \
115+
+ 'also activates --no-sort-keys',
113116
)
114117
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
115118
args = parser.parse_args(argv)

tests/pretty_format_json_test.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,24 +146,29 @@ def test_empty_object_with_newline(tmpdir):
146146
ret = main(["--empty-object-with-newline", str(sameline)])
147147
assert ret == 1
148148

149+
# a template to be compared against.
149150
multiline = get_resource_path("empty_object_json_multiline.json")
150-
to_be_formatted_sameline = tmpdir.join(
151-
"not_pretty_formatted_empty_object_json_sameline.json"
152-
)
153-
shutil.copyfile(str(sameline), str(to_be_formatted_sameline))
154151

155152
# file has empty object with newline => expect fail with default settings
156153
ret = main([str(multiline)])
157154
assert ret == 1
158155

159-
# now launch the autofix with empty object with newline support on that file
156+
# launch the autofix with empty object with newline support on that file
157+
to_be_formatted_sameline = tmpdir.join(
158+
"not_pretty_formatted_empty_object_json_sameline.json"
159+
)
160+
shutil.copyfile(str(sameline), str(to_be_formatted_sameline))
160161
ret = main(
161-
["--autofix", "--empty-object-with-newline", str(to_be_formatted_sameline)]
162+
["--autofix",
163+
"--empty-object-with-newline",
164+
str(to_be_formatted_sameline)]
162165
)
163166
# it should have formatted it and don't raise an error code
167+
# to not stop the the commit
164168
assert ret == 0
165169

166-
# file was formatted (shouldn't trigger linter with --empty-object-with-newline switch)
170+
# file was formatted (shouldn't trigger linter with
171+
# --empty-object-with-newline switch)
167172
ret = main(["--empty-object-with-newline", str(to_be_formatted_sameline)])
168173
assert ret == 0
169174
assert filecmp.cmp(to_be_formatted_sameline, multiline)

0 commit comments

Comments
 (0)