|
1
|
""" |
|
2
|
React Native framework enricher. |
|
3
|
|
|
4
|
Promotes generic Function/Class nodes to semantic React Native types: |
|
5
|
- Component — JSX/TSX files (same heuristic as React web) |
|
6
|
- Screen — files under screens/ directory or names ending in "Screen" |
|
7
|
- Hook — functions whose name starts with "use" |
|
8
|
- Navigation — createStackNavigator / createBottomTabNavigator / NavigationContainer etc. |
|
9
|
""" |
|
10
|
|
|
11
|
from navegador.enrichment.base import EnrichmentResult, FrameworkEnricher |
|
12
|
|
|
13
|
_NAVIGATION_PATTERNS = ( |
|
14
|
"createStackNavigator", |
|
15
|
"createBottomTabNavigator", |
|
16
|
"createDrawerNavigator", |
|
17
|
"createNativeStackNavigator", |
|
18
|
"NavigationContainer", |
|
19
|
"useNavigation", |
|
20
|
"useRoute", |
|
21
|
) |
|
22
|
|
|
23
|
|
|
24
|
class ReactNativeEnricher(FrameworkEnricher): |
|
25
|
"""Enricher for React Native and Expo codebases.""" |
|
26
|
|
|
27
|
@property |
|
28
|
def framework_name(self) -> str: |
|
29
|
return "react-native" |
|
30
|
|
|
31
|
@property |
|
32
|
def detection_patterns(self) -> list[str]: |
|
33
|
return ["react-native", "expo"] |
|
34
|
|
|
35
|
@property |
|
36
|
def detection_files(self) -> list[str]: |
|
37
|
return ["app.json"] |
|
38
|
|
|
39
|
def enrich(self) -> EnrichmentResult: |
|
40
|
result = EnrichmentResult() |
|
41
|
|
|
42
|
# ── Components: functions/classes in .jsx or .tsx files ────────────── |
|
43
|
component_rows = ( |
|
44
|
self.store.query( |
|
45
|
"MATCH (n) WHERE (n.file_path CONTAINS '.jsx' OR n.file_path CONTAINS '.tsx') " |
|
46
|
"AND n.name IS NOT NULL " |
|
47
|
"RETURN n.name, n.file_path", |
|
48
|
).result_set |
|
49
|
or [] |
|
50
|
) |
|
51
|
for name, file_path in component_rows: |
|
52
|
self._promote_node(name, file_path, "RNComponent") |
|
53
|
result.promoted += 1 |
|
54
|
result.patterns_found["components"] = len(component_rows) |
|
55
|
|
|
56
|
# ── Screens: nodes under screens/ or whose names end with "Screen" ─── |
|
57
|
screen_rows = ( |
|
58
|
self.store.query( |
|
59
|
"MATCH (n) WHERE (n.file_path CONTAINS '/screens/' " |
|
60
|
"OR (n.name IS NOT NULL AND n.name ENDS WITH 'Screen')) " |
|
61
|
"AND n.name IS NOT NULL " |
|
62
|
"RETURN n.name, n.file_path", |
|
63
|
).result_set |
|
64
|
or [] |
|
65
|
) |
|
66
|
for name, file_path in screen_rows: |
|
67
|
self._promote_node(name, file_path, "RNScreen") |
|
68
|
result.promoted += 1 |
|
69
|
result.patterns_found["screens"] = len(screen_rows) |
|
70
|
|
|
71
|
# ── Hooks: functions whose name starts with "use" ───────────────────── |
|
72
|
hook_rows = ( |
|
73
|
self.store.query( |
|
74
|
"MATCH (n) WHERE n.name STARTS WITH 'use' " |
|
75
|
"AND n.name <> 'use' " |
|
76
|
"AND n.file_path IS NOT NULL " |
|
77
|
"RETURN n.name, n.file_path", |
|
78
|
).result_set |
|
79
|
or [] |
|
80
|
) |
|
81
|
for name, file_path in hook_rows: |
|
82
|
self._promote_node(name, file_path, "RNHook") |
|
83
|
result.promoted += 1 |
|
84
|
result.patterns_found["hooks"] = len(hook_rows) |
|
85
|
|
|
86
|
# ── Navigation: navigator factory / container patterns ──────────────── |
|
87
|
nav_conditions = " OR ".join(f"n.name CONTAINS '{pat}'" for pat in _NAVIGATION_PATTERNS) |
|
88
|
nav_rows = ( |
|
89
|
self.store.query( |
|
90
|
f"MATCH (n) WHERE ({nav_conditions}) " |
|
91
|
"AND n.file_path IS NOT NULL " |
|
92
|
"RETURN n.name, n.file_path", |
|
93
|
).result_set |
|
94
|
or [] |
|
95
|
) |
|
96
|
for name, file_path in nav_rows: |
|
97
|
self._promote_node(name, file_path, "RNNavigation") |
|
98
|
result.promoted += 1 |
|
99
|
result.patterns_found["navigation"] = len(nav_rows) |
|
100
|
|
|
101
|
return result |
|
102
|
|