Home / File/ utils.js — react Source File

utils.js — react Source File

Architecture documentation for utils.js, a javascript file in the react codebase. 1 imports, 3 dependents.

File javascript BabelCompiler Validation 1 imports 3 dependents 1 functions

Entity Profile

Dependency Diagram

graph LR
  df34d6bc_88ee_9d70_cb1b_fbe686dca6da["utils.js"]
  d8f20c67_f5fa_0f0a_c967_c41fd9ffce07["ReactTypes"]
  df34d6bc_88ee_9d70_cb1b_fbe686dca6da --> d8f20c67_f5fa_0f0a_c967_c41fd9ffce07
  b5bf8091_2b63_1c9d_a667_8b4f387e11b9["OpenInEditorButton.js"]
  b5bf8091_2b63_1c9d_a667_8b4f387e11b9 --> df34d6bc_88ee_9d70_cb1b_fbe686dca6da
  5eb75744_619e_d7d3_c988_8d5803563f16["OpenInEditorButton.js"]
  5eb75744_619e_d7d3_c988_8d5803563f16 --> df34d6bc_88ee_9d70_cb1b_fbe686dca6da
  4d6a124e_fdc4_9da9_8ac7_85f75cd106fe["useOpenResource.js"]
  4d6a124e_fdc4_9da9_8ac7_85f75cd106fe --> df34d6bc_88ee_9d70_cb1b_fbe686dca6da
  style df34d6bc_88ee_9d70_cb1b_fbe686dca6da 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.
 *
 * @flow
 */

import type {ReactFunctionLocation, ReactCallSite} from 'shared/ReactTypes';

export function checkConditions(
  editorURL: string,
  source: ReactFunctionLocation | ReactCallSite,
): {url: URL | null, shouldDisableButton: boolean} {
  try {
    const url = new URL(editorURL);

    const [, sourceURL, line, column] = source;
    let filePath;

    // Check if sourceURL is a correct URL, which has a protocol specified
    if (sourceURL.startsWith('file:///')) {
      filePath = new URL(sourceURL).pathname;
    } else if (sourceURL.includes('://')) {
      // $FlowFixMe[cannot-resolve-name]
      if (!__IS_INTERNAL_VERSION__) {
        // In this case, we can't really determine the path to a file, disable a button
        return {url: null, shouldDisableButton: true};
      } else {
        const endOfSourceMapURLPattern = '.js/';
        const endOfSourceMapURLIndex = sourceURL.lastIndexOf(
          endOfSourceMapURLPattern,
        );

        if (endOfSourceMapURLIndex === -1) {
          return {url: null, shouldDisableButton: true};
        } else {
          filePath = sourceURL.slice(
            endOfSourceMapURLIndex + endOfSourceMapURLPattern.length,
            sourceURL.length,
          );
        }
      }
    } else {
      filePath = sourceURL;
    }

    const lineNumberAsString = String(line);
    const columnNumberAsString = String(column);

    url.href = url.href
      .replace('{path}', filePath)
      .replace('{line}', lineNumberAsString)
      .replace('{column}', columnNumberAsString)
      .replace('%7Bpath%7D', filePath)
      .replace('%7Bline%7D', lineNumberAsString)
      .replace('%7Bcolumn%7D', columnNumberAsString);

    return {url, shouldDisableButton: false};
  } catch (e) {
    // User has provided incorrect editor url
    return {url: null, shouldDisableButton: true};
  }
}

Domain

Subdomains

Functions

Dependencies

  • ReactTypes

Frequently Asked Questions

What does utils.js do?
utils.js is a source file in the react codebase, written in javascript. It belongs to the BabelCompiler domain, Validation subdomain.
What functions are defined in utils.js?
utils.js defines 1 function(s): checkConditions.
What does utils.js depend on?
utils.js imports 1 module(s): ReactTypes.
What files import utils.js?
utils.js is imported by 3 file(s): OpenInEditorButton.js, OpenInEditorButton.js, useOpenResource.js.
Where is utils.js in the architecture?
utils.js is located at packages/react-devtools-shared/src/devtools/views/Editor/utils.js (domain: BabelCompiler, subdomain: Validation, directory: packages/react-devtools-shared/src/devtools/views/Editor).

Analyze Your Own Codebase

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

Try Supermodel Free