Home / Function/ visitInstructionId() — react Function Reference

visitInstructionId() — react Function Reference

Architecture documentation for the visitInstructionId() function in MergeOverlappingReactiveScopesHIR.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  8fe866b3_2a9c_9e4d_fd10_7ac8435fc8db["visitInstructionId()"]
  78244170_3d42_f86a_ea59_bce3e065fcfd["MergeOverlappingReactiveScopesHIR.ts"]
  8fe866b3_2a9c_9e4d_fd10_7ac8435fc8db -->|defined in| 78244170_3d42_f86a_ea59_bce3e065fcfd
  764ec9fb_0ee9_a9ca_abc4_d7c652d01a09["getOverlappingReactiveScopes()"]
  764ec9fb_0ee9_a9ca_abc4_d7c652d01a09 -->|calls| 8fe866b3_2a9c_9e4d_fd10_7ac8435fc8db
  style 8fe866b3_2a9c_9e4d_fd10_7ac8435fc8db fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/HIR/MergeOverlappingReactiveScopesHIR.ts lines 200–260

function visitInstructionId(
  id: InstructionId,
  {scopeEnds, scopeStarts}: ScopeInfo,
  {activeScopes, joined}: TraversalState,
): void {
  /**
   * Handle all scopes that end at this instruction.
   */
  const scopeEndTop = scopeEnds.at(-1);
  if (scopeEndTop != null && scopeEndTop.id <= id) {
    scopeEnds.pop();

    /**
     * Match scopes that end at this instruction with our stack of active
     * scopes (from traversal state). We need to sort these in descending
     * order of start IDs because the scopes stack is ordered as such
     */
    const scopesSortedStartDescending = [...scopeEndTop.scopes].sort(
      (a, b) => b.range.start - a.range.start,
    );
    for (const scope of scopesSortedStartDescending) {
      const idx = activeScopes.indexOf(scope);
      if (idx !== -1) {
        /**
         * Detect and merge all overlapping scopes. `activeScopes` is ordered
         * by scope start, so every active scope between a completed scope s
         * and the top of the stack (1) started later than s and (2) completes after s.
         */
        if (idx !== activeScopes.length - 1) {
          joined.union([scope, ...activeScopes.slice(idx + 1)]);
        }
        activeScopes.splice(idx, 1);
      }
    }
  }

  /**
   * Handle all scopes that begin at this instruction by adding them
   * to the scopes stack
   */
  const scopeStartTop = scopeStarts.at(-1);
  if (scopeStartTop != null && scopeStartTop.id <= id) {
    scopeStarts.pop();

    const scopesSortedEndDescending = [...scopeStartTop.scopes].sort(
      (a, b) => b.range.end - a.range.end,
    );
    activeScopes.push(...scopesSortedEndDescending);
    /**
     * Merge all identical scopes (ones with the same start and end),
     * as they end up with the same reactive block
     */
    for (let i = 1; i < scopesSortedEndDescending.length; i++) {
      const prev = scopesSortedEndDescending[i - 1];
      const curr = scopesSortedEndDescending[i];
      if (prev.range.end === curr.range.end) {
        joined.union([prev, curr]);
      }
    }
  }
}

Subdomains

Frequently Asked Questions

What does visitInstructionId() do?
visitInstructionId() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/HIR/MergeOverlappingReactiveScopesHIR.ts.
Where is visitInstructionId() defined?
visitInstructionId() is defined in compiler/packages/babel-plugin-react-compiler/src/HIR/MergeOverlappingReactiveScopesHIR.ts at line 200.
What calls visitInstructionId()?
visitInstructionId() is called by 1 function(s): getOverlappingReactiveScopes.

Analyze Your Own Codebase

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

Try Supermodel Free