From c212a1f4dceaab9d9da2d14162399417cb236808 Mon Sep 17 00:00:00 2001 From: empyrical Date: Sun, 4 Nov 2018 00:15:19 -0600 Subject: [PATCH 1/2] Warn that 'WebView' has moved to 'react-native-webview' --- Libraries/Utilities/warnMoved.js | 43 +++++++++++++++++++ .../react-native-implementation.js | 12 +----- 2 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 Libraries/Utilities/warnMoved.js diff --git a/Libraries/Utilities/warnMoved.js b/Libraries/Utilities/warnMoved.js new file mode 100644 index 00000000000000..f59005098ec9c0 --- /dev/null +++ b/Libraries/Utilities/warnMoved.js @@ -0,0 +1,43 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + * @flow + */ + +'use strict'; + +const warning = require('fbjs/lib/warning'); + +const warnedApis: {[string]: boolean} = {}; + +/** + * A simple function that warns the user once if an API has been moved to + * another module. + * + * @param {string} api - The name of the API or component that moved + * @param {string} destModule - The name of the module that the API moved to + * @param {string} [renamed] - Optional; name of the API in the dest module, + * if it was renamed + */ +function warnMoved(api: string, destModule: string, renamed?: string) { + if (warnedApis[api]) { + return; + } + + const destName = renamed != null ? renamed : api; + + warning( + false, + `'${api}' has moved to another module and will be removed from 'react-native' ` + + `in a future release. You can instead import it from the module '${destModule}':` + + `\n import {${destName}} from '${destModule}';`, + ); + + warnedApis[api] = true; +} + +module.exports = warnMoved; diff --git a/Libraries/react-native/react-native-implementation.js b/Libraries/react-native/react-native-implementation.js index e9b7e56aba1939..df588afb780b34 100644 --- a/Libraries/react-native/react-native-implementation.js +++ b/Libraries/react-native/react-native-implementation.js @@ -11,10 +11,10 @@ 'use strict'; const invariant = require('invariant'); +const warnMoved = require('warnMoved'); let showedListViewDeprecation = false; let showedSwipeableListViewDeprecation = false; -let showedWebWiewDeprecation = false; // Export React, plus some native additions. module.exports = { @@ -165,15 +165,7 @@ module.exports = { return require('VirtualizedList'); }, get WebView() { - if (!showedWebWiewDeprecation) { - console.warn( - 'WebView has been extracted from react-native core and will be removed in a future release. ' + - "It can now be installed and imported from 'react-native-webview' instead of 'react-native'. " + - 'See https://github.com/react-native-community/react-native-webview for more informations.', - ); - - showedWebWiewDeprecation = true; - } + warnMoved('WebView', 'react-native-webview'); return require('WebView'); }, From 39fe07c0698c2395a184ddcd22a5faac473b36aa Mon Sep 17 00:00:00 2001 From: empyrical Date: Mon, 4 Feb 2019 13:44:00 -0700 Subject: [PATCH 2/2] Make warnMoved a more generic function --- Libraries/Utilities/warnMoved.js | 43 ------------------- Libraries/Utilities/warnOnce.js | 34 +++++++++++++++ .../react-native-implementation.js | 38 ++++++++-------- 3 files changed, 51 insertions(+), 64 deletions(-) delete mode 100644 Libraries/Utilities/warnMoved.js create mode 100644 Libraries/Utilities/warnOnce.js diff --git a/Libraries/Utilities/warnMoved.js b/Libraries/Utilities/warnMoved.js deleted file mode 100644 index f59005098ec9c0..00000000000000 --- a/Libraries/Utilities/warnMoved.js +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - * @flow - */ - -'use strict'; - -const warning = require('fbjs/lib/warning'); - -const warnedApis: {[string]: boolean} = {}; - -/** - * A simple function that warns the user once if an API has been moved to - * another module. - * - * @param {string} api - The name of the API or component that moved - * @param {string} destModule - The name of the module that the API moved to - * @param {string} [renamed] - Optional; name of the API in the dest module, - * if it was renamed - */ -function warnMoved(api: string, destModule: string, renamed?: string) { - if (warnedApis[api]) { - return; - } - - const destName = renamed != null ? renamed : api; - - warning( - false, - `'${api}' has moved to another module and will be removed from 'react-native' ` + - `in a future release. You can instead import it from the module '${destModule}':` + - `\n import {${destName}} from '${destModule}';`, - ); - - warnedApis[api] = true; -} - -module.exports = warnMoved; diff --git a/Libraries/Utilities/warnOnce.js b/Libraries/Utilities/warnOnce.js new file mode 100644 index 00000000000000..8f7350444f15b4 --- /dev/null +++ b/Libraries/Utilities/warnOnce.js @@ -0,0 +1,34 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + * @flow + */ + +'use strict'; + +const warning = require('fbjs/lib/warning'); + +const warnedKeys: {[string]: boolean} = {}; + +/** + * A simple function that prints a warning message once per session. + * + * @param {string} key - The key used to ensure the message is printed once. + * This should be unique to the callsite. + * @param {string} message - The message to print + */ +function warnOnce(key: string, message: string) { + if (warnedKeys[key]) { + return; + } + + warning(false, message); + + warnedKeys[key] = true; +} + +module.exports = warnOnce; diff --git a/Libraries/react-native/react-native-implementation.js b/Libraries/react-native/react-native-implementation.js index df588afb780b34..d14722973dd932 100644 --- a/Libraries/react-native/react-native-implementation.js +++ b/Libraries/react-native/react-native-implementation.js @@ -11,10 +11,7 @@ 'use strict'; const invariant = require('invariant'); -const warnMoved = require('warnMoved'); - -let showedListViewDeprecation = false; -let showedSwipeableListViewDeprecation = false; +const warnOnce = require('warnOnce'); // Export React, plus some native additions. module.exports = { @@ -62,14 +59,11 @@ module.exports = { return require('KeyboardAvoidingView'); }, get ListView() { - if (!showedListViewDeprecation) { - console.warn( - 'ListView is deprecated and will be removed in a future release. ' + - 'See https://fb.me/nolistview for more information', - ); - - showedListViewDeprecation = true; - } + warnOnce( + 'listview-deprecation', + 'ListView is deprecated and will be removed in a future release. ' + + 'See https://fb.me/nolistview for more information', + ); return require('ListView'); }, get MaskedViewIOS() { @@ -121,14 +115,11 @@ module.exports = { return require('SwipeableFlatList'); }, get SwipeableListView() { - if (!showedSwipeableListViewDeprecation) { - console.warn( - 'ListView and SwipeableListView are deprecated and will be removed in a future release. ' + - 'See https://fb.me/nolistview for more information', - ); - - showedSwipeableListViewDeprecation = true; - } + warnOnce( + 'swipablelistview-deprecation', + 'ListView and SwipeableListView are deprecated and will be removed in a future release. ' + + 'See https://fb.me/nolistview for more information', + ); return require('SwipeableListView'); }, get Text() { @@ -165,7 +156,12 @@ module.exports = { return require('VirtualizedList'); }, get WebView() { - warnMoved('WebView', 'react-native-webview'); + warnOnce( + 'webview-moved', + 'WebView has been extracted from react-native core and will be removed in a future release. ' + + "It can now be installed and imported from 'react-native-webview' instead of 'react-native'. " + + 'See https://github.com/react-native-community/react-native-webview for more informations.', + ); return require('WebView'); },