-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommentpilot.py
More file actions
1202 lines (1047 loc) Β· 43.4 KB
/
commentpilot.py
File metadata and controls
1202 lines (1047 loc) Β· 43.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
CommentPilot - Lightweight Code Comment Quality Intelligent Analysis Engine
θ½»ι级代η 注ι质ιζΊθ½εζεΌζ
A zero-dependency CLI tool for analyzing code comment quality, coverage,
and detecting missing or outdated comments across multiple programming languages.
Author: CommentPilot Team
License: MIT
Version: 1.0.0
"""
import argparse
import ast
import json
import os
import re
import sys
from dataclasses import dataclass, field, asdict
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Optional, Set, Tuple, Any
from collections import defaultdict
from enum import Enum
import hashlib
__version__ = "1.0.0"
__author__ = "CommentPilot Team"
class CommentType(Enum):
"""Types of comments"""
SINGLE_LINE = "single_line"
MULTI_LINE = "multi_line"
DOCSTRING = "docstring"
TODO = "todo"
FIXME = "fixme"
XXX = "xxx"
HACK = "hack"
NOTE = "note"
class Language(Enum):
"""Supported programming languages"""
PYTHON = "python"
JAVASCRIPT = "javascript"
TYPESCRIPT = "typescript"
GO = "go"
JAVA = "java"
RUST = "rust"
C = "c"
CPP = "cpp"
CSHARP = "csharp"
PHP = "php"
RUBY = "ruby"
SWIFT = "swift"
KOTLIN = "kotlin"
SCALA = "scala"
LUA = "lua"
SHELL = "shell"
UNKNOWN = "unknown"
@dataclass
class CommentInfo:
"""Information about a single comment"""
file_path: str
line_start: int
line_end: int
comment_type: CommentType
content: str
is_docstring: bool = False
associated_element: Optional[str] = None # function/class/variable name
element_type: Optional[str] = None # function/class/module
quality_score: float = 0.0
issues: List[str] = field(default_factory=list)
@dataclass
class CodeElement:
"""Information about a code element (function, class, etc.)"""
name: str
element_type: str # function, class, method, module
line_start: int
line_end: int
has_comment: bool = False
comment: Optional[CommentInfo] = None
complexity: int = 1
parameters: List[str] = field(default_factory=list)
docstring_content: Optional[str] = None
@dataclass
class FileAnalysisResult:
"""Analysis result for a single file"""
file_path: str
language: Language
total_lines: int
code_lines: int
comment_lines: int
blank_lines: int
coverage_ratio: float
elements: List[CodeElement] = field(default_factory=list)
comments: List[CommentInfo] = field(default_factory=list)
todos: List[CommentInfo] = field(default_factory=list)
missing_comments: List[CodeElement] = field(default_factory=list)
quality_score: float = 0.0
issues: List[Dict[str, Any]] = field(default_factory=list)
@dataclass
class AnalysisReport:
"""Complete analysis report"""
project_path: str
analyzed_at: str
total_files: int
total_lines: int
total_code_lines: int
total_comment_lines: int
overall_coverage: float
overall_quality_score: float
file_results: List[FileAnalysisResult] = field(default_factory=list)
language_stats: Dict[str, Dict[str, Any]] = field(default_factory=dict)
todo_summary: Dict[str, int] = field(default_factory=dict)
recommendations: List[str] = field(default_factory=list)
class LanguageDetector:
"""Detect programming language from file extension"""
EXTENSION_MAP = {
'.py': Language.PYTHON,
'.js': Language.JAVASCRIPT,
'.mjs': Language.JAVASCRIPT,
'.cjs': Language.JAVASCRIPT,
'.ts': Language.TYPESCRIPT,
'.tsx': Language.TYPESCRIPT,
'.go': Language.GO,
'.java': Language.JAVA,
'.rs': Language.RUST,
'.c': Language.C,
'.h': Language.C,
'.cpp': Language.CPP,
'.hpp': Language.CPP,
'.cc': Language.CPP,
'.cs': Language.CSHARP,
'.php': Language.PHP,
'.rb': Language.RUBY,
'.swift': Language.SWIFT,
'.kt': Language.KOTLIN,
'.kts': Language.KOTLIN,
'.scala': Language.SCALA,
'.lua': Language.LUA,
'.sh': Language.SHELL,
'.bash': Language.SHELL,
'.zsh': Language.SHELL,
}
@classmethod
def detect(cls, file_path: str) -> Language:
"""Detect language from file extension"""
ext = Path(file_path).suffix.lower()
return cls.EXTENSION_MAP.get(ext, Language.UNKNOWN)
class CommentParser:
"""Parse comments from various programming languages"""
# Comment patterns for different languages
PATTERNS = {
Language.PYTHON: {
'single_line': r'#.*$',
'multi_line': r'"""[\s\S]*?"""|\'\'\'[\s\S]*?\'\'\'',
'docstring': r'"""[\s\S]*?"""|\'\'\'[\s\S]*?\'\'\'',
},
Language.JAVASCRIPT: {
'single_line': r'//.*$',
'multi_line': r'/\*[\s\S]*?\*/',
'docstring': r'/\*\*[\s\S]*?\*/',
},
Language.TYPESCRIPT: {
'single_line': r'//.*$',
'multi_line': r'/\*[\s\S]*?\*/',
'docstring': r'/\*\*[\s\S]*?\*/',
},
Language.GO: {
'single_line': r'//.*$',
'multi_line': r'/\*[\s\S]*?\*/',
'docstring': r'//.*$', # Go uses // for doc comments
},
Language.JAVA: {
'single_line': r'//.*$',
'multi_line': r'/\*[\s\S]*?\*/',
'docstring': r'/\*\*[\s\S]*?\*/',
},
Language.RUST: {
'single_line': r'//.*$',
'multi_line': r'/\*[\s\S]*?\*/',
'docstring': r'///.*$|/\*![\s\S]*?\*/',
},
Language.C: {
'single_line': r'//.*$',
'multi_line': r'/\*[\s\S]*?\*/',
'docstring': r'/\*\*[\s\S]*?\*/',
},
Language.CPP: {
'single_line': r'//.*$',
'multi_line': r'/\*[\s\S]*?\*/',
'docstring': r'/\*\*[\s\S]*?\*/',
},
Language.CSHARP: {
'single_line': r'//.*$',
'multi_line': r'/\*[\s\S]*?\*/',
'docstring': r'///.*$|/\*\*[\s\S]*?\*/',
},
Language.PHP: {
'single_line': r'//.*$|#.*$',
'multi_line': r'/\*[\s\S]*?\*/',
'docstring': r'/\*\*[\s\S]*?\*/',
},
Language.RUBY: {
'single_line': r'#.*$',
'multi_line': r'=begin[\s\S]*?=end',
'docstring': r'##[\s\S]*?##',
},
Language.SWIFT: {
'single_line': r'//.*$',
'multi_line': r'/\*[\s\S]*?\*/',
'docstring': r'///.*$',
},
Language.KOTLIN: {
'single_line': r'//.*$',
'multi_line': r'/\*[\s\S]*?\*/',
'docstring': r'/\*\*[\s\S]*?\*/',
},
Language.SCALA: {
'single_line': r'//.*$',
'multi_line': r'/\*[\s\S]*?\*/',
'docstring': r'/\*\*[\s\S]*?\*/',
},
Language.LUA: {
'single_line': r'--.*$',
'multi_line': r'--\[\[[\s\S]*?\]\]',
'docstring': r'---.*$',
},
Language.SHELL: {
'single_line': r'#.*$',
'multi_line': r': << ?\'EOF\'[\s\S]*?EOF|: << ?"EOF"[\s\S]*?EOF',
'docstring': r'##.*$',
},
}
# TODO/FIXME patterns
TODO_PATTERNS = [
(r'(?i)TODO\s*[:\(]?\s*(.+)', CommentType.TODO),
(r'(?i)FIXME\s*[:\(]?\s*(.+)', CommentType.FIXME),
(r'(?i)XXX\s*[:\(]?\s*(.+)', CommentType.XXX),
(r'(?i)HACK\s*[:\(]?\s*(.+)', CommentType.HACK),
(r'(?i)NOTE\s*[:\(]?\s*(.+)', CommentType.NOTE),
]
@classmethod
def parse_comments(cls, content: str, language: Language) -> List[CommentInfo]:
"""Parse all comments from source code"""
comments = []
lines = content.split('\n')
if language not in cls.PATTERNS:
return comments
patterns = cls.PATTERNS[language]
# Parse single-line comments
single_pattern = patterns.get('single_line', '')
if single_pattern:
for i, line in enumerate(lines, 1):
for match in re.finditer(single_pattern, line, re.MULTILINE):
comment_text = match.group(0)
comment_info = cls._create_comment_info(
line, i, i, comment_text, CommentType.SINGLE_LINE
)
comments.append(comment_info)
# Parse multi-line comments
multi_pattern = patterns.get('multi_line', '')
if multi_pattern:
for match in re.finditer(multi_pattern, content, re.MULTILINE):
start_line = content[:match.start()].count('\n') + 1
end_line = start_line + match.group(0).count('\n')
comment_text = match.group(0)
# Check if it's a docstring
is_docstring = comment_text.startswith('/**') or comment_text.startswith('"""') or comment_text.startswith("'''")
comment_type = CommentType.DOCSTRING if is_docstring else CommentType.MULTI_LINE
comment_info = cls._create_comment_info(
"", start_line, end_line, comment_text, comment_type
)
comments.append(comment_info)
return comments
@classmethod
def _create_comment_info(cls, file_path: str, line_start: int, line_end: int,
content: str, comment_type: CommentType) -> CommentInfo:
"""Create a CommentInfo object"""
issues = []
quality_score = cls._calculate_comment_quality(content)
# Check for TODOs
for pattern, todo_type in cls.TODO_PATTERNS:
if re.search(pattern, content):
comment_type = todo_type
break
return CommentInfo(
file_path=file_path,
line_start=line_start,
line_end=line_end,
comment_type=comment_type,
content=content,
quality_score=quality_score,
issues=issues
)
@classmethod
def _calculate_comment_quality(cls, content: str) -> float:
"""Calculate comment quality score (0-100)"""
if not content:
return 0.0
score = 50.0 # Base score
# Remove comment markers
clean_content = re.sub(r'^[#/*\s]+|[#/*\s]+$', '', content, flags=re.MULTILINE)
clean_content = clean_content.strip()
if not clean_content:
return 10.0 # Empty comment
# Length bonus (reasonable length)
word_count = len(clean_content.split())
if word_count >= 3:
score += min(20, word_count * 2) # Up to 20 points for length
# Check for meaningful content
if re.search(r'\b(param|return|arg|example|note|warning|raise|throw)\b', clean_content, re.I):
score += 15 # Documentation keywords
# Check for code examples
if re.search(r'`[^`]+`|```', content):
score += 10 # Code examples
# Penalize very short comments
if word_count < 2:
score -= 20
# Penalize repeated characters (like "/////")
if re.search(r'(.)\1{4,}', content):
score -= 15
return max(0, min(100, score))
class PythonAnalyzer:
"""Analyze Python code using AST"""
@staticmethod
def analyze(content: str, file_path: str) -> Tuple[List[CodeElement], List[CommentInfo]]:
"""Analyze Python code for comments and code elements"""
elements = []
comments = []
try:
tree = ast.parse(content)
except SyntaxError:
return elements, comments
lines = content.split('\n')
# Parse module docstring
module_docstring = ast.get_docstring(tree)
if module_docstring:
docstring_line = 1
for i, line in enumerate(lines):
if '"""' in line or "'''" in line:
docstring_line = i + 1
break
elements.append(CodeElement(
name='<module>',
element_type='module',
line_start=1,
line_end=docstring_line,
has_comment=True,
docstring_content=module_docstring
))
# Parse classes and functions
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef) or isinstance(node, ast.AsyncFunctionDef):
docstring = ast.get_docstring(node)
element = CodeElement(
name=node.name,
element_type='function' if not any(isinstance(p, ast.FunctionDef) for p in ast.walk(tree) if p != node and hasattr(p, 'body') and node in getattr(p, 'body', [])) else 'method',
line_start=node.lineno,
line_end=node.end_lineno or node.lineno,
has_comment=docstring is not None,
docstring_content=docstring,
parameters=[arg.arg for arg in node.args.args]
)
elements.append(element)
if docstring:
comments.append(CommentInfo(
file_path=file_path,
line_start=node.lineno + 1,
line_end=node.lineno + docstring.count('\n') + 1,
comment_type=CommentType.DOCSTRING,
content=docstring,
is_docstring=True,
associated_element=node.name,
element_type='function'
))
elif isinstance(node, ast.ClassDef):
docstring = ast.get_docstring(node)
element = CodeElement(
name=node.name,
element_type='class',
line_start=node.lineno,
line_end=node.end_lineno or node.lineno,
has_comment=docstring is not None,
docstring_content=docstring
)
elements.append(element)
if docstring:
comments.append(CommentInfo(
file_path=file_path,
line_start=node.lineno + 1,
line_end=node.lineno + docstring.count('\n') + 1,
comment_type=CommentType.DOCSTRING,
content=docstring,
is_docstring=True,
associated_element=node.name,
element_type='class'
))
# Parse inline comments
for i, line in enumerate(lines, 1):
if '#' in line:
# Find the comment part
comment_match = re.search(r'#.*$', line)
if comment_match:
comment_text = comment_match.group(0)
comments.append(CommentInfo(
file_path=file_path,
line_start=i,
line_end=i,
comment_type=CommentType.SINGLE_LINE,
content=comment_text
))
return elements, comments
class GenericAnalyzer:
"""Generic analyzer for non-Python languages"""
@staticmethod
def analyze(content: str, file_path: str, language: Language) -> Tuple[List[CodeElement], List[CommentInfo]]:
"""Analyze code for comments and code elements"""
elements = []
comments = CommentParser.parse_comments(content, language)
lines = content.split('\n')
# Detect functions and classes using regex
if language in [Language.JAVASCRIPT, Language.TYPESCRIPT]:
# JavaScript/TypeScript patterns
func_pattern = r'(?:function\s+(\w+)|(?:const|let|var)\s+(\w+)\s*=\s*(?:async\s*)?\(|(\w+)\s*:\s*(?:async\s*)?\()'
class_pattern = r'class\s+(\w+)'
for i, line in enumerate(lines, 1):
for match in re.finditer(func_pattern, line):
name = match.group(1) or match.group(2) or match.group(3)
elements.append(CodeElement(
name=name,
element_type='function',
line_start=i,
line_end=i
))
for match in re.finditer(class_pattern, line):
elements.append(CodeElement(
name=match.group(1),
element_type='class',
line_start=i,
line_end=i
))
elif language == Language.GO:
# Go patterns
func_pattern = r'func\s+(?:\([^)]+\)\s*)?(\w+)\s*\('
struct_pattern = r'type\s+(\w+)\s+struct'
for i, line in enumerate(lines, 1):
for match in re.finditer(func_pattern, line):
elements.append(CodeElement(
name=match.group(1),
element_type='function',
line_start=i,
line_end=i
))
for match in re.finditer(struct_pattern, line):
elements.append(CodeElement(
name=match.group(1),
element_type='struct',
line_start=i,
line_end=i
))
elif language == Language.JAVA:
# Java patterns
method_pattern = r'(?:public|private|protected)?\s*(?:static)?\s*\w+\s+(\w+)\s*\('
class_pattern = r'(?:public\s+)?class\s+(\w+)'
for i, line in enumerate(lines, 1):
for match in re.finditer(method_pattern, line):
name = match.group(1)
if name not in ['if', 'for', 'while', 'switch', 'catch']:
elements.append(CodeElement(
name=name,
element_type='method',
line_start=i,
line_end=i
))
for match in re.finditer(class_pattern, line):
elements.append(CodeElement(
name=match.group(1),
element_type='class',
line_start=i,
line_end=i
))
elif language == Language.RUST:
# Rust patterns
func_pattern = r'(?:pub\s+)?fn\s+(\w+)'
struct_pattern = r'(?:pub\s+)?struct\s+(\w+)'
for i, line in enumerate(lines, 1):
for match in re.finditer(func_pattern, line):
elements.append(CodeElement(
name=match.group(1),
element_type='function',
line_start=i,
line_end=i
))
for match in re.finditer(struct_pattern, line):
elements.append(CodeElement(
name=match.group(1),
element_type='struct',
line_start=i,
line_end=i
))
# Associate comments with elements
for element in elements:
# Check if there's a comment just before the element
for comment in comments:
if comment.line_end == element.line_start - 1 or comment.line_start == element.line_start + 1:
if comment.comment_type in [CommentType.DOCSTRING, CommentType.MULTI_LINE]:
element.has_comment = True
element.comment = comment
comment.associated_element = element.name
comment.element_type = element.element_type
break
return elements, comments
class QualityAnalyzer:
"""Analyze comment quality and detect issues"""
@staticmethod
def analyze_file(file_result: FileAnalysisResult) -> None:
"""Analyze comment quality for a file"""
issues = []
# Check for missing comments on public elements
for element in file_result.elements:
if not element.has_comment:
# Only flag functions and classes
if element.element_type in ['function', 'class', 'method', 'struct']:
# Check if it's a simple getter/setter
if element.element_type == 'function' and element.name:
if element.name.startswith('get_') or element.name.startswith('set_'):
continue # Skip simple getters/setters
file_result.missing_comments.append(element)
issues.append({
'type': 'missing_comment',
'element': element.name,
'element_type': element.element_type,
'line': element.line_start,
'message': f"Missing comment for {element.element_type} '{element.name}'"
})
# Check for low-quality comments
for comment in file_result.comments:
if comment.quality_score < 30:
issues.append({
'type': 'low_quality',
'line': comment.line_start,
'message': f"Low quality comment (score: {comment.quality_score:.1f})",
'content': comment.content[:50] + '...' if len(comment.content) > 50 else comment.content
})
# Check for TODO/FIXME items
for comment in file_result.comments:
if comment.comment_type in [CommentType.TODO, CommentType.FIXME, CommentType.XXX, CommentType.HACK]:
file_result.todos.append(comment)
file_result.issues = issues
# Calculate quality score
if file_result.elements:
commented_count = sum(1 for e in file_result.elements if e.has_comment)
file_result.quality_score = (commented_count / len(file_result.elements)) * 100
else:
file_result.quality_score = 100.0
class CommentPilot:
"""Main CommentPilot analyzer class"""
def __init__(self, project_path: str, exclude_patterns: List[str] = None):
self.project_path = Path(project_path).resolve()
self.exclude_patterns = exclude_patterns or [
'node_modules', '.git', '__pycache__', 'venv', '.venv',
'dist', 'build', '.idea', '.vscode', 'target', 'vendor',
'*.min.js', '*.min.css', 'migrations', '.tox', 'env'
]
def should_analyze(self, file_path: Path) -> bool:
"""Check if a file should be analyzed"""
# Check extension
if LanguageDetector.detect(str(file_path)) == Language.UNKNOWN:
return False
# Check exclude patterns
path_str = str(file_path)
for pattern in self.exclude_patterns:
if pattern.startswith('*'):
if path_str.endswith(pattern[1:]):
return False
elif pattern in path_str:
return False
return True
def analyze_file(self, file_path: Path) -> Optional[FileAnalysisResult]:
"""Analyze a single file"""
language = LanguageDetector.detect(str(file_path))
if language == Language.UNKNOWN:
return None
try:
content = file_path.read_text(encoding='utf-8', errors='ignore')
except Exception:
return None
lines = content.split('\n')
total_lines = len(lines)
# Count different line types
code_lines = 0
comment_lines = 0
blank_lines = 0
in_multiline_comment = False
for line in lines:
stripped = line.strip()
if not stripped:
blank_lines += 1
continue
# Check for multiline comments
if language in [Language.PYTHON]:
if '"""' in stripped or "'''" in stripped:
in_multiline_comment = not in_multiline_comment
comment_lines += 1
continue
elif language in [Language.JAVASCRIPT, Language.TYPESCRIPT, Language.JAVA,
Language.C, Language.CPP, Language.CSHARP, Language.PHP,
Language.KOTLIN, Language.SCALA, Language.RUST, Language.GO]:
if stripped.startswith('/*') or stripped.endswith('*/'):
in_multiline_comment = not in_multiline_comment
comment_lines += 1
continue
if in_multiline_comment:
comment_lines += 1
continue
# Check for single-line comments
if language == Language.PYTHON:
if stripped.startswith('#'):
comment_lines += 1
continue
elif language in [Language.JAVASCRIPT, Language.TYPESCRIPT, Language.JAVA,
Language.C, Language.CPP, Language.CSHARP, Language.PHP,
Language.KOTLIN, Language.SCALA, Language.RUST, Language.GO]:
if stripped.startswith('//') or stripped.startswith('/*'):
comment_lines += 1
continue
elif language == Language.RUBY:
if stripped.startswith('#'):
comment_lines += 1
continue
elif language == Language.LUA:
if stripped.startswith('--'):
comment_lines += 1
continue
elif language == Language.SHELL:
if stripped.startswith('#'):
comment_lines += 1
continue
code_lines += 1
# Analyze code elements and comments
if language == Language.PYTHON:
elements, comments = PythonAnalyzer.analyze(content, str(file_path))
else:
elements, comments = GenericAnalyzer.analyze(content, str(file_path), language)
# Calculate coverage
coverage_ratio = (comment_lines / total_lines * 100) if total_lines > 0 else 0
result = FileAnalysisResult(
file_path=str(file_path.relative_to(self.project_path)),
language=language,
total_lines=total_lines,
code_lines=code_lines,
comment_lines=comment_lines,
blank_lines=blank_lines,
coverage_ratio=coverage_ratio,
elements=elements,
comments=comments
)
# Analyze quality
QualityAnalyzer.analyze_file(result)
return result
def analyze(self) -> AnalysisReport:
"""Analyze entire project"""
file_results = []
language_stats = defaultdict(lambda: {
'files': 0,
'total_lines': 0,
'code_lines': 0,
'comment_lines': 0,
'coverage': 0.0
})
todo_summary = defaultdict(int)
# Find all files to analyze
for file_path in self.project_path.rglob('*'):
if file_path.is_file() and self.should_analyze(file_path):
result = self.analyze_file(file_path)
if result:
file_results.append(result)
# Update language stats
lang_name = result.language.value
language_stats[lang_name]['files'] += 1
language_stats[lang_name]['total_lines'] += result.total_lines
language_stats[lang_name]['code_lines'] += result.code_lines
language_stats[lang_name]['comment_lines'] += result.comment_lines
# Update TODO summary
for todo in result.todos:
todo_summary[todo.comment_type.value] += 1
# Calculate overall stats
total_lines = sum(r.total_lines for r in file_results)
total_code_lines = sum(r.code_lines for r in file_results)
total_comment_lines = sum(r.comment_lines for r in file_results)
overall_coverage = (total_comment_lines / total_lines * 100) if total_lines > 0 else 0
overall_quality = sum(r.quality_score for r in file_results) / len(file_results) if file_results else 0
# Calculate language coverage
for lang, stats in language_stats.items():
stats['coverage'] = (stats['comment_lines'] / stats['total_lines'] * 100) if stats['total_lines'] > 0 else 0
# Generate recommendations
recommendations = self._generate_recommendations(file_results, overall_coverage, overall_quality)
return AnalysisReport(
project_path=str(self.project_path),
analyzed_at=datetime.now().isoformat(),
total_files=len(file_results),
total_lines=total_lines,
total_code_lines=total_code_lines,
total_comment_lines=total_comment_lines,
overall_coverage=overall_coverage,
overall_quality_score=overall_quality,
file_results=file_results,
language_stats=dict(language_stats),
todo_summary=dict(todo_summary),
recommendations=recommendations
)
def _generate_recommendations(self, file_results: List[FileAnalysisResult],
coverage: float, quality: float) -> List[str]:
"""Generate improvement recommendations"""
recommendations = []
if coverage < 10:
recommendations.append("π¨ Critical: Comment coverage is very low (<10%). Consider adding documentation to key files.")
elif coverage < 20:
recommendations.append("β οΈ Warning: Comment coverage is low (<20%). Add comments to improve code maintainability.")
elif coverage < 30:
recommendations.append("π Notice: Comment coverage could be improved. Focus on documenting public APIs.")
else:
recommendations.append("β
Good: Comment coverage is reasonable.")
if quality < 50:
recommendations.append("π Quality Alert: Many elements lack proper documentation. Review missing comments.")
elif quality < 70:
recommendations.append("π Quality Notice: Some elements need better documentation.")
# Check for TODOs
total_todos = sum(len(r.todos) for r in file_results)
if total_todos > 10:
recommendations.append(f"π Found {total_todos} TODO/FIXME items. Consider addressing them.")
elif total_todos > 0:
recommendations.append(f"π Found {total_todos} TODO/FIXME items to review.")
# Check for files with no comments
no_comment_files = [r for r in file_results if r.coverage_ratio == 0 and r.code_lines > 50]
if no_comment_files:
recommendations.append(f"π {len(no_comment_files)} files with >50 lines have no comments.")
return recommendations
class ReportFormatter:
"""Format analysis reports in various formats"""
@staticmethod
def to_json(report: AnalysisReport, indent: int = 2) -> str:
"""Convert report to JSON"""
def dataclass_to_dict(obj):
if hasattr(obj, '__dataclass_fields__'):
result = {}
for key, value in asdict(obj).items():
if isinstance(value, Enum):
result[key] = value.value
elif isinstance(value, list):
result[key] = [dataclass_to_dict(item) for item in value]
elif isinstance(value, dict):
result[key] = {k: dataclass_to_dict(v) for k, v in value.items()}
else:
result[key] = value
return result
return obj
return json.dumps(dataclass_to_dict(report), indent=indent, ensure_ascii=False)
@staticmethod
def to_markdown(report: AnalysisReport) -> str:
"""Convert report to Markdown"""
lines = [
"# π CommentPilot Analysis Report",
"",
f"**Project**: `{report.project_path}`",
f"**Analyzed**: {report.analyzed_at}",
"",
"## π Summary",
"",
f"| Metric | Value |",
f"|--------|-------|",
f"| Total Files | {report.total_files} |",
f"| Total Lines | {report.total_lines:,} |",
f"| Code Lines | {report.total_code_lines:,} |",
f"| Comment Lines | {report.total_comment_lines:,} |",
f"| Coverage | {report.overall_coverage:.1f}% |",
f"| Quality Score | {report.overall_quality_score:.1f}/100 |",
"",
"## π Language Statistics",
"",
"| Language | Files | Lines | Coverage |",
"|----------|-------|-------|----------|",
]
for lang, stats in sorted(report.language_stats.items(), key=lambda x: x[1]['files'], reverse=True):
lines.append(f"| {lang} | {stats['files']} | {stats['total_lines']:,} | {stats['coverage']:.1f}% |")
if report.todo_summary:
lines.extend([
"",
"## π TODO Summary",
"",
"| Type | Count |",
"|------|-------|",
])
for todo_type, count in report.todo_summary.items():
lines.append(f"| {todo_type.upper()} | {count} |")
lines.extend([
"",
"## π‘ Recommendations",
"",
])
for rec in report.recommendations:
lines.append(f"- {rec}")
# Top files needing attention
files_needing_attention = sorted(
[r for r in report.file_results if r.missing_comments],
key=lambda x: len(x.missing_comments),
reverse=True
)[:10]
if files_needing_attention:
lines.extend([
"",
"## π Files Needing Attention",
"",
"| File | Missing Comments | Coverage |",
"|------|-----------------|----------|",
])
for f in files_needing_attention:
lines.append(f"| `{f.file_path}` | {len(f.missing_comments)} | {f.coverage_ratio:.1f}% |")
return '\n'.join(lines)
@staticmethod
def to_html(report: AnalysisReport) -> str:
"""Convert report to HTML"""
return f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CommentPilot Report</title>
<style>
:root {{
--primary: #4F46E5;
--success: #10B981;
--warning: #F59E0B;
--danger: #EF4444;
--bg: #F9FAFB;
--card: #FFFFFF;
--text: #1F2937;
}}
body {{
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: var(--bg);
color: var(--text);
margin: 0;
padding: 20px;
}}
.container {{
max-width: 1200px;
margin: 0 auto;
}}
h1 {{
color: var(--primary);
border-bottom: 3px solid var(--primary);
padding-bottom: 10px;
}}
.stats-grid {{
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
margin: 20px 0;
}}
.stat-card {{
background: var(--card);
border-radius: 12px;
padding: 20px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}}
.stat-value {{
font-size: 2em;
font-weight: bold;