|  | 
|  | 1 | +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | 
|  | 2 | +// This file is mostly copy-pasted from the original ReactIs library, | 
|  | 3 | +// with some modifications to support React versions < 19. | 
|  | 4 | +// https://github.com/facebook/react/blob/main/packages/react-is/src/ReactIs.js | 
|  | 5 | + | 
|  | 6 | +// Customizations: | 
|  | 7 | +// - Added support for REACT_LEGACY_ELEMENT_TYPE wherever REACT_ELEMENT_TYPE is used. | 
|  | 8 | + | 
|  | 9 | +// Types collected from https://github.com/facebook/react/blob/main/packages/shared/ReactSymbols.js | 
|  | 10 | +const REACT_LEGACY_ELEMENT_TYPE: symbol = Symbol.for('react.element'); | 
|  | 11 | +const REACT_ELEMENT_TYPE: symbol = Symbol.for('react.transitional.element'); | 
|  | 12 | +const REACT_PORTAL_TYPE: symbol = Symbol.for('react.portal'); | 
|  | 13 | +const REACT_FRAGMENT_TYPE: symbol = Symbol.for('react.fragment'); | 
|  | 14 | +const REACT_STRICT_MODE_TYPE: symbol = Symbol.for('react.strict_mode'); | 
|  | 15 | +const REACT_PROFILER_TYPE: symbol = Symbol.for('react.profiler'); | 
|  | 16 | +const REACT_CONSUMER_TYPE: symbol = Symbol.for('react.consumer'); | 
|  | 17 | +const REACT_CONTEXT_TYPE: symbol = Symbol.for('react.context'); | 
|  | 18 | +const REACT_FORWARD_REF_TYPE: symbol = Symbol.for('react.forward_ref'); | 
|  | 19 | +const REACT_SUSPENSE_TYPE: symbol = Symbol.for('react.suspense'); | 
|  | 20 | +const REACT_SUSPENSE_LIST_TYPE: symbol = Symbol.for('react.suspense_list'); | 
|  | 21 | +const REACT_MEMO_TYPE: symbol = Symbol.for('react.memo'); | 
|  | 22 | +const REACT_LAZY_TYPE: symbol = Symbol.for('react.lazy'); | 
|  | 23 | +const REACT_VIEW_TRANSITION_TYPE: symbol = Symbol.for('react.view_transition'); | 
|  | 24 | +const REACT_CLIENT_REFERENCE: symbol = Symbol.for('react.client.reference'); | 
|  | 25 | + | 
|  | 26 | +// Below is a copy of ReactIs.js with customizations listed above. | 
|  | 27 | + | 
|  | 28 | +export function typeOf(object: any): any { | 
|  | 29 | +  if (typeof object === 'object' && object !== null) { | 
|  | 30 | +    const $$typeof = object.$$typeof; | 
|  | 31 | +    switch ($$typeof) { | 
|  | 32 | +      case REACT_LEGACY_ELEMENT_TYPE: | 
|  | 33 | +      case REACT_ELEMENT_TYPE: | 
|  | 34 | +        const type = object.type; | 
|  | 35 | + | 
|  | 36 | +        switch (type) { | 
|  | 37 | +          case REACT_FRAGMENT_TYPE: | 
|  | 38 | +          case REACT_PROFILER_TYPE: | 
|  | 39 | +          case REACT_STRICT_MODE_TYPE: | 
|  | 40 | +          case REACT_SUSPENSE_TYPE: | 
|  | 41 | +          case REACT_SUSPENSE_LIST_TYPE: | 
|  | 42 | +          case REACT_VIEW_TRANSITION_TYPE: | 
|  | 43 | +            return type; | 
|  | 44 | +          default: | 
|  | 45 | +            const $$typeofType = type && type.$$typeof; | 
|  | 46 | + | 
|  | 47 | +            switch ($$typeofType) { | 
|  | 48 | +              case REACT_CONTEXT_TYPE: | 
|  | 49 | +              case REACT_FORWARD_REF_TYPE: | 
|  | 50 | +              case REACT_LAZY_TYPE: | 
|  | 51 | +              case REACT_MEMO_TYPE: | 
|  | 52 | +                return $$typeofType; | 
|  | 53 | +              case REACT_CONSUMER_TYPE: | 
|  | 54 | +                return $$typeofType; | 
|  | 55 | +              // Fall through | 
|  | 56 | +              default: | 
|  | 57 | +                return $$typeof; | 
|  | 58 | +            } | 
|  | 59 | +        } | 
|  | 60 | +      case REACT_PORTAL_TYPE: | 
|  | 61 | +        return $$typeof; | 
|  | 62 | +    } | 
|  | 63 | +  } | 
|  | 64 | + | 
|  | 65 | +  return undefined; | 
|  | 66 | +} | 
|  | 67 | + | 
|  | 68 | +export const ContextConsumer: symbol = REACT_CONSUMER_TYPE; | 
|  | 69 | +export const ContextProvider: symbol = REACT_CONTEXT_TYPE; | 
|  | 70 | +export const Element = REACT_ELEMENT_TYPE; | 
|  | 71 | +export const ForwardRef = REACT_FORWARD_REF_TYPE; | 
|  | 72 | +export const Fragment = REACT_FRAGMENT_TYPE; | 
|  | 73 | +export const Lazy = REACT_LAZY_TYPE; | 
|  | 74 | +export const Memo = REACT_MEMO_TYPE; | 
|  | 75 | +export const Portal = REACT_PORTAL_TYPE; | 
|  | 76 | +export const Profiler = REACT_PROFILER_TYPE; | 
|  | 77 | +export const StrictMode = REACT_STRICT_MODE_TYPE; | 
|  | 78 | +export const Suspense = REACT_SUSPENSE_TYPE; | 
|  | 79 | +export const SuspenseList = REACT_SUSPENSE_LIST_TYPE; | 
|  | 80 | + | 
|  | 81 | +export function isValidElementType(type: any): boolean { | 
|  | 82 | +  if (typeof type === 'string' || typeof type === 'function') { | 
|  | 83 | +    return true; | 
|  | 84 | +  } | 
|  | 85 | + | 
|  | 86 | +  // Note: typeof might be other than 'symbol' or 'number' (e.g. if it's a polyfill). | 
|  | 87 | +  if ( | 
|  | 88 | +    type === REACT_FRAGMENT_TYPE || | 
|  | 89 | +    type === REACT_PROFILER_TYPE || | 
|  | 90 | +    type === REACT_STRICT_MODE_TYPE || | 
|  | 91 | +    type === REACT_SUSPENSE_TYPE || | 
|  | 92 | +    type === REACT_SUSPENSE_LIST_TYPE | 
|  | 93 | +  ) { | 
|  | 94 | +    return true; | 
|  | 95 | +  } | 
|  | 96 | + | 
|  | 97 | +  if (typeof type === 'object' && type !== null) { | 
|  | 98 | +    if ( | 
|  | 99 | +      type.$$typeof === REACT_LAZY_TYPE || | 
|  | 100 | +      type.$$typeof === REACT_MEMO_TYPE || | 
|  | 101 | +      type.$$typeof === REACT_CONTEXT_TYPE || | 
|  | 102 | +      type.$$typeof === REACT_CONSUMER_TYPE || | 
|  | 103 | +      type.$$typeof === REACT_FORWARD_REF_TYPE || | 
|  | 104 | +      // This needs to include all possible module reference object | 
|  | 105 | +      // types supported by any Flight configuration anywhere since | 
|  | 106 | +      // we don't know which Flight build this will end up being used | 
|  | 107 | +      // with. | 
|  | 108 | +      type.$$typeof === REACT_CLIENT_REFERENCE || | 
|  | 109 | +      type.getModuleId !== undefined | 
|  | 110 | +    ) { | 
|  | 111 | +      return true; | 
|  | 112 | +    } | 
|  | 113 | +  } | 
|  | 114 | + | 
|  | 115 | +  return false; | 
|  | 116 | +} | 
|  | 117 | + | 
|  | 118 | +export function isContextConsumer(object: any): boolean { | 
|  | 119 | +  return typeOf(object) === REACT_CONSUMER_TYPE; | 
|  | 120 | +} | 
|  | 121 | +export function isContextProvider(object: any): boolean { | 
|  | 122 | +  return typeOf(object) === REACT_CONTEXT_TYPE; | 
|  | 123 | +} | 
|  | 124 | +export function isElement(object: any): boolean { | 
|  | 125 | +  return ( | 
|  | 126 | +    typeof object === 'object' && | 
|  | 127 | +    object !== null && | 
|  | 128 | +    [REACT_ELEMENT_TYPE, REACT_LEGACY_ELEMENT_TYPE].includes(object.$$typeof) | 
|  | 129 | +  ); | 
|  | 130 | +} | 
|  | 131 | +export function isForwardRef(object: any): boolean { | 
|  | 132 | +  return typeOf(object) === REACT_FORWARD_REF_TYPE; | 
|  | 133 | +} | 
|  | 134 | +export function isFragment(object: any): boolean { | 
|  | 135 | +  return typeOf(object) === REACT_FRAGMENT_TYPE; | 
|  | 136 | +} | 
|  | 137 | +export function isLazy(object: any): boolean { | 
|  | 138 | +  return typeOf(object) === REACT_LAZY_TYPE; | 
|  | 139 | +} | 
|  | 140 | +export function isMemo(object: any): boolean { | 
|  | 141 | +  return typeOf(object) === REACT_MEMO_TYPE; | 
|  | 142 | +} | 
|  | 143 | +export function isPortal(object: any): boolean { | 
|  | 144 | +  return typeOf(object) === REACT_PORTAL_TYPE; | 
|  | 145 | +} | 
|  | 146 | +export function isProfiler(object: any): boolean { | 
|  | 147 | +  return typeOf(object) === REACT_PROFILER_TYPE; | 
|  | 148 | +} | 
|  | 149 | +export function isStrictMode(object: any): boolean { | 
|  | 150 | +  return typeOf(object) === REACT_STRICT_MODE_TYPE; | 
|  | 151 | +} | 
|  | 152 | +export function isSuspense(object: any): boolean { | 
|  | 153 | +  return typeOf(object) === REACT_SUSPENSE_TYPE; | 
|  | 154 | +} | 
|  | 155 | +export function isSuspenseList(object: any): boolean { | 
|  | 156 | +  return typeOf(object) === REACT_SUSPENSE_LIST_TYPE; | 
|  | 157 | +} | 
0 commit comments