1+ # src/deepbase/parsers/javascript.py
2+ import re
3+ from .interface import LanguageParser
4+
5+ class JavaScriptParser (LanguageParser ):
6+ """
7+ Parser per JavaScript, TypeScript e React Native (.js, .jsx, .ts, .tsx).
8+ Versione 1.1: Logica Regex base + Supporto Export Default.
9+ """
10+
11+ def parse (self , content : str , file_path : str ) -> str :
12+ lines = []
13+
14+ # Regex patterns per catturare le definizioni strutturali (classi, funzioni, var, tipi)
15+ patterns = [
16+ # Class definition
17+ re .compile (r'^\s*(export\s+)?(default\s+)?(abstract\s+)?class\s+([a-zA-Z0-9_]+)(.*)?\{' ),
18+
19+ # Function definition standard
20+ re .compile (r'^\s*(export\s+)?(default\s+)?(async\s+)?function\s+([a-zA-Z0-9_]+)\s*\(.*' ),
21+
22+ # Arrow Function / Variable Assignments
23+ re .compile (r'^\s*(export\s+)?(const|let|var)\s+([a-zA-Z0-9_]+)\s*=\s*(async\s*)?(\(.*\)|[^=]+)\s*=>.*' ),
24+
25+ # TypeScript Interfaces & Types
26+ re .compile (r'^\s*(export\s+)?(interface|type)\s+([a-zA-Z0-9_]+).*' ),
27+ ]
28+
29+ # --- NEW: Regex specifica per Export Default diretto (V2 Feature) ---
30+ # Cattura: export default router; | export default MyComponent;
31+ # Il (?!...) assicura che non catturi "class" o "function" che sono gestiti meglio dai pattern sopra.
32+ re_export_default = re .compile (r'^\s*export\s+default\s+(?!class|function)([a-zA-Z0-9_]+);?' )
33+
34+ # JSDoc pattern
35+ in_comment = False
36+ source_lines = content .splitlines ()
37+
38+ for i , line in enumerate (source_lines ):
39+ stripped = line .strip ()
40+
41+ # Gestione commenti JSDoc
42+ if stripped .startswith ("/**" ):
43+ in_comment = True
44+ lines .append (stripped )
45+ if stripped .endswith ("*/" ):
46+ in_comment = False
47+ continue
48+
49+ if in_comment :
50+ lines .append (stripped )
51+ if stripped .endswith ("*/" ):
52+ in_comment = False
53+ continue
54+
55+ # Ignora commenti single line o righe vuote
56+ if not stripped or stripped .startswith ("//" ):
57+ continue
58+
59+ # --- NEW: Controllo Export Default ---
60+ # Se è un export default semplice, lo aggiungiamo così com'è (senza { ... })
61+ if re_export_default .match (stripped ):
62+ lines .append (stripped )
63+ continue
64+
65+ # Verifica patterns standard
66+ is_match = False
67+ for pattern in patterns :
68+ if pattern .match (stripped ):
69+ # Pulizia fine riga: se finisce con '{', lo sostituiamo con '...'
70+ clean_line = stripped
71+ if clean_line .endswith ("{" ):
72+ clean_line = clean_line [:- 1 ].strip ()
73+
74+ # Aggiunge firma + { ... } per indicare struttura compressa
75+ lines .append (f"{ clean_line } {{ ... }}" )
76+ is_match = True
77+ break
78+
79+ # Fallback per decoratori
80+ if not is_match and stripped .startswith ("@" ):
81+ if i + 1 < len (source_lines ) and "class " in source_lines [i + 1 ]:
82+ lines .append (stripped )
83+
84+ if not lines :
85+ return f"(No exported functions, classes or components found in { file_path } )"
86+
87+ return "\n " .join (lines )
0 commit comments