From 7b5c268daf4da468d60fc839adea320cff7fac41 Mon Sep 17 00:00:00 2001 From: LiDing <72634327+linkedlist771@users.noreply.github.com> Date: Mon, 20 May 2024 21:42:40 +0800 Subject: [PATCH] Feature: Add Py Scirpt Enforce CamelCase Naming Convention in Scala Code In Scala, camelCase is the preferred naming convention for variables, functions, and methods. It enhances code clarity by visually grouping words together and avoiding potential confusion with underscores (which often have special meanings in Scala). --- check_camel_case.py | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 check_camel_case.py diff --git a/check_camel_case.py b/check_camel_case.py new file mode 100644 index 000000000..1ce443ed6 --- /dev/null +++ b/check_camel_case.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 + +import sys +import os +import re + +# Color constants +RED = "\033[0;31m" +GREEN = "\033[0;32m" +RESET = "\033[0m" + +def print_usage(program_name): + print(f"{RED}Usage: {program_name} [--fix]{RESET}") + sys.exit(1) + +def find_snake_case(file_path, fix): + modified_lines = [] + with open(file_path, "r") as file: + for line_num, line in enumerate(file, start=1): + modified_line = line + for match in re.finditer(r"\b[a-z0-9]+_[a-z0-9]+\b", line): + snake_case = match.group() + camel_case = convert_to_camel(snake_case) + print(f"{RED}{file_path}:{GREEN}{line_num}:{GREEN}{match.start()+1}: {RED}{snake_case} is not in camelCase{RESET}") + if fix: + modified_line = modified_line.replace(snake_case, camel_case) + print(f"{GREEN}Fixed: {RED}{snake_case} {GREEN}-> {camel_case}{RESET}") + modified_lines.append(modified_line) + return modified_lines + +def convert_to_camel(snake_str): + parts = snake_str.split("_") + return parts[0] + "".join(part.capitalize() for part in parts[1:]) + +def main(): + if len(sys.argv) < 2: + print_usage(sys.argv[0]) + + directory = sys.argv[1] + fix = "--fix" in sys.argv + + for root, _, files in os.walk(directory): + for file in files: + if file.endswith(".scala"): + file_path = os.path.join(root, file) + modified_content = find_snake_case(file_path, fix) + if fix: + with open(file_path, "w") as file: + file.writelines(modified_content) + +if __name__ == "__main__": + main()