Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace React.error and React.warn with getComponentStack #16017

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/react/src/React.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {
jsxWithValidationDynamic,
} from './ReactElementValidator';
import ReactSharedInternals from './ReactSharedInternals';
import {error, warn} from './withComponentStack';
import getComponentStack from './getComponentStack';
import createEvent from 'shared/createEventComponent';
import {enableJSXTransformAPI, enableFlareAPI} from 'shared/ReactFeatureFlags';
const React = {
Expand All @@ -72,8 +72,7 @@ const React = {
lazy,
memo,

error,
warn,
getComponentStack,

useCallback,
useContext,
Expand Down
125 changes: 125 additions & 0 deletions packages/react/src/__tests__/getComponentStack-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/

'use strict';

function normalizeCodeLocInfo(str) {
return str && str.replace(/at .+?:\d+/g, 'at **');
}

describe('getComponentStack', () => {
let React = null;
let ReactTestRenderer = null;
let getComponentStack = null;

beforeEach(() => {
jest.resetModules();

React = require('react');
ReactTestRenderer = require('react-test-renderer');

getComponentStack = React.getComponentStack;
});

if (!__DEV__) {
it('does nothing in production mode', () => {
expect(getComponentStack()).toBe('');
});
}

if (__DEV__) {
it('returns an empty component stack when called outside of render', () => {
expect(getComponentStack()).toBe('');
});

it('includes component stack when called from a render method', () => {
class Parent extends React.Component {
render() {
return <Child />;
}
}

let capturedComponentStack = null;
function Child() {
capturedComponentStack = getComponentStack();
return null;
}

ReactTestRenderer.create(<Parent />);
expect(normalizeCodeLocInfo(capturedComponentStack)).toBe(
'\n in Child (at **)' + '\n in Parent (at **)',
);
});

it('returns component stack when called from a render phase lifecycle method', () => {
function Parent() {
return <Child />;
}

let capturedComponentStack = null;
class Child extends React.Component {
UNSAFE_componentWillMount() {
capturedComponentStack = getComponentStack();
}
render() {
return null;
}
}

ReactTestRenderer.create(<Parent />);
expect(normalizeCodeLocInfo(capturedComponentStack)).toBe(
'\n in Child (at **)' + '\n in Parent (at **)',
);
});

it('returns component stack when called from a commit phase lifecycle method', () => {
function Parent() {
return <Child />;
}

let capturedComponentStack = null;
class Child extends React.Component {
componentDidMount() {
capturedComponentStack = getComponentStack();
}
render() {
return null;
}
}

ReactTestRenderer.create(<Parent />);
expect(normalizeCodeLocInfo(capturedComponentStack)).toBe(
'\n in Child (at **)' + '\n in Parent (at **)',
);
});

it('returns component stack when called from a passive effect handler', () => {
class Parent extends React.Component {
render() {
return <Child />;
}
}

let capturedComponentStack = null;
function Child() {
React.useEffect(() => {
capturedComponentStack = getComponentStack();
});
return null;
}

ReactTestRenderer.act(() => {
ReactTestRenderer.create(<Parent />);
});
expect(normalizeCodeLocInfo(capturedComponentStack)).toBe(
'\n in Child (at **)' + '\n in Parent (at **)',
);
});
}
});
188 changes: 0 additions & 188 deletions packages/react/src/__tests__/withComponentStack-test.js

This file was deleted.

22 changes: 22 additions & 0 deletions packages/react/src/getComponentStack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import ReactSharedInternals from 'shared/ReactSharedInternals';

let getComponentStack = function() {
return '';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this not return anything? Seemed weird to return void/undefined in prod and an empty string when called outside of the render phase in DEV, so I opted to return empty string in both cases for now.

};

if (__DEV__) {
const ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;

getComponentStack = function() {
return ReactDebugCurrentFrame.getStackAddendum();
};
}

export default getComponentStack;
Loading