| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | 'use strict'; |
| 11 | |
| 12 | import { |
| 13 | REACT_CONTEXT_TYPE, |
| 14 | REACT_ELEMENT_TYPE, |
| 15 | REACT_FORWARD_REF_TYPE, |
| 16 | REACT_FRAGMENT_TYPE, |
| 17 | REACT_LAZY_TYPE, |
| 18 | REACT_MEMO_TYPE, |
| 19 | REACT_PORTAL_TYPE, |
| 20 | REACT_PROFILER_TYPE, |
| 21 | REACT_CONSUMER_TYPE, |
| 22 | REACT_STRICT_MODE_TYPE, |
| 23 | REACT_SUSPENSE_TYPE, |
| 24 | REACT_SUSPENSE_LIST_TYPE, |
| 25 | REACT_VIEW_TRANSITION_TYPE, |
| 26 | REACT_SCOPE_TYPE, |
| 27 | REACT_LEGACY_HIDDEN_TYPE, |
| 28 | REACT_TRACING_MARKER_TYPE, |
| 29 | } from 'shared/ReactSymbols'; |
| 30 | |
| 31 | import { |
| 32 | enableScopeAPI, |
| 33 | enableTransitionTracing, |
| 34 | enableLegacyHidden, |
| 35 | enableViewTransition, |
| 36 | } from 'shared/ReactFeatureFlags'; |
| 37 | |
| 38 | const REACT_CLIENT_REFERENCE: symbol = Symbol.for('react.client.reference'); |
| 39 | |
| 40 | export function typeOf(object: any): mixed { |
| 41 | if (typeof object === 'object' && object !== null) { |
| 42 | const $$typeof = object.$$typeof; |
| 43 | switch ($$typeof) { |
| 44 | case REACT_ELEMENT_TYPE: |
| 45 | const type = object.type; |
| 46 | |
| 47 | switch (type) { |
| 48 | case REACT_FRAGMENT_TYPE: |
| 49 | case REACT_PROFILER_TYPE: |
| 50 | case REACT_STRICT_MODE_TYPE: |
| 51 | case REACT_SUSPENSE_TYPE: |
| 52 | case REACT_SUSPENSE_LIST_TYPE: |
| 53 | case REACT_VIEW_TRANSITION_TYPE: |
| 54 | return type; |
| 55 | default: |
| 56 | const $$typeofType = type && type.$$typeof; |
| 57 | |
| 58 | switch ($$typeofType) { |
| 59 | case REACT_CONTEXT_TYPE: |
| 60 | case REACT_FORWARD_REF_TYPE: |
| 61 | case REACT_LAZY_TYPE: |
| 62 | case REACT_MEMO_TYPE: |
| 63 | return $$typeofType; |
| 64 | case REACT_CONSUMER_TYPE: |
| 65 | return $$typeofType; |
| 66 | // Fall through |
| 67 | default: |
| 68 | return $$typeof; |
| 69 | } |
| 70 | } |
| 71 | case REACT_PORTAL_TYPE: |
| 72 | return $$typeof; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return undefined; |
| 77 | } |
| 78 | |
| 79 | export const ContextConsumer: symbol = REACT_CONSUMER_TYPE; |
| 80 | export const ContextProvider: symbol = REACT_CONTEXT_TYPE; |
| 81 | export const Element = REACT_ELEMENT_TYPE; |
| 82 | export const ForwardRef = REACT_FORWARD_REF_TYPE; |
| 83 | export const Fragment = REACT_FRAGMENT_TYPE; |
| 84 | export const Lazy = REACT_LAZY_TYPE; |
| 85 | export const Memo = REACT_MEMO_TYPE; |
| 86 | export const Portal = REACT_PORTAL_TYPE; |
| 87 | export const Profiler = REACT_PROFILER_TYPE; |
| 88 | export const StrictMode = REACT_STRICT_MODE_TYPE; |
| 89 | export const Suspense = REACT_SUSPENSE_TYPE; |
| 90 | export const SuspenseList = REACT_SUSPENSE_LIST_TYPE; |
| 91 | |
| 92 | export function isValidElementType(type: mixed): boolean { |
| 93 | if (typeof type === 'string' || typeof type === 'function') { |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | // Note: typeof might be other than 'symbol' or 'number' (e.g. if it's a polyfill). |
| 98 | if ( |
| 99 | type === REACT_FRAGMENT_TYPE || |
| 100 | type === REACT_PROFILER_TYPE || |
| 101 | type === REACT_STRICT_MODE_TYPE || |
| 102 | type === REACT_SUSPENSE_TYPE || |
| 103 | type === REACT_SUSPENSE_LIST_TYPE || |
| 104 | (enableLegacyHidden && type === REACT_LEGACY_HIDDEN_TYPE) || |
| 105 | (enableScopeAPI && type === REACT_SCOPE_TYPE) || |
| 106 | (enableTransitionTracing && type === REACT_TRACING_MARKER_TYPE) || |
| 107 | (enableViewTransition && type === REACT_VIEW_TRANSITION_TYPE) |
| 108 | ) { |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | if (typeof type === 'object' && type !== null) { |
| 113 | if ( |
| 114 | type.$$typeof === REACT_LAZY_TYPE || |
| 115 | type.$$typeof === REACT_MEMO_TYPE || |
| 116 | type.$$typeof === REACT_CONTEXT_TYPE || |
| 117 | type.$$typeof === REACT_CONSUMER_TYPE || |
| 118 | type.$$typeof === REACT_FORWARD_REF_TYPE || |
| 119 | // This needs to include all possible module reference object |
| 120 | // types supported by any Flight configuration anywhere since |
| 121 | // we don't know which Flight build this will end up being used |
| 122 | // with. |
| 123 | type.$$typeof === REACT_CLIENT_REFERENCE || |
| 124 | type.getModuleId !== undefined |
| 125 | ) { |
| 126 | return true; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | export function isContextConsumer(object: any): boolean { |
| 134 | return typeOf(object) === REACT_CONSUMER_TYPE; |
| 135 | } |
| 136 | export function isContextProvider(object: any): boolean { |
| 137 | return typeOf(object) === REACT_CONTEXT_TYPE; |
| 138 | } |
| 139 | export function isElement(object: any): boolean { |
| 140 | return ( |
| 141 | typeof object === 'object' && |
| 142 | object !== null && |
| 143 | object.$$typeof === REACT_ELEMENT_TYPE |
| 144 | ); |
| 145 | } |
| 146 | export function isForwardRef(object: any): boolean { |
| 147 | return typeOf(object) === REACT_FORWARD_REF_TYPE; |
| 148 | } |
| 149 | export function isFragment(object: any): boolean { |
| 150 | return typeOf(object) === REACT_FRAGMENT_TYPE; |
| 151 | } |
| 152 | export function isLazy(object: any): boolean { |
| 153 | return typeOf(object) === REACT_LAZY_TYPE; |
| 154 | } |
| 155 | export function isMemo(object: any): boolean { |
| 156 | return typeOf(object) === REACT_MEMO_TYPE; |
| 157 | } |
| 158 | export function isPortal(object: any): boolean { |
| 159 | return typeOf(object) === REACT_PORTAL_TYPE; |
| 160 | } |
| 161 | export function isProfiler(object: any): boolean { |
| 162 | return typeOf(object) === REACT_PROFILER_TYPE; |
| 163 | } |
| 164 | export function isStrictMode(object: any): boolean { |
| 165 | return typeOf(object) === REACT_STRICT_MODE_TYPE; |
| 166 | } |
| 167 | export function isSuspense(object: any): boolean { |
| 168 | return typeOf(object) === REACT_SUSPENSE_TYPE; |
| 169 | } |
| 170 | export function isSuspenseList(object: any): boolean { |
| 171 | return typeOf(object) === REACT_SUSPENSE_LIST_TYPE; |
| 172 | } |