Skip to content

Commit

Permalink
RN: Remove fbjs/warning Dependency
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yungsters authored and facebook-github-bot committed Aug 25, 2020
1 parent 287cf07 commit 9822729
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 51 deletions.
3 changes: 1 addition & 2 deletions IntegrationTests/LoggingTestModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

const BatchedBridge = require('react-native/Libraries/BatchedBridge/BatchedBridge');

const warning = require('fbjs/lib/warning');
const invariant = require('invariant');

const LoggingTestModule = {
Expand All @@ -24,7 +23,7 @@ const LoggingTestModule = {
}, timeout_ms);
},
warning: function(str) {
warning(false, str);
console.warn(str);
},
invariant: function(str) {
invariant(false, str);
Expand Down
8 changes: 3 additions & 5 deletions Libraries/Components/ToastAndroid/ToastAndroid.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@

'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(
message: string,
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(
Expand All @@ -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.');
},
};

Expand Down
11 changes: 6 additions & 5 deletions Libraries/Core/Timers/JSTimers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 9 additions & 11 deletions Libraries/Core/Timers/__tests__/JSTimers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down
7 changes: 3 additions & 4 deletions Libraries/Lists/FillRateHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => {
Expand Down
12 changes: 6 additions & 6 deletions Libraries/Lists/VirtualizedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -850,11 +849,12 @@ class VirtualizedList extends React.PureComponent<Props, State> {
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,
Expand Down
4 changes: 1 addition & 3 deletions Libraries/Network/XMLHttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions Libraries/Network/__tests__/XMLHttpRequest-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 1 addition & 2 deletions Libraries/ReactNative/getNativeComponentAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 = {
Expand Down
10 changes: 5 additions & 5 deletions Libraries/Utilities/__tests__/warnOnce-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
4 changes: 1 addition & 3 deletions Libraries/Utilities/warnOnce.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

'use strict';

const warning = require('fbjs/lib/warning');

const warnedKeys: {[string]: boolean, ...} = {};

/**
Expand All @@ -26,7 +24,7 @@ function warnOnce(key: string, message: string) {
return;
}

warning(false, message);
console.warn(message);

warnedKeys[key] = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9822729

Please sign in to comment.