@samitouri / QOS-React-2 / commits / 8d413bf2c3

Remove React.error and React.warn (#16126)

* Remove React.error/React.warn with React.getComponentStack

Brian Vaughn committed Jul 12, 2019 at 15:41 UTC 8d413bf2c3679befe8c6b56733b7c6f6e467ec07
3 files changed -240
packages/react/src/React.js
-4
@@ -51,7 +51,6 @@ import {
51 jsxWithValidationDynamic,
52 } from './ReactElementValidator';
53 import ReactSharedInternals from './ReactSharedInternals';
54 -import {error, warn} from './withComponentStack';
54 import createEvent from 'shared/createEventComponent';
55 import {enableJSXTransformAPI, enableFlareAPI} from 'shared/ReactFeatureFlags';
56 const React = {
@@ -72,9 +71,6 @@ const React = {
71 lazy,
72 memo,
73
75 - error,
76 - warn,
77 -
74 useCallback,
75 useContext,
76 useEffect,
packages/react/src/__tests__/withComponentStack-test.js deleted
-188
@@ -1,188 +0,0 @@
1 -/**
2 - * Copyright (c) Facebook, Inc. and its affiliates.
3 - *
4 - * This source code is licensed under the MIT license found in the
5 - * LICENSE file in the root directory of this source tree.
6 - *
7 - * @emails react-core
8 - */
9 -
10 -'use strict';
11 -
12 -function normalizeCodeLocInfo(str) {
13 - return str && str.replace(/at .+?:\d+/g, 'at **');
14 -}
15 -
16 -function expectHelper(spy, prefix, ...expectedArgs) {
17 - const expectedStack = expectedArgs.pop();
18 -
19 - expect(spy).toHaveBeenCalledTimes(1);
20 -
21 - const actualArgs = spy.calls.mostRecent().args;
22 -
23 - let actualStack = undefined;
24 - if (expectedStack !== undefined) {
25 - actualStack = actualArgs.pop();
26 - expect(normalizeCodeLocInfo(actualStack)).toBe(expectedStack);
27 - }
28 -
29 - expect(actualArgs).toHaveLength(expectedArgs.length);
30 - actualArgs.forEach((actualArg, index) => {
31 - const expectedArg = expectedArgs[index];
32 - expect(actualArg).toBe(
33 - index === 0 ? `${prefix}: ${expectedArg}` : expectedArg,
34 - );
35 - });
36 -}
37 -
38 -function expectMessageAndStack(...expectedArgs) {
39 - expectHelper(console.error, 'error', ...expectedArgs);
40 - expectHelper(console.warn, 'warn', ...expectedArgs);
41 -}
42 -
43 -describe('withComponentStack', () => {
44 - let React = null;
45 - let ReactTestRenderer = null;
46 - let error = null;
47 - let warn = null;
48 -
49 - beforeEach(() => {
50 - jest.resetModules();
51 -
52 - React = require('react');
53 - ReactTestRenderer = require('react-test-renderer');
54 -
55 - error = React.error;
56 - warn = React.warn;
57 -
58 - spyOnDevAndProd(console, 'error');
59 - spyOnDevAndProd(console, 'warn');
60 - });
61 -
62 - if (!__DEV__) {
63 - it('does nothing in production mode', () => {
64 - error('error');
65 - warn('warning');
66 -
67 - expect(console.error).toHaveBeenCalledTimes(0);
68 - expect(console.warn).toHaveBeenCalledTimes(0);
69 - });
70 - }
71 -
72 - if (__DEV__) {
73 - it('does not include component stack when called outside of render', () => {
74 - error('error: logged outside of render');
75 - warn('warn: logged outside of render');
76 - expectMessageAndStack('logged outside of render', undefined);
77 - });
78 -
79 - it('should support multiple args', () => {
80 - function Component() {
81 - error('error: number:', 123, 'boolean:', true);
82 - warn('warn: number:', 123, 'boolean:', true);
83 - return null;
84 - }
85 -
86 - ReactTestRenderer.create(<Component />);
87 -
88 - expectMessageAndStack(
89 - 'number:',
90 - 123,
91 - 'boolean:',
92 - true,
93 - '\n in Component (at **)',
94 - );
95 - });
96 -
97 - it('includes component stack when called from a render method', () => {
98 - class Parent extends React.Component {
99 - render() {
100 - return <Child />;
101 - }
102 - }
103 -
104 - function Child() {
105 - error('error: logged in child render method');
106 - warn('warn: logged in child render method');
107 - return null;
108 - }
109 -
110 - ReactTestRenderer.create(<Parent />);
111 -
112 - expectMessageAndStack(
113 - 'logged in child render method',
114 - '\n in Child (at **)' + '\n in Parent (at **)',
115 - );
116 - });
117 -
118 - it('includes component stack when called from a render phase lifecycle method', () => {
119 - function Parent() {
120 - return <Child />;
121 - }
122 -
123 - class Child extends React.Component {
124 - UNSAFE_componentWillMount() {
125 - error('error: logged in child cWM lifecycle');
126 - warn('warn: logged in child cWM lifecycle');
127 - }
128 - render() {
129 - return null;
130 - }
131 - }
132 -
133 - ReactTestRenderer.create(<Parent />);
134 -
135 - expectMessageAndStack(
136 - 'logged in child cWM lifecycle',
137 - '\n in Child (at **)' + '\n in Parent (at **)',
138 - );
139 - });
140 -
141 - it('includes component stack when called from a commit phase lifecycle method', () => {
142 - function Parent() {
143 - return <Child />;
144 - }
145 -
146 - class Child extends React.Component {
147 - componentDidMount() {
148 - error('error: logged in child cDM lifecycle');
149 - warn('warn: logged in child cDM lifecycle');
150 - }
151 - render() {
152 - return null;
153 - }
154 - }
155 -
156 - ReactTestRenderer.create(<Parent />);
157 -
158 - expectMessageAndStack(
159 - 'logged in child cDM lifecycle',
160 - '\n in Child (at **)' + '\n in Parent (at **)',
161 - );
162 - });
163 -
164 - it('includes component stack when called from a passive effect handler', () => {
165 - class Parent extends React.Component {
166 - render() {
167 - return <Child />;
168 - }
169 - }
170 -
171 - function Child() {
172 - React.useEffect(() => {
173 - error('error: logged in child render method');
174 - warn('warn: logged in child render method');
175 - });
176 - return null;
177 - }
178 - ReactTestRenderer.act(() => {
179 - ReactTestRenderer.create(<Parent />);
180 - });
181 -
182 - expectMessageAndStack(
183 - 'logged in child render method',
184 - '\n in Child (at **)' + '\n in Parent (at **)',
185 - );
186 - });
187 - }
188 -});
packages/react/src/withComponentStack.js deleted
-48
@@ -1,48 +0,0 @@
1 -/**
2 - * Copyright (c) Facebook, Inc. and its affiliates.
3 - *
4 - * This source code is licensed under the MIT license found in the
5 - * LICENSE file in the root directory of this source tree.
6 - */
7 -
8 -import ReactSharedInternals from 'shared/ReactSharedInternals';
9 -
10 -function noop() {}
11 -
12 -let error = noop;
13 -let warn = noop;
14 -if (__DEV__) {
15 - const ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
16 -
17 - error = function() {
18 - const stack = ReactDebugCurrentFrame.getStackAddendum();
19 - if (stack !== '') {
20 - const length = arguments.length;
21 - const args = new Array(length + 1);
22 - for (let i = 0; i < length; i++) {
23 - args[i] = arguments[i];
24 - }
25 - args[length] = stack;
26 - console.error.apply(console, args);
27 - } else {
28 - console.error.apply(console, arguments);
29 - }
30 - };
31 -
32 - warn = function() {
33 - const stack = ReactDebugCurrentFrame.getStackAddendum();
34 - if (stack !== '') {
35 - const length = arguments.length;
36 - const args = new Array(length + 1);
37 - for (let i = 0; i < length; i++) {
38 - args[i] = arguments[i];
39 - }
40 - args[length] = stack;
41 - console.warn.apply(console, args);
42 - } else {
43 - console.warn.apply(console, arguments);
44 - }
45 - };
46 -}
47 -
48 -export {error, warn};