-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdescription.ts
More file actions
220 lines (183 loc) · 6.14 KB
/
description.ts
File metadata and controls
220 lines (183 loc) · 6.14 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { ReactNativeFiberNode } from "./types";
import { getRenderedBy } from "./get-rendered-by";
import type { RenderedByFrame } from "./get-rendered-by";
import type { ReactNativeGrabContextValue } from "./grab-context";
import { ReactNativeGrabInternalContext } from "./grab-context";
const MAX_STACK_LINES = 6;
const MAX_TEXT_LENGTH = 120;
const MAX_ATTR_VALUE_LENGTH = 80;
const MAX_ATTRS = 6;
const PRIORITY_ATTRS = [
"testID",
"nativeID",
"accessibilityLabel",
"accessibilityRole",
"accessibilityHint",
"accessibilityValue",
] as const;
const truncate = (value: string, maxLength: number): string => {
if (value.length <= maxLength) return value;
return `${value.slice(0, maxLength - 3)}...`;
};
const escapeAttr = (value: string): string => {
return value
.replace(/\\/g, "\\\\")
.replace(/\r?\n|\r/g, " ")
.replace(/"/g, '\\"');
};
const stringifyAttrValue = (value: unknown): string | null => {
if (value == null) return null;
if (typeof value === "string") {
if (value.trim().length === 0) return null;
return value;
}
if (typeof value === "number" || typeof value === "boolean") {
return String(value);
}
if (typeof value === "object") {
try {
return JSON.stringify(value);
} catch {
return String(value);
}
}
return null;
};
const collectPrimitiveText = (value: unknown, out: string[]) => {
if (typeof value === "string" || typeof value === "number") {
const text = String(value).trim();
if (text.length > 0) out.push(text);
return;
}
if (Array.isArray(value)) {
for (const item of value) {
collectPrimitiveText(item, out);
}
}
};
const extractTextPreview = (props: Record<string, unknown> | null): string => {
if (!props || !("children" in props)) return "";
const parts: string[] = [];
collectPrimitiveText(props.children, parts);
if (parts.length === 0) return "";
const normalized = parts.join(" ").replace(/\s+/g, " ").trim();
return truncate(normalized, MAX_TEXT_LENGTH);
};
const getHostFiber = (node: ReactNativeFiberNode): ReactNativeFiberNode | null => {
let current: ReactNativeFiberNode | null = node;
while (current) {
if (typeof current.type === "string") {
return current;
}
current = current.return ?? null;
}
return (node as unknown as ReactNativeFiberNode) ?? null;
};
const getHostComponentName = (fiber: ReactNativeFiberNode | null): string => {
const componentType = fiber?.type;
if (typeof componentType === "string" && componentType.length > 0) {
return componentType;
}
return "(unknown)";
};
const getMemoizedProps = (fiber: ReactNativeFiberNode | null): Record<string, unknown> | null => {
const props = fiber?.memoizedProps;
if (!props || typeof props !== "object") return null;
return props;
};
const extractPriorityAttrs = (props: Record<string, unknown> | null): string => {
if (!props) return "";
const pairs: string[] = [];
for (const key of PRIORITY_ATTRS) {
if (pairs.length >= MAX_ATTRS) break;
const rawValue = stringifyAttrValue(props[key]);
if (!rawValue) continue;
const value = escapeAttr(truncate(rawValue, MAX_ATTR_VALUE_LENGTH));
pairs.push(`${key}="${value}"`);
}
return pairs.length > 0 ? ` ${pairs.join(" ")}` : "";
};
const getPreviewComponentName = (
node: ReactNativeFiberNode,
renderedBy: RenderedByFrame[],
): string => {
const firstRenderedBy = renderedBy[0]?.name?.trim();
if (firstRenderedBy) return firstRenderedBy;
const hostFiber = getHostFiber(node);
return getHostComponentName(hostFiber);
};
const buildElementPreview = (node: ReactNativeFiberNode, renderedBy: RenderedByFrame[]): string => {
const componentName = getPreviewComponentName(node, renderedBy);
const hostFiber = getHostFiber(node);
const props = getMemoizedProps(hostFiber);
const attrs = extractPriorityAttrs(props);
const text = extractTextPreview(props);
if (!text) {
return `<${componentName}${attrs} />`;
}
return `<${componentName}${attrs}>\n ${text}\n</${componentName}>`;
};
const formatFrameLocation = (
file: string | null,
line: number | null,
column: number | null,
): string | null => {
if (!file) return null;
if (line == null) return file;
if (column == null) return `${file}:${line}`;
return `${file}:${line}:${column}`;
};
const buildStackContext = (renderedBy: RenderedByFrame[]): string => {
const lines = renderedBy
.filter((frame) => Boolean(frame.file))
.slice(0, MAX_STACK_LINES)
.map((frame) => {
const location = formatFrameLocation(frame.file, frame.line, frame.column);
if (!location) return "";
return `\n in ${frame.name} (at ${location})`;
})
.filter(Boolean);
return lines.join("");
};
type ReactProviderType = {
Provider?: unknown;
};
type ContextProviderFiberNode = ReactNativeFiberNode & {
type?: unknown;
memoizedProps?: {
value?: ReactNativeGrabContextValue;
} | null;
};
const isGrabContextProviderFiber = (fiber: ContextProviderFiberNode): boolean => {
const providerType = fiber.type;
return (
providerType === ReactNativeGrabInternalContext ||
providerType === (ReactNativeGrabInternalContext as ReactProviderType).Provider
);
};
const getGrabContextFromFiber = (
node: ReactNativeFiberNode,
): ReactNativeGrabContextValue | null => {
let current: ContextProviderFiberNode | null = node;
while (current) {
if (isGrabContextProviderFiber(current)) {
return current.memoizedProps?.value ?? null;
}
current = current.return ?? null;
}
return null;
};
const buildContextBlock = (contextValue: ReactNativeGrabContextValue | null): string => {
if (!contextValue || Object.keys(contextValue).length === 0) {
return "";
}
return `\n\nContext:\n${JSON.stringify(contextValue, null, 2)}`;
};
export const getDescription = async (node: ReactNativeFiberNode): Promise<string> => {
let renderedBy = await getRenderedBy(node);
const preview = buildElementPreview(node, renderedBy);
const stackContext = buildStackContext(renderedBy);
const contextBlock = buildContextBlock(getGrabContextFromFiber(node));
if (!stackContext) return `${preview}${contextBlock}`;
return `${preview}${stackContext}${contextBlock}`;
};