Skip to content

Commit

Permalink
Migrate AppState and BatchedBridge files to use export syntax. (#…
Browse files Browse the repository at this point in the history
…48737)

Summary:
Pull Request resolved: #48737

## Motivation
Modernising the react-native codebase to allow for ingestion by modern Flow tooling.

## This diff
- Updates files in Libraries/AppState and Libraries/BatchedBridge to use `export` syntax
  - `export default` for qualified objects, many `export` statements for collections (determined by how it's imported)
- Appends `.default` to requires of the changed files.
- Updates Jest mocks.
- Updates the public API snapshot (intented breaking change)

Changelog:
[General][Breaking] - Files inside `Libraries/BatchedBridge` and `Libraries/AppState` use `export` syntax, which requires the addition of `.default` when imported with the CJS `require` syntax.

Reviewed By: robhogan

Differential Revision: D68275767

fbshipit-source-id: 97dc84c04a8dd9c9022e53fc4595302efc848338
  • Loading branch information
iwoplaza authored and facebook-github-bot committed Jan 17, 2025
1 parent d9a77e1 commit 135277a
Show file tree
Hide file tree
Showing 25 changed files with 266 additions and 236 deletions.
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/AppState/AppState.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,4 @@ class AppState {
}
}

module.exports = (new AppState(): AppState);
export default (new AppState(): AppState);
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

'use strict';

const MessageQueue = require('./MessageQueue');
import typeof MessageQueueT from './MessageQueue';

const MessageQueue: MessageQueueT = require('./MessageQueue').default;

const BatchedBridge: MessageQueue = new MessageQueue();

Expand All @@ -25,4 +27,4 @@ Object.defineProperty(global, '__fbBatchedBridge', {
value: BatchedBridge,
});

module.exports = BatchedBridge;
export default BatchedBridge;
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,4 @@ class MessageQueue {
}
}

module.exports = MessageQueue;
export default MessageQueue;
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import type {ExtendedError} from '../Core/ExtendedError';

const BatchedBridge = require('./BatchedBridge');
const BatchedBridge = require('./BatchedBridge').default;
const invariant = require('invariant');

export type ModuleConfig = [
Expand Down Expand Up @@ -209,4 +209,4 @@ if (global.nativeModuleProxy) {
);
}

module.exports = NativeModules;
export default NativeModules;
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const assertQueue = (flushedQueue, index, moduleID, methodID, params) => {
describe('MessageQueue', function () {
beforeEach(function () {
jest.resetModules();
MessageQueue = require('../MessageQueue');
MessageQueue = require('../MessageQueue').default;
MessageQueueTestModule = require('../__mocks__/MessageQueueTestModule');
queue = new MessageQueue();
queue.registerCallableModule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ describe('MessageQueue', function () {
jest.resetModules();

global.__fbBatchedBridgeConfig = require('../__mocks__/MessageQueueTestConfig');
BatchedBridge = require('../BatchedBridge');
NativeModules = require('../NativeModules');
BatchedBridge = require('../BatchedBridge').default;
NativeModules = require('../NativeModules').default;
});

it('should generate native modules', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/react-native/Libraries/Blob/__tests__/Blob-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
'use strict';

jest.setMock('../../BatchedBridge/NativeModules', {
BlobModule: require('../__mocks__/BlobModule'),
__esModule: true,
default: {
BlobModule: require('../__mocks__/BlobModule'),
},
});

const Blob = require('../Blob');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
'use strict';

jest.setMock('../../BatchedBridge/NativeModules', {
BlobModule: require('../__mocks__/BlobModule'),
__esModule: true,
default: {
BlobModule: require('../__mocks__/BlobModule'),
},
});

const Blob = require('../Blob');
Expand Down
5 changes: 4 additions & 1 deletion packages/react-native/Libraries/Blob/__tests__/File-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
'use strict';

jest.setMock('../../BatchedBridge/NativeModules', {
BlobModule: require('../__mocks__/BlobModule'),
__esModule: true,
default: {
BlobModule: require('../__mocks__/BlobModule'),
},
});

const Blob = require('../Blob');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
'use strict';

jest.unmock('event-target-shim').setMock('../../BatchedBridge/NativeModules', {
BlobModule: require('../__mocks__/BlobModule'),
FileReaderModule: require('../__mocks__/FileReaderModule'),
__esModule: true,
default: {
BlobModule: require('../__mocks__/BlobModule'),
FileReaderModule: require('../__mocks__/FileReaderModule'),
},
});

const Blob = require('../Blob');
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/Core/Timers/JSTimers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import NativeTiming from './NativeTiming';

const BatchedBridge = require('../../BatchedBridge/BatchedBridge');
const BatchedBridge = require('../../BatchedBridge/BatchedBridge').default;
const Systrace = require('../../Performance/Systrace');
const invariant = require('invariant');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const registerCallableModule: RegisterCallableModule = (function () {
};
}

const BatchedBridge = require('../BatchedBridge/BatchedBridge');
const BatchedBridge = require('../BatchedBridge/BatchedBridge').default;
return (name, moduleOrFactory) => {
if (typeof moduleOrFactory === 'function') {
BatchedBridge.registerLazyCallableModule(name, moduleOrFactory);
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/Core/setUpReactDevTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ if (__DEV__) {
// not when debugging in chrome
// TODO(t12832058) This check is broken
if (!window.document) {
const AppState = require('../AppState/AppState');
const AppState = require('../AppState/AppState').default;
const getDevServer = require('./Devtools/getDevServer');

// Don't steal the DevTools from currently active app.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {Task} from './TaskQueue';
import * as ReactNativeFeatureFlags from '../../src/private/featureflags/ReactNativeFeatureFlags';
import EventEmitter from '../vendor/emitter/EventEmitter';

const BatchedBridge = require('../BatchedBridge/BatchedBridge');
const BatchedBridge = require('../BatchedBridge/BatchedBridge').default;
const infoLog = require('../Utilities/infoLog');
const TaskQueue = require('./TaskQueue');
const invariant = require('invariant');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

'use strict';

const BatchedBridge = require('../../BatchedBridge/BatchedBridge');
const BatchedBridge = require('../../BatchedBridge/BatchedBridge').default;

jest.mock('../../vendor/core/ErrorUtils');
jest.mock('../../BatchedBridge/BatchedBridge');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,23 @@ function setRequestId(id) {
jest
.dontMock('event-target-shim')
.setMock('../../BatchedBridge/NativeModules', {
Networking: {
addListener: function () {},
removeListeners: function () {},
sendRequest(options, callback) {
if (typeof callback === 'function') {
// android does not pass a callback
callback(requestId);
}
__esModule: true,
default: {
Networking: {
addListener: function () {},
removeListeners: function () {},
sendRequest(options, callback) {
if (typeof callback === 'function') {
// android does not pass a callback
callback(requestId);
}
},
abortRequest: function () {},
},
abortRequest: function () {},
},
PlatformConstants: {
getConstants() {
return {};
PlatformConstants: {
getConstants() {
return {};
},
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {UIManagerJSInterface} from '../Types/UIManagerJSInterface';
import NativeUIManager from './NativeUIManager';
import nullthrows from 'nullthrows';

const NativeModules = require('../BatchedBridge/NativeModules');
const NativeModules = require('../BatchedBridge/NativeModules').default;
const defineLazyObjectProperty = require('../Utilities/defineLazyObjectProperty');
const Platform = require('../Utilities/Platform');
const UIManagerProperties = require('./UIManagerProperties');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import typeof Platform from '../Utilities/Platform';
// flowlint unsafe-getters-setters:off
module.exports = {
get BatchedBridge(): BatchedBridge {
return require('../BatchedBridge/BatchedBridge');
return require('../BatchedBridge/BatchedBridge').default;
},
get ExceptionsManager(): ExceptionsManager {
return require('../Core/ExceptionsManager');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {TurboModule} from './RCTExport';

import invariant from 'invariant';

const NativeModules = require('../BatchedBridge/NativeModules');
const NativeModules = require('../BatchedBridge/NativeModules').default;

const turboModuleProxy = global.__turboModuleProxy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@

jest.mock('../../EventEmitter/NativeEventEmitter');
jest.setMock('../../BatchedBridge/NativeModules', {
WebSocketModule: {
connect: () => {},
__esModule: true,
default: {
WebSocketModule: {
connect: () => {},
},
PlatformConstants: {},
},
PlatformConstants: {},
});

const WebSocket = require('../WebSocket');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,7 @@ declare class AppState {
handler: (...$ElementType<AppStateEventDefinitions, K>) => void
): EventSubscription;
}
declare module.exports: AppState;
declare export default AppState;
"
`;

Expand All @@ -1258,7 +1258,7 @@ declare export default typeof NativeAppState;

exports[`public API should not change unintentionally Libraries/BatchedBridge/BatchedBridge.js 1`] = `
"declare const BatchedBridge: MessageQueue;
declare module.exports: BatchedBridge;
declare export default typeof BatchedBridge;
"
`;

Expand Down Expand Up @@ -1335,7 +1335,7 @@ declare class MessageQueue {
__callFunction(module: string, method: string, args: mixed[]): void;
__invokeCallback(cbID: number, args: mixed[]): void;
}
declare module.exports: MessageQueue;
declare export default typeof MessageQueue;
"
`;

Expand All @@ -1349,7 +1349,7 @@ exports[`public API should not change unintentionally Libraries/BatchedBridge/Na
];
export type MethodType = \\"async\\" | \\"promise\\" | \\"sync\\";
declare let NativeModules: { [moduleName: string]: $FlowFixMe, ... };
declare module.exports: NativeModules;
declare export default typeof NativeModules;
"
`;

Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ module.exports = {
return require('./Libraries/ReactNative/AppRegistry');
},
get AppState(): AppState {
return require('./Libraries/AppState/AppState');
return require('./Libraries/AppState/AppState').default;
},
get BackHandler(): BackHandler {
return require('./Libraries/Utilities/BackHandler');
Expand Down Expand Up @@ -368,7 +368,7 @@ module.exports = {
return require('./Libraries/EventEmitter/RCTNativeAppEventEmitter');
},
get NativeModules(): NativeModules {
return require('./Libraries/BatchedBridge/NativeModules');
return require('./Libraries/BatchedBridge/NativeModules').default;
},
get Platform(): Platform {
return require('./Libraries/Utilities/Platform');
Expand Down
Loading

0 comments on commit 135277a

Please sign in to comment.