mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
f9358c51c8
* Use %s in the console calls * Add shared/warningWithStack * Convert some warning callsites to warningWithStack * Use warningInStack in shared utilities and remove unnecessary checks * Replace more warning() calls with warningWithStack() * Fixes after rebase + use warningWithStack in react * Make warning have stack by default; warningWithoutStack opts out * Forbid builds that may not use internals * Revert newly added stacks I changed my mind and want to keep this PR without functional changes. So we won't "fix" any warnings that are already missing stacks. We'll do it in follow-ups instead. * Fix silly find/replace mistake * Reorder imports * Add protection against warning argument count mismatches * Address review
75 lines
1.9 KiB
JavaScript
75 lines
1.9 KiB
JavaScript
/**
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
import warningWithoutStack from 'shared/warningWithoutStack';
|
|
import {
|
|
REACT_ASYNC_MODE_TYPE,
|
|
REACT_CONTEXT_TYPE,
|
|
REACT_FORWARD_REF_TYPE,
|
|
REACT_FRAGMENT_TYPE,
|
|
REACT_PORTAL_TYPE,
|
|
REACT_PROFILER_TYPE,
|
|
REACT_PROVIDER_TYPE,
|
|
REACT_STRICT_MODE_TYPE,
|
|
REACT_PLACEHOLDER_TYPE,
|
|
} from 'shared/ReactSymbols';
|
|
|
|
function getComponentName(type: mixed): string | null {
|
|
if (type == null) {
|
|
// Host root, text node or just invalid type.
|
|
return null;
|
|
}
|
|
if (__DEV__) {
|
|
if (typeof (type: any).tag === 'number') {
|
|
warningWithoutStack(
|
|
false,
|
|
'Received an unexpected object in getComponentName(). ' +
|
|
'This is likely a bug in React. Please file an issue.',
|
|
);
|
|
}
|
|
}
|
|
if (typeof type === 'function') {
|
|
return type.displayName || type.name || null;
|
|
}
|
|
if (typeof type === 'string') {
|
|
return type;
|
|
}
|
|
switch (type) {
|
|
case REACT_ASYNC_MODE_TYPE:
|
|
return 'AsyncMode';
|
|
case REACT_FRAGMENT_TYPE:
|
|
return 'Fragment';
|
|
case REACT_PORTAL_TYPE:
|
|
return 'Portal';
|
|
case REACT_PROFILER_TYPE:
|
|
return `Profiler`;
|
|
case REACT_STRICT_MODE_TYPE:
|
|
return 'StrictMode';
|
|
case REACT_PLACEHOLDER_TYPE:
|
|
return 'Placeholder';
|
|
}
|
|
if (typeof type === 'object') {
|
|
switch (type.$$typeof) {
|
|
case REACT_CONTEXT_TYPE:
|
|
return 'Context.Consumer';
|
|
case REACT_PROVIDER_TYPE:
|
|
return 'Context.Provider';
|
|
case REACT_FORWARD_REF_TYPE:
|
|
const renderFn = (type.render: any);
|
|
const functionName = renderFn.displayName || renderFn.name || '';
|
|
return functionName !== ''
|
|
? `ForwardRef(${functionName})`
|
|
: 'ForwardRef';
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export default getComponentName;
|