mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-11 21:54:16 +00:00
pretty-format-json: ignore original newline presence or absence
This commit is contained in:
parent
2d948977cf
commit
10e3730a81
4 changed files with 15 additions and 1 deletions
|
|
@ -4,6 +4,7 @@ repos:
|
||||||
hooks:
|
hooks:
|
||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
- id: end-of-file-fixer
|
- id: end-of-file-fixer
|
||||||
|
exclude: testing/resources/pretty_formatted_json_no_endline.json
|
||||||
- id: check-yaml
|
- id: check-yaml
|
||||||
- id: debug-statements
|
- id: debug-statements
|
||||||
- id: double-quote-string-fixer
|
- id: double-quote-string-fixer
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,8 @@ def _get_pretty_format(
|
||||||
indent=indent,
|
indent=indent,
|
||||||
ensure_ascii=ensure_ascii,
|
ensure_ascii=ensure_ascii,
|
||||||
)
|
)
|
||||||
return f'{json_pretty}\n'
|
finisher = '\n' if contents.endswith('\n') else ''
|
||||||
|
return f'{json_pretty}{finisher}'
|
||||||
|
|
||||||
|
|
||||||
def _autofix(filename: str, new_contents: str) -> None:
|
def _autofix(filename: str, new_contents: str) -> None:
|
||||||
|
|
|
||||||
9
testing/resources/pretty_formatted_json_no_endline.json
Normal file
9
testing/resources/pretty_formatted_json_no_endline.json
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"alist": [
|
||||||
|
2,
|
||||||
|
34,
|
||||||
|
234
|
||||||
|
],
|
||||||
|
"blah": null,
|
||||||
|
"foo": "bar"
|
||||||
|
}
|
||||||
|
|
@ -23,6 +23,7 @@ def test_parse_num_to_int():
|
||||||
('unsorted_pretty_formatted_json.json', 1),
|
('unsorted_pretty_formatted_json.json', 1),
|
||||||
('non_ascii_pretty_formatted_json.json', 1),
|
('non_ascii_pretty_formatted_json.json', 1),
|
||||||
('pretty_formatted_json.json', 0),
|
('pretty_formatted_json.json', 0),
|
||||||
|
('pretty_formatted_json_no_endline.json', 0),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
def test_main(filename, expected_retval):
|
def test_main(filename, expected_retval):
|
||||||
|
|
@ -36,6 +37,7 @@ def test_main(filename, expected_retval):
|
||||||
('unsorted_pretty_formatted_json.json', 0),
|
('unsorted_pretty_formatted_json.json', 0),
|
||||||
('non_ascii_pretty_formatted_json.json', 1),
|
('non_ascii_pretty_formatted_json.json', 1),
|
||||||
('pretty_formatted_json.json', 0),
|
('pretty_formatted_json.json', 0),
|
||||||
|
('pretty_formatted_json_no_endline.json', 0),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
def test_unsorted_main(filename, expected_retval):
|
def test_unsorted_main(filename, expected_retval):
|
||||||
|
|
@ -49,6 +51,7 @@ def test_unsorted_main(filename, expected_retval):
|
||||||
('unsorted_pretty_formatted_json.json', 1),
|
('unsorted_pretty_formatted_json.json', 1),
|
||||||
('non_ascii_pretty_formatted_json.json', 1),
|
('non_ascii_pretty_formatted_json.json', 1),
|
||||||
('pretty_formatted_json.json', 1),
|
('pretty_formatted_json.json', 1),
|
||||||
|
('pretty_formatted_json_no_endline.json', 1),
|
||||||
('tab_pretty_formatted_json.json', 0),
|
('tab_pretty_formatted_json.json', 0),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue