mirror of
https://github.com/zebrajr/react.git
synced 2025-12-06 12:20:20 +01:00
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.
383 lines
13 KiB
JavaScript
383 lines
13 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 {LazyComponent} from 'react/src/ReactLazy';
|
|
|
|
import {enableComponentStackLocations} from 'shared/ReactFeatureFlags';
|
|
|
|
import {
|
|
REACT_SUSPENSE_TYPE,
|
|
REACT_SUSPENSE_LIST_TYPE,
|
|
REACT_FORWARD_REF_TYPE,
|
|
REACT_MEMO_TYPE,
|
|
REACT_LAZY_TYPE,
|
|
} from 'shared/ReactSymbols';
|
|
|
|
import {disableLogs, reenableLogs} from 'shared/ConsolePatchingDev';
|
|
|
|
import ReactSharedInternals from 'shared/ReactSharedInternals';
|
|
|
|
const {ReactCurrentDispatcher} = ReactSharedInternals;
|
|
|
|
let prefix;
|
|
export function describeBuiltInComponentFrame(
|
|
name: string,
|
|
ownerFn: void | null | Function,
|
|
): string {
|
|
if (enableComponentStackLocations) {
|
|
if (prefix === undefined) {
|
|
// Extract the VM specific prefix used by each line.
|
|
try {
|
|
throw Error();
|
|
} catch (x) {
|
|
const match = x.stack.trim().match(/\n( *(at )?)/);
|
|
prefix = (match && match[1]) || '';
|
|
}
|
|
}
|
|
// We use the prefix to ensure our stacks line up with native stack frames.
|
|
return '\n' + prefix + name;
|
|
} else {
|
|
let ownerName = null;
|
|
if (__DEV__ && ownerFn) {
|
|
ownerName = ownerFn.displayName || ownerFn.name || null;
|
|
}
|
|
return describeComponentFrame(name, ownerName);
|
|
}
|
|
}
|
|
|
|
let reentry = false;
|
|
let componentFrameCache;
|
|
if (__DEV__) {
|
|
const PossiblyWeakMap = typeof WeakMap === 'function' ? WeakMap : Map;
|
|
componentFrameCache = new PossiblyWeakMap<Function, string>();
|
|
}
|
|
|
|
/**
|
|
* Leverages native browser/VM stack frames to get proper details (e.g.
|
|
* filename, line + col number) for a single component in a component stack. We
|
|
* do this by:
|
|
* (1) throwing and catching an error in the function - this will be our
|
|
* control error.
|
|
* (2) calling the component which will eventually throw an error that we'll
|
|
* catch - this will be our sample error.
|
|
* (3) diffing the control and sample error stacks to find the stack frame
|
|
* which represents our component.
|
|
*/
|
|
export function describeNativeComponentFrame(
|
|
fn: Function,
|
|
construct: boolean,
|
|
): string {
|
|
// If something asked for a stack inside a fake render, it should get ignored.
|
|
if (!fn || reentry) {
|
|
return '';
|
|
}
|
|
|
|
if (__DEV__) {
|
|
const frame = componentFrameCache.get(fn);
|
|
if (frame !== undefined) {
|
|
return frame;
|
|
}
|
|
}
|
|
|
|
reentry = true;
|
|
const previousPrepareStackTrace = Error.prepareStackTrace;
|
|
// $FlowFixMe[incompatible-type] It does accept undefined.
|
|
Error.prepareStackTrace = undefined;
|
|
let previousDispatcher;
|
|
|
|
if (__DEV__) {
|
|
previousDispatcher = ReactCurrentDispatcher.current;
|
|
// Set the dispatcher in DEV because this might be call in the render function
|
|
// for warnings.
|
|
ReactCurrentDispatcher.current = null;
|
|
disableLogs();
|
|
}
|
|
|
|
/**
|
|
* Finding a common stack frame between sample and control errors can be
|
|
* tricky given the different types and levels of stack trace truncation from
|
|
* different JS VMs. So instead we'll attempt to control what that common
|
|
* frame should be through this object method:
|
|
* Having both the sample and control errors be in the function under the
|
|
* `DescribeNativeComponentFrameRoot` property, + setting the `name` and
|
|
* `displayName` properties of the function ensures that a stack
|
|
* frame exists that has the method name `DescribeNativeComponentFrameRoot` in
|
|
* it for both control and sample stacks.
|
|
*/
|
|
const RunInRootFrame = {
|
|
DetermineComponentFrameRoot(): [?string, ?string] {
|
|
let control;
|
|
try {
|
|
// This should throw.
|
|
if (construct) {
|
|
// Something should be setting the props in the constructor.
|
|
const Fake = function () {
|
|
throw Error();
|
|
};
|
|
// $FlowFixMe[prop-missing]
|
|
Object.defineProperty(Fake.prototype, 'props', {
|
|
set: function () {
|
|
// We use a throwing setter instead of frozen or non-writable props
|
|
// because that won't throw in a non-strict mode function.
|
|
throw Error();
|
|
},
|
|
});
|
|
if (typeof Reflect === 'object' && Reflect.construct) {
|
|
// We construct a different control for this case to include any extra
|
|
// frames added by the construct call.
|
|
try {
|
|
Reflect.construct(Fake, []);
|
|
} catch (x) {
|
|
control = x;
|
|
}
|
|
Reflect.construct(fn, [], Fake);
|
|
} else {
|
|
try {
|
|
Fake.call();
|
|
} catch (x) {
|
|
control = x;
|
|
}
|
|
// $FlowFixMe[prop-missing] found when upgrading Flow
|
|
fn.call(Fake.prototype);
|
|
}
|
|
} else {
|
|
try {
|
|
throw Error();
|
|
} catch (x) {
|
|
control = x;
|
|
}
|
|
// TODO(luna): This will currently only throw if the function component
|
|
// tries to access React/ReactDOM/props. We should probably make this throw
|
|
// in simple components too
|
|
const maybePromise = fn();
|
|
|
|
// If the function component returns a promise, it's likely an async
|
|
// component, which we don't yet support. Attach a noop catch handler to
|
|
// silence the error.
|
|
// TODO: Implement component stacks for async client components?
|
|
if (maybePromise && typeof maybePromise.catch === 'function') {
|
|
maybePromise.catch(() => {});
|
|
}
|
|
}
|
|
} catch (sample) {
|
|
// This is inlined manually because closure doesn't do it for us.
|
|
if (sample && control && typeof sample.stack === 'string') {
|
|
return [sample.stack, control.stack];
|
|
}
|
|
}
|
|
return [null, null];
|
|
},
|
|
};
|
|
// $FlowFixMe[prop-missing]
|
|
RunInRootFrame.DetermineComponentFrameRoot.displayName =
|
|
'DetermineComponentFrameRoot';
|
|
const namePropDescriptor = Object.getOwnPropertyDescriptor(
|
|
RunInRootFrame.DetermineComponentFrameRoot,
|
|
'name',
|
|
);
|
|
// Before ES6, the `name` property was not configurable.
|
|
if (namePropDescriptor && namePropDescriptor.configurable) {
|
|
// V8 utilizes a function's `name` property when generating a stack trace.
|
|
Object.defineProperty(
|
|
RunInRootFrame.DetermineComponentFrameRoot,
|
|
// Configurable properties can be updated even if its writable descriptor
|
|
// is set to `false`.
|
|
// $FlowFixMe[cannot-write]
|
|
'name',
|
|
{value: 'DetermineComponentFrameRoot'},
|
|
);
|
|
}
|
|
|
|
try {
|
|
const [sampleStack, controlStack] =
|
|
RunInRootFrame.DetermineComponentFrameRoot();
|
|
if (sampleStack && controlStack) {
|
|
// This extracts the first frame from the sample that isn't also in the control.
|
|
// Skipping one frame that we assume is the frame that calls the two.
|
|
const sampleLines = sampleStack.split('\n');
|
|
const controlLines = controlStack.split('\n');
|
|
let s = 0;
|
|
let c = 0;
|
|
while (
|
|
s < sampleLines.length &&
|
|
!sampleLines[s].includes('DetermineComponentFrameRoot')
|
|
) {
|
|
s++;
|
|
}
|
|
while (
|
|
c < controlLines.length &&
|
|
!controlLines[c].includes('DetermineComponentFrameRoot')
|
|
) {
|
|
c++;
|
|
}
|
|
// We couldn't find our intentionally injected common root frame, attempt
|
|
// to find another common root frame by search from the bottom of the
|
|
// control stack...
|
|
if (s === sampleLines.length || c === controlLines.length) {
|
|
s = sampleLines.length - 1;
|
|
c = controlLines.length - 1;
|
|
while (s >= 1 && c >= 0 && sampleLines[s] !== controlLines[c]) {
|
|
// We expect at least one stack frame to be shared.
|
|
// Typically this will be the root most one. However, stack frames may be
|
|
// cut off due to maximum stack limits. In this case, one maybe cut off
|
|
// earlier than the other. We assume that the sample is longer or the same
|
|
// and there for cut off earlier. So we should find the root most frame in
|
|
// the sample somewhere in the control.
|
|
c--;
|
|
}
|
|
}
|
|
for (; s >= 1 && c >= 0; s--, c--) {
|
|
// Next we find the first one that isn't the same which should be the
|
|
// frame that called our sample function and the control.
|
|
if (sampleLines[s] !== controlLines[c]) {
|
|
// In V8, the first line is describing the message but other VMs don't.
|
|
// If we're about to return the first line, and the control is also on the same
|
|
// line, that's a pretty good indicator that our sample threw at same line as
|
|
// the control. I.e. before we entered the sample frame. So we ignore this result.
|
|
// This can happen if you passed a class to function component, or non-function.
|
|
if (s !== 1 || c !== 1) {
|
|
do {
|
|
s--;
|
|
c--;
|
|
// We may still have similar intermediate frames from the construct call.
|
|
// The next one that isn't the same should be our match though.
|
|
if (c < 0 || sampleLines[s] !== controlLines[c]) {
|
|
// V8 adds a "new" prefix for native classes. Let's remove it to make it prettier.
|
|
let frame = '\n' + sampleLines[s].replace(' at new ', ' at ');
|
|
|
|
// If our component frame is labeled "<anonymous>"
|
|
// but we have a user-provided "displayName"
|
|
// splice it in to make the stack more readable.
|
|
if (fn.displayName && frame.includes('<anonymous>')) {
|
|
frame = frame.replace('<anonymous>', fn.displayName);
|
|
}
|
|
|
|
if (__DEV__) {
|
|
if (typeof fn === 'function') {
|
|
componentFrameCache.set(fn, frame);
|
|
}
|
|
}
|
|
// Return the line we found.
|
|
return frame;
|
|
}
|
|
} while (s >= 1 && c >= 0);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
} finally {
|
|
reentry = false;
|
|
if (__DEV__) {
|
|
ReactCurrentDispatcher.current = previousDispatcher;
|
|
reenableLogs();
|
|
}
|
|
Error.prepareStackTrace = previousPrepareStackTrace;
|
|
}
|
|
// Fallback to just using the name if we couldn't make it throw.
|
|
const name = fn ? fn.displayName || fn.name : '';
|
|
const syntheticFrame = name ? describeBuiltInComponentFrame(name) : '';
|
|
if (__DEV__) {
|
|
if (typeof fn === 'function') {
|
|
componentFrameCache.set(fn, syntheticFrame);
|
|
}
|
|
}
|
|
return syntheticFrame;
|
|
}
|
|
|
|
function describeComponentFrame(name: null | string, ownerName: null | string) {
|
|
let sourceInfo = '';
|
|
if (ownerName) {
|
|
sourceInfo = ' (created by ' + ownerName + ')';
|
|
}
|
|
return '\n in ' + (name || 'Unknown') + sourceInfo;
|
|
}
|
|
|
|
export function describeClassComponentFrame(
|
|
ctor: Function,
|
|
ownerFn: void | null | Function,
|
|
): string {
|
|
if (enableComponentStackLocations) {
|
|
return describeNativeComponentFrame(ctor, true);
|
|
} else {
|
|
return describeFunctionComponentFrame(ctor, ownerFn);
|
|
}
|
|
}
|
|
|
|
export function describeFunctionComponentFrame(
|
|
fn: Function,
|
|
ownerFn: void | null | Function,
|
|
): string {
|
|
if (enableComponentStackLocations) {
|
|
return describeNativeComponentFrame(fn, false);
|
|
} else {
|
|
if (!fn) {
|
|
return '';
|
|
}
|
|
const name = fn.displayName || fn.name || null;
|
|
let ownerName = null;
|
|
if (__DEV__ && ownerFn) {
|
|
ownerName = ownerFn.displayName || ownerFn.name || null;
|
|
}
|
|
return describeComponentFrame(name, ownerName);
|
|
}
|
|
}
|
|
|
|
function shouldConstruct(Component: Function) {
|
|
const prototype = Component.prototype;
|
|
return !!(prototype && prototype.isReactComponent);
|
|
}
|
|
|
|
export function describeUnknownElementTypeFrameInDEV(
|
|
type: any,
|
|
ownerFn: void | null | Function,
|
|
): string {
|
|
if (!__DEV__) {
|
|
return '';
|
|
}
|
|
if (type == null) {
|
|
return '';
|
|
}
|
|
if (typeof type === 'function') {
|
|
if (enableComponentStackLocations) {
|
|
return describeNativeComponentFrame(type, shouldConstruct(type));
|
|
} else {
|
|
return describeFunctionComponentFrame(type, ownerFn);
|
|
}
|
|
}
|
|
if (typeof type === 'string') {
|
|
return describeBuiltInComponentFrame(type, ownerFn);
|
|
}
|
|
switch (type) {
|
|
case REACT_SUSPENSE_TYPE:
|
|
return describeBuiltInComponentFrame('Suspense', ownerFn);
|
|
case REACT_SUSPENSE_LIST_TYPE:
|
|
return describeBuiltInComponentFrame('SuspenseList', ownerFn);
|
|
}
|
|
if (typeof type === 'object') {
|
|
switch (type.$$typeof) {
|
|
case REACT_FORWARD_REF_TYPE:
|
|
return describeFunctionComponentFrame(type.render, ownerFn);
|
|
case REACT_MEMO_TYPE:
|
|
// Memo may contain any component type so we recursively resolve it.
|
|
return describeUnknownElementTypeFrameInDEV(type.type, ownerFn);
|
|
case REACT_LAZY_TYPE: {
|
|
const lazyComponent: LazyComponent<any, any> = (type: any);
|
|
const payload = lazyComponent._payload;
|
|
const init = lazyComponent._init;
|
|
try {
|
|
// Lazy may contain any component type so we recursively resolve it.
|
|
return describeUnknownElementTypeFrameInDEV(init(payload), ownerFn);
|
|
} catch (x) {}
|
|
}
|
|
}
|
|
}
|
|
return '';
|
|
}
|