Download app/modules/analyzer_javascript.py from rk147/secforge: direct link, hf CLI and curl.
- Browser
- Download file 3.52 kB
-
https://huggingface.co/spaces/rk147/secforge/resolve/main/app/modules/analyzer_javascript.py
- Command line
-
hf download hf://spaces/rk147/secforge/app/modules/analyzer_javascript.py
-
curl -L -o analyzer_javascript.py https://huggingface.co/spaces/rk147/secforge/resolve/main/app/modules/analyzer_javascript.py
3.52 kB
| """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]) |