|
1 | 1 | // @flow
|
| 2 | +import type { ReactFrame } from '../effects/proxyConsole'; |
2 | 3 |
|
3 |
| -// This is a list of React warnings to display |
4 |
| -// There must be zero or one capture group; and the capture group is assumed to be a missing stack frame. |
5 |
| -const warnings = [ |
6 |
| - /^.*React.createElement: type is invalid.+Check your code at (.*?:.*)[.]$/, |
7 |
| -]; |
8 |
| -// This is a list of information to remove from React warnings, it's not particularly useful to show |
| 4 | +// This is a list of information to remove from React warnings, it's not particularly useful to show. |
9 | 5 | const removals = [/Check your code at (.*?:.*)[.]/];
|
10 | 6 |
|
11 |
| -function massage(warning: string): { message: string, stack: string } | null { |
12 |
| - const nIndex = warning.indexOf('\n'); |
13 |
| - let message = warning; |
14 |
| - if (nIndex !== -1) { |
15 |
| - message = message.substring(0, nIndex); |
16 |
| - } |
17 |
| - let stack = warning.substring(nIndex + 1); |
18 |
| - |
19 |
| - let found = false; |
20 |
| - for (const warning of warnings) { |
21 |
| - const m = message.match(warning); |
22 |
| - if (!m) { |
23 |
| - continue; |
24 |
| - } |
25 |
| - found = true; |
26 |
| - if (!m[1]) { |
27 |
| - break; |
28 |
| - } |
29 |
| - stack = `in (render function) (at ${m[1]})\n${stack}`; |
30 |
| - break; |
31 |
| - } |
32 |
| - if (!found) { |
| 7 | +function massage( |
| 8 | + warning: string, |
| 9 | + frames: ReactFrame[] | void |
| 10 | +): { message: string, stack: string } | null { |
| 11 | + if (!frames) { |
33 | 12 | return null;
|
34 | 13 | }
|
35 | 14 |
|
| 15 | + let message = warning; |
| 16 | + const nIndex = message.indexOf('\n'); |
| 17 | + if (nIndex !== -1) message = message.substring(0, nIndex); |
| 18 | + |
36 | 19 | for (const trim of removals) {
|
37 | 20 | message = message.replace(trim, '');
|
38 | 21 | }
|
39 | 22 |
|
| 23 | + let stack = ''; |
| 24 | + for (let index = 0; index < frames.length; ++index) { |
| 25 | + const { fileName, lineNumber } = frames[index]; |
| 26 | + if (fileName == null || lineNumber == null) continue; |
| 27 | + let { functionName } = frames[index]; |
| 28 | + if (functionName == null && index === 0 && index + 1 < frames.length) { |
| 29 | + functionName = frames[index + 1].functionName; |
| 30 | + if (functionName !== null) functionName = `(${functionName})`; |
| 31 | + } |
| 32 | + functionName = functionName || '(unknown function)'; |
| 33 | + |
| 34 | + stack += `in ${functionName} (at ${fileName}:${lineNumber})\n`; |
| 35 | + } |
40 | 36 | return { message, stack };
|
41 | 37 | }
|
42 | 38 |
|
|
0 commit comments