Skip to content

Commit 9822729

Browse files
yungstersfacebook-github-bot
authored andcommitted
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
1 parent 287cf07 commit 9822729

File tree

12 files changed

+41
-51
lines changed

12 files changed

+41
-51
lines changed

IntegrationTests/LoggingTestModule.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

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

14-
const warning = require('fbjs/lib/warning');
1514
const invariant = require('invariant');
1615

1716
const LoggingTestModule = {
@@ -24,7 +23,7 @@ const LoggingTestModule = {
2423
}, timeout_ms);
2524
},
2625
warning: function(str) {
27-
warning(false, str);
26+
console.warn(str);
2827
},
2928
invariant: function(str) {
3029
invariant(false, str);

Libraries/Components/ToastAndroid/ToastAndroid.ios.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@
1010

1111
'use strict';
1212

13-
const warning = require('fbjs/lib/warning');
14-
1513
const ToastAndroid = {
1614
show: function(message: string, duration: number): void {
17-
warning(false, 'ToastAndroid is not supported on this platform.');
15+
console.warn('ToastAndroid is not supported on this platform.');
1816
},
1917

2018
showWithGravity: function(
2119
message: string,
2220
duration: number,
2321
gravity: number,
2422
): void {
25-
warning(false, 'ToastAndroid is not supported on this platform.');
23+
console.warn('ToastAndroid is not supported on this platform.');
2624
},
2725

2826
showWithGravityAndOffset: function(
@@ -32,7 +30,7 @@ const ToastAndroid = {
3230
xOffset: number,
3331
yOffset: number,
3432
): void {
35-
warning(false, 'ToastAndroid is not supported on this platform.');
33+
console.warn('ToastAndroid is not supported on this platform.');
3634
},
3735
};
3836

Libraries/Core/Timers/JSTimers.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ function _allocateCallback(func: Function, type: JSTimerType): number {
8181
* recurring (setInterval).
8282
*/
8383
function _callTimer(timerID: number, frameTime: number, didTimeout: ?boolean) {
84-
require('fbjs/lib/warning')(
85-
timerID <= GUID,
86-
'Tried to call timer with ID %s but no such timer exists.',
87-
timerID,
88-
);
84+
if (timerID > GUID) {
85+
console.warn(
86+
'Tried to call timer with ID %s but no such timer exists.',
87+
timerID,
88+
);
89+
}
8990

9091
// timerIndex of -1 means that no timer with that ID exists. There are
9192
// two situations when this happens, when a garbage timer ID was given

Libraries/Core/Timers/__tests__/JSTimers-test.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ const NativeTiming = {
1616
setSendIdleEvents: jest.fn(),
1717
};
1818

19-
const warning = jest.fn();
20-
2119
jest
2220
.enableAutomock()
23-
.mock('fbjs/lib/warning', () => warning, {virtual: true})
2421
.mock('../NativeTiming', () => ({
2522
__esModule: true,
2623
default: NativeTiming,
@@ -30,14 +27,15 @@ jest
3027
const JSTimers = require('../JSTimers');
3128

3229
describe('JSTimers', function() {
33-
const firstArgumentOfTheLastCallTo = function(func) {
34-
return func.mock.calls[func.mock.calls.length - 1][0];
35-
};
36-
3730
beforeEach(function() {
31+
jest.spyOn(console, 'warn');
3832
global.setTimeout = JSTimers.setTimeout;
3933
});
4034

35+
afterEach(() => {
36+
console.warn.mockRestore();
37+
});
38+
4139
it('should call function with setTimeout', function() {
4240
let didCall = false;
4341
const id = JSTimers.setTimeout(function() {
@@ -277,12 +275,12 @@ describe('JSTimers', function() {
277275
JSTimers.clearTimeout(timerID);
278276
JSTimers.callTimers([timerID]);
279277
expect(callback).not.toBeCalled();
280-
expect(firstArgumentOfTheLastCallTo(warning)).toBe(true);
278+
expect(console.warn).not.toBeCalled();
281279
});
282280

283281
it('should warn when callTimers is called with garbage timer id', function() {
284282
JSTimers.callTimers([1337]);
285-
expect(firstArgumentOfTheLastCallTo(warning)).toBe(false);
283+
expect(console.warn).toBeCalled();
286284
});
287285

288286
it('should only call callback once for setTimeout', function() {
@@ -294,7 +292,7 @@ describe('JSTimers', function() {
294292
// Second time it should be ignored
295293
JSTimers.callTimers([timerID]);
296294
expect(callback).toBeCalledTimes(1);
297-
expect(firstArgumentOfTheLastCallTo(warning)).toBe(true);
295+
expect(console.warn).not.toBeCalled();
298296
});
299297

300298
it('should only call callback once for requestAnimationFrame', function() {
@@ -306,7 +304,7 @@ describe('JSTimers', function() {
306304
// Second time it should be ignored
307305
JSTimers.callTimers([timerID]);
308306
expect(callback).toBeCalledTimes(1);
309-
expect(firstArgumentOfTheLastCallTo(warning)).toBe(true);
307+
expect(console.warn).not.toBeCalled();
310308
});
311309

312310
it('should re-throw first exception', function() {

Libraries/Lists/FillRateHelper.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,9 @@ class FillRateHelper {
5959
static addListener(
6060
callback: FillRateInfo => void,
6161
): {remove: () => void, ...} {
62-
warning(
63-
_sampleRate !== null,
64-
'Call `FillRateHelper.setSampleRate` before `addListener`.',
65-
);
62+
if (_sampleRate === null) {
63+
console.warn('Call `FillRateHelper.setSampleRate` before `addListener`.');
64+
}
6665
_listeners.push(callback);
6766
return {
6867
remove: () => {

Libraries/Lists/VirtualizedList.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const ViewabilityHelper = require('./ViewabilityHelper');
2222
const flattenStyle = require('../StyleSheet/flattenStyle');
2323
const infoLog = require('../Utilities/infoLog');
2424
const invariant = require('invariant');
25-
const warning = require('fbjs/lib/warning');
2625

2726
const {computeWindowedRenderLimits} = require('./VirtualizeUtils');
2827

@@ -850,11 +849,12 @@ class VirtualizedList extends React.PureComponent<Props, State> {
850849
render(): React.Node {
851850
if (__DEV__) {
852851
const flatStyles = flattenStyle(this.props.contentContainerStyle);
853-
warning(
854-
flatStyles == null || flatStyles.flexWrap !== 'wrap',
855-
'`flexWrap: `wrap`` is not supported with the `VirtualizedList` components.' +
856-
'Consider using `numColumns` with `FlatList` instead.',
857-
);
852+
if (flatStyles != null && flatStyles.flexWrap === 'wrap') {
853+
console.warn(
854+
'`flexWrap: `wrap`` is not supported with the `VirtualizedList` components.' +
855+
'Consider using `numColumns` with `FlatList` instead.',
856+
);
857+
}
858858
}
859859
const {
860860
ListEmptyComponent,

Libraries/Network/XMLHttpRequest.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const RCTNetworking = require('./RCTNetworking');
1717

1818
const base64 = require('base64-js');
1919
const invariant = require('invariant');
20-
const warning = require('fbjs/lib/warning');
2120

2221
const DEBUG_NETWORK_SEND_DELAY: false = false; // Set to a number of milliseconds when debugging
2322

@@ -184,8 +183,7 @@ class XMLHttpRequest extends (EventTarget(...XHR_EVENTS): any) {
184183
);
185184
}
186185
if (!SUPPORTED_RESPONSE_TYPES.hasOwnProperty(responseType)) {
187-
warning(
188-
false,
186+
console.warn(
189187
`The provided value '${responseType}' is not a valid 'responseType'.`,
190188
);
191189
return;

Libraries/Network/__tests__/XMLHttpRequest-test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,16 @@ describe('XMLHttpRequest', function() {
9595
it('should expose responseType correctly', function() {
9696
expect(xhr.responseType).toBe('');
9797

98-
jest.spyOn(console, 'error').mockImplementationOnce(() => {});
98+
jest.spyOn(console, 'warn').mockReturnValue(undefined);
9999

100100
// Setting responseType to an unsupported value has no effect.
101101
xhr.responseType = 'arrayblobbuffertextfile';
102102
expect(xhr.responseType).toBe('');
103103

104-
expect(console.error).toBeCalledWith(
105-
"Warning: The provided value 'arrayblobbuffertextfile' is not a valid 'responseType'.",
104+
expect(console.warn).toBeCalledWith(
105+
"The provided value 'arrayblobbuffertextfile' is not a valid 'responseType'.",
106106
);
107-
console.error.mockRestore();
107+
console.warn.mockRestore();
108108

109109
xhr.responseType = 'arraybuffer';
110110
expect(xhr.responseType).toBe('arraybuffer');

Libraries/ReactNative/getNativeComponentAttributes.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const processColor = require('../StyleSheet/processColor');
2121
const processColorArray = require('../StyleSheet/processColorArray');
2222
const resolveAssetSource = require('../Image/resolveAssetSource');
2323
const sizesDiffer = require('../Utilities/differ/sizesDiffer');
24-
const warning = require('fbjs/lib/warning');
2524

2625
function getNativeComponentAttributes(uiViewClassName: string): any {
2726
const viewConfig = UIManager.getViewManagerConfig(uiViewClassName);
@@ -39,7 +38,7 @@ function getNativeComponentAttributes(uiViewClassName: string): any {
3938
while (baseModuleName) {
4039
const baseModule = UIManager.getViewManagerConfig(baseModuleName);
4140
if (!baseModule) {
42-
warning(false, 'Base module "%s" does not exist', baseModuleName);
41+
console.warn('Base module "%s" does not exist', baseModuleName);
4342
baseModuleName = null;
4443
} else {
4544
bubblingEventTypes = {

Libraries/Utilities/__tests__/warnOnce-test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ describe('warnOnce', () => {
1414
const warnOnce = require('../warnOnce');
1515

1616
it('logs warning messages to the console exactly once', () => {
17-
console.error = jest.fn();
17+
jest.spyOn(console, 'warn').mockReturnValue(undefined);
1818

1919
warnOnce('test-message', 'This is a log message');
2020
warnOnce('test-message', 'This is a second log message');
2121

22-
expect(console.error).toHaveBeenCalledWith(
23-
'Warning: This is a log message',
24-
);
25-
expect(console.error).toHaveBeenCalledTimes(1);
22+
expect(console.warn).toHaveBeenCalledWith('This is a log message');
23+
expect(console.warn).toHaveBeenCalledTimes(1);
24+
25+
console.warn.mockRestore();
2626
});
2727
});

Libraries/Utilities/warnOnce.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
'use strict';
1212

13-
const warning = require('fbjs/lib/warning');
14-
1513
const warnedKeys: {[string]: boolean, ...} = {};
1614

1715
/**
@@ -26,7 +24,7 @@ function warnOnce(key: string, message: string) {
2624
return;
2725
}
2826

29-
warning(false, message);
27+
console.warn(message);
3028

3129
warnedKeys[key] = true;
3230
}

packages/rn-tester/RNTesterIntegrationTests/RCTLoggingTests.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ - (void)testLogging
8383

8484
XCTAssertEqual(_lastLogLevel, RCTLogLevelWarning);
8585
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
86-
XCTAssertEqualObjects(_lastLogMessage, @"Warning: Generating warning");
86+
XCTAssertEqualObjects(_lastLogMessage, @"Generating warning");
8787

8888
[_bridge enqueueJSCall:@"LoggingTestModule.invariant" args:@[@"Invariant failed"]];
8989
dispatch_semaphore_wait(_logSem, DISPATCH_TIME_FOREVER);

0 commit comments

Comments
 (0)