Home / Function/ canMergeScopes() — react Function Reference

canMergeScopes() — react Function Reference

Architecture documentation for the canMergeScopes() function in MergeReactiveScopesThatInvalidateTogether.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  f767b6e4_1078_08b0_bc4b_f27f690658e6["canMergeScopes()"]
  006711fe_6949_9be8_4726_2031284f3328["MergeReactiveScopesThatInvalidateTogether.ts"]
  f767b6e4_1078_08b0_bc4b_f27f690658e6 -->|defined in| 006711fe_6949_9be8_4726_2031284f3328
  11690004_c468_3b6b_f07f_36a6501f870a["visitBlock()"]
  11690004_c468_3b6b_f07f_36a6501f870a -->|calls| f767b6e4_1078_08b0_bc4b_f27f690658e6
  2ac731a7_1748_e943_7a74_4b9c344e4398["log()"]
  f767b6e4_1078_08b0_bc4b_f27f690658e6 -->|calls| 2ac731a7_1748_e943_7a74_4b9c344e4398
  7bed2ca3_d37d_8807_1d7c_c0ae2d6dbc64["areEqualDependencies()"]
  f767b6e4_1078_08b0_bc4b_f27f690658e6 -->|calls| 7bed2ca3_d37d_8807_1d7c_c0ae2d6dbc64
  aed61a37_60c8_9eae_ac35_b13a46157096["isAlwaysInvalidatingType()"]
  f767b6e4_1078_08b0_bc4b_f27f690658e6 -->|calls| aed61a37_60c8_9eae_ac35_b13a46157096
  527555c0_9544_ae6b_5f83_952272d2caa1["Iterable_some()"]
  f767b6e4_1078_08b0_bc4b_f27f690658e6 -->|calls| 527555c0_9544_ae6b_5f83_952272d2caa1
  694a6a07_b3ca_4ab4_beba_18f4053a49f2["printReactiveScopeSummary()"]
  f767b6e4_1078_08b0_bc4b_f27f690658e6 -->|calls| 694a6a07_b3ca_4ab4_beba_18f4053a49f2
  style f767b6e4_1078_08b0_bc4b_f27f690658e6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/MergeReactiveScopesThatInvalidateTogether.ts lines 437–502

function canMergeScopes(
  current: ReactiveScopeBlock,
  next: ReactiveScopeBlock,
  temporaries: Map<DeclarationId, DeclarationId>,
): boolean {
  // Don't merge scopes with reassignments
  if (
    current.scope.reassignments.size !== 0 ||
    next.scope.reassignments.size !== 0
  ) {
    log(`  cannot merge, has reassignments`);
    return false;
  }
  // Merge scopes whose dependencies are identical
  if (
    areEqualDependencies(current.scope.dependencies, next.scope.dependencies)
  ) {
    log(`  canMergeScopes: dependencies are equal`);
    return true;
  }
  /*
   * Merge scopes where the outputs of the previous scope are the inputs
   * of the subsequent scope. Note that the output of a scope is not
   * guaranteed to change when its inputs change, for example `foo(x)`
   * may not change when `x` changes, for example `foo(x) { return x < 10}`
   * will not change as x changes from 0 -> 1.
   * Therefore we check that the outputs of the previous scope are of a type
   * that is guaranteed to invalidate with its inputs, and only merge in this case.
   */
  if (
    areEqualDependencies(
      new Set(
        [...current.scope.declarations.values()].map(declaration => ({
          identifier: declaration.identifier,
          reactive: true,
          path: [],
        })),
      ),
      next.scope.dependencies,
    ) ||
    (next.scope.dependencies.size !== 0 &&
      [...next.scope.dependencies].every(
        dep =>
          dep.path.length === 0 &&
          isAlwaysInvalidatingType(dep.identifier.type) &&
          Iterable_some(
            current.scope.declarations.values(),
            decl =>
              decl.identifier.declarationId === dep.identifier.declarationId ||
              decl.identifier.declarationId ===
                temporaries.get(dep.identifier.declarationId),
          ),
      ))
  ) {
    log(`  outputs of prev are input to current`);
    return true;
  }
  log(`  cannot merge scopes:`);
  log(
    `  ${printReactiveScopeSummary(current.scope)} ${[...current.scope.declarations.values()].map(decl => decl.identifier.declarationId)}`,
  );
  log(
    `  ${printReactiveScopeSummary(next.scope)} ${[...next.scope.dependencies].map(dep => `${dep.identifier.declarationId} ${temporaries.get(dep.identifier.declarationId) ?? dep.identifier.declarationId}`)}`,
  );
  return false;
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does canMergeScopes() do?
canMergeScopes() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/MergeReactiveScopesThatInvalidateTogether.ts.
Where is canMergeScopes() defined?
canMergeScopes() is defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/MergeReactiveScopesThatInvalidateTogether.ts at line 437.
What does canMergeScopes() call?
canMergeScopes() calls 5 function(s): Iterable_some, areEqualDependencies, isAlwaysInvalidatingType, log, printReactiveScopeSummary.
What calls canMergeScopes()?
canMergeScopes() is called by 1 function(s): visitBlock.

Analyze Your Own Codebase

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

Try Supermodel Free