Skip to content

Commit 3e9db01

Browse files
committed
Add logic to print line number of JSON errors
This commit makes the pretty JSON check more verbose when it encounters errors, that way developers can see which lines are causing errors in order to debug.
1 parent 34a14fb commit 3e9db01

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

pre_commit_hooks/pretty_format_json.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
115115
if contents != pretty_contents:
116116
print('File {} is not pretty-formatted'.format(json_file))
117117

118+
contents_by_line = ''.join(contents).split('\n')
119+
pretty_contents_by_line = ''.join(pretty_contents).split('\n')
120+
121+
diff = len(contents_by_line) - len(pretty_contents_by_line)
122+
if diff > 0:
123+
pretty_contents_by_line.extend([''] * diff)
124+
125+
for line_num, line in enumerate(contents_by_line):
126+
if line != pretty_contents_by_line[line_num]:
127+
print('{}:{}'.format(json_file, line_num))
128+
118129
if args.autofix:
119130
_autofix(json_file, pretty_contents)
120131

0 commit comments

Comments
 (0)