From 3e9db01775648dc08689dd1bff3da2d54fc8ffda Mon Sep 17 00:00:00 2001 From: Joey Pinhas Date: Thu, 15 Aug 2019 12:32:33 -0400 Subject: [PATCH] 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. --- pre_commit_hooks/pretty_format_json.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pre_commit_hooks/pretty_format_json.py b/pre_commit_hooks/pretty_format_json.py index e734ca8..f42630a 100644 --- a/pre_commit_hooks/pretty_format_json.py +++ b/pre_commit_hooks/pretty_format_json.py @@ -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)