Skip to content

Commit eff8399

Browse files
committed
Provide non-standard stack with invalid type warnings
1 parent 2bbe024 commit eff8399

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed

.flowconfig

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<PROJECT_ROOT>/examples/.*
44
<PROJECT_ROOT>/fixtures/.*
55
<PROJECT_ROOT>/build/.*
6+
<PROJECT_ROOT>/node_modules/chrome-devtools-frontend/.*
7+
<PROJECT_ROOT>/.*/node_modules/chrome-devtools-frontend/.*
68
<PROJECT_ROOT>/.*/node_modules/y18n/.*
79
<PROJECT_ROOT>/.*/__mocks__/.*
810
<PROJECT_ROOT>/.*/__tests__/.*

src/isomorphic/classic/element/ReactElementValidator.js

+10
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@ var ReactElementValidator = {
223223

224224
info += ReactComponentTreeHook.getCurrentStackAddendum();
225225

226+
var currentSource = props !== null &&
227+
props !== undefined &&
228+
props.__source !== undefined
229+
? props.__source
230+
: null;
231+
ReactComponentTreeHook.pushNonStandardWarningStack(
232+
true,
233+
currentSource,
234+
);
226235
warning(
227236
false,
228237
'React.createElement: type is invalid -- expected a string (for ' +
@@ -231,6 +240,7 @@ var ReactElementValidator = {
231240
type == null ? type : typeof type,
232241
info,
233242
);
243+
ReactComponentTreeHook.popNonStandardWarningStack();
234244
}
235245
}
236246

src/isomorphic/classic/element/__tests__/ReactElementValidator-test.js

+45
Original file line numberDiff line numberDiff line change
@@ -525,4 +525,49 @@ describe('ReactElementValidator', () => {
525525
"component from the file it's defined in. Check your code at **.",
526526
);
527527
});
528+
529+
it('provides stack via non-standard console.stack for invalid types', () => {
530+
spyOn(console, 'error');
531+
532+
function Foo() {
533+
var Bad = undefined;
534+
return React.createElement(Bad);
535+
}
536+
537+
function App() {
538+
return React.createElement(Foo);
539+
}
540+
541+
try {
542+
console.stack = jest.fn();
543+
console.stackEnd = jest.fn();
544+
545+
expect(() => {
546+
ReactTestUtils.renderIntoDocument(React.createElement(App));
547+
}).toThrow(
548+
'Element type is invalid: expected a string (for built-in components) ' +
549+
'or a class/function (for composite components) but got: undefined. ' +
550+
"You likely forgot to export your component from the file it's " +
551+
'defined in. Check the render method of `Foo`.',
552+
);
553+
554+
expect(console.stack.mock.calls.length).toBe(1);
555+
expect(console.stackEnd.mock.calls.length).toBe(1);
556+
557+
var stack = console.stack.mock.calls[0][0];
558+
expect(Array.isArray(stack)).toBe(true);
559+
expect(stack.map(frame => frame.functionName)).toEqual([
560+
'Foo',
561+
'App',
562+
null,
563+
]);
564+
expect(
565+
stack.map(frame => frame.fileName && frame.fileName.slice(-8)),
566+
).toEqual([null, null, null]);
567+
expect(stack.map(frame => frame.lineNumber)).toEqual([null, null, null]);
568+
} finally {
569+
delete console.stack;
570+
delete console.stackEnd;
571+
}
572+
});
528573
});

src/isomorphic/hooks/ReactComponentTreeHook.js

+51
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,57 @@ var ReactComponentTreeHook = {
402402

403403
getRootIDs,
404404
getRegisteredIDs: getItemIDs,
405+
406+
pushNonStandardWarningStack(
407+
isCreatingElement: boolean,
408+
currentSource: ?Source,
409+
) {
410+
if (typeof console.stack !== 'function') {
411+
return;
412+
}
413+
414+
var stack = [];
415+
var currentOwner = ReactCurrentOwner.current;
416+
var id = currentOwner && currentOwner._debugID;
417+
418+
try {
419+
if (isCreatingElement) {
420+
stack.push({
421+
fileName: currentSource ? currentSource.fileName : null,
422+
lineNumber: currentSource ? currentSource.lineNumber : null,
423+
functionName: id ? ReactComponentTreeHook.getDisplayName(id) : null,
424+
});
425+
}
426+
427+
while (id) {
428+
var element = ReactComponentTreeHook.getElement(id);
429+
var ownerID = ReactComponentTreeHook.getOwnerID(id);
430+
var ownerName = ownerID
431+
? ReactComponentTreeHook.getDisplayName(ownerID)
432+
: null;
433+
var source = element && element._source;
434+
stack.push({
435+
fileName: source ? source.fileName : null,
436+
lineNumber: source ? source.lineNumber : null,
437+
functionName: ownerName,
438+
});
439+
// Owner stack is more useful for visual representation
440+
id = ownerID || ReactComponentTreeHook.getParentID(id);
441+
}
442+
} catch (err) {
443+
// Internal state is messed up.
444+
// Stop building the stack (it's just a nice to have).
445+
}
446+
447+
console.stack(stack);
448+
},
449+
450+
popNonStandardWarningStack() {
451+
if (typeof console.stackEnd !== 'function') {
452+
return;
453+
}
454+
console.stackEnd();
455+
},
405456
};
406457

407458
module.exports = ReactComponentTreeHook;

src/isomorphic/modern/element/__tests__/ReactJSXElementValidator-test.js

+49
Original file line numberDiff line numberDiff line change
@@ -400,4 +400,53 @@ describe('ReactJSXElementValidator', () => {
400400
' Use a static property named `defaultProps` instead.',
401401
);
402402
});
403+
404+
it('provides stack via non-standard console.stack for invalid types', () => {
405+
spyOn(console, 'error');
406+
407+
function Foo() {
408+
var Bad = undefined;
409+
return <Bad />;
410+
}
411+
412+
function App() {
413+
return <Foo />;
414+
}
415+
416+
try {
417+
console.stack = jest.fn();
418+
console.stackEnd = jest.fn();
419+
420+
expect(() => {
421+
ReactTestUtils.renderIntoDocument(<App />);
422+
}).toThrow(
423+
'Element type is invalid: expected a string (for built-in components) ' +
424+
'or a class/function (for composite components) but got: undefined. ' +
425+
"You likely forgot to export your component from the file it's " +
426+
'defined in. Check the render method of `Foo`.',
427+
);
428+
429+
expect(console.stack.mock.calls.length).toBe(1);
430+
expect(console.stackEnd.mock.calls.length).toBe(1);
431+
432+
var stack = console.stack.mock.calls[0][0];
433+
expect(Array.isArray(stack)).toBe(true);
434+
expect(stack.map(frame => frame.functionName)).toEqual([
435+
'Foo',
436+
'App',
437+
null,
438+
]);
439+
expect(
440+
stack.map(frame => frame.fileName && frame.fileName.slice(-8)),
441+
).toEqual(['-test.js', '-test.js', '-test.js']);
442+
expect(stack.map(frame => typeof frame.lineNumber)).toEqual([
443+
'number',
444+
'number',
445+
'number',
446+
]);
447+
} finally {
448+
delete console.stack;
449+
delete console.stackEnd;
450+
}
451+
});
403452
});

0 commit comments

Comments
 (0)