Moar warnings with solid prefixes!
This ensures that we have a prefix that can be easily identified in logs so that we can filter out warnings based on their prefix. This also turns the remaining two monitorCodeUse callers into warnings. We'll probably still use monitorCodeUse until we know if we want to deprecate but most releases should only have warnings.
Sebastian Markbage committed
Feb 12, 2015 at 13:37 UTC
f5038829d87b97f610e94b0b4c7b905cb58a4b63
7 files changed
+65
-32
src/classic/__tests__/ReactContextValidator-test.js
+10
-5
@@ -147,7 +147,8 @@ describe('ReactContextValidator', function() {
147
148
expect(console.warn.argsForCall.length).toBe(1);
149
expect(console.warn.argsForCall[0][0]).toBe(
150
- 'Warning: Required context `foo` was not specified in `Component`.'
150
+ 'Warning: Failed Context Types: ' +
151
+ 'Required context `foo` was not specified in `Component`.'
152
);
153
154
var ComponentInFooStringContext = React.createClass({
@@ -193,7 +194,8 @@ describe('ReactContextValidator', function() {
194
195
expect(console.warn.argsForCall.length).toBe(2);
196
expect(console.warn.argsForCall[1][0]).toBe(
196
- 'Warning: Invalid context `foo` of type `number` supplied ' +
197
+ 'Warning: Failed Context Types: ' +
198
+ 'Invalid context `foo` of type `number` supplied ' +
199
'to `Component`, expected `string`.' +
200
' Check the render method of `ComponentInFooNumberContext`.'
201
);
@@ -218,17 +220,20 @@ describe('ReactContextValidator', function() {
220
ReactTestUtils.renderIntoDocument(<Component testContext={{bar: 123}} />);
221
expect(console.warn.argsForCall.length).toBe(2);
222
expect(console.warn.argsForCall[0][0]).toBe(
221
- 'Warning: Required child context `foo` was not specified in `Component`.'
223
+ 'Warning: Failed Context Types: ' +
224
+ 'Required child context `foo` was not specified in `Component`.'
225
);
226
expect(console.warn.argsForCall[1][0]).toBe(
224
- 'Warning: Required child context `foo` was not specified in `Component`.'
227
+ 'Warning: Failed Context Types: ' +
228
+ 'Required child context `foo` was not specified in `Component`.'
229
);
230
231
ReactTestUtils.renderIntoDocument(<Component testContext={{foo: 123}} />);
232
233
expect(console.warn.argsForCall.length).toBe(4);
234
expect(console.warn.argsForCall[3][0]).toBe(
231
- 'Warning: Invalid child context `foo` of type `number` ' +
235
+ 'Warning: Failed Context Types: ' +
236
+ 'Invalid child context `foo` of type `number` ' +
237
'supplied to `Component`, expected `string`.'
238
);
239
src/classic/element/ReactElementValidator.js
+6
-10
@@ -44,10 +44,7 @@ function getDeclarationErrorAddendum() {
44
* object keys are not valid. This allows us to keep track of children between
45
* updates.
46
*/
47
-var ownerHasKeyUseWarning = {
48
- 'react_key_warning': {},
49
- 'react_numeric_key_warning': {}
50
-};
47
+var ownerHasKeyUseWarning = {};
48
49
var loggedTypeFailures = {};
50
@@ -101,7 +98,6 @@ function validateExplicitKey(element, parentType) {
98
element._store.validated = true;
99
100
warnAndMonitorForKeyUse(
104
- 'react_key_warning',
101
'Each child in an array or iterator should have a unique "key" prop.',
102
element,
103
parentType
@@ -122,7 +118,6 @@ function validatePropertyKey(name, element, parentType) {
118
return;
119
}
120
warnAndMonitorForKeyUse(
125
- 'react_numeric_key_warning',
121
'Child objects should have non-numeric keys so ordering is preserved.',
122
element,
123
parentType
@@ -133,17 +128,18 @@ function validatePropertyKey(name, element, parentType) {
128
* Shared warning and monitoring code for the key warnings.
129
*
130
* @internal
136
- * @param {string} warningID The id used when logging.
131
* @param {string} message The base warning that gets output.
132
* @param {ReactElement} element Component that requires a key.
133
* @param {*} parentType element's parent's type.
134
*/
141
-function warnAndMonitorForKeyUse(warningID, message, element, parentType) {
135
+function warnAndMonitorForKeyUse(message, element, parentType) {
136
var ownerName = getCurrentOwnerDisplayName();
137
var parentName = parentType.displayName || parentType.name;
138
139
var useName = ownerName || parentName;
146
- var memoizer = ownerHasKeyUseWarning[warningID];
140
+ var memoizer = ownerHasKeyUseWarning[message] || (
141
+ ownerHasKeyUseWarning[message] = {}
142
+ );
143
if (memoizer.hasOwnProperty(useName)) {
144
return;
145
}
@@ -166,7 +162,7 @@ function warnAndMonitorForKeyUse(warningID, message, element, parentType) {
162
}
163
164
message += ' See http://fb.me/react-warning-keys for more information.';
169
- warning(false, '%s', warningID + ': ' + message);
165
+ warning(false, message);
166
}
167
168
/**
src/core/ReactCompositeComponent.js
+10
-3
@@ -447,10 +447,17 @@ var ReactCompositeComponentMixin = {
447
448
if (location === ReactPropTypeLocations.prop) {
449
// Preface gives us something to blacklist in warning module
450
- var preface = 'Failed CompositeComponent proptype check. ';
451
- warning(false, preface + error.message + addendum);
450
+ warning(
451
+ false,
452
+ 'Failed Composite propType: %s',
453
+ error.message + addendum
454
+ );
455
} else {
453
- warning(false, error.message + addendum);
456
+ warning(
457
+ false,
458
+ 'Failed Context Types: %s',
459
+ error.message + addendum
460
+ );
461
}
462
}
463
}
src/core/ReactContext.js
+12
-2
@@ -13,7 +13,9 @@
13
14
var assign = require('Object.assign');
15
var emptyObject = require('emptyObject');
16
-var monitorCodeUse = require('monitorCodeUse');
16
+var warning = require('warning');
17
+
18
+var didWarn = false;
19
20
/**
21
* Keeps track of the current context.
@@ -46,7 +48,15 @@ var ReactContext = {
48
* @return {ReactComponent|array<ReactComponent>}
49
*/
50
withContext: function(newContext, scopedCallback) {
49
- monitorCodeUse('react_with_context', {newContext: newContext});
51
+ if (__DEV__) {
52
+ warning(
53
+ didWarn,
54
+ 'withContext is deprecated and will be removed in a future version. ' +
55
+ 'Use a wrapper component with getChildContext instead.'
56
+ );
57
+
58
+ didWarn = true;
59
+ }
60
61
var result;
62
var previousContext = ReactContext.current;
src/core/__tests__/ReactCompositeComponent-test.js
+6
-2
@@ -73,6 +73,12 @@ describe('ReactCompositeComponent', function() {
73
}
74
});
75
76
+ // Ignore the first warning which is fired by using withContext at all.
77
+ // That way we don't have to reset and assert it on every subsequent test.
78
+ // This will be killed soon anyway.
79
+ console.warn = mocks.getMockFunction();
80
+ React.withContext({}, function() { });
81
+
82
spyOn(console, 'warn');
83
});
84
@@ -571,8 +577,6 @@ describe('ReactCompositeComponent', function() {
577
ReactTestUtils.renderIntoDocument(<Component />);
578
});
579
574
- // Two warnings, one for the component and one for the div
575
- // We may want to make this expect one warning in the future
580
expect(console.warn.argsForCall.length).toBe(1);
581
expect(console.warn.argsForCall[0][0]).toBe(
582
'Warning: owner-based and parent-based contexts differ '+
src/core/shouldUpdateReactComponent.js
+13
-9
@@ -12,7 +12,7 @@
12
13
'use strict';
14
15
-var monitorCodeUse = require('monitorCodeUse');
15
+var warning = require('warning');
16
17
/**
18
* Given a `prevElement` and `nextElement`, determines if the existing
@@ -57,14 +57,18 @@ function shouldUpdateReactComponent(prevElement, nextElement) {
57
nextElement.type.displayName != null) {
58
nextDisplayName = nextElement.type.displayName;
59
}
60
- monitorCodeUse(
61
- 'react_should_update_owner_is_useful',
62
- {
63
- key: prevElement.key,
64
- prevOwner: prevName,
65
- nextOwner: nextName,
66
- nextDisplayName: nextDisplayName
67
- }
60
+ warning(
61
+ false,
62
+ '<%s /> is being rendered by both %s and %s using the same key ' +
63
+ '(%s) in the same place. Currently, this means that they ' +
64
+ 'don\'t preserve state. This behavior should be very rare ' +
65
+ 'so we\'re considering deprecating it. Please contact the ' +
66
+ 'React team and explain your use case so that we can take that ' +
67
+ 'into consideration.',
68
+ nextDisplayName || 'Unknown Component',
69
+ prevName || '[Unknown]',
70
+ nextName || '[Unknown]',
71
+ prevElement.key
72
);
73
}
74
}
src/vendor/core/warning.js
+8
-1
@@ -31,7 +31,14 @@ if (__DEV__) {
31
);
32
}
33
34
- if (format.indexOf('Failed CompositeComponent proptype check. ') === 0) {
34
+ if (format.length < 10 || /^[s\W]*$/.test(format)) {
35
+ throw new Error(
36
+ 'The warning format should be able to uniquely identify this ' +
37
+ 'warning. Please, use a more descriptive format than: ' + format
38
+ );
39
+ }
40
+
41
+ if (format.indexOf('Failed Composite propType: ') === 0) {
42
return; // Ignore CompositeComponent proptype check.
43
}
44