mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-05 04:15:51 +00:00
style: Add pre-commit hook to fix JSON style in C++ code (#2266)
This commit is contained in:
@@ -55,15 +55,30 @@ repos:
|
||||
--ignore-words=pre-commit-hooks/codespell_ignore.txt,
|
||||
]
|
||||
|
||||
# Running fix-local-includes before clang-format
|
||||
# to ensure that the include order is correct.
|
||||
# Running some C++ hooks before clang-format
|
||||
# to ensure that the style is consistent.
|
||||
- repo: local
|
||||
hooks:
|
||||
- id: json-in-cpp
|
||||
name: Fix JSON style in C++
|
||||
entry: pre-commit-hooks/json_in_cpp.py
|
||||
types: [c++]
|
||||
language: python
|
||||
exclude: |
|
||||
(?x)^(
|
||||
tests/unit/etl/SubscriptionSourceTests.cpp|
|
||||
tests/unit/web/ServerTests.cpp|
|
||||
tests/unit/web/impl/ErrorHandlingTests.cpp|
|
||||
tests/unit/web/ng/ServerTests.cpp|
|
||||
tests/unit/web/ng/impl/ErrorHandlingTests.cpp
|
||||
)$
|
||||
|
||||
- id: fix-local-includes
|
||||
name: Fix Local Includes
|
||||
entry: pre-commit-hooks/fix-local-includes.sh
|
||||
types: [c++]
|
||||
language: script
|
||||
|
||||
- repo: https://github.com/pre-commit/mirrors-clang-format
|
||||
rev: f9a52e87b6cdcb01b0a62b8611d9ba9f2dad0067 # frozen: v19.1.7
|
||||
hooks:
|
||||
|
||||
76
pre-commit-hooks/json_in_cpp.py
Executable file
76
pre-commit-hooks/json_in_cpp.py
Executable file
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def fix_json_style(cpp_content: str) -> str:
|
||||
cpp_content = cpp_content.replace('R"json(', 'R"JSON(').replace(')json"', ')JSON"')
|
||||
|
||||
pattern = r'R"JSON\((.*?)\)JSON"'
|
||||
|
||||
def replace_json(match):
|
||||
raw_json = match.group(1)
|
||||
|
||||
raw_json = (
|
||||
raw_json.replace(" :", ":")
|
||||
.replace(" ,", ",")
|
||||
.replace(" null", "null")
|
||||
.replace(':"', ': "')
|
||||
.replace(',"', ', "')
|
||||
.replace('":{', '": {')
|
||||
.replace('":[', '": [')
|
||||
.replace('":true', '": true')
|
||||
.replace('":false', '": false')
|
||||
.replace('":null', '": null')
|
||||
)
|
||||
for digit in range(10):
|
||||
raw_json = raw_json.replace(f'":{digit}', f'": {digit}')
|
||||
return f'R"JSON({raw_json})JSON"'
|
||||
|
||||
return re.sub(pattern, replace_json, cpp_content, flags=re.DOTALL)
|
||||
|
||||
|
||||
def process_file(file_path: Path, dry_run: bool) -> bool:
|
||||
content = file_path.read_text(encoding="utf-8")
|
||||
new_content = fix_json_style(content)
|
||||
|
||||
if new_content != content:
|
||||
print(f"Processing file: {file_path}")
|
||||
if dry_run:
|
||||
print("Dry run: changes won't be written to the file.")
|
||||
else:
|
||||
print("Writing changes to file.")
|
||||
file_path.write_text(new_content, encoding="utf-8")
|
||||
return new_content == content
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Fix JSON style in C++ files",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--dry-run",
|
||||
default=False,
|
||||
action="store_true",
|
||||
help="Don't modify files, just print what would be changed",
|
||||
)
|
||||
parser.add_argument(
|
||||
"files",
|
||||
nargs="*",
|
||||
help="Specific files to process",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
success = True
|
||||
for file in args.files:
|
||||
success = success and process_file(Path(file), dry_run=args.dry_run)
|
||||
if not success:
|
||||
print("Errors occurred while processing files.")
|
||||
exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user