mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-29 10:16:52 +00:00
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:
parent
34a14fb7bc
commit
3e9db01775
1 changed files with 11 additions and 0 deletions
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue