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.
This commit is contained in:
Joey Pinhas
2019-08-15 12:32:33 -04:00
parent 34a14fb7bc
commit 3e9db01775

View File

@ -115,6 +115,17 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
if contents != pretty_contents:
print('File {} is not pretty-formatted'.format(json_file))
contents_by_line = ''.join(contents).split('\n')
pretty_contents_by_line = ''.join(pretty_contents).split('\n')
diff = len(contents_by_line) - len(pretty_contents_by_line)
if diff > 0:
pretty_contents_by_line.extend([''] * diff)
for line_num, line in enumerate(contents_by_line):
if line != pretty_contents_by_line[line_num]:
print('{}:{}'.format(json_file, line_num))
if args.autofix:
_autofix(json_file, pretty_contents)