|
8 | 8 | // This refers to a WWW module.
|
9 | 9 | const warningWWW = require('warning');
|
10 | 10 |
|
11 |
| -export function warn() { |
12 |
| - // TODO: use different level for "warn". |
13 |
| - const args = Array.prototype.slice.call(arguments); |
14 |
| - args.unshift(false); |
15 |
| - warningWWW.apply(null, args); |
| 11 | +export function warn(format, ...args) { |
| 12 | + if (__DEV__) { |
| 13 | + printWarning('warn', format, args); |
| 14 | + } |
16 | 15 | }
|
17 | 16 |
|
18 |
| -export function error() { |
19 |
| - const args = Array.prototype.slice.call(arguments); |
20 |
| - args.unshift(false); |
21 |
| - warningWWW.apply(null, args); |
| 17 | +export function error(format, ...args) { |
| 18 | + if (__DEV__) { |
| 19 | + printWarning('error', format, args); |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function printWarning(level, format, args) { |
| 24 | + if (__DEV__) { |
| 25 | + const hasExistingStack = |
| 26 | + args.length > 0 && |
| 27 | + typeof args[args.length - 1] === 'string' && |
| 28 | + args[args.length - 1].indexOf('\n in') === 0; |
| 29 | + |
| 30 | + if (!hasExistingStack) { |
| 31 | + const React = require('react'); |
| 32 | + const ReactSharedInternals = |
| 33 | + React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; |
| 34 | + // Defensive in case this is fired before React is initialized. |
| 35 | + if (ReactSharedInternals != null) { |
| 36 | + const ReactDebugCurrentFrame = |
| 37 | + ReactSharedInternals.ReactDebugCurrentFrame; |
| 38 | + const stack = ReactDebugCurrentFrame.getStackAddendum(); |
| 39 | + if (stack !== '') { |
| 40 | + format += '%s'; |
| 41 | + args.push(stack); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + // TODO: don't ignore level and pass it down somewhere too. |
| 46 | + args.unshift(format); |
| 47 | + args.unshift(false); |
| 48 | + warningWWW.apply(null, args); |
| 49 | + } |
22 | 50 | }
|
0 commit comments