File size: 3,515 Bytes
e903db0 cf28cad e903db0 cf28cad e903db0 cf28cad e903db0 cf28cad e903db0 cf28cad e903db0 cf28cad e903db0 cf28cad e903db0 cf28cad e903db0 cf28cad e903db0 cf28cad e903db0 cf28cad e903db0 cf28cad e903db0 cf28cad e903db0 cf28cad e903db0 cf28cad e903db0 cf28cad e903db0 cf28cad e903db0 cf28cad e903db0 cf28cad e903db0 cf28cad | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | """JavaScript/TypeScript code analyzer using ESLint."""
import subprocess
import tempfile
import json
import os
from app.modules.analyzer_base import CodeAnalyzer, AnalysisResult
class JavaScriptAnalyzer(CodeAnalyzer):
def __init__(self, language: str = "javascript"):
super().__init__(language)
self.eslint_available = self._check_eslint()
def _check_eslint(self) -> bool:
try:
result = subprocess.run(
["npx", "eslint", "--version"],
capture_output=True, timeout=5
)
return result.returncode == 0
except Exception:
return False
def analyze(self, code: str) -> AnalysisResult:
if not self.eslint_available:
return self._analyze_fallback(code)
ext = ".ts" if self.language == "typescript" else ".js"
with tempfile.NamedTemporaryFile(mode="w", suffix=ext, delete=False, encoding="utf-8") as f:
f.write(code)
temp_path = f.name
try:
result = subprocess.run(
["npx", "eslint", "--format=json", temp_path],
capture_output=True, text=True, timeout=15
)
raw_issues = []
if result.stdout.strip():
try:
raw_data = json.loads(result.stdout)
raw_issues = raw_data[0].get("messages", []) if raw_data else []
except json.JSONDecodeError:
pass
issues = []
for issue in raw_issues[:5]:
severity_map = {2: "high", 1: "medium"}
severity = severity_map.get(issue.get("severity", 1), "low")
issues.append(self.format_issue(
line=issue.get("line", 0),
symbol=issue.get("ruleId", ""),
message=issue.get("message", ""),
category="style",
severity=severity,
))
return AnalysisResult(self.language, issues)
except Exception as e:
return AnalysisResult(self.language, error=str(e))
finally:
if os.path.exists(temp_path):
os.unlink(temp_path)
def _analyze_fallback(self, code: str) -> AnalysisResult:
issues = []
lines = code.split("\n")
for i, line in enumerate(lines, 1):
if "var " in line and "var" in line.split(";")[0]:
issues.append(self.format_issue(
line=i,
symbol="no-var",
message="Use 'let' or 'const' instead of 'var'",
category="style",
severity="low",
))
# FIX: use " == " with spaces to avoid matching === and !==
if " == " in line and " === " not in line:
issues.append(self.format_issue(
line=i,
symbol="eqeqeq",
message="Use '===' instead of '=='",
category="correctness",
severity="medium",
))
if line.rstrip() != line:
issues.append(self.format_issue(
line=i,
symbol="trailing-spaces",
message="Trailing whitespace",
category="style",
severity="low",
))
return AnalysisResult(self.language, issues[:5]) |