From 5802220a751f33e8d60639af924416befdf63f04 Mon Sep 17 00:00:00 2001 From: rami Date: Fri, 1 Mar 2024 17:20:52 +0530 Subject: [PATCH] Added check_naming_convention hook --- pre_commit_hooks/check_naming_convention.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 pre_commit_hooks/check_naming_convention.py diff --git a/pre_commit_hooks/check_naming_convention.py b/pre_commit_hooks/check_naming_convention.py new file mode 100644 index 0000000..52c80e5 --- /dev/null +++ b/pre_commit_hooks/check_naming_convention.py @@ -0,0 +1,20 @@ +import re +import sys +import os + +def check_naming_convention(files): + for file_path in files: + with open(file_path, 'r') as file: + lines = file.readlines() + for line_num, line in enumerate(lines, start=1): + words = re.findall(r'\b[a-zA-Z0-9_]+\b', line) + for word in words: + if re.match(r'^[a-z]+(?:_[a-z]+)*$', word): + continue # Valid snake_case + elif re.match(r'^[A-Z][a-zA-Z0-9]*$', word): + if '_' in word: + print(f"WARNING: CamelCase with underscores found in {os.path.basename(file_path)}:{line_num}: {word}") + continue # Valid CamelCase + else: + print(f"ERROR: Invalid naming convention in {os.path.basename(file_path)}:{line_num}: {word}") + sys.exit(1)