MCPcopy Create free account
hub / github.com/donghaxkim/react-rewrite / buildJSXPath

Function buildJSXPath

packages/overlay/src/utils/jsx-path.ts:26–136  ·  view source on GitHub ↗
(
  element: HTMLElement,
  filePath: string,
  componentName: string,
)

Source from the content-addressed store, hash-verified

24 * isn't found.
25 */
26export function buildJSXPath(
27 element: HTMLElement,
28 filePath: string,
29 componentName: string,
30): JSXStructuralPath | null {
31 const fiber = getFiberFromHostInstance(element);
32 if (!fiber) return null;
33
34 const segments: JSXPathSegment[] = [];
35 let current = fiber;
36 let foundBoundary = false;
37
38 while (current) {
39 // Check if this is the component boundary (composite fiber matching componentName)
40 if (isCompositeFiber(current)) {
41 const name = getDisplayName(current);
42 if (name === componentName) {
43 foundBoundary = true;
44 break;
45 }
46 }
47
48 // Determine if this fiber should be included as a path segment
49 const fiberType = current.type;
50
51 // Skip fibers with symbol types (Fragment, StrictMode, Suspense, Context, etc.)
52 if (typeof fiberType === "symbol") {
53 current = current.return;
54 continue;
55 }
56
57 let name: string | null = null;
58
59 if (typeof fiberType === "string") {
60 // Host fiber (div, span, etc.)
61 name = fiberType;
62 } else if (isCompositeFiber(current)) {
63 // Composite fiber — get display name
64 const displayName = getDisplayName(current);
65 // Only include user-level components (uppercase first letter)
66 if (displayName && displayName[0] === displayName[0].toUpperCase() && /^[A-Z]/.test(displayName)) {
67 name = displayName;
68 }
69 }
70
71 if (name === null) {
72 current = current.return;
73 continue;
74 }
75
76 // Skip non-HTML lowercase names that slipped through (e.g. from non-string non-symbol types)
77 if (name[0] === name[0].toLowerCase() && !HTML_TAGS.has(name)) {
78 current = current.return;
79 continue;
80 }
81
82 // Determine discriminator
83 let discriminator: JSXPathSegment["discriminator"];

Callers 3

resolveComponentFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected