Home / File/ PruneUnusedScopes.ts — react Source File

PruneUnusedScopes.ts — react Source File

Architecture documentation for PruneUnusedScopes.ts, a typescript file in the react codebase. 9 imports, 0 dependents.

File typescript BabelCompiler Validation 9 imports 2 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  c273bd3a_951b_cab6_1196_8484358b901c["PruneUnusedScopes.ts"]
  18a78965_f593_105b_e5e8_07001321c2ec["HIR.ts"]
  c273bd3a_951b_cab6_1196_8484358b901c --> 18a78965_f593_105b_e5e8_07001321c2ec
  c0555545_1ea3_9d37_62ad_521eafe2daa8["ReactiveFunction"]
  c273bd3a_951b_cab6_1196_8484358b901c --> c0555545_1ea3_9d37_62ad_521eafe2daa8
  0fba6722_c385_0076_d75b_47ee3ff87ee3["ReactiveScopeBlock"]
  c273bd3a_951b_cab6_1196_8484358b901c --> 0fba6722_c385_0076_d75b_47ee3ff87ee3
  d2571adf_c2aa_1e5e_5c56_f26810a3829e["ReactiveStatement"]
  c273bd3a_951b_cab6_1196_8484358b901c --> d2571adf_c2aa_1e5e_5c56_f26810a3829e
  97848b70_4205_af8b_cc43_f635e9b08163["ReactiveTerminalStatement"]
  c273bd3a_951b_cab6_1196_8484358b901c --> 97848b70_4205_af8b_cc43_f635e9b08163
  21609915_b03a_fd75_b58a_4cb86ef9315b["visitors.ts"]
  c273bd3a_951b_cab6_1196_8484358b901c --> 21609915_b03a_fd75_b58a_4cb86ef9315b
  af3ace55_db6d_865e_92b9_81486f6af1e7["ReactiveFunctionTransform"]
  c273bd3a_951b_cab6_1196_8484358b901c --> af3ace55_db6d_865e_92b9_81486f6af1e7
  da7ad665_d709_433d_facd_dc3b2c4d34f5["Transformed"]
  c273bd3a_951b_cab6_1196_8484358b901c --> da7ad665_d709_433d_facd_dc3b2c4d34f5
  2435b5f8_41a6_0458_ba88_4479b965455f["visitReactiveFunction"]
  c273bd3a_951b_cab6_1196_8484358b901c --> 2435b5f8_41a6_0458_ba88_4479b965455f
  style c273bd3a_951b_cab6_1196_8484358b901c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import {
  ReactiveFunction,
  ReactiveScopeBlock,
  ReactiveStatement,
  ReactiveTerminalStatement,
} from '../HIR/HIR';
import {
  ReactiveFunctionTransform,
  Transformed,
  visitReactiveFunction,
} from './visitors';

// Converts scopes without outputs into regular blocks.
export function pruneUnusedScopes(fn: ReactiveFunction): void {
  visitReactiveFunction(fn, new Transform(), {
    hasReturnStatement: false,
  } as State);
}

type State = {
  hasReturnStatement: boolean;
};

class Transform extends ReactiveFunctionTransform<State> {
  override visitTerminal(stmt: ReactiveTerminalStatement, state: State): void {
    this.traverseTerminal(stmt, state);
    if (stmt.terminal.kind === 'return') {
      state.hasReturnStatement = true;
    }
  }
  override transformScope(
    scopeBlock: ReactiveScopeBlock,
    _state: State,
  ): Transformed<ReactiveStatement> {
    const scopeState: State = {hasReturnStatement: false};
    this.visitScope(scopeBlock, scopeState);
    if (
      !scopeState.hasReturnStatement &&
      scopeBlock.scope.reassignments.size === 0 &&
      (scopeBlock.scope.declarations.size === 0 ||
        /*
         * Can prune scopes where all declarations bubbled up from inner
         * scopes
         */
        !hasOwnDeclaration(scopeBlock))
    ) {
      return {
        kind: 'replace',
        value: {
          kind: 'pruned-scope',
          scope: scopeBlock.scope,
          instructions: scopeBlock.instructions,
        },
      };
    } else {
      return {kind: 'keep'};
    }
  }
}

/*
 * Does the scope block declare any values of its own? This can return
 * false if all the block's declarations are propagated from nested scopes.
 */
function hasOwnDeclaration(block: ReactiveScopeBlock): boolean {
  for (const declaration of block.scope.declarations.values()) {
    if (declaration.scope.id === block.scope.id) {
      return true;
    }
  }
  return false;
}

Domain

Subdomains

Classes

Types

Frequently Asked Questions

What does PruneUnusedScopes.ts do?
PruneUnusedScopes.ts is a source file in the react codebase, written in typescript. It belongs to the BabelCompiler domain, Validation subdomain.
What functions are defined in PruneUnusedScopes.ts?
PruneUnusedScopes.ts defines 2 function(s): hasOwnDeclaration, pruneUnusedScopes.
What does PruneUnusedScopes.ts depend on?
PruneUnusedScopes.ts imports 9 module(s): HIR.ts, ReactiveFunction, ReactiveFunctionTransform, ReactiveScopeBlock, ReactiveStatement, ReactiveTerminalStatement, Transformed, visitReactiveFunction, and 1 more.
Where is PruneUnusedScopes.ts in the architecture?
PruneUnusedScopes.ts is located at compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PruneUnusedScopes.ts (domain: BabelCompiler, subdomain: Validation, directory: compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes).

Analyze Your Own Codebase

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

Try Supermodel Free