mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
37d901e2b8
Along with all the places using it like the `_debugSource` on Fiber. This still lets them be passed into `createElement` (and JSX dev runtime) since those can still be used in existing already compiled code and we don't want that to start spreading to DOM attributes. We used to have a DEV mode that compiles the source location of JSX into the compiled output. This was nice because we could get the actual call site of the JSX (instead of just somewhere in the component). It had a bunch of issues though: - It only works with JSX. - The way this source location is compiled is different in all the pipelines along the way. It relies on this transform being first and the source location we want to extract but it doesn't get preserved along source maps and don't have a way to be connected to the source hosted by the source maps. Ideally it should just use the mechanism other source maps use. - Since it's expensive it only works in DEV so if it's used for component stacks it would vary between dev and prod. - It only captures the callsite of the JSX and not the stack between the component and that callsite. In the happy case it's in the component but not always. Instead, we have another zero-cost trick to extract the call site of each component lazily only if it's needed. This ensures that component stacks are the same in DEV and PROD. At the cost of worse line number information. The better way to get the JSX call site would be to get it from `new Error()` or `console.createTask()` inside the JSX runtime which can capture the whole stack in a consistent way with other source mappings. We might explore that in the future. This removes source location info from React DevTools and React Native Inspector. The "jump to source code" feature or inspection can be made lazy instead by invoking the lazy component stack frame generation. That way it can be made to work in prod too. The filtering based on file path is a bit trickier. When redesigned this UI should ideally also account for more than one stack frame. With this change the DEV only Babel transforms are effectively deprecated since they're not necessary for anything.
260 lines
7.5 KiB
JavaScript
260 lines
7.5 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
|
|
import type {TouchedViewDataAtPoint, InspectorData} from './ReactNativeTypes';
|
|
|
|
import {
|
|
findCurrentHostFiber,
|
|
findCurrentFiberUsingSlowPath,
|
|
} from 'react-reconciler/src/ReactFiberTreeReflection';
|
|
import getComponentNameFromType from 'shared/getComponentNameFromType';
|
|
import {HostComponent} from 'react-reconciler/src/ReactWorkTags';
|
|
// Module provided by RN:
|
|
import {
|
|
UIManager,
|
|
getNodeFromPublicInstance,
|
|
} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
|
|
import {enableGetInspectorDataForInstanceInProduction} from 'shared/ReactFeatureFlags';
|
|
import {getClosestInstanceFromNode} from './ReactNativeComponentTree';
|
|
import {getNodeFromInternalInstanceHandle} from './ReactNativePublicCompat';
|
|
import {getStackByFiberInDevAndProd} from 'react-reconciler/src/ReactFiberComponentStack';
|
|
|
|
const emptyObject = {};
|
|
if (__DEV__) {
|
|
Object.freeze(emptyObject);
|
|
}
|
|
|
|
// $FlowFixMe[missing-local-annot]
|
|
function createHierarchy(fiberHierarchy) {
|
|
return fiberHierarchy.map(fiber => ({
|
|
name: getComponentNameFromType(fiber.type),
|
|
getInspectorData: findNodeHandle => {
|
|
return {
|
|
props: getHostProps(fiber),
|
|
measure: callback => {
|
|
// If this is Fabric, we'll find a shadow node and use that to measure.
|
|
const hostFiber = findCurrentHostFiber(fiber);
|
|
const node =
|
|
hostFiber != null &&
|
|
hostFiber.stateNode !== null &&
|
|
hostFiber.stateNode.node;
|
|
|
|
if (node) {
|
|
nativeFabricUIManager.measure(node, callback);
|
|
} else {
|
|
return UIManager.measure(
|
|
getHostNode(fiber, findNodeHandle),
|
|
callback,
|
|
);
|
|
}
|
|
},
|
|
};
|
|
},
|
|
}));
|
|
}
|
|
|
|
// $FlowFixMe[missing-local-annot]
|
|
function getHostNode(fiber: Fiber | null, findNodeHandle) {
|
|
if (__DEV__ || enableGetInspectorDataForInstanceInProduction) {
|
|
let hostNode;
|
|
// look for children first for the hostNode
|
|
// as composite fibers do not have a hostNode
|
|
while (fiber) {
|
|
if (fiber.stateNode !== null && fiber.tag === HostComponent) {
|
|
hostNode = findNodeHandle(fiber.stateNode);
|
|
}
|
|
if (hostNode) {
|
|
return hostNode;
|
|
}
|
|
fiber = fiber.child;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// $FlowFixMe[missing-local-annot]
|
|
function getHostProps(fiber) {
|
|
const host = findCurrentHostFiber(fiber);
|
|
if (host) {
|
|
return host.memoizedProps || emptyObject;
|
|
}
|
|
return emptyObject;
|
|
}
|
|
|
|
function getInspectorDataForInstance(
|
|
closestInstance: Fiber | null,
|
|
): InspectorData {
|
|
if (__DEV__ || enableGetInspectorDataForInstanceInProduction) {
|
|
// Handle case where user clicks outside of ReactNative
|
|
if (!closestInstance) {
|
|
return {
|
|
hierarchy: [],
|
|
props: emptyObject,
|
|
selectedIndex: null,
|
|
componentStack: '',
|
|
};
|
|
}
|
|
|
|
const fiber = findCurrentFiberUsingSlowPath(closestInstance);
|
|
const fiberHierarchy = getOwnerHierarchy(fiber);
|
|
const instance = lastNonHostInstance(fiberHierarchy);
|
|
const hierarchy = createHierarchy(fiberHierarchy);
|
|
const props = getHostProps(instance);
|
|
const selectedIndex = fiberHierarchy.indexOf(instance);
|
|
const componentStack =
|
|
fiber !== null ? getStackByFiberInDevAndProd(fiber) : '';
|
|
|
|
return {
|
|
closestInstance: instance,
|
|
hierarchy,
|
|
props,
|
|
selectedIndex,
|
|
componentStack,
|
|
};
|
|
}
|
|
|
|
throw new Error(
|
|
'getInspectorDataForInstance() is not available in production',
|
|
);
|
|
}
|
|
|
|
function getOwnerHierarchy(instance: any) {
|
|
const hierarchy: Array<$FlowFixMe> = [];
|
|
traverseOwnerTreeUp(hierarchy, instance);
|
|
return hierarchy;
|
|
}
|
|
|
|
// $FlowFixMe[missing-local-annot]
|
|
function lastNonHostInstance(hierarchy) {
|
|
for (let i = hierarchy.length - 1; i > 1; i--) {
|
|
const instance = hierarchy[i];
|
|
|
|
if (instance.tag !== HostComponent) {
|
|
return instance;
|
|
}
|
|
}
|
|
return hierarchy[0];
|
|
}
|
|
|
|
// $FlowFixMe[missing-local-annot]
|
|
function traverseOwnerTreeUp(
|
|
hierarchy: Array<$FlowFixMe>,
|
|
instance: any,
|
|
): void {
|
|
if (__DEV__ || enableGetInspectorDataForInstanceInProduction) {
|
|
if (instance) {
|
|
hierarchy.unshift(instance);
|
|
traverseOwnerTreeUp(hierarchy, instance._debugOwner);
|
|
}
|
|
}
|
|
}
|
|
|
|
function getInspectorDataForViewTag(viewTag: number): InspectorData {
|
|
if (__DEV__) {
|
|
const closestInstance = getClosestInstanceFromNode(viewTag);
|
|
|
|
return getInspectorDataForInstance(closestInstance);
|
|
} else {
|
|
throw new Error(
|
|
'getInspectorDataForViewTag() is not available in production',
|
|
);
|
|
}
|
|
}
|
|
|
|
function getInspectorDataForViewAtPoint(
|
|
findNodeHandle: (componentOrHandle: any) => ?number,
|
|
inspectedView: Object,
|
|
locationX: number,
|
|
locationY: number,
|
|
callback: (viewData: TouchedViewDataAtPoint) => mixed,
|
|
): void {
|
|
if (__DEV__) {
|
|
let closestInstance = null;
|
|
|
|
const fabricNode = getNodeFromPublicInstance(inspectedView);
|
|
if (fabricNode) {
|
|
// For Fabric we can look up the instance handle directly and measure it.
|
|
nativeFabricUIManager.findNodeAtPoint(
|
|
fabricNode,
|
|
locationX,
|
|
locationY,
|
|
internalInstanceHandle => {
|
|
const node =
|
|
internalInstanceHandle != null
|
|
? getNodeFromInternalInstanceHandle(internalInstanceHandle)
|
|
: null;
|
|
if (internalInstanceHandle == null || node == null) {
|
|
callback({
|
|
pointerY: locationY,
|
|
frame: {left: 0, top: 0, width: 0, height: 0},
|
|
...getInspectorDataForInstance(closestInstance),
|
|
});
|
|
return;
|
|
}
|
|
|
|
closestInstance =
|
|
internalInstanceHandle.stateNode.canonical.internalInstanceHandle;
|
|
|
|
// Note: this is deprecated and we want to remove it ASAP. Keeping it here for React DevTools compatibility for now.
|
|
const nativeViewTag =
|
|
internalInstanceHandle.stateNode.canonical.nativeTag;
|
|
|
|
nativeFabricUIManager.measure(
|
|
node,
|
|
(x, y, width, height, pageX, pageY) => {
|
|
const inspectorData =
|
|
getInspectorDataForInstance(closestInstance);
|
|
callback({
|
|
...inspectorData,
|
|
pointerY: locationY,
|
|
frame: {left: pageX, top: pageY, width, height},
|
|
touchedViewTag: nativeViewTag,
|
|
});
|
|
},
|
|
);
|
|
},
|
|
);
|
|
} else if (inspectedView._internalFiberInstanceHandleDEV != null) {
|
|
// For Paper we fall back to the old strategy using the React tag.
|
|
UIManager.findSubviewIn(
|
|
findNodeHandle(inspectedView),
|
|
[locationX, locationY],
|
|
(nativeViewTag, left, top, width, height) => {
|
|
const inspectorData = getInspectorDataForInstance(
|
|
getClosestInstanceFromNode(nativeViewTag),
|
|
);
|
|
callback({
|
|
...inspectorData,
|
|
pointerY: locationY,
|
|
frame: {left, top, width, height},
|
|
touchedViewTag: nativeViewTag,
|
|
});
|
|
},
|
|
);
|
|
} else {
|
|
console.error(
|
|
'getInspectorDataForViewAtPoint expects to receive a host component',
|
|
);
|
|
|
|
return;
|
|
}
|
|
} else {
|
|
throw new Error(
|
|
'getInspectorDataForViewAtPoint() is not available in production.',
|
|
);
|
|
}
|
|
}
|
|
|
|
export {
|
|
getInspectorDataForInstance,
|
|
getInspectorDataForViewAtPoint,
|
|
getInspectorDataForViewTag,
|
|
};
|