Skip to content

Commit

Permalink
Remove __self and __source location from elements (#28265)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sebmarkbage authored Feb 7, 2024
1 parent 5c08662 commit 37d901e
Show file tree
Hide file tree
Showing 26 changed files with 66 additions and 211 deletions.
12 changes: 0 additions & 12 deletions packages/react-client/src/ReactFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,18 +475,6 @@ function createElement(
writable: true,
value: true, // This element has already been validated on the server.
});
Object.defineProperty(element, '_self', {
configurable: false,
enumerable: false,
writable: false,
value: null,
});
Object.defineProperty(element, '_source', {
configurable: false,
enumerable: false,
writable: false,
value: null,
});
}
return element;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,15 @@ test.describe('Components', () => {
? valueElement.value
: valueElement.innerText;

return [name, value, source.innerText];
return [name, value, source ? source.innerText : null];
},
{name: isEditableName, value: isEditableValue}
);

expect(propName).toBe('label');
expect(propValue).toBe('"one"');
expect(sourceText).toMatch(/ListApp[a-zA-Z]*\.js/);
expect(sourceText).toBe(null);
// TODO: expect(sourceText).toMatch(/ListApp[a-zA-Z]*\.js/);
});

test('should allow props to be edited', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,13 @@ describe('Store component filters', () => {
]),
);

expect(store).toMatchInlineSnapshot(`[root]`);
// TODO: Filtering should work on component location.
// expect(store).toMatchInlineSnapshot(`[root]`);
expect(store).toMatchInlineSnapshot(`
[root]
▾ <Component>
<div>
`);

await actAsync(
async () =>
Expand Down
5 changes: 0 additions & 5 deletions packages/react-devtools-shared/src/backend/legacy/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,12 +773,10 @@ export function attach(
let owners = null;
let props = null;
let state = null;
let source = null;

const element = internalInstance._currentElement;
if (element !== null) {
props = element.props;
source = element._source != null ? element._source : null;

let owner = element._owner;
if (owner) {
Expand Down Expand Up @@ -851,9 +849,6 @@ export function attach(
// List of owners
owners,

// Location of component in source code.
source,

rootType: null,
rendererPackageName: null,
rendererVersion: null,
Expand Down
27 changes: 10 additions & 17 deletions packages/react-devtools-shared/src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ export function attach(

// NOTICE Keep in sync with get*ForFiber methods
function shouldFilterFiber(fiber: Fiber): boolean {
const {_debugSource, tag, type, key} = fiber;
const {tag, type, key} = fiber;

switch (tag) {
case DehydratedSuspenseComponent:
Expand Down Expand Up @@ -1010,15 +1010,15 @@ export function attach(
}
}

if (_debugSource != null && hideElementsWithPaths.size > 0) {
const {fileName} = _debugSource;
// eslint-disable-next-line no-for-of-loops/no-for-of-loops
for (const pathRegExp of hideElementsWithPaths) {
if (pathRegExp.test(fileName)) {
return true;
}
}
}
// TODO: Figure out a way to filter by path in the new model which has no debug info.
// if (hideElementsWithPaths.size > 0) {
// const {fileName} = ...;
// for (const pathRegExp of hideElementsWithPaths) {
// if (pathRegExp.test(fileName)) {
// return true;
// }
// }
// }

return false;
}
Expand Down Expand Up @@ -3132,7 +3132,6 @@ export function attach(

const {
_debugOwner,
_debugSource,
stateNode,
key,
memoizedProps,
Expand Down Expand Up @@ -3362,9 +3361,6 @@ export function attach(
// List of owners
owners,

// Location of component in source code.
source: _debugSource || null,

rootType,
rendererPackageName: renderer.rendererPackageName,
rendererVersion: renderer.version,
Expand Down Expand Up @@ -3725,9 +3721,6 @@ export function attach(
if (nativeNodes !== null) {
console.log('Nodes:', nativeNodes);
}
if (result.source !== null) {
console.log('Location:', result.source);
}
if (window.chrome || /firefox/i.test(navigator.userAgent)) {
console.log(
'Right-click any value to save it as a global variable for further inspection.',
Expand Down
4 changes: 0 additions & 4 deletions packages/react-devtools-shared/src/backend/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

import type {ReactContext, Wakeable} from 'shared/ReactTypes';
import type {Source} from 'shared/ReactElementType';
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
import type {
ComponentFilter,
Expand Down Expand Up @@ -280,9 +279,6 @@ export type InspectedElement = {
// List of owners
owners: Array<SerializedElement> | null,

// Location of component in source code.
source: Source | null,

type: ElementType,

// Meta information about the root this element belongs to.
Expand Down
3 changes: 1 addition & 2 deletions packages/react-devtools-shared/src/backendAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ export function convertInspectedElementBackendToFrontend(
canViewSource,
hasLegacyContext,
id,
source,
type,
owners,
context,
Expand Down Expand Up @@ -261,7 +260,7 @@ export function convertInspectedElementBackendToFrontend(
rendererPackageName,
rendererVersion,
rootType,
source,
source: null, // TODO: Load source location lazily.
type,
owners:
owners === null
Expand Down
3 changes: 1 addition & 2 deletions packages/react-devtools-shared/src/frontend/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* Be mindful of backwards compatibility when making changes.
*/

import type {Source} from 'shared/ReactElementType';
import type {
Dehydrated,
Unserializable,
Expand Down Expand Up @@ -220,7 +219,7 @@ export type InspectedElement = {
owners: Array<SerializedElement> | null,

// Location of component in source code.
source: Source | null,
source: null, // TODO: Reinstate a way to load this lazily.

type: ElementType,

Expand Down
12 changes: 10 additions & 2 deletions packages/react-dom/src/__tests__/ReactDeprecationWarnings-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ describe('ReactDeprecationWarnings', () => {
'We ask you to manually fix this case by using useRef() or createRef() instead. ' +
'Learn more about using refs safely here: ' +
'https://reactjs.org/link/strict-mode-string-ref',
'Warning: Component "Component" contains the string ref "refComponent". ' +
'Support for string refs will be removed in a future major release. We recommend ' +
'using useRef() or createRef() instead. Learn more about using refs safely here: ' +
'https://reactjs.org/link/strict-mode-string-ref',
]);
});

Expand All @@ -155,14 +159,18 @@ describe('ReactDeprecationWarnings', () => {
}

ReactNoop.render(<Component />);
await expect(async () => await waitForAll([])).toErrorDev(
await expect(async () => await waitForAll([])).toErrorDev([
'Warning: Component "Component" contains the string ref "refComponent". ' +
'Support for string refs will be removed in a future major release. ' +
'This case cannot be automatically converted to an arrow function. ' +
'We ask you to manually fix this case by using useRef() or createRef() instead. ' +
'Learn more about using refs safely here: ' +
'https://reactjs.org/link/strict-mode-string-ref',
);
'Warning: Component "Component" contains the string ref "refComponent". ' +
'Support for string refs will be removed in a future major release. We recommend ' +
'using useRef() or createRef() instead. Learn more about using refs safely here: ' +
'https://reactjs.org/link/strict-mode-string-ref',
]);
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ describe('ReactFunctionComponent', () => {
return <FunctionComponent name="A" ref={() => {}} />;
}
}
Object.defineProperty(AnonymousParentUsingJSX, 'name', {value: undefined});

let instance1;

Expand All @@ -293,9 +292,6 @@ describe('ReactFunctionComponent', () => {
});
}
}
Object.defineProperty(AnonymousParentNotUsingJSX, 'name', {
value: undefined,
});

let instance2;
expect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import {enableGetInspectorDataForInstanceInProduction} from 'shared/ReactFeatureFlags';
import {getClosestInstanceFromNode} from './ReactNativeComponentTree';
import {getNodeFromInternalInstanceHandle} from './ReactNativePublicCompat';
import {getStackByFiberInDevAndProd} from 'react-reconciler/src/ReactFiberComponentStack';

const emptyObject = {};
if (__DEV__) {
Expand All @@ -37,7 +38,6 @@ function createHierarchy(fiberHierarchy) {
getInspectorData: findNodeHandle => {
return {
props: getHostProps(fiber),
source: fiber._debugSource,
measure: callback => {
// If this is Fabric, we'll find a shadow node and use that to measure.
const hostFiber = findCurrentHostFiber(fiber);
Expand Down Expand Up @@ -98,7 +98,7 @@ function getInspectorDataForInstance(
hierarchy: [],
props: emptyObject,
selectedIndex: null,
source: null,
componentStack: '',
};
}

Expand All @@ -107,15 +107,16 @@ function getInspectorDataForInstance(
const instance = lastNonHostInstance(fiberHierarchy);
const hierarchy = createHierarchy(fiberHierarchy);
const props = getHostProps(instance);
const source = instance._debugSource;
const selectedIndex = fiberHierarchy.indexOf(instance);
const componentStack =
fiber !== null ? getStackByFiberInDevAndProd(fiber) : '';

return {
closestInstance: instance,
hierarchy,
props,
selectedIndex,
source,
componentStack,
};
}

Expand Down
8 changes: 1 addition & 7 deletions packages/react-native-renderer/src/ReactNativeTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,13 @@ type InspectorDataProps = $ReadOnly<{
...
}>;

type InspectorDataSource = $ReadOnly<{
fileName?: string,
lineNumber?: number,
}>;

type InspectorDataGetter = (
<TElementType: ElementType>(
componentOrHandle: ElementRef<TElementType> | number,
) => ?number,
) => $ReadOnly<{
measure: (callback: MeasureOnSuccessCallback) => void,
props: InspectorDataProps,
source: InspectorDataSource,
}>;

export type InspectorData = $ReadOnly<{
Expand All @@ -165,7 +159,7 @@ export type InspectorData = $ReadOnly<{
}>,
selectedIndex: ?number,
props: InspectorDataProps,
source: ?InspectorDataSource,
componentStack: string,
}>;

export type TouchedViewDataAtPoint = $ReadOnly<{
Expand Down
11 changes: 0 additions & 11 deletions packages/react-reconciler/src/ReactChildFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,6 @@ function coerceRef(
) {
if (__DEV__) {
if (
// We warn in ReactElement.js if owner and self are equal for string refs
// because these cannot be automatically converted to an arrow function
// using a codemod. Therefore, we don't have to warn about string refs again.
!(
element._owner &&
element._self &&
element._owner.stateNode !== element._self
) &&
// Will already throw with "Function components cannot have string refs"
!(
element._owner &&
Expand Down Expand Up @@ -446,7 +438,6 @@ function createChildReconciler(
existing.ref = coerceRef(returnFiber, current, element);
existing.return = returnFiber;
if (__DEV__) {
existing._debugSource = element._source;
existing._debugOwner = element._owner;
}
return existing;
Expand Down Expand Up @@ -1234,7 +1225,6 @@ function createChildReconciler(
const existing = useFiber(child, element.props.children);
existing.return = returnFiber;
if (__DEV__) {
existing._debugSource = element._source;
existing._debugOwner = element._owner;
}
return existing;
Expand All @@ -1260,7 +1250,6 @@ function createChildReconciler(
existing.ref = coerceRef(returnFiber, child, element);
existing.return = returnFiber;
if (__DEV__) {
existing._debugSource = element._source;
existing._debugOwner = element._owner;
}
return existing;
Expand Down
Loading

0 comments on commit 37d901e

Please sign in to comment.