@treelocator/runtime 0.3.1 → 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.
@@ -1,23 +1,23 @@
1
1
 
2
- > @treelocator/runtime@0.3.1 build /Users/wende/projects/treelocatorjs/packages/runtime
2
+ > @treelocator/runtime@0.3.2 build /Users/wende/projects/treelocatorjs/packages/runtime
3
3
  > concurrently pnpm:build:*
4
4
 
5
5
  [wrapImage]
6
- [wrapImage] > @treelocator/runtime@0.3.1 build:wrapImage /Users/wende/projects/treelocatorjs/packages/runtime
6
+ [wrapImage] > @treelocator/runtime@0.3.2 build:wrapImage /Users/wende/projects/treelocatorjs/packages/runtime
7
7
  [wrapImage] > node ./scripts/wrapImage.js
8
8
  [wrapImage]
9
9
  [ts]
10
- [ts] > @treelocator/runtime@0.3.1 build:ts /Users/wende/projects/treelocatorjs/packages/runtime
10
+ [ts] > @treelocator/runtime@0.3.2 build:ts /Users/wende/projects/treelocatorjs/packages/runtime
11
11
  [ts] > tsc --declaration --emitDeclarationOnly --noEmit false --outDir dist
12
12
  [ts]
13
- [babel]
14
- [babel] > @treelocator/runtime@0.3.1 build:babel /Users/wende/projects/treelocatorjs/packages/runtime
15
- [babel] > babel src --out-dir dist --extensions .js,.jsx,.ts,.tsx
16
- [babel]
17
13
  [tailwind]
18
- [tailwind] > @treelocator/runtime@0.3.1 build:tailwind /Users/wende/projects/treelocatorjs/packages/runtime
14
+ [tailwind] > @treelocator/runtime@0.3.2 build:tailwind /Users/wende/projects/treelocatorjs/packages/runtime
19
15
  [tailwind] > tailwindcss -i ./src/main.css -o ./dist/output.css
20
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
21
  [wrapImage] Tree icon file generated
22
22
  [wrapImage] pnpm run build:wrapImage exited with code 0
23
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`
@@ -25,8 +25,8 @@
25
25
  [tailwind]
26
26
  [tailwind] Rebuilding...
27
27
  [tailwind]
28
- [tailwind] Done in 175ms.
28
+ [tailwind] Done in 179ms.
29
29
  [tailwind] pnpm run build:tailwind exited with code 0
30
- [babel] Successfully compiled 82 files with Babel (602ms).
30
+ [babel] Successfully compiled 82 files with Babel (538ms).
31
31
  [babel] pnpm run build:babel exited with code 0
32
32
  [ts] pnpm run build:ts exited with code 0
@@ -1,14 +1,14 @@
1
1
 
2
- > @treelocator/runtime@0.3.0 test /Users/wende/projects/treelocatorjs/packages/runtime
2
+ > @treelocator/runtime@0.3.2 test /Users/wende/projects/treelocatorjs/packages/runtime
3
3
  > vitest run
4
4
 
5
5
 
6
6
  RUN v2.1.9 /Users/wende/projects/treelocatorjs/packages/runtime
7
7
 
8
- ✓ src/functions/formatAncestryChain.test.ts (9 tests) 2ms
9
- ✓ src/functions/evalTemplate.test.ts (1 test) 1ms
10
- ✓ src/functions/cropPath.test.ts (2 tests) 1ms
11
- ✓ src/functions/mergeRects.test.ts (1 test) 1ms
8
+ ✓ src/functions/transformPath.test.ts (3 tests) 2ms
12
9
  ✓ src/functions/getUsableFileName.test.tsx (4 tests) 2ms
13
- ✓ src/functions/transformPath.test.ts (3 tests) 6ms
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
14
   ELIFECYCLE  Test failed. See above for more details.
@@ -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.1",
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": "f53c9d698aaee64837698b5c6803df5485d2a229"
76
+ "gitHead": "511af51703d71e9a5c4d4990c66140b283f05afe"
77
77
  }
@@ -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
  }