Checker class for Serial.printf usage in example files.
| 10 | |
| 11 | |
| 12 | class SerialPrintfChecker(FileContentChecker): |
| 13 | """Checker class for Serial.printf usage in example files.""" |
| 14 | |
| 15 | def __init__(self): |
| 16 | self.violations: dict[str, list[tuple[int, str]]] = {} |
| 17 | |
| 18 | def should_process_file(self, file_path: str) -> bool: |
| 19 | """Check if file should be processed for Serial.printf usage.""" |
| 20 | # Only check files in examples directory |
| 21 | if not file_path.startswith(str(EXAMPLES_ROOT)): |
| 22 | return False |
| 23 | |
| 24 | # Check file extension - .h, .cpp, .hpp, and .ino files |
| 25 | if not file_path.endswith((".cpp", ".h", ".hpp", ".ino")): |
| 26 | return False |
| 27 | |
| 28 | return True |
| 29 | |
| 30 | def check_file_content(self, file_content: FileContent) -> list[str]: |
| 31 | """Check file content for Serial.printf usage.""" |
| 32 | violations: list[tuple[int, str]] = [] |
| 33 | in_multiline_comment = False |
| 34 | |
| 35 | # Check each line for Serial.printf usage |
| 36 | for line_number, line in enumerate(file_content.lines, 1): |
| 37 | stripped = line.strip() |
| 38 | |
| 39 | # Track multi-line comment state |
| 40 | if "/*" in line: |
| 41 | in_multiline_comment = True |
| 42 | if "*/" in line: |
| 43 | in_multiline_comment = False |
| 44 | continue # Skip the line with closing */ |
| 45 | |
| 46 | # Skip if we're inside a multi-line comment |
| 47 | if in_multiline_comment: |
| 48 | continue |
| 49 | |
| 50 | # Skip single-line comment lines |
| 51 | if stripped.startswith("//"): |
| 52 | continue |
| 53 | |
| 54 | # Remove single-line comment portion before checking |
| 55 | code_part = line.split("//")[0] |
| 56 | |
| 57 | # Check for Serial.printf usage in code portion only |
| 58 | if "Serial.printf" in code_part: |
| 59 | violations.append((line_number, line.strip())) |
| 60 | |
| 61 | # Store violations if any found |
| 62 | if violations: |
| 63 | self.violations[file_content.path] = violations |
| 64 | |
| 65 | return [] # We collect violations internally |
| 66 | |
| 67 | |
| 68 | def main() -> None: |
no outgoing calls
no test coverage detected