37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
import esprima
|
||
from ..utils.logger import log
|
||
|
||
class FileAnalyzer:
|
||
def __init__(self):
|
||
pass
|
||
|
||
def analyze_file(self, file_path: str) -> dict:
|
||
"""
|
||
Анализирует файл и извлекает информацию о функциях.
|
||
"""
|
||
log(f"Analyzing file: {file_path}")
|
||
with open(file_path, "r", encoding="utf-8") as f:
|
||
code = f.read()
|
||
|
||
try:
|
||
parsed = esprima.parseScript(code, loc=True)
|
||
functions = self._extract_functions(parsed)
|
||
return functions
|
||
except Exception as e:
|
||
log(f"Error parsing file {file_path}: {e}", level="error")
|
||
return {}
|
||
|
||
def _extract_functions(self, ast: dict) -> dict:
|
||
"""
|
||
Извлекает информацию о функциях из AST.
|
||
"""
|
||
functions = {}
|
||
for node in ast.body:
|
||
if node.type == "FunctionDeclaration":
|
||
name = node.id.name
|
||
params = [param.name for param in node.params]
|
||
functions[name] = {
|
||
"params": params,
|
||
"return_type": "unknown", # Тип возвращаемого значения не всегда доступен
|
||
}
|
||
return functions |