rewriteInstruction() — react Function Reference
Architecture documentation for the rewriteInstruction() function in DeadCodeElimination.ts from the react codebase.
Entity Profile
Dependency Diagram
graph TD b0733383_adae_e3a5_c873_9a645cf0ce31["rewriteInstruction()"] 12a58551_b77c_3215_7e97_0c27aabd262e["DeadCodeElimination.ts"] b0733383_adae_e3a5_c873_9a645cf0ce31 -->|defined in| 12a58551_b77c_3215_7e97_0c27aabd262e e6e7641a_0356_e256_1ee6_a60705f7edfe["deadCodeElimination()"] e6e7641a_0356_e256_1ee6_a60705f7edfe -->|calls| b0733383_adae_e3a5_c873_9a645cf0ce31 1ddcaaa6_a274_5751_db05_5448b1a769b9["isIdOrNameUsed()"] b0733383_adae_e3a5_c873_9a645cf0ce31 -->|calls| 1ddcaaa6_a274_5751_db05_5448b1a769b9 d7fde76c_4fd9_feb3_299b_798689f05bc6["assertExhaustive()"] b0733383_adae_e3a5_c873_9a645cf0ce31 -->|calls| d7fde76c_4fd9_feb3_299b_798689f05bc6 568e9c36_44d8_d092_103e_197bdf559e4e["isIdUsed()"] b0733383_adae_e3a5_c873_9a645cf0ce31 -->|calls| 568e9c36_44d8_d092_103e_197bdf559e4e style b0733383_adae_e3a5_c873_9a645cf0ce31 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
compiler/packages/babel-plugin-react-compiler/src/Optimization/DeadCodeElimination.ts lines 187–272
function rewriteInstruction(instr: Instruction, state: State): void {
if (instr.value.kind === 'Destructure') {
// Remove unused lvalues
switch (instr.value.lvalue.pattern.kind) {
case 'ArrayPattern': {
/*
* For arrays, we can prune items prior to the end by replacing
* them with a hole. Items at the end can simply be dropped.
*/
let lastEntryIndex = 0;
const items = instr.value.lvalue.pattern.items;
for (let i = 0; i < items.length; i++) {
const item = items[i];
if (item.kind === 'Identifier') {
if (!state.isIdOrNameUsed(item.identifier)) {
items[i] = {kind: 'Hole'};
} else {
lastEntryIndex = i;
}
} else if (item.kind === 'Spread') {
if (!state.isIdOrNameUsed(item.place.identifier)) {
items[i] = {kind: 'Hole'};
} else {
lastEntryIndex = i;
}
}
}
items.length = lastEntryIndex + 1;
break;
}
case 'ObjectPattern': {
/*
* For objects we can prune any unused properties so long as there is no used rest element
* (`const {x, ...y} = z`). If a rest element exists and is used, then nothing can be pruned
* because it would change the set of properties which are copied into the rest value.
* In the `const {x, ...y} = z` example, removing the `x` property would mean that `y` now
* has an `x` property, changing the semantics.
*/
let nextProperties: ObjectPattern['properties'] | null = null;
for (const property of instr.value.lvalue.pattern.properties) {
if (property.kind === 'ObjectProperty') {
if (state.isIdOrNameUsed(property.place.identifier)) {
nextProperties ??= [];
nextProperties.push(property);
}
} else {
if (state.isIdOrNameUsed(property.place.identifier)) {
nextProperties = null;
break;
}
}
}
if (nextProperties !== null) {
instr.value.lvalue.pattern.properties = nextProperties;
}
break;
}
default: {
assertExhaustive(
instr.value.lvalue.pattern,
`Unexpected pattern kind '${
(instr.value.lvalue.pattern as any).kind
}'`,
);
}
}
} else if (instr.value.kind === 'StoreLocal') {
if (
instr.value.lvalue.kind !== InstructionKind.Reassign &&
!state.isIdUsed(instr.value.lvalue.place.identifier)
) {
/*
* This is a const/let declaration where the variable is accessed later,
* but where the value is always overwritten before being read. Ie the
* initializer value is never read. We rewrite to a DeclareLocal so
* that the initializer value can be DCE'd
*/
instr.value = {
kind: 'DeclareLocal',
lvalue: instr.value.lvalue,
type: instr.value.type,
Domain
Subdomains
Called By
Source
Frequently Asked Questions
What does rewriteInstruction() do?
rewriteInstruction() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Optimization/DeadCodeElimination.ts.
Where is rewriteInstruction() defined?
rewriteInstruction() is defined in compiler/packages/babel-plugin-react-compiler/src/Optimization/DeadCodeElimination.ts at line 187.
What does rewriteInstruction() call?
rewriteInstruction() calls 3 function(s): assertExhaustive, isIdOrNameUsed, isIdUsed.
What calls rewriteInstruction()?
rewriteInstruction() is called by 1 function(s): deadCodeElimination.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free