From 982272932cee3be599076bd18b290bc812285533 Mon Sep 17 00:00:00 2001 From: Tim Yung Date: Tue, 25 Aug 2020 14:12:19 -0700 Subject: [PATCH] RN: Remove `fbjs/warning` Dependency Summary: Replaces `fbjs/warning` call sites in React Native with `console.warn`. A few warnings will now log as warnings without the "Warning:" prefix. Changelog: [General][Changed] - Some warnings changed to use `console.warn` without the "Warning:" prefix. Reviewed By: TheSavior, cpojer Differential Revision: D22445946 fbshipit-source-id: 96b01e1bdee52b89ff3b808bc9d6cd494f6787f5 --- IntegrationTests/LoggingTestModule.js | 3 +-- .../ToastAndroid/ToastAndroid.ios.js | 8 +++----- Libraries/Core/Timers/JSTimers.js | 11 +++++----- .../Core/Timers/__tests__/JSTimers-test.js | 20 +++++++++---------- Libraries/Lists/FillRateHelper.js | 7 +++---- Libraries/Lists/VirtualizedList.js | 12 +++++------ Libraries/Network/XMLHttpRequest.js | 4 +--- .../Network/__tests__/XMLHttpRequest-test.js | 8 ++++---- .../getNativeComponentAttributes.js | 3 +-- .../Utilities/__tests__/warnOnce-test.js | 10 +++++----- Libraries/Utilities/warnOnce.js | 4 +--- .../RCTLoggingTests.m | 2 +- 12 files changed, 41 insertions(+), 51 deletions(-) diff --git a/IntegrationTests/LoggingTestModule.js b/IntegrationTests/LoggingTestModule.js index 6eab2d7337fab7..dc1dfb8ac03360 100644 --- a/IntegrationTests/LoggingTestModule.js +++ b/IntegrationTests/LoggingTestModule.js @@ -11,7 +11,6 @@ const BatchedBridge = require('react-native/Libraries/BatchedBridge/BatchedBridge'); -const warning = require('fbjs/lib/warning'); const invariant = require('invariant'); const LoggingTestModule = { @@ -24,7 +23,7 @@ const LoggingTestModule = { }, timeout_ms); }, warning: function(str) { - warning(false, str); + console.warn(str); }, invariant: function(str) { invariant(false, str); diff --git a/Libraries/Components/ToastAndroid/ToastAndroid.ios.js b/Libraries/Components/ToastAndroid/ToastAndroid.ios.js index d0528ff65dc35e..611e50fe87b5b1 100644 --- a/Libraries/Components/ToastAndroid/ToastAndroid.ios.js +++ b/Libraries/Components/ToastAndroid/ToastAndroid.ios.js @@ -10,11 +10,9 @@ 'use strict'; -const warning = require('fbjs/lib/warning'); - const ToastAndroid = { show: function(message: string, duration: number): void { - warning(false, 'ToastAndroid is not supported on this platform.'); + console.warn('ToastAndroid is not supported on this platform.'); }, showWithGravity: function( @@ -22,7 +20,7 @@ const ToastAndroid = { duration: number, gravity: number, ): void { - warning(false, 'ToastAndroid is not supported on this platform.'); + console.warn('ToastAndroid is not supported on this platform.'); }, showWithGravityAndOffset: function( @@ -32,7 +30,7 @@ const ToastAndroid = { xOffset: number, yOffset: number, ): void { - warning(false, 'ToastAndroid is not supported on this platform.'); + console.warn('ToastAndroid is not supported on this platform.'); }, }; diff --git a/Libraries/Core/Timers/JSTimers.js b/Libraries/Core/Timers/JSTimers.js index 00a25d93d9146c..9c5d2a099e3887 100644 --- a/Libraries/Core/Timers/JSTimers.js +++ b/Libraries/Core/Timers/JSTimers.js @@ -81,11 +81,12 @@ function _allocateCallback(func: Function, type: JSTimerType): number { * recurring (setInterval). */ function _callTimer(timerID: number, frameTime: number, didTimeout: ?boolean) { - require('fbjs/lib/warning')( - timerID <= GUID, - 'Tried to call timer with ID %s but no such timer exists.', - timerID, - ); + if (timerID > GUID) { + console.warn( + 'Tried to call timer with ID %s but no such timer exists.', + timerID, + ); + } // timerIndex of -1 means that no timer with that ID exists. There are // two situations when this happens, when a garbage timer ID was given diff --git a/Libraries/Core/Timers/__tests__/JSTimers-test.js b/Libraries/Core/Timers/__tests__/JSTimers-test.js index baa281f03e6bcd..46fbd0d10236fe 100644 --- a/Libraries/Core/Timers/__tests__/JSTimers-test.js +++ b/Libraries/Core/Timers/__tests__/JSTimers-test.js @@ -16,11 +16,8 @@ const NativeTiming = { setSendIdleEvents: jest.fn(), }; -const warning = jest.fn(); - jest .enableAutomock() - .mock('fbjs/lib/warning', () => warning, {virtual: true}) .mock('../NativeTiming', () => ({ __esModule: true, default: NativeTiming, @@ -30,14 +27,15 @@ jest const JSTimers = require('../JSTimers'); describe('JSTimers', function() { - const firstArgumentOfTheLastCallTo = function(func) { - return func.mock.calls[func.mock.calls.length - 1][0]; - }; - beforeEach(function() { + jest.spyOn(console, 'warn'); global.setTimeout = JSTimers.setTimeout; }); + afterEach(() => { + console.warn.mockRestore(); + }); + it('should call function with setTimeout', function() { let didCall = false; const id = JSTimers.setTimeout(function() { @@ -277,12 +275,12 @@ describe('JSTimers', function() { JSTimers.clearTimeout(timerID); JSTimers.callTimers([timerID]); expect(callback).not.toBeCalled(); - expect(firstArgumentOfTheLastCallTo(warning)).toBe(true); + expect(console.warn).not.toBeCalled(); }); it('should warn when callTimers is called with garbage timer id', function() { JSTimers.callTimers([1337]); - expect(firstArgumentOfTheLastCallTo(warning)).toBe(false); + expect(console.warn).toBeCalled(); }); it('should only call callback once for setTimeout', function() { @@ -294,7 +292,7 @@ describe('JSTimers', function() { // Second time it should be ignored JSTimers.callTimers([timerID]); expect(callback).toBeCalledTimes(1); - expect(firstArgumentOfTheLastCallTo(warning)).toBe(true); + expect(console.warn).not.toBeCalled(); }); it('should only call callback once for requestAnimationFrame', function() { @@ -306,7 +304,7 @@ describe('JSTimers', function() { // Second time it should be ignored JSTimers.callTimers([timerID]); expect(callback).toBeCalledTimes(1); - expect(firstArgumentOfTheLastCallTo(warning)).toBe(true); + expect(console.warn).not.toBeCalled(); }); it('should re-throw first exception', function() { diff --git a/Libraries/Lists/FillRateHelper.js b/Libraries/Lists/FillRateHelper.js index e9c579e656e2f2..671d61a37dc6de 100644 --- a/Libraries/Lists/FillRateHelper.js +++ b/Libraries/Lists/FillRateHelper.js @@ -59,10 +59,9 @@ class FillRateHelper { static addListener( callback: FillRateInfo => void, ): {remove: () => void, ...} { - warning( - _sampleRate !== null, - 'Call `FillRateHelper.setSampleRate` before `addListener`.', - ); + if (_sampleRate === null) { + console.warn('Call `FillRateHelper.setSampleRate` before `addListener`.'); + } _listeners.push(callback); return { remove: () => { diff --git a/Libraries/Lists/VirtualizedList.js b/Libraries/Lists/VirtualizedList.js index ee07cf321a1dee..2249d54c4aaa94 100644 --- a/Libraries/Lists/VirtualizedList.js +++ b/Libraries/Lists/VirtualizedList.js @@ -22,7 +22,6 @@ const ViewabilityHelper = require('./ViewabilityHelper'); const flattenStyle = require('../StyleSheet/flattenStyle'); const infoLog = require('../Utilities/infoLog'); const invariant = require('invariant'); -const warning = require('fbjs/lib/warning'); const {computeWindowedRenderLimits} = require('./VirtualizeUtils'); @@ -850,11 +849,12 @@ class VirtualizedList extends React.PureComponent { render(): React.Node { if (__DEV__) { const flatStyles = flattenStyle(this.props.contentContainerStyle); - warning( - flatStyles == null || flatStyles.flexWrap !== 'wrap', - '`flexWrap: `wrap`` is not supported with the `VirtualizedList` components.' + - 'Consider using `numColumns` with `FlatList` instead.', - ); + if (flatStyles != null && flatStyles.flexWrap === 'wrap') { + console.warn( + '`flexWrap: `wrap`` is not supported with the `VirtualizedList` components.' + + 'Consider using `numColumns` with `FlatList` instead.', + ); + } } const { ListEmptyComponent, diff --git a/Libraries/Network/XMLHttpRequest.js b/Libraries/Network/XMLHttpRequest.js index 7bfb0ffa35830d..27df314c5244eb 100644 --- a/Libraries/Network/XMLHttpRequest.js +++ b/Libraries/Network/XMLHttpRequest.js @@ -17,7 +17,6 @@ const RCTNetworking = require('./RCTNetworking'); const base64 = require('base64-js'); const invariant = require('invariant'); -const warning = require('fbjs/lib/warning'); const DEBUG_NETWORK_SEND_DELAY: false = false; // Set to a number of milliseconds when debugging @@ -184,8 +183,7 @@ class XMLHttpRequest extends (EventTarget(...XHR_EVENTS): any) { ); } if (!SUPPORTED_RESPONSE_TYPES.hasOwnProperty(responseType)) { - warning( - false, + console.warn( `The provided value '${responseType}' is not a valid 'responseType'.`, ); return; diff --git a/Libraries/Network/__tests__/XMLHttpRequest-test.js b/Libraries/Network/__tests__/XMLHttpRequest-test.js index 4c0f8a889176e5..f7f1bfcf32d56f 100644 --- a/Libraries/Network/__tests__/XMLHttpRequest-test.js +++ b/Libraries/Network/__tests__/XMLHttpRequest-test.js @@ -95,16 +95,16 @@ describe('XMLHttpRequest', function() { it('should expose responseType correctly', function() { expect(xhr.responseType).toBe(''); - jest.spyOn(console, 'error').mockImplementationOnce(() => {}); + jest.spyOn(console, 'warn').mockReturnValue(undefined); // Setting responseType to an unsupported value has no effect. xhr.responseType = 'arrayblobbuffertextfile'; expect(xhr.responseType).toBe(''); - expect(console.error).toBeCalledWith( - "Warning: The provided value 'arrayblobbuffertextfile' is not a valid 'responseType'.", + expect(console.warn).toBeCalledWith( + "The provided value 'arrayblobbuffertextfile' is not a valid 'responseType'.", ); - console.error.mockRestore(); + console.warn.mockRestore(); xhr.responseType = 'arraybuffer'; expect(xhr.responseType).toBe('arraybuffer'); diff --git a/Libraries/ReactNative/getNativeComponentAttributes.js b/Libraries/ReactNative/getNativeComponentAttributes.js index 226e3ba9342b42..2ec8950379d766 100644 --- a/Libraries/ReactNative/getNativeComponentAttributes.js +++ b/Libraries/ReactNative/getNativeComponentAttributes.js @@ -21,7 +21,6 @@ const processColor = require('../StyleSheet/processColor'); const processColorArray = require('../StyleSheet/processColorArray'); const resolveAssetSource = require('../Image/resolveAssetSource'); const sizesDiffer = require('../Utilities/differ/sizesDiffer'); -const warning = require('fbjs/lib/warning'); function getNativeComponentAttributes(uiViewClassName: string): any { const viewConfig = UIManager.getViewManagerConfig(uiViewClassName); @@ -39,7 +38,7 @@ function getNativeComponentAttributes(uiViewClassName: string): any { while (baseModuleName) { const baseModule = UIManager.getViewManagerConfig(baseModuleName); if (!baseModule) { - warning(false, 'Base module "%s" does not exist', baseModuleName); + console.warn('Base module "%s" does not exist', baseModuleName); baseModuleName = null; } else { bubblingEventTypes = { diff --git a/Libraries/Utilities/__tests__/warnOnce-test.js b/Libraries/Utilities/__tests__/warnOnce-test.js index ee66723c7cf517..f37d8955d5afd9 100644 --- a/Libraries/Utilities/__tests__/warnOnce-test.js +++ b/Libraries/Utilities/__tests__/warnOnce-test.js @@ -14,14 +14,14 @@ describe('warnOnce', () => { const warnOnce = require('../warnOnce'); it('logs warning messages to the console exactly once', () => { - console.error = jest.fn(); + jest.spyOn(console, 'warn').mockReturnValue(undefined); warnOnce('test-message', 'This is a log message'); warnOnce('test-message', 'This is a second log message'); - expect(console.error).toHaveBeenCalledWith( - 'Warning: This is a log message', - ); - expect(console.error).toHaveBeenCalledTimes(1); + expect(console.warn).toHaveBeenCalledWith('This is a log message'); + expect(console.warn).toHaveBeenCalledTimes(1); + + console.warn.mockRestore(); }); }); diff --git a/Libraries/Utilities/warnOnce.js b/Libraries/Utilities/warnOnce.js index adbc64ebc8598a..244cfbe59060c4 100644 --- a/Libraries/Utilities/warnOnce.js +++ b/Libraries/Utilities/warnOnce.js @@ -10,8 +10,6 @@ 'use strict'; -const warning = require('fbjs/lib/warning'); - const warnedKeys: {[string]: boolean, ...} = {}; /** @@ -26,7 +24,7 @@ function warnOnce(key: string, message: string) { return; } - warning(false, message); + console.warn(message); warnedKeys[key] = true; } diff --git a/packages/rn-tester/RNTesterIntegrationTests/RCTLoggingTests.m b/packages/rn-tester/RNTesterIntegrationTests/RCTLoggingTests.m index 9d7844fe713833..3c04e32fe1bc7b 100644 --- a/packages/rn-tester/RNTesterIntegrationTests/RCTLoggingTests.m +++ b/packages/rn-tester/RNTesterIntegrationTests/RCTLoggingTests.m @@ -83,7 +83,7 @@ - (void)testLogging XCTAssertEqual(_lastLogLevel, RCTLogLevelWarning); XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript); - XCTAssertEqualObjects(_lastLogMessage, @"Warning: Generating warning"); + XCTAssertEqualObjects(_lastLogMessage, @"Generating warning"); [_bridge enqueueJSCall:@"LoggingTestModule.invariant" args:@[@"Invariant failed"]]; dispatch_semaphore_wait(_logSem, DISPATCH_TIME_FOREVER);