Skip to content

Commit

Permalink
Add stack addendum to invalid element type warning
Browse files Browse the repository at this point in the history
- Show stack of element if element’s _source available (`in Unknown (at
file.js:10)
- Show parent’s generated stack when no _source available (since `in
Unknown` is useless)
  • Loading branch information
n3tr committed Dec 4, 2016
1 parent e44b526 commit fa04ddf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
15 changes: 11 additions & 4 deletions src/isomorphic/classic/element/ReactElementValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,24 +184,31 @@ function validatePropTypes(element) {
var ReactElementValidator = {

createElement: function(type, props, children) {
var element = ReactElement.createElement.apply(this, arguments);

var validType =
typeof type === 'string' ||
typeof type === 'function' ||
type !== null && typeof type === 'object' && typeof type.tag === 'number';
// We warn in this case but don't throw. We expect the element creation to
// succeed and there will likely be errors in render.
if (!validType) {
var info = '';
if (element._source !== null) {
info = ReactComponentTreeHook.getCurrentStackAddendum(element);
} else {
info = ReactComponentTreeHook.getCurrentStackAddendum();
}
warning(
false,
'React.createElement: type should not be null, undefined, boolean, or ' +
'number. It should be a string (for DOM elements) or a ReactClass ' +
'(for composite components).%s',
getDeclarationErrorAddendum()
'(for composite components).%s%s',
getDeclarationErrorAddendum(),
info
);
}

var element = ReactElement.createElement.apply(this, arguments);

// The result can be nullish if a mock or a custom function is used.
// TODO: Drop this when these are no longer allowed as the type argument.
if (element == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ describe('ReactElementValidator', () => {
'Warning: React.createElement: type should not be null, undefined, ' +
'boolean, or number. It should be a string (for DOM elements) or a ' +
'ReactClass (for composite components). Check the render method of ' +
'`ParentComp`.'
'`ParentComp`.' +
'\n in ParentComp'
);
});

Expand Down Expand Up @@ -535,12 +536,21 @@ describe('ReactElementValidator', () => {
spyOn(console, 'error');
var Foo = undefined;
void <Foo>{[<div />]}</Foo>;

var info = console.error.calls.argsFor(0)[0];
var lines = info.split('\n');

expectDev(console.error.calls.count()).toBe(1);
expectDev(console.error.calls.argsFor(0)[0]).toBe(
expectDev(lines.length).toBe(2);
expectDev(lines[0]).toBe(
'Warning: React.createElement: type should not be null, undefined, ' +
'boolean, or number. It should be a string (for DOM elements) or a ' +
'ReactClass (for composite components).'
);
// The format is ' in Unknown (at ReactElementValidator-test.js:539)'
expectDev(lines[1]).toMatch(
/in Unknown \(at ReactElementValidator-test\.js\:\d+\)/
);
});

});
2 changes: 2 additions & 0 deletions src/isomorphic/hooks/ReactComponentTreeHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ function getDisplayName(element: ?ReactElement): string {
return '#text';
} else if (typeof element.type === 'string') {
return element.type;
} else if (element.type === null || typeof element.type === 'undefined') {
return 'Unknown';
} else {
return element.type.displayName || element.type.name || 'Unknown';
}
Expand Down

0 comments on commit fa04ddf

Please sign in to comment.