Home / Function/ simplifySwitchStatements() — react Function Reference

simplifySwitchStatements() — react Function Reference

Architecture documentation for the simplifySwitchStatements() function in minimize.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  514f9253_6a56_a788_a7c4_91816f4b20a7["simplifySwitchStatements()"]
  9c061da8_dbf8_4168_823d_991654a3c55d["minimize.ts"]
  514f9253_6a56_a788_a7c4_91816f4b20a7 -->|defined in| 9c061da8_dbf8_4168_823d_991654a3c55d
  7a9af496_ef6a_d9e8_8eaf_1f386f49e4b3["cloneAst()"]
  514f9253_6a56_a788_a7c4_91816f4b20a7 -->|calls| 7a9af496_ef6a_d9e8_8eaf_1f386f49e4b3
  style 514f9253_6a56_a788_a7c4_91816f4b20a7 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/snap/src/minimize.ts lines 664–733

function* simplifySwitchStatements(ast: t.File): Generator<t.File> {
  // Count switch statements
  let switchCount = 0;
  t.traverseFast(ast, node => {
    if (t.isSwitchStatement(node)) {
      switchCount++;
    }
  });

  // Try replacing with discriminant
  for (let targetIdx = 0; targetIdx < switchCount; targetIdx++) {
    const cloned = cloneAst(ast);
    let idx = 0;
    let modified = false;

    traverse(cloned, {
      SwitchStatement(path) {
        if (modified) return;
        if (idx === targetIdx) {
          path.replaceWith(t.expressionStatement(path.node.discriminant));
          modified = true;
        }
        idx++;
      },
    });

    if (modified) {
      yield cloned;
    }
  }

  // For each switch, try replacing with each case's body
  for (let targetIdx = 0; targetIdx < switchCount; targetIdx++) {
    // Find case count for this switch
    let caseCount = 0;
    let currentIdx = 0;
    t.traverseFast(ast, node => {
      if (t.isSwitchStatement(node)) {
        if (currentIdx === targetIdx) {
          caseCount = node.cases.length;
        }
        currentIdx++;
      }
    });

    for (let caseIdx = 0; caseIdx < caseCount; caseIdx++) {
      const cloned = cloneAst(ast);
      let idx = 0;
      let modified = false;

      traverse(cloned, {
        SwitchStatement(path) {
          if (modified) return;
          if (idx === targetIdx) {
            const switchCase = path.node.cases[caseIdx];
            if (switchCase && switchCase.consequent.length > 0) {
              path.replaceWithMultiple(switchCase.consequent);
              modified = true;
            }
          }
          idx++;
        },
      });

      if (modified) {
        yield cloned;
      }
    }
  }
}

Domain

Subdomains

Calls

Frequently Asked Questions

What does simplifySwitchStatements() do?
simplifySwitchStatements() is a function in the react codebase, defined in compiler/packages/snap/src/minimize.ts.
Where is simplifySwitchStatements() defined?
simplifySwitchStatements() is defined in compiler/packages/snap/src/minimize.ts at line 664.
What does simplifySwitchStatements() call?
simplifySwitchStatements() calls 1 function(s): cloneAst.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free