Skip to content

Commit e8eb163

Browse files
chore(core): deprecation warning for v8 API ahead of future major release (#8132)
Co-authored-by: Mike Hardy <[email protected]>
1 parent 79d76ff commit e8eb163

File tree

7 files changed

+120
-17
lines changed

7 files changed

+120
-17
lines changed

packages/app/__tests__/app.test.ts

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { describe, expect, it } from '@jest/globals';
2-
3-
import {
1+
import { describe, expect, it, jest } from '@jest/globals';
2+
import { checkV9Deprecation } from '../lib/common/unitTestUtils';
3+
import firebase, {
44
deleteApp,
55
registerVersion,
66
onLog,
@@ -40,4 +40,57 @@ describe('App', function () {
4040
expect(setLogLevel).toBeDefined();
4141
});
4242
});
43+
44+
describe('`console.warn` only called for non-modular API', function () {
45+
it('deleteApp', function () {
46+
// this test has a slightly special setup
47+
// @ts-ignore test
48+
jest.spyOn(getApp(), '_deleteApp').mockImplementation(() => Promise.resolve(null));
49+
checkV9Deprecation(
50+
() => {}, // no modular replacement
51+
() => getApp().delete(), // modular getApp(), then non-modular to check
52+
);
53+
});
54+
55+
it('getApps', function () {
56+
checkV9Deprecation(
57+
() => getApps(),
58+
() => firebase.apps,
59+
);
60+
});
61+
62+
it('getApp', function () {
63+
checkV9Deprecation(
64+
() => getApp(),
65+
() => firebase.app(),
66+
);
67+
});
68+
69+
it('setLogLevel', function () {
70+
checkV9Deprecation(
71+
() => setLogLevel('debug'),
72+
() => firebase.setLogLevel('debug'),
73+
);
74+
});
75+
76+
it('FirebaseApp.toString()', function () {
77+
checkV9Deprecation(
78+
() => {}, // no modular replacement
79+
() => getApp().toString(), // modular getApp(), then non-modular to check
80+
);
81+
});
82+
83+
it('FirebaseApp.extendApp()', function () {
84+
checkV9Deprecation(
85+
// no modular replacement for this one so no modular func to send in
86+
() => {},
87+
// modular getApp(), then non-modular to check
88+
() => {
89+
const app = getApp();
90+
(app as any).extendApp({ some: 'property' });
91+
return;
92+
},
93+
);
94+
});
95+
});
4396
});

packages/app/lib/FirebaseApp.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
17+
import { warnIfNotModularCall } from '@react-native-firebase/app/lib/common';
1818
import { getAppModule } from './internal/registry/nativeModule';
1919

2020
export default class FirebaseApp {
@@ -61,16 +61,21 @@ export default class FirebaseApp {
6161
}
6262

6363
extendApp(extendedProps) {
64+
// this method has no modular alternative, send true for param 'noAlternative'
65+
warnIfNotModularCall(arguments, '', true);
6466
this._checkDestroyed();
6567
Object.assign(this, extendedProps);
6668
}
6769

6870
delete() {
71+
warnIfNotModularCall(arguments, 'deleteApp()');
6972
this._checkDestroyed();
7073
return this._deleteApp();
7174
}
7275

7376
toString() {
77+
// this method has no modular alternative, send true for param 'noAlternative'
78+
warnIfNotModularCall(arguments, '', true);
7479
return this.name;
7580
}
7681
}

packages/app/lib/common/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,23 @@ export function tryJSONStringify(data) {
102102
return null;
103103
}
104104
}
105+
106+
export const MODULAR_DEPRECATION_ARG = 'react-native-firebase-modular-method-call';
107+
108+
export function warnIfNotModularCall(args, replacementMethodName, noAlternative) {
109+
for (let i = 0; i < args.length; i++) {
110+
if (args[i] === MODULAR_DEPRECATION_ARG) {
111+
return;
112+
}
113+
}
114+
let message =
115+
'This v8 method is deprecated and will be removed in the next major release ' +
116+
'as part of move to match Firebase Web modular v9 SDK API.';
117+
118+
if (!noAlternative) {
119+
message += ` Please use \`${replacementMethodName}\` instead.`;
120+
}
121+
122+
// eslint-disable-next-line no-console
123+
console.warn(message);
124+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { expect, jest } from '@jest/globals';
2+
3+
export const checkV9Deprecation = (modularFunction: () => void, nonModularFunction: () => void) => {
4+
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
5+
modularFunction();
6+
expect(consoleWarnSpy).not.toHaveBeenCalled();
7+
nonModularFunction();
8+
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
9+
consoleWarnSpy.mockRestore();
10+
};

packages/app/lib/internal/registry/app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
isIOS,
2020
isOther,
2121
isNull,
22+
warnIfNotModularCall,
2223
isObject,
2324
isFunction,
2425
isString,
@@ -84,6 +85,7 @@ export function initializeNativeApps() {
8485
* @param name
8586
*/
8687
export function getApp(name = DEFAULT_APP_NAME) {
88+
warnIfNotModularCall(arguments, 'getApp()');
8789
if (!initializedNativeApps) {
8890
initializeNativeApps();
8991
}
@@ -100,6 +102,7 @@ export function getApp(name = DEFAULT_APP_NAME) {
100102
* Gets all app instances, used for `firebase.apps`
101103
*/
102104
export function getApps() {
105+
warnIfNotModularCall(arguments, 'getApps()');
103106
if (!initializedNativeApps) {
104107
initializeNativeApps();
105108
}
@@ -112,6 +115,7 @@ export function getApps() {
112115
* @param configOrName
113116
*/
114117
export function initializeApp(options = {}, configOrName) {
118+
warnIfNotModularCall(arguments, 'initializeApp()');
115119
let appConfig = configOrName;
116120

117121
if (!isObject(configOrName) || isNull(configOrName)) {
@@ -200,6 +204,7 @@ export function initializeApp(options = {}, configOrName) {
200204
}
201205

202206
export function setLogLevel(logLevel) {
207+
warnIfNotModularCall(arguments, 'setLogLevel()');
203208
if (!['error', 'warn', 'info', 'debug', 'verbose'].includes(logLevel)) {
204209
throw new Error('LogLevel must be one of "error", "warn", "info", "debug", "verbose"');
205210
}

packages/app/lib/modular/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { MODULAR_DEPRECATION_ARG } from '@react-native-firebase/app/lib/common';
12
/* eslint-disable @typescript-eslint/no-unused-vars */
23
import {
34
deleteApp as deleteAppCompat,
@@ -6,6 +7,7 @@ import {
67
initializeApp as initializeAppCompat,
78
setLogLevel as setLogLevelCompat,
89
} from '../internal';
10+
import sdkVersion from '../version';
911

1012
/**
1113
* @typedef {import('..').ReactNativeFirebase.FirebaseApp} FirebaseApp
@@ -19,7 +21,7 @@ import {
1921
* @returns {Promise<void>}
2022
*/
2123
export function deleteApp(app) {
22-
return deleteAppCompat(app.name, app._nativeInitialized);
24+
return deleteAppCompat.call(null, app.name, app._nativeInitialized, MODULAR_DEPRECATION_ARG);
2325
}
2426

2527
/**
@@ -48,7 +50,7 @@ export function onLog(logCallback, options) {
4850
* @returns {FirebaseApp[]} - An array of all initialized Firebase apps.
4951
*/
5052
export function getApps() {
51-
return getAppsCompat();
53+
return getAppsCompat.call(null, MODULAR_DEPRECATION_ARG);
5254
}
5355

5456
/**
@@ -58,7 +60,7 @@ export function getApps() {
5860
* @returns {FirebaseApp} - The initialized Firebase app.
5961
*/
6062
export function initializeApp(options, name) {
61-
return initializeAppCompat(options, name);
63+
return initializeAppCompat.call(null, options, name, MODULAR_DEPRECATION_ARG);
6264
}
6365

6466
/**
@@ -67,7 +69,7 @@ export function initializeApp(options, name) {
6769
* @returns {FirebaseApp} - The requested Firebase app instance.
6870
*/
6971
export function getApp(name) {
70-
return getAppCompat(name);
72+
return getAppCompat.call(null, name, MODULAR_DEPRECATION_ARG);
7173
}
7274

7375
/**
@@ -76,5 +78,7 @@ export function getApp(name) {
7678
* @returns {void}
7779
*/
7880
export function setLogLevel(logLevel) {
79-
return setLogLevelCompat(logLevel);
81+
return setLogLevelCompat.call(null, logLevel, MODULAR_DEPRECATION_ARG);
8082
}
83+
84+
export const SDK_VERSION = sdkVersion;

packages/crashlytics/e2e/crashlytics.e2e.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,13 @@ describe('crashlytics()', function () {
9090
let logged = false;
9191
// eslint-disable-next-line no-console
9292
console.warn = msg => {
93-
msg.should.containEql('expects an instance of Error');
94-
logged = true;
95-
// eslint-disable-next-line no-console
96-
console.warn = orig;
93+
// we console.warn for deprecated API, can be removed when we move to v9
94+
if (!msg.includes('v8 method is deprecated')) {
95+
msg.should.containEql('expects an instance of Error');
96+
logged = true;
97+
// eslint-disable-next-line no-console
98+
console.warn = orig;
99+
}
97100
};
98101

99102
firebase.crashlytics().recordError(1337);
@@ -261,10 +264,13 @@ describe('crashlytics()', function () {
261264
let logged = false;
262265
// eslint-disable-next-line no-console
263266
console.warn = msg => {
264-
msg.should.containEql('expects an instance of Error');
265-
logged = true;
266-
// eslint-disable-next-line no-console
267-
console.warn = orig;
267+
// we console.warn for deprecated API, can be removed when we move to v9
268+
if (!msg.includes('v8 method is deprecated')) {
269+
msg.should.containEql('expects an instance of Error');
270+
logged = true;
271+
// eslint-disable-next-line no-console
272+
console.warn = orig;
273+
}
268274
};
269275

270276
recordError(getCrashlytics(), 1337);

0 commit comments

Comments
 (0)