@treelocator/runtime 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,32 @@
1
+
2
+ > @treelocator/runtime@0.3.2 build /Users/wende/projects/treelocatorjs/packages/runtime
3
+ > concurrently pnpm:build:*
4
+
5
+ [wrapImage]
6
+ [wrapImage] > @treelocator/runtime@0.3.2 build:wrapImage /Users/wende/projects/treelocatorjs/packages/runtime
7
+ [wrapImage] > node ./scripts/wrapImage.js
8
+ [wrapImage]
9
+ [ts]
10
+ [ts] > @treelocator/runtime@0.3.2 build:ts /Users/wende/projects/treelocatorjs/packages/runtime
11
+ [ts] > tsc --declaration --emitDeclarationOnly --noEmit false --outDir dist
12
+ [ts]
13
+ [tailwind]
14
+ [tailwind] > @treelocator/runtime@0.3.2 build:tailwind /Users/wende/projects/treelocatorjs/packages/runtime
15
+ [tailwind] > tailwindcss -i ./src/main.css -o ./dist/output.css
16
+ [tailwind]
17
+ [babel]
18
+ [babel] > @treelocator/runtime@0.3.2 build:babel /Users/wende/projects/treelocatorjs/packages/runtime
19
+ [babel] > babel src --out-dir dist --extensions .js,.jsx,.ts,.tsx
20
+ [babel]
21
+ [wrapImage] Tree icon file generated
22
+ [wrapImage] pnpm run build:wrapImage exited with code 0
23
+ [babel] [baseline-browser-mapping] The data in this module is over two months old. To ensure accurate Baseline data, please update: `npm i baseline-browser-mapping@latest -D`
24
+ [tailwind] [baseline-browser-mapping] The data in this module is over two months old. To ensure accurate Baseline data, please update: `npm i baseline-browser-mapping@latest -D`
25
+ [tailwind]
26
+ [tailwind] Rebuilding...
27
+ [tailwind]
28
+ [tailwind] Done in 179ms.
29
+ [tailwind] pnpm run build:tailwind exited with code 0
30
+ [babel] Successfully compiled 82 files with Babel (538ms).
31
+ [babel] pnpm run build:babel exited with code 0
32
+ [ts] pnpm run build:ts exited with code 0
@@ -0,0 +1,32 @@
1
+
2
+ > @treelocator/runtime@0.1.8 dev /Users/wende/projects/locatorjs/packages/runtime
3
+ > concurrently pnpm:dev:*
4
+
5
+ [babel]
6
+ [babel] > @treelocator/runtime@0.1.8 dev:babel /Users/wende/projects/locatorjs/packages/runtime
7
+ [babel] > babel src --watch --out-dir dist --extensions .js,.jsx,.ts,.tsx
8
+ [babel]
9
+ [wrapImage]
10
+ [wrapImage] > @treelocator/runtime@0.1.8 dev:wrapImage /Users/wende/projects/locatorjs/packages/runtime
11
+ [wrapImage] > WATCH=true node ./scripts/wrapImage.js
12
+ [wrapImage]
13
+ [ts]
14
+ [ts] > @treelocator/runtime@0.1.8 dev:ts /Users/wende/projects/locatorjs/packages/runtime
15
+ [ts] > tsc --watch --declaration --emitDeclarationOnly --noEmit false --outDir dist --preserveWatchOutput
16
+ [ts]
17
+ [wrapCSS]
18
+ [wrapCSS] > @treelocator/runtime@0.1.8 dev:wrapCSS /Users/wende/projects/locatorjs/packages/runtime
19
+ [wrapCSS] > WATCH=true node ./scripts/wrapCSS.js
20
+ [wrapCSS]
21
+ [tailwind]
22
+ [tailwind] > @treelocator/runtime@0.1.8 dev:tailwind /Users/wende/projects/locatorjs/packages/runtime
23
+ [tailwind] > tailwindcss -i ./src/main.css -o ./dist/output.css --watch
24
+ [tailwind]
25
+ [tailwind]  ELIFECYCLE  Command failed.
26
+ [ts]  ELIFECYCLE  Command failed.
27
+ [wrapImage]  ELIFECYCLE  Command failed.
28
+ [wrapCSS]  ELIFECYCLE  Command failed.
29
+ [babel]  ELIFECYCLE  Command failed.
30
+ [babel] pnpm run dev:babel exited with code SIGINT
31
+ [wrapCSS] pnpm run dev:wrapCSS exited with code SIGINT
32
+ [wrapImage] pnpm run dev:wrapImage exited with code SIGINT
@@ -0,0 +1,14 @@
1
+
2
+ > @treelocator/runtime@0.3.2 test /Users/wende/projects/treelocatorjs/packages/runtime
3
+ > vitest run
4
+
5
+
6
+ RUN v2.1.9 /Users/wende/projects/treelocatorjs/packages/runtime
7
+
8
+ ✓ src/functions/transformPath.test.ts (3 tests) 2ms
9
+ ✓ src/functions/getUsableFileName.test.tsx (4 tests) 2ms
10
+ ✓ src/functions/evalTemplate.test.ts (1 test) 3ms
11
+ ✓ src/functions/cropPath.test.ts (2 tests) 2ms
12
+ ✓ src/functions/mergeRects.test.ts (1 test) 1ms
13
+ ✓ src/functions/formatAncestryChain.test.ts (14 tests) 3ms
14
+  ELIFECYCLE  Test failed. See above for more details.
@@ -0,0 +1,4 @@
1
+
2
+ > @locator/runtime@0.5.1 ts /Users/wende/projects/locatorjs/packages/runtime
3
+ > tsc --noEmit
4
+
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Krzysztof Wende
4
+ Copyright (c) 2023 Michael Musil (original LocatorJS)
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
@@ -5,4 +5,4 @@ export declare function getAllParentsElementsAndRootComponent(fiber: Fiber): {
5
5
  component: Fiber;
6
6
  componentBox: SimpleDOMRect;
7
7
  parentElements: ElementInfo[];
8
- };
8
+ } | null;
@@ -5,8 +5,9 @@ import { isStyledElement } from "./isStyled";
5
5
  export function getAllParentsElementsAndRootComponent(fiber) {
6
6
  const parentElements = [];
7
7
  const deepestElement = fiber.stateNode;
8
- if (!deepestElement || !(deepestElement instanceof HTMLElement)) {
9
- throw new Error("This functions works only for Fibres with HTMLElement stateNode");
8
+ if (!deepestElement || !(deepestElement instanceof Element)) {
9
+ console.warn("[TreeLocator] Skipping fiber with non-Element stateNode:", fiber.type, fiber.stateNode);
10
+ return null;
10
11
  }
11
12
  let componentBox = deepestElement.getBoundingClientRect();
12
13
 
@@ -15,7 +16,7 @@ export function getAllParentsElementsAndRootComponent(fiber) {
15
16
  while (currentFiber._debugOwner || currentFiber.return) {
16
17
  currentFiber = currentFiber._debugOwner || currentFiber.return;
17
18
  const currentElement = currentFiber.stateNode;
18
- if (!currentElement || !(currentElement instanceof HTMLElement)) {
19
+ if (!currentElement || !(currentElement instanceof Element)) {
19
20
  return {
20
21
  component: currentFiber,
21
22
  parentElements,
@@ -30,5 +31,6 @@ export function getAllParentsElementsAndRootComponent(fiber) {
30
31
  link: null
31
32
  });
32
33
  }
33
- throw new Error("Could not find root component");
34
+ console.warn("[TreeLocator] Could not find root component for fiber:", fiber.type);
35
+ return null;
34
36
  }
@@ -4,7 +4,7 @@ export function getAllWrappingParents(fiber) {
4
4
  let currentFiber = fiber;
5
5
  while (currentFiber.return) {
6
6
  currentFiber = currentFiber.return;
7
- if (currentFiber.stateNode && currentFiber.stateNode instanceof HTMLElement) {
7
+ if (currentFiber.stateNode && currentFiber.stateNode instanceof Element) {
8
8
  return parents;
9
9
  }
10
10
 
@@ -13,11 +13,15 @@ export function getElementInfo(found) {
13
13
  const labels = [];
14
14
  const fiber = findFiberByHtmlElement(found, false);
15
15
  if (fiber) {
16
+ const result = getAllParentsElementsAndRootComponent(fiber);
17
+ if (!result) {
18
+ return null;
19
+ }
16
20
  const {
17
21
  component,
18
22
  componentBox,
19
23
  parentElements
20
- } = getAllParentsElementsAndRootComponent(fiber);
24
+ } = result;
21
25
  const allPotentialComponentFibers = getAllWrappingParents(component);
22
26
 
23
27
  // This handles a common case when the component root is basically the comopnent itself, so I want to go to usage of the component
@@ -105,6 +105,22 @@ export function collectAncestry(node) {
105
105
  }
106
106
  return items;
107
107
  }
108
+ function getInnermostNamedComponent(item) {
109
+ if (!item) return undefined;
110
+ if (item.ownerComponents && item.ownerComponents.length > 0) {
111
+ // Find the innermost non-Anonymous component
112
+ for (let i = item.ownerComponents.length - 1; i >= 0; i--) {
113
+ if (item.ownerComponents[i].name !== "Anonymous") {
114
+ return item.ownerComponents[i].name;
115
+ }
116
+ }
117
+ }
118
+ // Fall back to componentName if it's not Anonymous
119
+ if (item.componentName && item.componentName !== "Anonymous") {
120
+ return item.componentName;
121
+ }
122
+ return undefined;
123
+ }
108
124
  export function formatAncestryChain(items) {
109
125
  if (items.length === 0) {
110
126
  return "";
@@ -118,11 +134,12 @@ export function formatAncestryChain(items) {
118
134
  const prefix = index === 0 ? "" : "└─ ";
119
135
 
120
136
  // Get the previous item's component to detect component boundaries
137
+ // Ignore "Anonymous" when resolving component names — these are framework internals
121
138
  const prevItem = index > 0 ? reversed[index - 1] : null;
122
- const prevComponentName = prevItem?.componentName || prevItem?.ownerComponents?.[prevItem.ownerComponents.length - 1]?.name;
139
+ const prevComponentName = getInnermostNamedComponent(prevItem);
123
140
 
124
- // Get current item's innermost component
125
- const currentComponentName = item.ownerComponents?.[item.ownerComponents.length - 1]?.name || item.componentName;
141
+ // Get current item's innermost named component
142
+ const currentComponentName = getInnermostNamedComponent(item);
126
143
 
127
144
  // Determine the display name for the element
128
145
  // Use component name ONLY when crossing a component boundary (root element of a component)
@@ -132,14 +149,17 @@ export function formatAncestryChain(items) {
132
149
  const isComponentBoundary = currentComponentName && currentComponentName !== prevComponentName;
133
150
  if (isComponentBoundary) {
134
151
  if (item.ownerComponents && item.ownerComponents.length > 0) {
135
- // Use innermost component as display name, show outer ones in "in X > Y"
136
- const innermost = item.ownerComponents[item.ownerComponents.length - 1];
152
+ // Use innermost named component as display name, show outer ones in "in X > Y"
153
+ // Filter out "Anonymous" components — these are framework-internal wrappers
154
+ // (e.g. Next.js App Router) that add noise without useful context
155
+ const named = item.ownerComponents.filter(c => c.name !== "Anonymous");
156
+ const innermost = named[named.length - 1];
137
157
  if (innermost) {
138
158
  displayName = innermost.name;
139
159
  // Outer components (excluding innermost)
140
- outerComponents = item.ownerComponents.slice(0, -1).map(c => c.name);
160
+ outerComponents = named.slice(0, -1).map(c => c.name);
141
161
  }
142
- } else if (item.componentName) {
162
+ } else if (item.componentName && item.componentName !== "Anonymous") {
143
163
  displayName = item.componentName;
144
164
  }
145
165
  }
@@ -125,4 +125,118 @@ describe("formatAncestryChain", () => {
125
125
  // Single component becomes the display name, no "in X" needed
126
126
  expect(result).toBe("Button at src/Button.tsx:10");
127
127
  });
128
+ describe("Anonymous component filtering", () => {
129
+ it("filters Anonymous from outer components in 'in X > Y' display", () => {
130
+ const items = [{
131
+ elementName: "div",
132
+ nthChild: 2,
133
+ componentName: "Anonymous",
134
+ filePath: "app/page.tsx",
135
+ line: 82,
136
+ ownerComponents: [{
137
+ name: "Anonymous"
138
+ }, {
139
+ name: "Home",
140
+ filePath: "app/page.tsx",
141
+ line: 82
142
+ }]
143
+ }, {
144
+ elementName: "div",
145
+ componentName: "App",
146
+ filePath: "src/App.tsx",
147
+ line: 1
148
+ }];
149
+ const result = formatAncestryChain(items);
150
+ // "Anonymous" should be filtered out — just "Home" as display name, no "in Anonymous"
151
+ expect(result).toBe(`App at src/App.tsx:1
152
+ └─ Home:nth-child(2) at app/page.tsx:82`);
153
+ });
154
+ it("filters Anonymous from component boundary detection", () => {
155
+ // When prev item's innermost component is Anonymous, it shouldn't count
156
+ // as a different component from the current item
157
+ const items = [{
158
+ elementName: "p",
159
+ componentName: "Home",
160
+ filePath: "app/page.tsx",
161
+ line: 100
162
+ }, {
163
+ elementName: "div",
164
+ componentName: "Anonymous",
165
+ filePath: "app/page.tsx",
166
+ line: 82,
167
+ ownerComponents: [{
168
+ name: "Anonymous"
169
+ }, {
170
+ name: "Home",
171
+ filePath: "app/page.tsx",
172
+ line: 82
173
+ }]
174
+ }];
175
+ const result = formatAncestryChain(items);
176
+ // Both items resolve to "Home" — no boundary crossing, so element name for child
177
+ expect(result).toBe(`Home at app/page.tsx:82
178
+ └─ p at app/page.tsx:100`);
179
+ });
180
+ it("handles all-Anonymous owner chain gracefully", () => {
181
+ const items = [{
182
+ elementName: "div",
183
+ componentName: "Anonymous",
184
+ filePath: "app/layout.tsx",
185
+ line: 10,
186
+ ownerComponents: [{
187
+ name: "Anonymous"
188
+ }, {
189
+ name: "Anonymous"
190
+ }]
191
+ }];
192
+ const result = formatAncestryChain(items);
193
+ // All components are Anonymous — falls back to element name
194
+ expect(result).toBe("div at app/layout.tsx:10");
195
+ });
196
+ it("skips Anonymous-only componentName without ownerComponents", () => {
197
+ const items = [{
198
+ elementName: "main",
199
+ componentName: "Anonymous",
200
+ filePath: "app/page.tsx",
201
+ line: 50
202
+ }, {
203
+ elementName: "div",
204
+ componentName: "App",
205
+ filePath: "src/App.tsx",
206
+ line: 1
207
+ }];
208
+ const result = formatAncestryChain(items);
209
+ // "Anonymous" componentName should not be used as display name
210
+ expect(result).toBe(`App at src/App.tsx:1
211
+ └─ main at app/page.tsx:50`);
212
+ });
213
+ it("preserves named components when mixed with Anonymous", () => {
214
+ const items = [{
215
+ elementName: "div",
216
+ componentName: "Sidebar",
217
+ filePath: "src/Sidebar.tsx",
218
+ line: 20,
219
+ ownerComponents: [{
220
+ name: "Sidebar",
221
+ filePath: "src/Sidebar.tsx",
222
+ line: 20
223
+ }, {
224
+ name: "Anonymous"
225
+ }, {
226
+ name: "GlassPanel",
227
+ filePath: "src/GlassPanel.tsx",
228
+ line: 5
229
+ }]
230
+ }, {
231
+ elementName: "div",
232
+ componentName: "App",
233
+ filePath: "src/App.tsx",
234
+ line: 1
235
+ }];
236
+ const result = formatAncestryChain(items);
237
+ // Anonymous in the middle is filtered, Sidebar (outer) and GlassPanel (inner) remain
238
+ expect(result).toBe(`App at src/App.tsx:1
239
+ └─ GlassPanel in Sidebar at src/Sidebar.tsx:20`);
240
+ });
241
+ });
128
242
  });
@@ -32,16 +32,38 @@ export function getUsableName(fiber) {
32
32
  if (fiber.elementType.name) {
33
33
  return fiber.elementType.name;
34
34
  }
35
- // Not sure about this
36
35
  if (fiber.elementType.displayName) {
37
36
  return fiber.elementType.displayName;
38
37
  }
39
- // Used in rect.memo
38
+ // Used in React.memo
40
39
  if (fiber.elementType.type?.name) {
41
40
  return fiber.elementType.type.name;
42
41
  }
42
+ if (fiber.elementType.type?.displayName) {
43
+ return fiber.elementType.type.displayName;
44
+ }
45
+ // React.forwardRef wraps the render function in .render
46
+ if (fiber.elementType.render?.name) {
47
+ return fiber.elementType.render.name;
48
+ }
49
+ if (fiber.elementType.render?.displayName) {
50
+ return fiber.elementType.render.displayName;
51
+ }
52
+ // React lazy components store resolved module in _payload._result
43
53
  if (fiber.elementType._payload?._result?.name) {
44
54
  return fiber.elementType._payload._result.name;
45
55
  }
56
+ if (fiber.elementType._payload?._result?.displayName) {
57
+ return fiber.elementType._payload._result.displayName;
58
+ }
59
+ // fiber.type can differ from elementType in some React internals
60
+ if (fiber.type && typeof fiber.type !== "string" && fiber.type !== fiber.elementType) {
61
+ if (fiber.type.name) {
62
+ return fiber.type.name;
63
+ }
64
+ if (fiber.type.displayName) {
65
+ return fiber.type.displayName;
66
+ }
67
+ }
46
68
  return "Anonymous";
47
69
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@treelocator/runtime",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "TreeLocatorJS runtime for component ancestry tracking. Alt+click any element to copy its component tree to clipboard. Exposes window.__treelocator__ API for browser automation (Playwright, Puppeteer, Selenium, Cypress).",
5
5
  "keywords": [
6
6
  "locator",
@@ -73,5 +73,5 @@
73
73
  "directory": "packages/runtime"
74
74
  },
75
75
  "license": "MIT",
76
- "gitHead": "afc8a534284e818665bb4d03b7fa940ec5ad5880"
76
+ "gitHead": "511af51703d71e9a5c4d4990c66140b283f05afe"
77
77
  }
@@ -10,13 +10,12 @@ export function getAllParentsElementsAndRootComponent(fiber: Fiber): {
10
10
  component: Fiber;
11
11
  componentBox: SimpleDOMRect;
12
12
  parentElements: ElementInfo[];
13
- } {
13
+ } | null {
14
14
  const parentElements: ElementInfo[] = [];
15
15
  const deepestElement = fiber.stateNode;
16
- if (!deepestElement || !(deepestElement instanceof HTMLElement)) {
17
- throw new Error(
18
- "This functions works only for Fibres with HTMLElement stateNode"
19
- );
16
+ if (!deepestElement || !(deepestElement instanceof Element)) {
17
+ console.warn("[TreeLocator] Skipping fiber with non-Element stateNode:", fiber.type, fiber.stateNode);
18
+ return null;
20
19
  }
21
20
  let componentBox: SimpleDOMRect = deepestElement.getBoundingClientRect();
22
21
 
@@ -26,7 +25,7 @@ export function getAllParentsElementsAndRootComponent(fiber: Fiber): {
26
25
  while (currentFiber._debugOwner || currentFiber.return) {
27
26
  currentFiber = currentFiber._debugOwner || currentFiber.return!;
28
27
  const currentElement = currentFiber.stateNode;
29
- if (!currentElement || !(currentElement instanceof HTMLElement)) {
28
+ if (!currentElement || !(currentElement instanceof Element)) {
30
29
  return {
31
30
  component: currentFiber,
32
31
  parentElements,
@@ -48,5 +47,6 @@ export function getAllParentsElementsAndRootComponent(fiber: Fiber): {
48
47
  link: null,
49
48
  });
50
49
  }
51
- throw new Error("Could not find root component");
50
+ console.warn("[TreeLocator] Could not find root component for fiber:", fiber.type);
51
+ return null;
52
52
  }
@@ -9,7 +9,7 @@ export function getAllWrappingParents(fiber: Fiber): Fiber[] {
9
9
  currentFiber = currentFiber.return;
10
10
  if (
11
11
  currentFiber.stateNode &&
12
- currentFiber.stateNode instanceof HTMLElement
12
+ currentFiber.stateNode instanceof Element
13
13
  ) {
14
14
  return parents;
15
15
  }
@@ -25,8 +25,11 @@ export function getElementInfo(found: HTMLElement): FullElementInfo | null {
25
25
 
26
26
  const fiber = findFiberByHtmlElement(found, false);
27
27
  if (fiber) {
28
- const { component, componentBox, parentElements } =
29
- getAllParentsElementsAndRootComponent(fiber);
28
+ const result = getAllParentsElementsAndRootComponent(fiber);
29
+ if (!result) {
30
+ return null;
31
+ }
32
+ const { component, componentBox, parentElements } = result;
30
33
 
31
34
  const allPotentialComponentFibers = getAllWrappingParents(component);
32
35
 
@@ -143,4 +143,127 @@ describe("formatAncestryChain", () => {
143
143
  // Single component becomes the display name, no "in X" needed
144
144
  expect(result).toBe("Button at src/Button.tsx:10");
145
145
  });
146
+
147
+ describe("Anonymous component filtering", () => {
148
+ it("filters Anonymous from outer components in 'in X > Y' display", () => {
149
+ const items: AncestryItem[] = [
150
+ {
151
+ elementName: "div",
152
+ nthChild: 2,
153
+ componentName: "Anonymous",
154
+ filePath: "app/page.tsx",
155
+ line: 82,
156
+ ownerComponents: [
157
+ { name: "Anonymous" },
158
+ { name: "Home", filePath: "app/page.tsx", line: 82 },
159
+ ],
160
+ },
161
+ { elementName: "div", componentName: "App", filePath: "src/App.tsx", line: 1 },
162
+ ];
163
+
164
+ const result = formatAncestryChain(items);
165
+ // "Anonymous" should be filtered out — just "Home" as display name, no "in Anonymous"
166
+ expect(result).toBe(
167
+ `App at src/App.tsx:1
168
+ └─ Home:nth-child(2) at app/page.tsx:82`
169
+ );
170
+ });
171
+
172
+ it("filters Anonymous from component boundary detection", () => {
173
+ // When prev item's innermost component is Anonymous, it shouldn't count
174
+ // as a different component from the current item
175
+ const items: AncestryItem[] = [
176
+ {
177
+ elementName: "p",
178
+ componentName: "Home",
179
+ filePath: "app/page.tsx",
180
+ line: 100,
181
+ },
182
+ {
183
+ elementName: "div",
184
+ componentName: "Anonymous",
185
+ filePath: "app/page.tsx",
186
+ line: 82,
187
+ ownerComponents: [
188
+ { name: "Anonymous" },
189
+ { name: "Home", filePath: "app/page.tsx", line: 82 },
190
+ ],
191
+ },
192
+ ];
193
+
194
+ const result = formatAncestryChain(items);
195
+ // Both items resolve to "Home" — no boundary crossing, so element name for child
196
+ expect(result).toBe(
197
+ `Home at app/page.tsx:82
198
+ └─ p at app/page.tsx:100`
199
+ );
200
+ });
201
+
202
+ it("handles all-Anonymous owner chain gracefully", () => {
203
+ const items: AncestryItem[] = [
204
+ {
205
+ elementName: "div",
206
+ componentName: "Anonymous",
207
+ filePath: "app/layout.tsx",
208
+ line: 10,
209
+ ownerComponents: [
210
+ { name: "Anonymous" },
211
+ { name: "Anonymous" },
212
+ ],
213
+ },
214
+ ];
215
+
216
+ const result = formatAncestryChain(items);
217
+ // All components are Anonymous — falls back to element name
218
+ expect(result).toBe("div at app/layout.tsx:10");
219
+ });
220
+
221
+ it("skips Anonymous-only componentName without ownerComponents", () => {
222
+ const items: AncestryItem[] = [
223
+ {
224
+ elementName: "main",
225
+ componentName: "Anonymous",
226
+ filePath: "app/page.tsx",
227
+ line: 50,
228
+ },
229
+ {
230
+ elementName: "div",
231
+ componentName: "App",
232
+ filePath: "src/App.tsx",
233
+ line: 1,
234
+ },
235
+ ];
236
+
237
+ const result = formatAncestryChain(items);
238
+ // "Anonymous" componentName should not be used as display name
239
+ expect(result).toBe(
240
+ `App at src/App.tsx:1
241
+ └─ main at app/page.tsx:50`
242
+ );
243
+ });
244
+
245
+ it("preserves named components when mixed with Anonymous", () => {
246
+ const items: AncestryItem[] = [
247
+ {
248
+ elementName: "div",
249
+ componentName: "Sidebar",
250
+ filePath: "src/Sidebar.tsx",
251
+ line: 20,
252
+ ownerComponents: [
253
+ { name: "Sidebar", filePath: "src/Sidebar.tsx", line: 20 },
254
+ { name: "Anonymous" },
255
+ { name: "GlassPanel", filePath: "src/GlassPanel.tsx", line: 5 },
256
+ ],
257
+ },
258
+ { elementName: "div", componentName: "App", filePath: "src/App.tsx", line: 1 },
259
+ ];
260
+
261
+ const result = formatAncestryChain(items);
262
+ // Anonymous in the middle is filtered, Sidebar (outer) and GlassPanel (inner) remain
263
+ expect(result).toBe(
264
+ `App at src/App.tsx:1
265
+ └─ GlassPanel in Sidebar at src/Sidebar.tsx:20`
266
+ );
267
+ });
268
+ });
146
269
  });
@@ -147,6 +147,23 @@ export function collectAncestry(node: TreeNode): AncestryItem[] {
147
147
  return items;
148
148
  }
149
149
 
150
+ function getInnermostNamedComponent(item: AncestryItem | null | undefined): string | undefined {
151
+ if (!item) return undefined;
152
+ if (item.ownerComponents && item.ownerComponents.length > 0) {
153
+ // Find the innermost non-Anonymous component
154
+ for (let i = item.ownerComponents.length - 1; i >= 0; i--) {
155
+ if (item.ownerComponents[i]!.name !== "Anonymous") {
156
+ return item.ownerComponents[i]!.name;
157
+ }
158
+ }
159
+ }
160
+ // Fall back to componentName if it's not Anonymous
161
+ if (item.componentName && item.componentName !== "Anonymous") {
162
+ return item.componentName;
163
+ }
164
+ return undefined;
165
+ }
166
+
150
167
  export function formatAncestryChain(items: AncestryItem[]): string {
151
168
  if (items.length === 0) {
152
169
  return "";
@@ -162,11 +179,12 @@ export function formatAncestryChain(items: AncestryItem[]): string {
162
179
  const prefix = index === 0 ? "" : "└─ ";
163
180
 
164
181
  // Get the previous item's component to detect component boundaries
182
+ // Ignore "Anonymous" when resolving component names — these are framework internals
165
183
  const prevItem = index > 0 ? reversed[index - 1] : null;
166
- const prevComponentName = prevItem?.componentName || prevItem?.ownerComponents?.[prevItem.ownerComponents.length - 1]?.name;
184
+ const prevComponentName = getInnermostNamedComponent(prevItem);
167
185
 
168
- // Get current item's innermost component
169
- const currentComponentName = item.ownerComponents?.[item.ownerComponents.length - 1]?.name || item.componentName;
186
+ // Get current item's innermost named component
187
+ const currentComponentName = getInnermostNamedComponent(item);
170
188
 
171
189
  // Determine the display name for the element
172
190
  // Use component name ONLY when crossing a component boundary (root element of a component)
@@ -177,14 +195,17 @@ export function formatAncestryChain(items: AncestryItem[]): string {
177
195
 
178
196
  if (isComponentBoundary) {
179
197
  if (item.ownerComponents && item.ownerComponents.length > 0) {
180
- // Use innermost component as display name, show outer ones in "in X > Y"
181
- const innermost = item.ownerComponents[item.ownerComponents.length - 1];
198
+ // Use innermost named component as display name, show outer ones in "in X > Y"
199
+ // Filter out "Anonymous" components — these are framework-internal wrappers
200
+ // (e.g. Next.js App Router) that add noise without useful context
201
+ const named = item.ownerComponents.filter((c) => c.name !== "Anonymous");
202
+ const innermost = named[named.length - 1];
182
203
  if (innermost) {
183
204
  displayName = innermost.name;
184
205
  // Outer components (excluding innermost)
185
- outerComponents = item.ownerComponents.slice(0, -1).map((c) => c.name);
206
+ outerComponents = named.slice(0, -1).map((c) => c.name);
186
207
  }
187
- } else if (item.componentName) {
208
+ } else if (item.componentName && item.componentName !== "Anonymous") {
188
209
  displayName = item.componentName;
189
210
  }
190
211
  }
@@ -36,17 +36,39 @@ export function getUsableName(fiber: Fiber | null | undefined): string {
36
36
  if (fiber.elementType.name) {
37
37
  return fiber.elementType.name;
38
38
  }
39
- // Not sure about this
40
39
  if (fiber.elementType.displayName) {
41
40
  return fiber.elementType.displayName;
42
41
  }
43
- // Used in rect.memo
42
+ // Used in React.memo
44
43
  if (fiber.elementType.type?.name) {
45
44
  return fiber.elementType.type.name;
46
45
  }
46
+ if (fiber.elementType.type?.displayName) {
47
+ return fiber.elementType.type.displayName;
48
+ }
49
+ // React.forwardRef wraps the render function in .render
50
+ if (fiber.elementType.render?.name) {
51
+ return fiber.elementType.render.name;
52
+ }
53
+ if (fiber.elementType.render?.displayName) {
54
+ return fiber.elementType.render.displayName;
55
+ }
56
+ // React lazy components store resolved module in _payload._result
47
57
  if (fiber.elementType._payload?._result?.name) {
48
58
  return fiber.elementType._payload._result.name;
49
59
  }
60
+ if (fiber.elementType._payload?._result?.displayName) {
61
+ return fiber.elementType._payload._result.displayName;
62
+ }
63
+ // fiber.type can differ from elementType in some React internals
64
+ if (fiber.type && typeof fiber.type !== "string" && fiber.type !== fiber.elementType) {
65
+ if (fiber.type.name) {
66
+ return fiber.type.name;
67
+ }
68
+ if (fiber.type.displayName) {
69
+ return fiber.type.displayName;
70
+ }
71
+ }
50
72
 
51
73
  return "Anonymous";
52
74
  }