Home / Function/ traverseSegments() — react Function Reference

traverseSegments() — react Function Reference

Architecture documentation for the traverseSegments() function in code-path.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  3130e823_1a1d_c8ab_f87c_5946541f48b5["traverseSegments()"]
  9d0ad6be_3800_8694_06ab_1b89f16d8205["CodePath"]
  3130e823_1a1d_c8ab_f87c_5946541f48b5 -->|defined in| 9d0ad6be_3800_8694_06ab_1b89f16d8205
  style 3130e823_1a1d_c8ab_f87c_5946541f48b5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/eslint-plugin-react-hooks/src/code-path-analysis/code-path.js lines 137–236

  traverseSegments(options, callback) {
    let resolvedOptions;
    let resolvedCallback;

    if (typeof options === 'function') {
      resolvedCallback = options;
      resolvedOptions = {};
    } else {
      resolvedOptions = options || {};
      resolvedCallback = callback;
    }

    const startSegment = resolvedOptions.first || this.internal.initialSegment;
    const lastSegment = resolvedOptions.last;

    let item = null;
    let index = 0;
    let end = 0;
    let segment = null;
    const visited = Object.create(null);
    const stack = [[startSegment, 0]];
    let skippedSegment = null;
    let broken = false;
    const controller = {
      skip() {
        if (stack.length <= 1) {
          broken = true;
        } else {
          skippedSegment = stack[stack.length - 2][0];
        }
      },
      break() {
        broken = true;
      },
    };

    /**
     * Checks a given previous segment has been visited.
     * @param {CodePathSegment} prevSegment A previous segment to check.
     * @returns {boolean} `true` if the segment has been visited.
     */
    function isVisited(prevSegment) {
      return (
        visited[prevSegment.id] || segment.isLoopedPrevSegment(prevSegment)
      );
    }

    while (stack.length > 0) {
      item = stack[stack.length - 1];
      segment = item[0];
      index = item[1];

      if (index === 0) {
        // Skip if this segment has been visited already.
        if (visited[segment.id]) {
          stack.pop();
          continue;
        }

        // Skip if all previous segments have not been visited.
        if (
          segment !== startSegment &&
          segment.prevSegments.length > 0 &&
          !segment.prevSegments.every(isVisited)
        ) {
          stack.pop();
          continue;
        }

        // Reset the flag of skipping if all branches have been skipped.
        if (skippedSegment && segment.prevSegments.includes(skippedSegment)) {
          skippedSegment = null;
        }
        visited[segment.id] = true;

        // Call the callback when the first time.
        if (!skippedSegment) {
          resolvedCallback.call(this, segment, controller);
          if (segment === lastSegment) {
            controller.skip();
          }

Domain

Subdomains

Frequently Asked Questions

What does traverseSegments() do?
traverseSegments() is a function in the react codebase, defined in packages/eslint-plugin-react-hooks/src/code-path-analysis/code-path.js.
Where is traverseSegments() defined?
traverseSegments() is defined in packages/eslint-plugin-react-hooks/src/code-path-analysis/code-path.js at line 137.

Analyze Your Own Codebase

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

Try Supermodel Free