1- # this will count comment lines, since our AST/Parser doesn't include comment lines, this needs to be done in the tokenized output of the lexer
2- # not sure about that first line, im pretty sure like about 200% sure this is analyzing the raw code and not the tokenized code but ok
1+ # this will count comment lines for Python, JavaScript, Ruby, and Go
32# COMMENT LINE IS A LINE THAT EXCLUSIVELY HAS A COMMENT
4- # so like: y = 5 #sets y to 5 IS NOT A COMMENT LINE!!!!!!!!
5- def count_comment_lines (code ):
6- """Count lines that are exclusively comments (no code on the same line)"""
3+ def count_comment_lines (code , lang ):
74 # split the code into lines
85 lines = code .splitlines ()
96 comment_count = 0
107
8+ # Set language-specific comment markers
9+ if lang .lower () == "python" :
10+ single_comment = "#"
11+ multi_start = '"""'
12+ alt_multi_start = "'''"
13+ elif lang .lower () == "javascript" or lang .lower () == "go" :
14+ single_comment = "//"
15+ multi_start = "/*"
16+ elif lang .lower () == "ruby" :
17+ single_comment = "#"
18+ multi_start = "=begin"
19+ else :
20+ raise ValueError (f"Unsupported language: { lang } " )
21+
22+ # Track if we're inside a multi-line comment
23+ in_multi_comment = False
24+
1125 for line in lines :
1226 # Remove leading whitespace
1327 stripped = line .strip ()
14- # Check if this line consists only of a comment
15- if stripped and stripped .startswith ('#' ):
28+
29+ # Skip empty lines
30+ if not stripped :
31+ continue
32+
33+ # Handle multi-line comment blocks
34+ if in_multi_comment :
35+ comment_count += 1
36+ # Check for end of multi-line comment
37+ if lang == "python" and (stripped .endswith ('"""' ) or stripped .endswith ("'''" )):
38+ in_multi_comment = False
39+ elif (lang == "javascript" or lang == "go" ) and "*/" in stripped :
40+ in_multi_comment = False
41+ elif lang == "ruby" and stripped == "=end" :
42+ in_multi_comment = False
43+ continue
44+
45+ # Check for start of multi-line comment
46+ if lang == "python" and (stripped .startswith ('"""' ) or stripped .startswith ("'''" )):
47+ in_multi_comment = True
48+ comment_count += 1
49+ continue
50+ elif (lang == "javascript" or lang == "go" ) and stripped .startswith ("/*" ):
51+ in_multi_comment = True
52+ comment_count += 1
53+ continue
54+ elif lang == "ruby" and stripped == "=begin" :
55+ in_multi_comment = True
56+ comment_count += 1
57+ continue
58+
59+ # Check for single-line comments
60+ if stripped .startswith (single_comment ):
1661 comment_count += 1
1762
1863 return comment_count
0 commit comments