|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import ReactSharedInternals from 'shared/ReactSharedInternals'; |
| 9 | + |
| 10 | +let suppressWarning = false; |
| 11 | +export function setSuppressWarning(newSuppressWarning) { |
| 12 | + if (__DEV__) { |
| 13 | + suppressWarning = newSuppressWarning; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +// In DEV, calls to console.warn and console.error get replaced |
| 18 | +// by calls to these methods by a Babel plugin. |
| 19 | +// |
| 20 | +// In PROD (or in packages without access to React internals), |
| 21 | +// they are left as they are instead. |
| 22 | + |
| 23 | +export function warn(format, ...args) { |
| 24 | + if (__DEV__) { |
| 25 | + if (!suppressWarning) { |
| 26 | + printWarning('warn', format, args, new Error('react-stack-top-frame')); |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +export function error(format, ...args) { |
| 32 | + if (__DEV__) { |
| 33 | + if (!suppressWarning) { |
| 34 | + printWarning('error', format, args, new Error('react-stack-top-frame')); |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export let isWritingAppendedStack = false; |
| 40 | + |
| 41 | +function printWarning(level, format, args, currentStack) { |
| 42 | + if (__DEV__) { |
| 43 | + if (ReactSharedInternals.getCurrentStack) { |
| 44 | + const stack = ReactSharedInternals.getCurrentStack(currentStack); |
| 45 | + if (stack !== '') { |
| 46 | + isWritingAppendedStack = true; |
| 47 | + format += '%s'; |
| 48 | + args = args.concat([stack]); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + args.unshift(format); |
| 53 | + // We intentionally don't use spread (or .apply) directly because it |
| 54 | + // breaks IE9: https://github.com/facebook/react/issues/13610 |
| 55 | + // eslint-disable-next-line react-internal/no-production-logging |
| 56 | + Function.prototype.apply.call(console[level], console, args); |
| 57 | + isWritingAppendedStack = false; |
| 58 | + } |
| 59 | +} |
0 commit comments