Skip to content

Commit adaeb76

Browse files
committed
feat(trace-updates): extract HOC names and add markers to display names
1 parent 225789b commit adaeb76

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

packages/react-devtools-shared/src/backend/views/TraceUpdates/index.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import Agent from 'react-devtools-shared/src/backend/agent';
1111
import {destroy as destroyCanvas, draw} from './canvas';
12-
import {getNestedBoundingClientRect} from '../utils';
12+
import {extractHOCNames, getNestedBoundingClientRect} from '../utils';
1313

1414
import type {HostInstance} from '../../types';
1515
import type {Rect} from '../utils';
@@ -24,6 +24,12 @@ const MAX_DISPLAY_DURATION = 3000;
2424
// How long should a rect be considered valid for?
2525
const REMEASUREMENT_AFTER_DURATION = 250;
2626

27+
// Markers for different types of HOCs
28+
const HOC_MARKERS = new Map([
29+
['Forget', '✨ń'],
30+
['Memo', '🧠'],
31+
]);
32+
2733
// Some environments (e.g. React Native / Hermes) don't support the performance API yet.
2834
const getCurrentTime =
2935
// $FlowFixMe[method-unbinding]
@@ -81,24 +87,33 @@ export function toggleEnabled(value: boolean): void {
8187
}
8288

8389
function traceUpdates(nodes: Set<HostInstance>): void {
84-
if (!isEnabled) {
85-
return;
86-
}
90+
if (!isEnabled) return;
8791

8892
nodes.forEach(node => {
8993
const data = nodeToData.get(node);
9094
const now = getCurrentTime();
9195

9296
let lastMeasuredAt = data != null ? data.lastMeasuredAt : 0;
9397
let rect = data != null ? data.rect : null;
98+
9499
if (rect === null || lastMeasuredAt + REMEASUREMENT_AFTER_DURATION < now) {
95100
lastMeasuredAt = now;
96101
rect = measureNode(node);
97102
}
98103

99-
let displayName = this.showNames && agent.getComponentNameForHostInstance(node);
100-
if (displayName != null && displayName.startsWith('Forget(')) {
101-
displayName = '✨' + displayName.slice(7, -1);
104+
let displayName = showNames
105+
? agent.getComponentNameForHostInstance(node)
106+
: null;
107+
if (displayName) {
108+
const {baseComponentName, hocNames} = extractHOCNames(displayName);
109+
110+
const markers = hocNames.map(hoc => HOC_MARKERS.get(hoc) || '').join('');
111+
112+
const enhancedDisplayName = markers
113+
? `${markers}${baseComponentName}`
114+
: baseComponentName;
115+
116+
displayName = enhancedDisplayName;
102117
}
103118

104119
nodeToData.set(node, {

packages/react-devtools-shared/src/backend/views/utils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,25 @@ export function getElementDimensions(domElement: HTMLElement): {
138138
paddingBottom: parseInt(calculatedStyle.paddingBottom, 10),
139139
};
140140
}
141+
142+
export function extractHOCNames(displayName: string) {
143+
if (!displayName) return {baseComponentName: '', hocNames: []};
144+
145+
const hocRegex = /([A-Z][a-zA-Z0-9]*?)\((.*)\)/g;
146+
const hocNames: string[] = [];
147+
let baseComponentName = displayName;
148+
let match;
149+
150+
while ((match = hocRegex.exec(baseComponentName)) != null) {
151+
if (Array.isArray(match)) {
152+
const [, hocName, inner] = match;
153+
hocNames.push(hocName);
154+
baseComponentName = inner;
155+
}
156+
}
157+
158+
return {
159+
baseComponentName,
160+
hocNames,
161+
};
162+
}

0 commit comments

Comments
 (0)