Export violations to a JSON file. Args: checker: The IncludePathsChecker instance with violations output_path: Path to write the JSON file to
(checker: IncludePathsChecker, output_path: str)
| 573 | |
| 574 | |
| 575 | def export_violations_json(checker: IncludePathsChecker, output_path: str) -> None: |
| 576 | """Export violations to a JSON file. |
| 577 | |
| 578 | Args: |
| 579 | checker: The IncludePathsChecker instance with violations |
| 580 | output_path: Path to write the JSON file to |
| 581 | """ |
| 582 | import json |
| 583 | |
| 584 | violations_list = [] |
| 585 | for file_path, violations in checker.violations.items(): |
| 586 | for line_num, msg in violations: |
| 587 | # Extract the include path from the message |
| 588 | import re |
| 589 | |
| 590 | match = re.search(r'#include "([^"]+)"', msg) |
| 591 | include_path = match.group(1) if match else "unknown" |
| 592 | |
| 593 | # Extract if it's a header not found or other issue |
| 594 | is_not_found = "Header not found" in msg |
| 595 | |
| 596 | violations_list.append( |
| 597 | { |
| 598 | "file": file_path, |
| 599 | "line": line_num, |
| 600 | "offending_header": include_path, |
| 601 | "type": "header_not_found" if is_not_found else "invalid_path", |
| 602 | "message": msg, |
| 603 | "note": "This might be a system header - consider using #include <header> instead" |
| 604 | if is_not_found |
| 605 | else None, |
| 606 | } |
| 607 | ) |
| 608 | |
| 609 | # Write to JSON file |
| 610 | with open(output_path, "w") as f: |
| 611 | json.dump( |
| 612 | { |
| 613 | "total_violations": len(violations_list), |
| 614 | "files_affected": len(checker.violations), |
| 615 | "violations": violations_list, |
| 616 | }, |
| 617 | f, |
| 618 | indent=2, |
| 619 | ) |
| 620 | |
| 621 | print(f"✅ Violations exported to {output_path}") |
| 622 | |
| 623 | |
| 624 | def main() -> None: |