Coverage for patterns/shared.py: 65%

1"""Shared pattern detection utilities."""

2

3 from __future__ import annotations

4

5 from typing import TYPE_CHECKING

6

7 if TYPE_CHECKING:

8 from tree_sitter import Node

9

10

11 def check_patterns(

12 language: str, root: Node, patterns: list[str] | None = None

13) -> list:

14 """Run pattern checks for a language.

15

16 Args:

17 language: Language name ("python", "typescript", "go")

18 root: AST root node

19 patterns: List of specific pattern functions to run, or None for all

20

21 Returns:

22 List of PatternViolation objects

23 """

24 if language == "python":

25 from patterns.python import check_nested_imports, check_wildcard_imports

26

27 pattern_fns = {

28 "check_nested_imports": check_nested_imports,

29 "check_wildcard_imports": check_wildcard_imports,

30 }

31 elif language == "typescript":

32 pattern_fns = {}

33 elif language == "go":

34 pattern_fns = {}

35 else:

36 return []

37

38 if patterns:

39 pattern_fns = {k: v for k, v in pattern_fns.items() if k in patterns}

40

41 violations = []

42 for name, fn in pattern_fns.items():

43 violations.extend(fn(root))

44

45 return violations