Home / Function/ simplifyForStatements() — react Function Reference

simplifyForStatements() — react Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

compiler/packages/snap/src/minimize.ts lines 860–961

function* simplifyForStatements(ast: t.File): Generator<t.File> {
  // Count for statements
  let forCount = 0;
  t.traverseFast(ast, node => {
    if (t.isForStatement(node)) {
      forCount++;
    }
  });

  // Try replacing with init (if it's an expression)
  for (let targetIdx = 0; targetIdx < forCount; targetIdx++) {
    const cloned = cloneAst(ast);
    let idx = 0;
    let modified = false;

    traverse(cloned, {
      ForStatement(path) {
        if (modified) return;
        if (idx === targetIdx && path.node.init) {
          if (t.isExpression(path.node.init)) {
            path.replaceWith(t.expressionStatement(path.node.init));
          } else {
            // It's a VariableDeclaration
            path.replaceWith(path.node.init);
          }
          modified = true;
        }
        idx++;
      },
    });

    if (modified) {
      yield cloned;
    }
  }

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

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

    if (modified) {
      yield cloned;
    }
  }

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

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

    if (modified) {
      yield cloned;
    }
  }

  // Try replacing with body

Domain

Subdomains

Calls

Frequently Asked Questions

What does simplifyForStatements() do?
simplifyForStatements() is a function in the react codebase, defined in compiler/packages/snap/src/minimize.ts.
Where is simplifyForStatements() defined?
simplifyForStatements() is defined in compiler/packages/snap/src/minimize.ts at line 860.
What does simplifyForStatements() call?
simplifyForStatements() 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