Skip to content

Commit

Permalink
Split getComponentName into getComponentNameFromFiber and getComponen…
Browse files Browse the repository at this point in the history
…tNameFromType (#20940)

Split getComponentName into getComponentNameFromFiber and getComponentNameFromType
  • Loading branch information
Brian Vaughn committed Mar 5, 2021
1 parent ee43263 commit 7df6572
Show file tree
Hide file tree
Showing 40 changed files with 386 additions and 308 deletions.
4 changes: 2 additions & 2 deletions packages/react-dom/src/client/ReactDOMLegacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
findHostInstance,
findHostInstanceWithWarning,
} from 'react-reconciler/src/ReactFiberReconciler';
import getComponentName from 'shared/getComponentName';
import getComponentNameFromType from 'shared/getComponentNameFromType';
import invariant from 'shared/invariant';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import {has as hasInstance} from 'shared/ReactInstanceMap';
Expand Down Expand Up @@ -235,7 +235,7 @@ export function findDOMNode(
'never access something that requires stale data from the previous ' +
'render, such as refs. Move this logic to componentDidMount and ' +
'componentDidUpdate instead.',
getComponentName(owner.type) || 'A component',
getComponentNameFromType(owner.type) || 'A component',
);
}
owner.stateNode._warnedAboutRefsInRender = true;
Expand Down
29 changes: 17 additions & 12 deletions packages/react-dom/src/server/ReactPartialRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {ReactProvider, ReactContext} from 'shared/ReactTypes';

import * as React from 'react';
import invariant from 'shared/invariant';
import getComponentName from 'shared/getComponentName';
import getComponentNameFromType from 'shared/getComponentNameFromType';
import {describeUnknownElementTypeFrameInDEV} from 'shared/ReactComponentStackFrame';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import {
Expand Down Expand Up @@ -259,7 +259,7 @@ function warnNoop(
if (__DEV__) {
const constructor = publicInstance.constructor;
const componentName =
(constructor && getComponentName(constructor)) || 'ReactClass';
(constructor && getComponentNameFromType(constructor)) || 'ReactClass';
const warningKey = componentName + '.' + callerName;
if (didWarnAboutNoopUpdateForComponent[warningKey]) {
return;
Expand Down Expand Up @@ -404,7 +404,7 @@ function validateRenderResult(child, type) {
'%s(...): Nothing was returned from render. This usually means a ' +
'return statement is missing. Or, to render nothing, ' +
'return null.',
getComponentName(type) || 'Component',
getComponentNameFromType(type) || 'Component',
);
}
}
Expand Down Expand Up @@ -467,7 +467,8 @@ function resolve(
if (typeof Component.getDerivedStateFromProps === 'function') {
if (__DEV__) {
if (inst.state === null || inst.state === undefined) {
const componentName = getComponentName(Component) || 'Unknown';
const componentName =
getComponentNameFromType(Component) || 'Unknown';
if (!didWarnAboutUninitializedState[componentName]) {
console.error(
'`%s` uses `getDerivedStateFromProps` but its initial state is ' +
Expand All @@ -491,7 +492,8 @@ function resolve(

if (__DEV__) {
if (partialState === undefined) {
const componentName = getComponentName(Component) || 'Unknown';
const componentName =
getComponentNameFromType(Component) || 'Unknown';
if (!didWarnAboutUndefinedDerivedState[componentName]) {
console.error(
'%s.getDerivedStateFromProps(): A valid state object (or null) must be returned. ' +
Expand All @@ -513,7 +515,8 @@ function resolve(
Component.prototype &&
typeof Component.prototype.render === 'function'
) {
const componentName = getComponentName(Component) || 'Unknown';
const componentName =
getComponentNameFromType(Component) || 'Unknown';

if (!didWarnAboutBadClass[componentName]) {
console.error(
Expand All @@ -535,7 +538,8 @@ function resolve(
// Support for module components is deprecated and is removed behind a flag.
// Whether or not it would crash later, we want to show a good message in DEV first.
if (inst != null && inst.render != null) {
const componentName = getComponentName(Component) || 'Unknown';
const componentName =
getComponentNameFromType(Component) || 'Unknown';
if (!didWarnAboutModulePatternComponent[componentName]) {
console.error(
'The <%s /> component appears to be a function component that returns a class instance. ' +
Expand Down Expand Up @@ -583,7 +587,8 @@ function resolve(
warnAboutDeprecatedLifecycles &&
inst.componentWillMount.__suppressDeprecationWarning !== true
) {
const componentName = getComponentName(Component) || 'Unknown';
const componentName =
getComponentNameFromType(Component) || 'Unknown';

if (!didWarnAboutDeprecatedWillMount[componentName]) {
console.warn(
Expand Down Expand Up @@ -665,7 +670,7 @@ function resolve(
console.error(
'%s uses the legacy childContextTypes API which is no longer supported. ' +
'Use React.createContext() instead.',
getComponentName(Component) || 'Unknown',
getComponentNameFromType(Component) || 'Unknown',
);
}
}
Expand All @@ -678,7 +683,7 @@ function resolve(
invariant(
contextKey in childContextTypes,
'%s.getChildContext(): key "%s" is not defined in childContextTypes.',
getComponentName(Component) || 'Unknown',
getComponentNameFromType(Component) || 'Unknown',
contextKey,
);
}
Expand All @@ -687,7 +692,7 @@ function resolve(
console.error(
'%s.getChildContext(): childContextTypes must be defined in order to ' +
'use getChildContext().',
getComponentName(Component) || 'Unknown',
getComponentNameFromType(Component) || 'Unknown',
);
}
}
Expand Down Expand Up @@ -1298,7 +1303,7 @@ class ReactDOMServerRenderer {
"it's defined in, or you might have mixed up default and " +
'named imports.';
}
const ownerName = owner ? getComponentName(owner) : null;
const ownerName = owner ? getComponentNameFromType(owner) : null;
if (ownerName) {
info += '\n\nCheck the render method of `' + ownerName + '`.';
}
Expand Down
8 changes: 4 additions & 4 deletions packages/react-dom/src/server/ReactPartialRendererContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {ReactContext} from 'shared/ReactTypes';

import {disableLegacyContext} from 'shared/ReactFeatureFlags';
import {REACT_CONTEXT_TYPE, REACT_PROVIDER_TYPE} from 'shared/ReactSymbols';
import getComponentName from 'shared/getComponentName';
import getComponentNameFromType from 'shared/getComponentNameFromType';
import checkPropTypes from 'shared/checkPropTypes';

let didWarnAboutInvalidateContextType;
Expand Down Expand Up @@ -105,7 +105,7 @@ export function processContext(
console.error(
'%s defines an invalid contextType. ' +
'contextType should point to the Context object returned by React.createContext().%s',
getComponentName(type) || 'Component',
getComponentNameFromType(type) || 'Component',
addendum,
);
}
Expand All @@ -121,7 +121,7 @@ export function processContext(
console.error(
'%s uses the legacy contextTypes API which is no longer supported. ' +
'Use React.createContext() with static contextType instead.',
getComponentName(type) || 'Unknown',
getComponentNameFromType(type) || 'Unknown',
);
}
}
Expand All @@ -142,7 +142,7 @@ export function processContext(
console.error(
'%s uses the legacy contextTypes API which is no longer supported. ' +
'Use React.createContext() with React.useContext() instead.',
getComponentName(type) || 'Unknown',
getComponentNameFromType(type) || 'Unknown',
);
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/react-native-renderer/src/ReactFabric.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {
} from './ReactNativeFiberInspector';
import {LegacyRoot} from 'react-reconciler/src/ReactRootTags';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import getComponentName from 'shared/getComponentName';
import getComponentNameFromType from 'shared/getComponentNameFromType';

const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;

Expand All @@ -60,7 +60,7 @@ function findHostInstance_DEPRECATED<TElementType: ElementType>(
'never access something that requires stale data from the previous ' +
'render, such as refs. Move this logic to componentDidMount and ' +
'componentDidUpdate instead.',
getComponentName(owner.type) || 'A component',
getComponentNameFromType(owner.type) || 'A component',
);
}

Expand Down Expand Up @@ -111,7 +111,7 @@ function findNodeHandle(componentOrHandle: any): ?number {
'never access something that requires stale data from the previous ' +
'render, such as refs. Move this logic to componentDidMount and ' +
'componentDidUpdate instead.',
getComponentName(owner.type) || 'A component',
getComponentNameFromType(owner.type) || 'A component',
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
findCurrentHostFiber,
findCurrentFiberUsingSlowPath,
} from 'react-reconciler/src/ReactFiberTreeReflection';
import getComponentName from 'shared/getComponentName';
import getComponentNameFromType from 'shared/getComponentNameFromType';
import {HostComponent} from 'react-reconciler/src/ReactWorkTags';
import invariant from 'shared/invariant';
// Module provided by RN:
Expand Down Expand Up @@ -81,7 +81,7 @@ if (__DEV__) {

const createHierarchy = function(fiberHierarchy) {
return fiberHierarchy.map(fiber => ({
name: getComponentName(fiber.type),
name: getComponentNameFromType(fiber.type),
getInspectorData: findNodeHandle => {
return {
props: getHostProps(fiber),
Expand Down
6 changes: 3 additions & 3 deletions packages/react-native-renderer/src/ReactNativeRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import {
} from './ReactNativeFiberInspector';
import {LegacyRoot} from 'react-reconciler/src/ReactRootTags';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import getComponentName from 'shared/getComponentName';
import getComponentNameFromType from 'shared/getComponentNameFromType';

const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;

Expand All @@ -63,7 +63,7 @@ function findHostInstance_DEPRECATED(
'never access something that requires stale data from the previous ' +
'render, such as refs. Move this logic to componentDidMount and ' +
'componentDidUpdate instead.',
getComponentName(owner.type) || 'A component',
getComponentNameFromType(owner.type) || 'A component',
);
}

Expand Down Expand Up @@ -110,7 +110,7 @@ function findNodeHandle(componentOrHandle: any): ?number {
'never access something that requires stale data from the previous ' +
'render, such as refs. Move this logic to componentDidMount and ' +
'componentDidUpdate instead.',
getComponentName(owner.type) || 'A component',
getComponentNameFromType(owner.type) || 'A component',
);
}

Expand Down
11 changes: 6 additions & 5 deletions packages/react-reconciler/src/ReactChildFiber.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {ReactPortal} from 'shared/ReactTypes';
import type {Fiber} from './ReactInternalTypes';
import type {Lanes} from './ReactFiberLane.new';

import getComponentName from 'shared/getComponentName';
import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber';
import {Placement, ChildDeletion} from './ReactFiberFlags';
import {
getIteratorFn,
Expand Down Expand Up @@ -82,7 +82,7 @@ if (__DEV__) {
);
child._store.validated = true;

const componentName = getComponentName(returnFiber.type) || 'Component';
const componentName = getComponentNameFromFiber(returnFiber) || 'Component';

if (ownerHasKeyUseWarning[componentName]) {
return;
Expand Down Expand Up @@ -124,7 +124,8 @@ function coerceRef(
element._owner.stateNode !== element._self
)
) {
const componentName = getComponentName(returnFiber.type) || 'Component';
const componentName =
getComponentNameFromFiber(returnFiber) || 'Component';
if (!didWarnAboutStringRefs[componentName]) {
if (warnAboutStringRefs) {
console.error(
Expand Down Expand Up @@ -232,7 +233,7 @@ function throwOnInvalidObjectType(returnFiber: Fiber, newChild: Object) {

function warnOnFunctionType(returnFiber: Fiber) {
if (__DEV__) {
const componentName = getComponentName(returnFiber.type) || 'Component';
const componentName = getComponentNameFromFiber(returnFiber) || 'Component';

if (ownerHasFunctionTypeWarning[componentName]) {
return;
Expand Down Expand Up @@ -1333,7 +1334,7 @@ function ChildReconciler(shouldTrackSideEffects) {
'%s(...): Nothing was returned from render. This usually means a ' +
'return statement is missing. Or, to render nothing, ' +
'return null.',
getComponentName(returnFiber.type) || 'Component',
getComponentNameFromFiber(returnFiber) || 'Component',
);
}
}
Expand Down
11 changes: 6 additions & 5 deletions packages/react-reconciler/src/ReactChildFiber.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {ReactPortal} from 'shared/ReactTypes';
import type {Fiber} from './ReactInternalTypes';
import type {Lanes} from './ReactFiberLane.old';

import getComponentName from 'shared/getComponentName';
import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber';
import {Placement, ChildDeletion} from './ReactFiberFlags';
import {
getIteratorFn,
Expand Down Expand Up @@ -82,7 +82,7 @@ if (__DEV__) {
);
child._store.validated = true;

const componentName = getComponentName(returnFiber.type) || 'Component';
const componentName = getComponentNameFromFiber(returnFiber) || 'Component';

if (ownerHasKeyUseWarning[componentName]) {
return;
Expand Down Expand Up @@ -124,7 +124,8 @@ function coerceRef(
element._owner.stateNode !== element._self
)
) {
const componentName = getComponentName(returnFiber.type) || 'Component';
const componentName =
getComponentNameFromFiber(returnFiber) || 'Component';
if (!didWarnAboutStringRefs[componentName]) {
if (warnAboutStringRefs) {
console.error(
Expand Down Expand Up @@ -232,7 +233,7 @@ function throwOnInvalidObjectType(returnFiber: Fiber, newChild: Object) {

function warnOnFunctionType(returnFiber: Fiber) {
if (__DEV__) {
const componentName = getComponentName(returnFiber.type) || 'Component';
const componentName = getComponentNameFromFiber(returnFiber) || 'Component';

if (ownerHasFunctionTypeWarning[componentName]) {
return;
Expand Down Expand Up @@ -1333,7 +1334,7 @@ function ChildReconciler(shouldTrackSideEffects) {
'%s(...): Nothing was returned from render. This usually means a ' +
'return statement is missing. Or, to render nothing, ' +
'return null.',
getComponentName(returnFiber.type) || 'Component',
getComponentNameFromFiber(returnFiber) || 'Component',
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/react-reconciler/src/ReactCurrentFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {Fiber} from './ReactInternalTypes';

import ReactSharedInternals from 'shared/ReactSharedInternals';
import {getStackByFiberInDevAndProd} from './ReactFiberComponentStack';
import getComponentName from 'shared/getComponentName';
import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber';

const ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;

Expand All @@ -25,7 +25,7 @@ export function getCurrentFiberOwnerNameInDevOrNull(): string | null {
}
const owner = current._debugOwner;
if (owner !== null && typeof owner !== 'undefined') {
return getComponentName(owner.type);
return getComponentNameFromFiber(owner);
}
}
return null;
Expand Down
Loading

0 comments on commit 7df6572

Please sign in to comment.