Checker for detecting banned namespace patterns.
| 24 | |
| 25 | |
| 26 | class BannedNamespaceChecker(FileContentChecker): |
| 27 | """Checker for detecting banned namespace patterns.""" |
| 28 | |
| 29 | def __init__(self): |
| 30 | self.violations: dict[str, list[tuple[int, str]]] = {} |
| 31 | |
| 32 | def should_process_file(self, file_path: str) -> bool: |
| 33 | """Check if file should be processed.""" |
| 34 | # Skip build directories and third-party code |
| 35 | skip_patterns = [".build", ".pio", ".venv", "third_party", "vendor"] |
| 36 | if any(pattern in file_path for pattern in skip_patterns): |
| 37 | return False |
| 38 | |
| 39 | # Check C++ source and header files |
| 40 | return file_path.endswith((".cpp", ".h", ".hpp", ".cc")) |
| 41 | |
| 42 | def check_file_content(self, file_content: FileContent) -> list[str]: |
| 43 | """Check file content for banned namespace patterns.""" |
| 44 | # Fast file-level check: skip if no banned namespace keyword in file |
| 45 | if not any(ns in file_content.content for ns in BANNED_NAMESPACES): |
| 46 | return [] |
| 47 | |
| 48 | violations: list[tuple[int, str]] = [] |
| 49 | |
| 50 | for line_num, line in enumerate(file_content.lines, 1): |
| 51 | # Skip empty lines and comments |
| 52 | stripped_line = line.strip() |
| 53 | if not stripped_line or stripped_line.startswith("//"): |
| 54 | continue |
| 55 | |
| 56 | # Remove inline comments for more accurate checking |
| 57 | code_part = line.split("//")[0] |
| 58 | |
| 59 | # Fast first pass: skip regex if no banned namespace keyword in line |
| 60 | if not any(ns in code_part for ns in BANNED_NAMESPACES): |
| 61 | continue |
| 62 | |
| 63 | # Check against all banned namespace patterns (pre-compiled at module level) |
| 64 | for pattern in _BANNED_NS_PATTERNS: |
| 65 | if pattern.search(code_part): |
| 66 | violations.append((line_num, line.strip())) |
| 67 | break # Only report once per line |
| 68 | |
| 69 | if violations: |
| 70 | self.violations[file_content.path] = violations |
| 71 | |
| 72 | return [] # We collect violations internally |
| 73 | |
| 74 | |
| 75 | def main() -> None: |
no outgoing calls
no test coverage detected