mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-02 02:56:52 +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:
|
||||
- id: trailing-whitespace
|
||||
- id: end-of-file-fixer
|
||||
exclude: testing/resources/pretty_formatted_json_no_endline.json
|
||||
- id: check-yaml
|
||||
- id: debug-statements
|
||||
- id: double-quote-string-fixer
|
||||
|
|
|
|||
|
|
@ -27,7 +27,8 @@ def _get_pretty_format(
|
|||
indent=indent,
|
||||
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:
|
||||
|
|
|
|||
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),
|
||||
('non_ascii_pretty_formatted_json.json', 1),
|
||||
('pretty_formatted_json.json', 0),
|
||||
('pretty_formatted_json_no_endline.json', 0),
|
||||
),
|
||||
)
|
||||
def test_main(filename, expected_retval):
|
||||
|
|
@ -36,6 +37,7 @@ def test_main(filename, expected_retval):
|
|||
('unsorted_pretty_formatted_json.json', 0),
|
||||
('non_ascii_pretty_formatted_json.json', 1),
|
||||
('pretty_formatted_json.json', 0),
|
||||
('pretty_formatted_json_no_endline.json', 0),
|
||||
),
|
||||
)
|
||||
def test_unsorted_main(filename, expected_retval):
|
||||
|
|
@ -49,6 +51,7 @@ def test_unsorted_main(filename, expected_retval):
|
|||
('unsorted_pretty_formatted_json.json', 1),
|
||||
('non_ascii_pretty_formatted_json.json', 1),
|
||||
('pretty_formatted_json.json', 1),
|
||||
('pretty_formatted_json_no_endline.json', 1),
|
||||
('tab_pretty_formatted_json.json', 0),
|
||||
),
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue