|
| 1 | +"""Check orchestration for Hacktoolkit Flake8 rules.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import ast |
| 6 | + |
| 7 | +from . import datetime as datetime_checks |
| 8 | +from . import debugger, naming, structured |
| 9 | +from .types import Violation |
| 10 | + |
| 11 | + |
| 12 | +class HtkVisitor(ast.NodeVisitor): |
| 13 | + """Collect Hacktoolkit rule violations from a Python AST.""" |
| 14 | + |
| 15 | + def __init__( |
| 16 | + self, |
| 17 | + *, |
| 18 | + filename: str, |
| 19 | + structured_programming_files: tuple[str, ...] = (), |
| 20 | + ) -> None: |
| 21 | + self.filename = filename |
| 22 | + self.structured_programming_files = structured_programming_files |
| 23 | + self.violations: list[Violation] = [] |
| 24 | + self._debugger_state = debugger.DebuggerState() |
| 25 | + |
| 26 | + def visit_Import(self, node: ast.Import) -> None: |
| 27 | + for violation_node, message in debugger.check_import( |
| 28 | + node, |
| 29 | + self._debugger_state, |
| 30 | + ): |
| 31 | + self._add(violation_node, node, message) |
| 32 | + self.generic_visit(node) |
| 33 | + |
| 34 | + def visit_ImportFrom(self, node: ast.ImportFrom) -> None: |
| 35 | + for violation_node, message in datetime_checks.check_import_from(node): |
| 36 | + self._add(violation_node, node, message) |
| 37 | + for violation_node, message in debugger.check_import_from( |
| 38 | + node, |
| 39 | + self._debugger_state, |
| 40 | + ): |
| 41 | + self._add(violation_node, node, message) |
| 42 | + self.generic_visit(node) |
| 43 | + |
| 44 | + def visit_Call(self, node: ast.Call) -> None: |
| 45 | + for violation_node, message in debugger.check_call( |
| 46 | + node, |
| 47 | + self._debugger_state, |
| 48 | + ): |
| 49 | + self._add(violation_node, node, message) |
| 50 | + self.generic_visit(node) |
| 51 | + |
| 52 | + def visit_FunctionDef(self, node: ast.FunctionDef) -> None: |
| 53 | + self._check_function(node) |
| 54 | + self.generic_visit(node) |
| 55 | + |
| 56 | + def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None: |
| 57 | + self._check_function(node) |
| 58 | + self.generic_visit(node) |
| 59 | + |
| 60 | + def _check_function( |
| 61 | + self, |
| 62 | + node: ast.FunctionDef | ast.AsyncFunctionDef, |
| 63 | + ) -> None: |
| 64 | + for violation_node, message in naming.check_function(node): |
| 65 | + self._add(violation_node, node, message) |
| 66 | + |
| 67 | + if not structured.should_check_file( |
| 68 | + self.filename, |
| 69 | + self.structured_programming_files, |
| 70 | + ): |
| 71 | + return |
| 72 | + |
| 73 | + for violation_node, message in structured.check_function(node): |
| 74 | + self._add(violation_node, node, message) |
| 75 | + |
| 76 | + def _add(self, node: ast.AST, fallback: ast.AST, message: str) -> None: |
| 77 | + self.violations.append( |
| 78 | + Violation( |
| 79 | + line=getattr(node, "lineno", getattr(fallback, "lineno", 1)), |
| 80 | + column=getattr( |
| 81 | + node, |
| 82 | + "col_offset", |
| 83 | + getattr(fallback, "col_offset", 0), |
| 84 | + ), |
| 85 | + message=message, |
| 86 | + ) |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +__all__ = ["HtkVisitor", "Violation"] |
0 commit comments