From a974c140db605ecbdf8d3faa7a079b7e2dcebb09 Mon Sep 17 00:00:00 2001 From: Blair Vanderhoof Date: Tue, 2 May 2017 12:10:55 -0700 Subject: [PATCH] Add method on YellowBox to ignore warnings Reviewed By: yungsters Differential Revision: D4979460 fbshipit-source-id: 090a29009a1256809bd975184ad4957b2f6fc36d --- Libraries/ReactNative/YellowBox.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Libraries/ReactNative/YellowBox.js b/Libraries/ReactNative/YellowBox.js index 458441f805f680..d6d2514b829e54 100644 --- a/Libraries/ReactNative/YellowBox.js +++ b/Libraries/ReactNative/YellowBox.js @@ -33,6 +33,7 @@ type WarningInfo = { const _warningEmitter = new EventEmitter(); const _warningMap: Map = new Map(); +const IGNORED_WARNINGS: Array = []; /** * YellowBox renders warnings at the bottom of the app being developed. @@ -47,7 +48,11 @@ const _warningMap: Map = new Map(); * console.disableYellowBox = true; * console.warn('YellowBox is disabled.'); * - * Warnings can be ignored programmatically by setting the array: + * Ignore specific warnings by calling: + * + * YellowBox.ignoreWarnings(['Warning: ...']); + * + * (DEPRECATED) Warnings can be ignored programmatically by setting the array: * * console.ignoredYellowBox = ['Warning: ...']; * @@ -153,6 +158,16 @@ function ensureSymbolicatedWarning(warning: string): void { } function isWarningIgnored(warning: string): boolean { + const isIgnored = + IGNORED_WARNINGS.some( + (ignoredWarning: string) => warning.startsWith(ignoredWarning) + ); + + if (isIgnored) { + return true; + } + + // DEPRECATED return ( Array.isArray(console.ignoredYellowBox) && console.ignoredYellowBox.some( @@ -316,6 +331,14 @@ class YellowBox extends React.Component { }; } + static ignoreWarnings(warnings: Array): void { + warnings.forEach((warning: string) => { + if (IGNORED_WARNINGS.indexOf(warning) === -1) { + IGNORED_WARNINGS.push(warning); + } + }); + } + componentDidMount() { let scheduled = null; this._listener = _warningEmitter.addListener('warning', warningMap => {