Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ repos:
- repo: meta
hooks:
- id: check-useless-excludes
- repo: local
hooks:
- id: check-json-indentation
name: Check JSON Indentation Consistency
entry: pre-commit/check_json_indentation.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there isn't an existing hook for this, it would be simpler to use an inline pygrep hook.

language: python
files: \.json$
- repo: https://github.com/astral-sh/uv-pre-commit
# uv version.
rev: 0.8.15
Expand Down
8 changes: 8 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- id: check-json-indentation
name: Check JSON Indentation Consistency
description: This hook checks that JSON files do not have mixed tabs and spaces for indentation.
entry: pre-commit/check_json_indentation.py
language: python
files: \.json$
types: [text]
verbose: true
1 change: 1 addition & 0 deletions .taskcluster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ tasks:
- "git clone --quiet ${repository} bugbot &&
cd bugbot &&
git -c advice.detachedHead=false checkout ${head_rev} &&
chmod +x pre-commit/check_json_indentation.py &&
pip install --quiet -e '.[test]' &&
pre-commit run --all-files --show-diff-on-failure &&
tox -e $TOX_ENV &&
Expand Down
34 changes: 34 additions & 0 deletions pre-commit/check_json_indentation.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to create a custom hook? Have you tried finding an off-the-shelf solution?

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python3
import sys


def check_json_indentation(filename):
with open(filename, "r") as file:
lines = file.readlines()

tabs_found = False
spaces_found = False

for line in lines:
stripped_line = line.lstrip()
if stripped_line and line.startswith("\t"):
tabs_found = True
elif stripped_line and line.startswith(" "):
spaces_found = True

if tabs_found and spaces_found:
print(f"Error: {filename} contains mixed tabs and spaces for indentation.")
return False

return True


if __name__ == "__main__":
files_to_check = sys.argv[1:]
exit_code = 0

for json_file in files_to_check:
if not check_json_indentation(json_file):
exit_code = 1

sys.exit(exit_code)