diff --git a/insight/analyzer.py b/insight/analyzer.py index 111488d..eea7e1f 100644 --- a/insight/analyzer.py +++ b/insight/analyzer.py @@ -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 @@ -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}") diff --git a/tests/test_analyzer.py b/tests/test_analyzer.py new file mode 100644 index 0000000..edb9ab8 --- /dev/null +++ b/tests/test_analyzer.py @@ -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"] == ["."]