Coverage for lintro / parsers / prettier / prettier_parser.py: 86%

22 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-04-03 18:53 +0000

1"""Parser for prettier output. 

2 

3Handles typical Prettier CLI output for --check and --write modes, 

4including ANSI-colored lines produced in CI environments. 

5""" 

6 

7from loguru import logger 

8 

9from lintro.parsers.base_parser import strip_ansi_codes 

10from lintro.parsers.prettier.prettier_issue import PrettierIssue 

11 

12 

13def parse_prettier_output(output: str) -> list[PrettierIssue]: 

14 """Parse prettier output into a list of PrettierIssue objects. 

15 

16 Args: 

17 output: The raw output from prettier 

18 

19 Returns: 

20 List of PrettierIssue objects 

21 """ 

22 issues: list[PrettierIssue] = [] 

23 

24 if not output: 

25 return issues 

26 

27 # Prettier output format when issues are found: 

28 # "Checking formatting..." 

29 # "[warn] path/to/file.js" 

30 # "[warn] Code style issues found in the above file. Run Prettier with --write \ 

31 # to fix." 

32 # Normalize output by stripping ANSI escape sequences to make matching robust 

33 # across different terminals and CI runners. 

34 # Example: "[\x1b[33mwarn\x1b[39m] file.js" -> "[warn] file.js" 

35 normalized_output = strip_ansi_codes(output) 

36 

37 lines = normalized_output.splitlines() 

38 

39 for _i, line in enumerate(lines): 

40 try: 

41 line = line.strip() 

42 if not line: 

43 continue 

44 

45 # Look for [warn] lines that contain file paths 

46 if line.startswith("[warn]") and not line.endswith("fix."): 

47 # Extract the file path from the [warn] line 

48 file_path = line[6:].strip() # Remove "[warn] " prefix 

49 if file_path and not file_path.startswith("Code style issues"): 

50 # Create a generic issue for the file 

51 issues.append( 

52 PrettierIssue( 

53 file=file_path, 

54 line=1, # Prettier doesn't provide specific line numbers 

55 code="FORMAT", 

56 message="Code style issues found", 

57 # Prettier doesn't provide specific column numbers 

58 column=1, 

59 ), 

60 ) 

61 except (IndexError, AttributeError, TypeError) as e: 

62 logger.debug(f"Failed to parse prettier line '{line}': {e}") 

63 continue 

64 

65 return issues