Disable numeric element types
Grant Timmerman committed
Mar 26, 2015 at 10:07 UTC
59a914aac0b96c68c7218343e0c994f59ec73ddb
3 files changed
+42
-24
src/classic/element/ReactElementValidator.js
+4
-5
@@ -362,7 +362,7 @@ function checkAndWarnForMutatedProps(element) {
362
* @param {ReactElement} element
363
*/
364
function validatePropTypes(element) {
365
- if (element.type == null || !(typeof element.type === 'string' ||
365
+ if (!(typeof element.type === 'string' ||
366
typeof element.type === 'function')) {
367
// This has already warned. Don't throw.
368
return;
@@ -399,10 +399,9 @@ var ReactElementValidator = {
399
createElement: function(type, props, children) {
400
// We warn in this case but don't throw. We expect the element creation to
401
// succeed and there will likely be errors in render.
402
- warning(
403
- type != null && (typeof type === 'string' || typeof type === 'function'),
404
- 'React.createElement: type should not be null, undefined, or boolean. ' +
405
- 'It should be a string (for DOM elements) or a ReactClass ' +
402
+ warning(typeof type === 'string' || typeof type === 'function',
403
+ 'React.createElement: type should not be null, undefined, boolean, or ' +
404
+ 'number. It should be a string (for DOM elements) or a ReactClass ' +
405
'(for composite components).%s',
406
getDeclarationErrorAddendum()
407
);
src/classic/element/__tests__/ReactElementValidator-test.js
+21
-12
@@ -258,32 +258,40 @@ describe('ReactElementValidator', function() {
258
);
259
});
260
261
- it('gives a helpful error when passing null, undefined, or boolean', () => {
261
+ it('gives a helpful error when passing null, undefined, boolean, or number',
262
+ () => {
263
spyOn(console, 'error');
264
React.createElement(undefined);
265
React.createElement(null);
266
React.createElement(true);
266
- expect(console.error.calls.length).toBe(3);
267
+ React.createElement(123);
268
+ expect(console.error.calls.length).toBe(4);
269
expect(console.error.calls[0].args[0]).toBe(
270
'Warning: React.createElement: type should not be null, undefined, ' +
269
- 'or boolean. It should be a string (for DOM elements) or a ReactClass ' +
270
- '(for composite components).'
271
+ 'boolean, or number. It should be a string (for DOM elements) or a ' +
272
+ 'ReactClass (for composite components).'
273
);
274
expect(console.error.calls[1].args[0]).toBe(
275
'Warning: React.createElement: type should not be null, undefined, ' +
274
- 'or boolean. It should be a string (for DOM elements) or a ReactClass ' +
275
- '(for composite components).'
276
+ 'boolean, or number. It should be a string (for DOM elements) or a ' +
277
+ 'ReactClass (for composite components).'
278
);
279
expect(console.error.calls[2].args[0]).toBe(
280
'Warning: React.createElement: type should not be null, undefined, ' +
279
- 'or boolean. It should be a string (for DOM elements) or a ReactClass ' +
280
- '(for composite components).'
281
+ 'boolean, or number. It should be a string (for DOM elements) or a ' +
282
+ 'ReactClass (for composite components).'
283
+ );
284
+ expect(console.error.calls[3].args[0]).toBe(
285
+ 'Warning: React.createElement: type should not be null, undefined, ' +
286
+ 'boolean, or number. It should be a string (for DOM elements) or a ' +
287
+ 'ReactClass (for composite components).'
288
);
289
React.createElement('div');
283
- expect(console.error.calls.length).toBe(3);
290
+ expect(console.error.calls.length).toBe(4);
291
});
292
286
- it('includes the owner name when passing null, undefined, or boolean', () => {
293
+ it('includes the owner name when passing null, undefined, boolean, or number',
294
+ () => {
295
spyOn(console, 'error');
296
var ParentComp = React.createClass({
297
render: function() {
@@ -296,8 +304,9 @@ describe('ReactElementValidator', function() {
304
expect(console.error.calls.length).toBe(2);
305
expect(console.error.calls[0].args[0]).toBe(
306
'Warning: React.createElement: type should not be null, undefined, ' +
299
- 'or boolean. It should be a string (for DOM elements) or a ReactClass ' +
300
- '(for composite components). Check the render method of `ParentComp`.'
307
+ 'boolean, or number. It should be a string (for DOM elements) or a ' +
308
+ 'ReactClass (for composite components). Check the render method of ' +
309
+ '`ParentComp`.'
310
);
311
expect(console.error.calls[1].args[0]).toBe(
312
'Warning: Only functions or strings can be mounted as React components.'
src/modern/element/__tests__/ReactJSXElementValidator-test.js
+17
-7
@@ -211,23 +211,33 @@ describe('ReactJSXElementValidator', function() {
211
it('gives a helpful error when passing null, undefined, or boolean', () => {
212
var Undefined = undefined;
213
var Null = null;
214
- var Div = 'div';
214
var True = true;
215
+ var Num = 123;
216
+ var Div = 'div';
217
spyOn(console, 'error');
218
<Undefined />;
219
<Null />;
220
<True />;
220
- expect(console.error.calls.length).toBe(3);
221
+ <Num />;
222
+ expect(console.error.calls.length).toBe(4);
223
expect(console.error.calls[0].args[0]).toContain(
222
- 'type should not be null, undefined, or boolean. It should be a ' +
223
- 'string (for DOM elements) or a ReactClass (for composite components).'
224
+ 'type should not be null, undefined, boolean, or number. It should be ' +
225
+ 'a string (for DOM elements) or a ReactClass (for composite components).'
226
);
227
expect(console.error.calls[1].args[0]).toContain(
226
- 'type should not be null, undefined, or boolean. It should be a ' +
227
- 'string (for DOM elements) or a ReactClass (for composite components).'
228
+ 'type should not be null, undefined, boolean, or number. It should be ' +
229
+ 'a string (for DOM elements) or a ReactClass (for composite components).'
230
+ );
231
+ expect(console.error.calls[2].args[0]).toContain(
232
+ 'type should not be null, undefined, boolean, or number. It should be ' +
233
+ 'a string (for DOM elements) or a ReactClass (for composite components).'
234
+ );
235
+ expect(console.error.calls[3].args[0]).toContain(
236
+ 'type should not be null, undefined, boolean, or number. It should be ' +
237
+ 'a string (for DOM elements) or a ReactClass (for composite components).'
238
);
239
<Div />;
230
- expect(console.error.calls.length).toBe(3);
240
+ expect(console.error.calls.length).toBe(4);
241
});
242
243
it('should check default prop values', function() {