Home / Function/ getHookNameForLocation() — react Function Reference

getHookNameForLocation() — react Function Reference

Architecture documentation for the getHookNameForLocation() function in getHookNameForLocation.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  e39d8e56_d264_6a32_839e_aeea5b5a0b9b["getHookNameForLocation()"]
  5ab6cc34_8610_de9f_93dc_9e7a8742ce9b["getHookNameForLocation.js"]
  e39d8e56_d264_6a32_839e_aeea5b5a0b9b -->|defined in| 5ab6cc34_8610_de9f_93dc_9e7a8742ce9b
  53395c34_7472_4850_0d26_5722d5c6d970["hookNameFor()"]
  53395c34_7472_4850_0d26_5722d5c6d970 -->|calls| e39d8e56_d264_6a32_839e_aeea5b5a0b9b
  style e39d8e56_d264_6a32_839e_aeea5b5a0b9b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/react-devtools-shared/src/hooks/getHookNameForLocation.js lines 32–85

export function getHookNameForLocation(
  location: Position,
  hookMap: HookMap,
): string | null {
  const {names, mappings} = hookMap;

  // The HookMap mappings are grouped by lines, so first we look up
  // which line of mappings covers the target location.
  // Note that we expect to find a line since all the locations in the
  // source code are guaranteed to map to a name, including a '<no-hook>'
  // name.
  const foundLine = binSearch(location, mappings, compareLinePositions);
  if (foundLine == null) {
    throw new Error(
      `Expected to find a line in the HookMap that covers the target location at line: ${location.line}, column: ${location.column}`,
    );
  }

  let foundEntry;
  const foundLineNumber = getLineNumberFromLine(foundLine);
  // The line found in the mappings will never be larger than the target
  // line, and vice-versa, so if the target line doesn't match the found
  // line, we immediately know that it must correspond to the last mapping
  // entry for that line.
  if (foundLineNumber !== location.line) {
    foundEntry = foundLine[foundLine.length - 1];
  } else {
    foundEntry = binSearch(location, foundLine, compareColumnPositions);
  }

  if (foundEntry == null) {
    throw new Error(
      `Expected to find a mapping in the HookMap that covers the target location at line: ${location.line}, column: ${location.column}`,
    );
  }

  const foundNameIndex = getHookNameIndexFromEntry(foundEntry);
  if (foundNameIndex == null) {
    throw new Error(
      `Expected to find a name index in the HookMap that covers the target location at line: ${location.line}, column: ${location.column}`,
    );
  }
  const foundName = names[foundNameIndex];
  if (foundName == null) {
    throw new Error(
      `Expected to find a name in the HookMap that covers the target location at line: ${location.line}, column: ${location.column}`,
    );
  }

  if (foundName === NO_HOOK_NAME) {
    return null;
  }
  return foundName;
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does getHookNameForLocation() do?
getHookNameForLocation() is a function in the react codebase, defined in packages/react-devtools-shared/src/hooks/getHookNameForLocation.js.
Where is getHookNameForLocation() defined?
getHookNameForLocation() is defined in packages/react-devtools-shared/src/hooks/getHookNameForLocation.js at line 32.
What calls getHookNameForLocation()?
getHookNameForLocation() is called by 1 function(s): hookNameFor.

Analyze Your Own Codebase

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

Try Supermodel Free