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
15 changes: 14 additions & 1 deletion backend/app/services/ast_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,20 @@ def walk(node, depth):
return issues

def analyze(source: str) -> list[dict]:
tree = ast.parse(source)
try:
tree = ast.parse(source)
except SyntaxError as exc:
return [
_make_issue(
"Syntax Error",
exc.lineno or 1,
f"Python syntax error: {exc.msg}",
"Fix the syntax error before running analysis.",
"error",
""
)
]

issues = []
issues += detect_unreachable_code(tree, source)
issues += detect_unused_imports(tree, source)
Expand Down
7 changes: 7 additions & 0 deletions backend/tests/test_ast_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,10 @@ def test_deep_nesting_exact_boundary():
)
issues = analyze(code)
assert not any(i["type"] == "Deep Nesting" for i in issues)


def test_analyze_handles_syntax_error():
result = analyze("def f(:\n pass")

assert len(result) == 1
assert result[0]["type"] == "Syntax Error"