Home / Function/ simplifyForOfStatements() — react Function Reference

simplifyForOfStatements() — react Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

compiler/packages/snap/src/minimize.ts lines 1056–1135

function* simplifyForOfStatements(ast: t.File): Generator<t.File> {
  // Count for-of statements
  let forOfCount = 0;
  t.traverseFast(ast, node => {
    if (t.isForOfStatement(node)) {
      forOfCount++;
    }
  });

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

    traverse(cloned, {
      ForOfStatement(path) {
        if (modified) return;
        if (idx === targetIdx) {
          const left = path.node.left;
          if (t.isExpression(left)) {
            path.replaceWith(t.expressionStatement(left));
          } else {
            path.replaceWith(left);
          }
          modified = true;
        }
        idx++;
      },
    });

    if (modified) {
      yield cloned;
    }
  }

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

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

    if (modified) {
      yield cloned;
    }
  }

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

    traverse(cloned, {
      ForOfStatement(path) {
        if (modified) return;
        if (idx === targetIdx) {
          path.replaceWith(path.node.body);
          modified = true;
        }
        idx++;
      },
    });

    if (modified) {
      yield cloned;
    }
  }
}

Domain

Subdomains

Calls

Frequently Asked Questions

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