Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions insight/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ def extract_code_stats(file_path, content):
in_block_comment = False
for line in lines:
stripped = line.strip()
if stripped.startswith("//"):
if in_block_comment:
comment_count += 1
elif "/*" in line:
in_block_comment = True
comment_count += 1
elif "*/" in line:
in_block_comment = False
if "*/" in line:
in_block_comment = False
elif stripped.startswith("//"):
comment_count += 1
elif in_block_comment:
elif "/*" in line:
comment_count += 1
if "*/" not in line.split("/*", 1)[1]:
in_block_comment = True
stats["comments"] = comment_count
else:
# Default: count lines that look like comments
Expand All @@ -52,14 +52,14 @@ def extract_code_stats(file_path, content):
try:
tree = ast.parse(content)
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
stats["functions"] += 1
elif isinstance(node, ast.ClassDef):
stats["classes"] += 1
elif isinstance(node, ast.Import):
stats["imports"].extend(alias.name for alias in node.names)
elif isinstance(node, ast.ImportFrom):
stats["imports"].append(node.module)
stats["imports"].append(node.module or "." * node.level)
except Exception as e:
logging.warning(f"Could not parse {file_path}: {e}")

Expand Down
25 changes: 25 additions & 0 deletions tests/test_analyzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from insight.analyzer import extract_code_stats


def test_inline_block_comment_does_not_hide_following_code() -> None:
stats = extract_code_stats(
"sample.js",
"int count = 0; /* inline comment */\nint next = 1;\n",
)

assert stats["comments"] == 1


def test_async_functions_are_counted() -> None:
stats = extract_code_stats(
"sample.py",
"async def fetch():\n return 1\n",
)

assert stats["functions"] == 1


def test_relative_imports_are_rendered_without_none_values() -> None:
stats = extract_code_stats("sample.py", "from . import utils\n")

assert stats["imports"] == ["."]