Update app/modules/analyzer_base.py
Browse files- app/modules/analyzer_base.py +15 -46
app/modules/analyzer_base.py
CHANGED
|
@@ -6,58 +6,36 @@ from typing import List, Dict, Any
|
|
| 6 |
|
| 7 |
class AnalysisResult:
|
| 8 |
"""Represents the result of code analysis."""
|
| 9 |
-
|
| 10 |
-
def __init__(self, language: str, issues: List[Dict[str, Any]] = None,
|
| 11 |
raw_output: str = "", error: str = ""):
|
| 12 |
self.language = language
|
| 13 |
self.issues = issues or []
|
| 14 |
self.raw_output = raw_output
|
| 15 |
self.error = error
|
| 16 |
-
|
|
|
|
| 17 |
def to_dict(self) -> dict:
|
| 18 |
-
"""Convert to dictionary for serialization."""
|
| 19 |
return {
|
| 20 |
"language": self.language,
|
| 21 |
"issues": self.issues,
|
| 22 |
-
"total_issues":
|
| 23 |
"error": self.error,
|
| 24 |
}
|
| 25 |
|
| 26 |
|
| 27 |
class CodeAnalyzer(ABC):
|
| 28 |
"""Base class for language-specific code analyzers."""
|
| 29 |
-
|
| 30 |
def __init__(self, language: str):
|
| 31 |
self.language = language
|
| 32 |
-
|
| 33 |
@abstractmethod
|
| 34 |
def analyze(self, code: str) -> AnalysisResult:
|
| 35 |
-
"""
|
| 36 |
-
Analyze code and return issues found.
|
| 37 |
-
|
| 38 |
-
Args:
|
| 39 |
-
code: Source code to analyze
|
| 40 |
-
|
| 41 |
-
Returns:
|
| 42 |
-
AnalysisResult with found issues
|
| 43 |
-
"""
|
| 44 |
pass
|
| 45 |
-
|
| 46 |
-
def format_issue(self, line: int, symbol: str, message: str,
|
| 47 |
-
|
| 48 |
-
"""
|
| 49 |
-
Format an issue in standardized way.
|
| 50 |
-
|
| 51 |
-
Args:
|
| 52 |
-
line: Line number where issue occurs
|
| 53 |
-
symbol: Issue code/symbol (e.g., 'E501', 'unused-variable')
|
| 54 |
-
message: Human-readable message
|
| 55 |
-
category: Category (correctness, performance, security, style)
|
| 56 |
-
severity: Severity level (high, medium, low)
|
| 57 |
-
|
| 58 |
-
Returns:
|
| 59 |
-
Formatted issue dictionary
|
| 60 |
-
"""
|
| 61 |
return {
|
| 62 |
"line": line,
|
| 63 |
"symbol": symbol,
|
|
@@ -65,27 +43,18 @@ class CodeAnalyzer(ABC):
|
|
| 65 |
"category": category,
|
| 66 |
"severity": severity,
|
| 67 |
}
|
| 68 |
-
|
| 69 |
@staticmethod
|
| 70 |
def create_analyzer(language: str) -> 'CodeAnalyzer':
|
| 71 |
-
"""
|
| 72 |
-
Factory method to create appropriate analyzer for language.
|
| 73 |
-
|
| 74 |
-
Args:
|
| 75 |
-
language: Language identifier (e.g., 'python', 'javascript')
|
| 76 |
-
|
| 77 |
-
Returns:
|
| 78 |
-
CodeAnalyzer instance for the language
|
| 79 |
-
"""
|
| 80 |
from app.modules.analyzer_python import PythonAnalyzer
|
| 81 |
from app.modules.analyzer_javascript import JavaScriptAnalyzer
|
| 82 |
from app.modules.analyzer_java import JavaAnalyzer
|
| 83 |
from app.modules.analyzer_llm_fallback import LLMFallbackAnalyzer
|
| 84 |
-
|
| 85 |
analyzers = {
|
| 86 |
"python": PythonAnalyzer,
|
| 87 |
"javascript": JavaScriptAnalyzer,
|
| 88 |
-
"typescript": JavaScriptAnalyzer,
|
| 89 |
"java": JavaAnalyzer,
|
| 90 |
"go": LLMFallbackAnalyzer,
|
| 91 |
"rust": LLMFallbackAnalyzer,
|
|
@@ -93,6 +62,6 @@ class CodeAnalyzer(ABC):
|
|
| 93 |
"c": LLMFallbackAnalyzer,
|
| 94 |
"csharp": LLMFallbackAnalyzer,
|
| 95 |
}
|
| 96 |
-
|
| 97 |
analyzer_class = analyzers.get(language, LLMFallbackAnalyzer)
|
| 98 |
-
return analyzer_class(language)
|
|
|
|
| 6 |
|
| 7 |
class AnalysisResult:
|
| 8 |
"""Represents the result of code analysis."""
|
| 9 |
+
|
| 10 |
+
def __init__(self, language: str, issues: List[Dict[str, Any]] = None,
|
| 11 |
raw_output: str = "", error: str = ""):
|
| 12 |
self.language = language
|
| 13 |
self.issues = issues or []
|
| 14 |
self.raw_output = raw_output
|
| 15 |
self.error = error
|
| 16 |
+
self.total_issues = len(self.issues) # FIX: exposed as attribute, not just in to_dict
|
| 17 |
+
|
| 18 |
def to_dict(self) -> dict:
|
|
|
|
| 19 |
return {
|
| 20 |
"language": self.language,
|
| 21 |
"issues": self.issues,
|
| 22 |
+
"total_issues": self.total_issues,
|
| 23 |
"error": self.error,
|
| 24 |
}
|
| 25 |
|
| 26 |
|
| 27 |
class CodeAnalyzer(ABC):
|
| 28 |
"""Base class for language-specific code analyzers."""
|
| 29 |
+
|
| 30 |
def __init__(self, language: str):
|
| 31 |
self.language = language
|
| 32 |
+
|
| 33 |
@abstractmethod
|
| 34 |
def analyze(self, code: str) -> AnalysisResult:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
pass
|
| 36 |
+
|
| 37 |
+
def format_issue(self, line: int, symbol: str, message: str,
|
| 38 |
+
category: str = "style", severity: str = "low") -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
return {
|
| 40 |
"line": line,
|
| 41 |
"symbol": symbol,
|
|
|
|
| 43 |
"category": category,
|
| 44 |
"severity": severity,
|
| 45 |
}
|
| 46 |
+
|
| 47 |
@staticmethod
|
| 48 |
def create_analyzer(language: str) -> 'CodeAnalyzer':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
from app.modules.analyzer_python import PythonAnalyzer
|
| 50 |
from app.modules.analyzer_javascript import JavaScriptAnalyzer
|
| 51 |
from app.modules.analyzer_java import JavaAnalyzer
|
| 52 |
from app.modules.analyzer_llm_fallback import LLMFallbackAnalyzer
|
| 53 |
+
|
| 54 |
analyzers = {
|
| 55 |
"python": PythonAnalyzer,
|
| 56 |
"javascript": JavaScriptAnalyzer,
|
| 57 |
+
"typescript": JavaScriptAnalyzer,
|
| 58 |
"java": JavaAnalyzer,
|
| 59 |
"go": LLMFallbackAnalyzer,
|
| 60 |
"rust": LLMFallbackAnalyzer,
|
|
|
|
| 62 |
"c": LLMFallbackAnalyzer,
|
| 63 |
"csharp": LLMFallbackAnalyzer,
|
| 64 |
}
|
| 65 |
+
|
| 66 |
analyzer_class = analyzers.get(language, LLMFallbackAnalyzer)
|
| 67 |
+
return analyzer_class(language)
|