Skip to content

Commit 65e5e39

Browse files
Merge remote-tracking branch 'refs/remotes/origin/empty-object-with-newline' into empty-object-with-newline
2 parents 757f474 + 44843a8 commit 65e5e39

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

pre_commit_hooks/pretty_format_json.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
from collections.abc import Sequence
99
from difflib import unified_diff
1010

11+
1112
def _insert_linebreaks(json_str: str) -> str:
1213
return re.sub(
13-
r'\n(?P<spaces>\s*)(?P<json_key>.*): {}(?P<delim>,??)',
14-
'\n\g<spaces>\g<json_key>: {\n\g<spaces>}\g<delim>',
15-
json_str)
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,
17+
)
18+
1619

1720
def _get_pretty_format(
1821
contents: str,
@@ -110,8 +113,8 @@ def main(argv: Sequence[str] | None = None) -> int:
110113
action='store_true',
111114
dest='empty_object_with_newline',
112115
default=False,
113-
help='Format empty JSON objects to have a linebreak, ' \
114-
+ 'also activates --no-sort-keys',
116+
help='Format empty JSON objects to have a linebreak, ' +
117+
'also activates --no-sort-keys',
115118
)
116119
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
117120
args = parser.parse_args(argv)
@@ -150,7 +153,7 @@ def main(argv: Sequence[str] | None = None) -> int:
150153
diff_output = get_diff(
151154
contents,
152155
pretty_contents,
153-
json_file
156+
json_file,
154157
)
155158
sys.stdout.buffer.write(diff_output.encode())
156159
status = 1

tests/pretty_format_json_test.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,28 +165,30 @@ def test_empty_object_with_newline(tmpdir):
165165
assert ret == 1
166166

167167
# a template to be compared against.
168-
multiline = get_resource_path("empty_object_json_multiline.json")
168+
multiline = get_resource_path('empty_object_json_multiline.json')
169169

170170
# file has empty object with newline => expect fail with default settings
171171
ret = main([str(multiline)])
172172
assert ret == 1
173173

174174
# launch the autofix with empty object with newline support on that file
175175
to_be_formatted_sameline = tmpdir.join(
176-
"not_pretty_formatted_empty_object_json_sameline.json"
176+
'not_pretty_formatted_empty_object_json_sameline.json',
177177
)
178178
shutil.copyfile(str(sameline), str(to_be_formatted_sameline))
179179
ret = main(
180-
["--autofix",
181-
"--empty-object-with-newline",
182-
str(to_be_formatted_sameline)]
180+
[
181+
'--autofix',
182+
'--empty-object-with-newline',
183+
str(to_be_formatted_sameline),
184+
],
183185
)
184186
# it should have formatted it and don't raise an error code
185187
# to not stop the the commit
186188
assert ret == 0
187189

188-
# file was formatted (shouldn't trigger linter with
190+
# file was formatted (shouldn't trigger linter with
189191
# --empty-object-with-newline switch)
190-
ret = main(["--empty-object-with-newline", str(to_be_formatted_sameline)])
192+
ret = main(['--empty-object-with-newline', str(to_be_formatted_sameline)])
191193
assert ret == 0
192194
assert filecmp.cmp(to_be_formatted_sameline, multiline)

0 commit comments

Comments
 (0)