Skip to content

Commit 639d73d

Browse files
committed
fix(react-devtools): report every component variant of a name, and show report and commit arguments in usage
1 parent 6fb23c1 commit 639d73d

4 files changed

Lines changed: 65 additions & 27 deletions

File tree

‎src/cli/react-profiler/react-profile-command.ts‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
import {
3131
componentsByRenders,
3232
DEFAULT_FINDING_LIMIT,
33-
findComponent,
33+
findComponents,
3434
formatSeconds,
3535
formatCommit,
3636
formatComponentReport,
@@ -395,19 +395,19 @@ type SubcommandHandler = (
395395
emit: Emit,
396396
) => Promise<void>;
397397

398-
function requireComponent(
398+
function requireComponents(
399399
analysis: ReactProfileAnalysis,
400400
name: string | undefined,
401-
): ComponentProfile {
401+
): ComponentProfile[] {
402402
if (!name)
403403
throw new AppError('INVALID_ARGS', 'react-devtools profile report requires a component name.');
404-
const component = findComponent(analysis, name);
405-
if (!component) {
404+
const components = findComponents(analysis, name);
405+
if (components.length === 0) {
406406
throw new AppError('COMMAND_FAILED', `No renders of ${name} in the saved profile.`, {
407407
hint: 'Use a name from react-devtools profile slow or profile rerenders.',
408408
});
409409
}
410-
return component;
410+
return components;
411411
}
412412

413413
function requireCommit(analysis: ReactProfileAnalysis, target: string | undefined): CommitProfile {
@@ -459,8 +459,11 @@ const SUBCOMMAND_HANDLERS: Record<ReactProfileSubcommand, SubcommandHandler> = {
459459
},
460460
report: async (args, options, emit) => {
461461
const { analysis } = readSavedProfile(options);
462-
const component = requireComponent(analysis, args.target);
463-
emit(formatComponentReport(analysis, component), component);
462+
const components = requireComponents(analysis, args.target);
463+
emit(formatComponentReport(analysis, components), {
464+
components,
465+
findings: analysis.findings.filter((finding) => finding.component === components[0]!.name),
466+
});
464467
},
465468
timeline: async (args, options, emit) => {
466469
const { analysis } = readSavedProfile(options);

‎src/cli/react-profiler/react-profile-format.test.ts‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import assert from 'node:assert/strict';
33
import type { Finding, ReactProfileAnalysis } from './react-profile-analysis.ts';
44
import type { ComponentProfile } from './react-profile-renders.ts';
55
import {
6-
findComponent,
6+
findComponents,
77
formatCommit,
8+
formatComponentReport,
89
formatProfileSummary,
910
formatRerenders,
1011
formatSlowComponents,
@@ -130,8 +131,38 @@ test('the timeline lists commits with their trigger and mounts', () => {
130131
});
131132

132133
test('component lookup falls back to a case-insensitive match', () => {
133-
assert.equal(findComponent(analysis(), 'chart')?.name, 'Chart');
134-
assert.equal(findComponent(analysis(), 'Missing'), undefined);
134+
assert.deepEqual(
135+
findComponents(analysis(), 'chart').map((c) => c.name),
136+
['Chart'],
137+
);
138+
assert.deepEqual(findComponents(analysis(), 'Missing'), []);
139+
});
140+
141+
test('a report covers every component of the name, then its findings once', () => {
142+
const base = analysis();
143+
const row = base.components.find((c) => c.name === 'Row')!;
144+
const memoRow = { ...row, kind: 'memo' as const, renders: 2, selfMs: 4, maxSelfMs: 2 };
145+
const withVariants = {
146+
...base,
147+
components: [...base.components, memoRow],
148+
findings: [
149+
{
150+
kind: 'parent-cascade' as const,
151+
component: 'Row',
152+
costMs: 9,
153+
evidence: 'often re-renders',
154+
fix: 'memoize',
155+
},
156+
],
157+
};
158+
const variants = findComponents(withVariants, 'Row');
159+
assert.deepEqual(
160+
variants.map((c) => c.kind),
161+
['function', 'memo'],
162+
);
163+
const text = formatComponentReport(withVariants, variants);
164+
assert.match(text, /^Row \[function\][^\n]*\n[\s\S]*\nRow \[memo\] self 4\.0ms/);
165+
assert.equal(text.match(/finding parent-cascade/g)?.length, 1);
135166
});
136167

137168
test('a commit entry without recorded changes says its cause is unrecorded', () => {

‎src/cli/react-profiler/react-profile-format.ts‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,14 @@ export function formatRerenders(analysis: ReactProfileAnalysis, limit: number):
112112
].join('\n');
113113
}
114114

115-
export function findComponent(
116-
analysis: ReactProfileAnalysis,
117-
name: string,
118-
): ComponentProfile | undefined {
119-
return (
120-
analysis.components.find((c) => c.name === name) ??
121-
analysis.components.find((c) => c.name.toLowerCase() === name.toLowerCase())
122-
);
115+
/** Every component of that name, such as `Row [memo]` and `Row [function]`; case-insensitive when no exact match. */
116+
export function findComponents(analysis: ReactProfileAnalysis, name: string): ComponentProfile[] {
117+
const exact = analysis.components.filter((c) => c.name === name);
118+
if (exact.length > 0) return exact;
119+
return analysis.components.filter((c) => c.name.toLowerCase() === name.toLowerCase());
123120
}
124121

125-
export function formatComponentReport(
126-
analysis: ReactProfileAnalysis,
127-
component: ComponentProfile,
128-
): string {
122+
function componentDetail(analysis: ReactProfileAnalysis, component: ComponentProfile): string[] {
129123
const lines = [componentLine(component), `causes: ${causeSummary(component.causes)}`];
130124
const props = Object.entries(component.changedProps)
131125
.sort((a, b) => b[1] - a[1])
@@ -140,16 +134,25 @@ export function formatComponentReport(
140134
if (hooks) lines.push(`changed hooks (by index): ${hooks}`);
141135
const owners = keyCounts(component.owners, 3);
142136
if (owners) lines.push(`rendered by: ${owners}`);
143-
const findings = analysis.findings.filter((finding) => finding.component === component.name);
144-
for (const finding of findings)
145-
lines.push(`finding ${finding.kind}: ${finding.evidence} -> ${finding.fix}`);
146137
if (component.costliestCommits.length > 0) {
147138
const commits = component.costliestCommits.map(({ index, selfMs }) => {
148139
const commit = analysis.commits[index];
149140
return `#${index} ${ms(selfMs)}${commit ? ` of ${ms(commit.durationMs)}` : ''}`;
150141
});
151142
lines.push(`its costliest commits (self time of commit duration): ${commits.join(', ')}`);
152143
}
144+
return lines;
145+
}
146+
147+
/** Reports each component of one name, then the findings that name it. */
148+
export function formatComponentReport(
149+
analysis: ReactProfileAnalysis,
150+
components: ComponentProfile[],
151+
): string {
152+
const lines = components.flatMap((component) => componentDetail(analysis, component));
153+
const name = components[0]?.name;
154+
for (const finding of analysis.findings.filter((f) => f.component === name))
155+
lines.push(`finding ${finding.kind}: ${finding.evidence} -> ${finding.fix}`);
153156
return lines.join('\n');
154157
}
155158

‎src/commands/schema/command-overrides.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ const SCHEMA_ONLY_CLI_COMMAND_SCHEMAS = {
183183
description:
184184
'Profile React Native renders through the Metro dev server and report slow renders, rerenders, and their causes',
185185
},
186-
usageOverride: 'react-devtools profile start|stop|status|report|commit|slow|rerenders|timeline',
186+
usageOverride:
187+
'react-devtools profile start|stop|status|report <Component>|commit <N>|slow|rerenders|timeline',
187188
listUsageOverride: 'react-devtools',
188189
positionalArgs: ['args?'],
189190
allowsExtraPositionals: true,

0 commit comments

Comments
 (0)