Home / Function/ getGlobalDeclaration() — react Function Reference

getGlobalDeclaration() — react Function Reference

Architecture documentation for the getGlobalDeclaration() function in Environment.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  17e4ff3c_e626_9c9b_1534_00488fccdf5c["getGlobalDeclaration()"]
  cba0c8a2_0db5_48e2_0d19_b2c6a46799e8["Environment"]
  17e4ff3c_e626_9c9b_1534_00488fccdf5c -->|defined in| cba0c8a2_0db5_48e2_0d19_b2c6a46799e8
  628fae4b_1807_dcbc_24d1_59d8ce7820a2["getPropertyType()"]
  17e4ff3c_e626_9c9b_1534_00488fccdf5c -->|calls| 628fae4b_1807_dcbc_24d1_59d8ce7820a2
  c32c4801_8f1e_6ab5_ff15_ac8ec7df6945["isHookName()"]
  17e4ff3c_e626_9c9b_1534_00488fccdf5c -->|calls| c32c4801_8f1e_6ab5_ff15_ac8ec7df6945
  67f87937_36ed_a7a2_a22b_50ebabdcd7ed["getHookKindForType()"]
  17e4ff3c_e626_9c9b_1534_00488fccdf5c -->|calls| 67f87937_36ed_a7a2_a22b_50ebabdcd7ed
  f4dabc03_d648_e2d6_19ef_83872ae711d3["throwInvalidConfig()"]
  17e4ff3c_e626_9c9b_1534_00488fccdf5c -->|calls| f4dabc03_d648_e2d6_19ef_83872ae711d3
  style 17e4ff3c_e626_9c9b_1534_00488fccdf5c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts lines 1028–1151

  getGlobalDeclaration(
    binding: NonLocalBinding,
    loc: SourceLocation,
  ): Global | null {
    if (this.config.hookPattern != null) {
      const match = new RegExp(this.config.hookPattern).exec(binding.name);
      if (
        match != null &&
        typeof match[1] === 'string' &&
        isHookName(match[1])
      ) {
        const resolvedName = match[1];
        return this.#globals.get(resolvedName) ?? this.#getCustomHookType();
      }
    }

    switch (binding.kind) {
      case 'ModuleLocal': {
        // don't resolve module locals
        return isHookName(binding.name) ? this.#getCustomHookType() : null;
      }
      case 'Global': {
        return (
          this.#globals.get(binding.name) ??
          (isHookName(binding.name) ? this.#getCustomHookType() : null)
        );
      }
      case 'ImportSpecifier': {
        if (this.#isKnownReactModule(binding.module)) {
          /**
           * For `import {imported as name} from "..."` form, we use the `imported`
           * name rather than the local alias. Because we don't have definitions for
           * every React builtin hook yet, we also check to see if the imported name
           * is hook-like (whereas the fall-through below is checking if the aliased
           * name is hook-like)
           */
          return (
            this.#globals.get(binding.imported) ??
            (isHookName(binding.imported) || isHookName(binding.name)
              ? this.#getCustomHookType()
              : null)
          );
        } else {
          const moduleType = this.#resolveModuleType(binding.module, loc);
          if (moduleType !== null) {
            const importedType = this.getPropertyType(
              moduleType,
              binding.imported,
            );
            if (importedType != null) {
              /*
               * Check that hook-like export names are hook types, and non-hook names are non-hook types.
               * The user-assigned alias isn't decidable by the type provider, so we ignore that for the check.
               * Thus we allow `import {fooNonHook as useFoo} from ...` because the name and type both say
               * that it's not a hook.
               */
              const expectHook = isHookName(binding.imported);
              const isHook = getHookKindForType(this, importedType) != null;
              if (expectHook !== isHook) {
                CompilerError.throwInvalidConfig({
                  reason: `Invalid type configuration for module`,
                  description: `Expected type for \`import {${binding.imported}} from '${binding.module}'\` ${expectHook ? 'to be a hook' : 'not to be a hook'} based on the exported name`,
                  loc,
                });
              }
              return importedType;
            }
          }

          /**
           * For modules we don't own, we look at whether the original name or import alias
           * are hook-like. Both of the following are likely hooks so we would return a hook
           * type for both:
           *
           * `import {useHook as foo} ...`
           * `import {foo as useHook} ...`
           */
          return isHookName(binding.imported) || isHookName(binding.name)
            ? this.#getCustomHookType()
            : null;
        }

Subdomains

Frequently Asked Questions

What does getGlobalDeclaration() do?
getGlobalDeclaration() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts.
Where is getGlobalDeclaration() defined?
getGlobalDeclaration() is defined in compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts at line 1028.
What does getGlobalDeclaration() call?
getGlobalDeclaration() calls 4 function(s): getHookKindForType, getPropertyType, isHookName, throwInvalidConfig.

Analyze Your Own Codebase

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

Try Supermodel Free