Skip to content

Commit c28c050

Browse files
Merge pull request #351 from preactjs/fix-hoc-reorder
2 parents 41d681c + 1c7b388 commit c28c050

File tree

6 files changed

+65
-71
lines changed

6 files changed

+65
-71
lines changed

src/adapter/shared/traverse.ts

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ export function shouldFilter<T extends SharedVNode>(
113113
return true;
114114
}
115115

116+
// TODO: Add a virtual root node to be able to filter the actual
117+
// ones. Currently we have a workaround on the extension side
118+
// that filters it there, but we should really do it here to be
119+
// consistent with all other filters.
120+
116121
if (vnode.type === config.Fragment && filters.type.has("fragment")) {
117122
const parent = bindings.getVNodeParent(vnode);
118123
// Only filter non-root nodes
@@ -121,6 +126,12 @@ export function shouldFilter<T extends SharedVNode>(
121126
return false;
122127
} else if (bindings.isElement(vnode) && filters.type.has("dom")) {
123128
return true;
129+
} else if (filters.type.has("hoc")) {
130+
const name = bindings.getDisplayName(vnode, config);
131+
132+
if (name.indexOf("(") > -1 && !name.startsWith("ForwardRef")) {
133+
return true;
134+
}
124135
}
125136

126137
if (filters.regex.length > 0) {
@@ -169,75 +180,62 @@ function mount<T extends SharedVNode>(
169180
const root = bindings.isRoot(vnode, config);
170181

171182
const skip = shouldFilter(vnode, filters, config, bindings);
172-
173-
if (root || !skip) {
174-
record: {
175-
let name = bindings.getDisplayName(vnode, config);
176-
177-
if (filters.type.has("hoc")) {
178-
const hocName = getHocName(name);
179-
180-
// Filter out HOC-Components
181-
if (hocName) {
182-
if (name.startsWith("ForwardRef")) {
183-
hocs = [...hocs, hocName];
184-
const idx = name.indexOf("(");
185-
name = name.slice(idx + 1, -1) || "Anonymous";
186-
} else {
187-
hocs = [...hocs, hocName];
188-
break record;
189-
}
190-
}
183+
let name = bindings.getDisplayName(vnode, config);
184+
185+
if (filters.type.has("hoc")) {
186+
const hocName = getHocName(name);
187+
if (hocName) {
188+
hocs = [...hocs, hocName];
189+
if (name.startsWith("ForwardRef")) {
190+
const idx = name.indexOf("(");
191+
name = name.slice(idx + 1, -1) || "Anonymous";
191192
}
193+
}
194+
}
192195

193-
const id = getOrCreateVNodeId(ids, vnode);
194-
if (bindings.isRoot(vnode, config)) {
195-
commit.operations.push(MsgTypes.ADD_ROOT, id);
196-
}
196+
if (root || !skip) {
197+
const id = getOrCreateVNodeId(ids, vnode);
198+
if (bindings.isRoot(vnode, config)) {
199+
commit.operations.push(MsgTypes.ADD_ROOT, id);
200+
}
197201

198-
if (!timings.start.has(id)) {
199-
timings.start.set(id, timingsByVNode.start.get(vnode) || 0);
200-
}
201-
if (!timings.end.has(id)) {
202-
timings.end.set(id, timingsByVNode.end.get(vnode) || 0);
203-
}
202+
if (!timings.start.has(id)) {
203+
timings.start.set(id, timingsByVNode.start.get(vnode) || 0);
204+
}
205+
if (!timings.end.has(id)) {
206+
timings.end.set(id, timingsByVNode.end.get(vnode) || 0);
207+
}
204208

205-
commit.operations.push(
206-
MsgTypes.ADD_VNODE,
207-
id,
208-
getDevtoolsType(vnode, bindings), // Type
209-
ancestorId,
210-
9999, // owner
211-
getStringId(commit.strings, name),
212-
vnode.key ? getStringId(commit.strings, vnode.key) : 0,
213-
// Multiply, because operations array only supports integers
214-
// and would otherwise cut off floats
215-
(timings.start.get(id) || 0) * 1000,
216-
(timings.end.get(id) || 0) * 1000,
217-
);
209+
commit.operations.push(
210+
MsgTypes.ADD_VNODE,
211+
id,
212+
getDevtoolsType(vnode, bindings), // Type
213+
ancestorId,
214+
9999, // owner
215+
getStringId(commit.strings, name),
216+
vnode.key ? getStringId(commit.strings, vnode.key) : 0,
217+
// Multiply, because operations array only supports integers
218+
// and would otherwise cut off floats
219+
(timings.start.get(id) || 0) * 1000,
220+
(timings.end.get(id) || 0) * 1000,
221+
);
218222

219-
if (hocs.length > 0) {
220-
addHocs(commit, id, hocs);
221-
hocs = [];
222-
}
223+
if (hocs.length > 0) {
224+
addHocs(commit, id, hocs);
225+
hocs = [];
226+
}
223227

224-
// Capture render reason (mount here)
225-
if (profiler.isProfiling && profiler.captureRenderReasons) {
226-
commit.operations.push(
227-
MsgTypes.RENDER_REASON,
228-
id,
229-
RenderReason.MOUNT,
230-
0,
231-
);
232-
}
228+
// Capture render reason (mount here)
229+
if (profiler.isProfiling && profiler.captureRenderReasons) {
230+
commit.operations.push(MsgTypes.RENDER_REASON, id, RenderReason.MOUNT, 0);
231+
}
233232

234-
updateHighlight(profiler, vnode, bindings);
233+
updateHighlight(profiler, vnode, bindings);
235234

236-
ancestorId = id;
237-
}
235+
ancestorId = id;
238236
}
239237

240-
if (skip && typeof vnode.type !== "function") {
238+
if (skip && !bindings.isComponent(vnode)) {
241239
const dom = bindings.getDom(vnode);
242240
if (dom) domCache.set(dom, vnode);
243241
}

src/view/components/elements/TreeView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export function MarkResult(props: { text: string; id: ID }) {
169169
const start = idx > 0 ? text.slice(0, idx) : "";
170170
const end = idx < text.length ? text.slice(idx + m[0].length) : "";
171171
return (
172-
<span ref={ref}>
172+
<span ref={ref} data-testid="node-name">
173173
{start}
174174
<mark
175175
class={`${s.mark} ${isActive ? s.markSelected : ""}`}
@@ -181,7 +181,7 @@ export function MarkResult(props: { text: string; id: ID }) {
181181
</span>
182182
);
183183
}
184-
return <span>{text}</span>;
184+
return <span data-testid="node-name">{text}</span>;
185185
}
186186

187187
export function TreeItem(props: { key: any; id: ID; top: number }) {

src/view/components/profiler/flamegraph/modes/FlamegraphLayout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ export function FlamegraphLayout({
4747
const node = commit.nodes.get(pos.id)!;
4848
let text: any = "";
4949
if (pos.commitParent || pos.weight === -1) {
50-
text = node.name;
50+
text = <span data-testid="node-name">{node.name}</span>;
5151
} else {
5252
const self = formatTime(commit.selfDurations.get(node.id)!);
5353
const total = formatTime(node.endTime - node.startTime);
5454
text = (
5555
<>
56-
{node.name}
56+
<span data-testid="node-name">{node.name}</span>
5757
{filterHoc && node.hocs ? (
5858
<HocLabels hocs={node.hocs} nodeId={node.id} />
5959
) : (

src/view/components/profiler/flamegraph/ranked/RankedLayout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export function RankedLayout({
6868
);
6969
const text = (
7070
<>
71-
{node.name}
71+
<span data-testid="node-name">{node.name}</span>
7272
{hocs} ({formatTime(selfDuration)})
7373
</>
7474
);

test-e2e/tests/hooks/hook-name.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { newTestPage, getHooks, waitForSelector } from "../../test-utils";
22
import { expect } from "chai";
3-
import { clickNestedText } from "pentf/browser_utils";
3+
import { clickNestedText, clickSelector } from "pentf/browser_utils";
44

55
export const description = "Show custom hook name";
66

@@ -10,7 +10,7 @@ export async function run(config: any) {
1010
const hooksPanel = '[data-testid="props-row"]';
1111

1212
// Counter
13-
await clickNestedText(devtools, /Counter$/, {
13+
await clickSelector(devtools, '[data-name="Counter"]', {
1414
retryUntil: async () => {
1515
return !!(await devtools.$('[data-testid="Hooks"]'));
1616
},

test-e2e/tests/profiler/flamegraph/utils.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ import { Page } from "puppeteer";
33
export async function getFlameNodes(page: Page) {
44
return await page.$$eval('[data-type="flamegraph"] > *', els => {
55
return els.map(el => {
6-
const text = el.textContent!;
76
return {
87
maximized: el.hasAttribute("data-maximized"),
9-
name: text.slice(
10-
0,
11-
text.includes("(") ? text.lastIndexOf("(") - 1 : text.length,
12-
),
8+
name: el.querySelector('[data-testid="node-name"]')!.textContent,
139
visible: el.hasAttribute("data-visible"),
1410
};
1511
});

0 commit comments

Comments
 (0)