From 248d783e95ff8c64c90fb48ab9be3a7bd5068970 Mon Sep 17 00:00:00 2001 From: Iwo Plaza Date: Tue, 14 Jan 2025 07:24:19 -0800 Subject: [PATCH] Migrate Libraries/ReactNative/*.js to use `export` syntax. (#48650) Summary: ## Motivation Modernising the RN codebase to allow for modern Flow tooling to process it. ## This diff - Migrates the `Libraries/ReactNative/*.js` files to use the `export` syntax. - Updates deep-imports of these files to use `.default` - Updates the current iteration of API snapshots (intended). Changelog: [General][Breaking] - Deep imports to modules inside `Libraries/ReactNative` with `require` syntax need to be appended with '.default'. Differential Revision: D68109193 --- .../Libraries/Inspector/resolveBoxStyle.js | 2 +- .../LayoutAnimation/LayoutAnimation.js | 2 +- .../react-native/Libraries/Modal/Modal.js | 4 ++-- .../__tests__/Pressability-test.js | 7 ++++--- .../Libraries/ReactNative/AppContainer.js | 2 +- .../Libraries/ReactNative/AppRegistry.js | 2 +- .../ReactNative/BridgelessUIManager.js | 2 +- .../Libraries/ReactNative/I18nManager.js | 2 +- .../Libraries/ReactNative/PaperUIManager.js | 4 ++-- .../ReactNative/ReactNativeFeatureFlags.js | 2 +- .../ReactNativeRuntimeDiagnostics.js | 2 +- .../Libraries/ReactNative/UIManager.d.ts | 1 + .../Libraries/ReactNative/UIManager.js | 6 +++--- .../ReactNative/UIManagerProperties.js | 2 +- .../getNativeComponentAttributes.js | 4 ++-- .../ReactNative/requireNativeComponent.js | 3 ++- .../ReactNativePrivateInterface.js | 2 +- .../__tests__/codegenNativeComponent-test.js | 2 +- .../__snapshots__/public-api-test.js.snap | 20 +++++++++---------- packages/react-native/index.js | 6 +++--- 20 files changed, 40 insertions(+), 37 deletions(-) diff --git a/packages/react-native/Libraries/Inspector/resolveBoxStyle.js b/packages/react-native/Libraries/Inspector/resolveBoxStyle.js index 4a8711f5e92230..fa45add625b4d2 100644 --- a/packages/react-native/Libraries/Inspector/resolveBoxStyle.js +++ b/packages/react-native/Libraries/Inspector/resolveBoxStyle.js @@ -10,7 +10,7 @@ 'use strict'; -const I18nManager = require('../ReactNative/I18nManager'); +const I18nManager = require('../ReactNative/I18nManager').default; /** * Resolve a style property into its component parts. diff --git a/packages/react-native/Libraries/LayoutAnimation/LayoutAnimation.js b/packages/react-native/Libraries/LayoutAnimation/LayoutAnimation.js index a14283e1e7ec0c..220453bc598ea0 100644 --- a/packages/react-native/Libraries/LayoutAnimation/LayoutAnimation.js +++ b/packages/react-native/Libraries/LayoutAnimation/LayoutAnimation.js @@ -20,7 +20,7 @@ import * as ReactNativeFeatureFlags from '../../src/private/featureflags/ReactNa import {getFabricUIManager} from '../ReactNative/FabricUIManager'; import Platform from '../Utilities/Platform'; -const UIManager = require('../ReactNative/UIManager'); +const UIManager = require('../ReactNative/UIManager').default; // Reexport type export type LayoutAnimationConfig = LayoutAnimationConfig_; diff --git a/packages/react-native/Libraries/Modal/Modal.js b/packages/react-native/Libraries/Modal/Modal.js index 8d0a85b15fb9ac..c680f75bbb9860 100644 --- a/packages/react-native/Libraries/Modal/Modal.js +++ b/packages/react-native/Libraries/Modal/Modal.js @@ -22,8 +22,8 @@ import React from 'react'; const ScrollView = require('../Components/ScrollView/ScrollView'); const View = require('../Components/View/View'); -const AppContainer = require('../ReactNative/AppContainer'); -const I18nManager = require('../ReactNative/I18nManager'); +const AppContainer = require('../ReactNative/AppContainer').default; +const I18nManager = require('../ReactNative/I18nManager').default; const {RootTagContext} = require('../ReactNative/RootTag'); const StyleSheet = require('../StyleSheet/StyleSheet'); const Platform = require('../Utilities/Platform'); diff --git a/packages/react-native/Libraries/Pressability/__tests__/Pressability-test.js b/packages/react-native/Libraries/Pressability/__tests__/Pressability-test.js index a70cd006fc998f..c061adf67d1e6e 100644 --- a/packages/react-native/Libraries/Pressability/__tests__/Pressability-test.js +++ b/packages/react-native/Libraries/Pressability/__tests__/Pressability-test.js @@ -15,10 +15,11 @@ jest.useFakeTimers({legacyFakeTimers: true}); import type {PressEvent} from '../../Types/CoreEventTypes'; import type {PressabilityConfig} from '../Pressability'; -const UIManager = require('../../ReactNative/UIManager'); -const Platform = require('../../Utilities/Platform'); +import UIManager from '../../ReactNative/UIManager'; +import Platform from '../../Utilities/Platform'; +import Pressability from '../Pressability'; + const HoverState = require('../HoverState'); -const Pressability = require('../Pressability').default; const invariant = require('invariant'); const isWindows = process.platform === 'win32'; diff --git a/packages/react-native/Libraries/ReactNative/AppContainer.js b/packages/react-native/Libraries/ReactNative/AppContainer.js index afea7ca8631d01..91ff5fba4e7c45 100644 --- a/packages/react-native/Libraries/ReactNative/AppContainer.js +++ b/packages/react-native/Libraries/ReactNative/AppContainer.js @@ -28,4 +28,4 @@ const AppContainer: component(...Props) = __DEV__ ? require('./AppContainer-dev').default : require('./AppContainer-prod').default; -module.exports = AppContainer; +export default AppContainer; diff --git a/packages/react-native/Libraries/ReactNative/AppRegistry.js b/packages/react-native/Libraries/ReactNative/AppRegistry.js index 6c18c8d6170a9c..dff4e625ba023a 100644 --- a/packages/react-native/Libraries/ReactNative/AppRegistry.js +++ b/packages/react-native/Libraries/ReactNative/AppRegistry.js @@ -357,4 +357,4 @@ global.RN$SurfaceRegistry = { registerCallableModule('AppRegistry', AppRegistry); -module.exports = AppRegistry; +export default AppRegistry; diff --git a/packages/react-native/Libraries/ReactNative/BridgelessUIManager.js b/packages/react-native/Libraries/ReactNative/BridgelessUIManager.js index 6bed4989c535a2..f3e46e91fc11eb 100644 --- a/packages/react-native/Libraries/ReactNative/BridgelessUIManager.js +++ b/packages/react-native/Libraries/ReactNative/BridgelessUIManager.js @@ -416,4 +416,4 @@ if (getUIManagerConstants) { } } -module.exports = UIManagerJS; +export default UIManagerJS; diff --git a/packages/react-native/Libraries/ReactNative/I18nManager.js b/packages/react-native/Libraries/ReactNative/I18nManager.js index 1c3225638e94ea..6339b2ff50ac2d 100644 --- a/packages/react-native/Libraries/ReactNative/I18nManager.js +++ b/packages/react-native/Libraries/ReactNative/I18nManager.js @@ -27,7 +27,7 @@ function getI18nManagerConstants(): I18nManagerConstants { }; } -module.exports = { +export default { getConstants: (): I18nManagerConstants => { return i18nConstants; }, diff --git a/packages/react-native/Libraries/ReactNative/PaperUIManager.js b/packages/react-native/Libraries/ReactNative/PaperUIManager.js index abdf455fabd8e3..3759b78911611d 100644 --- a/packages/react-native/Libraries/ReactNative/PaperUIManager.js +++ b/packages/react-native/Libraries/ReactNative/PaperUIManager.js @@ -17,7 +17,7 @@ import nullthrows from 'nullthrows'; const NativeModules = require('../BatchedBridge/NativeModules'); const defineLazyObjectProperty = require('../Utilities/defineLazyObjectProperty'); const Platform = require('../Utilities/Platform'); -const UIManagerProperties = require('./UIManagerProperties'); +const UIManagerProperties = require('./UIManagerProperties').default; const viewManagerConfigs: {[string]: any | null} = {}; @@ -188,4 +188,4 @@ if (!global.nativeCallSyncHook) { }); } -module.exports = UIManagerJS; +export default UIManagerJS; diff --git a/packages/react-native/Libraries/ReactNative/ReactNativeFeatureFlags.js b/packages/react-native/Libraries/ReactNative/ReactNativeFeatureFlags.js index 61f37d20a66b95..9fd0f63c448a86 100644 --- a/packages/react-native/Libraries/ReactNative/ReactNativeFeatureFlags.js +++ b/packages/react-native/Libraries/ReactNative/ReactNativeFeatureFlags.js @@ -28,4 +28,4 @@ const ReactNativeFeatureFlags: FeatureFlags = { shouldPressibilityUseW3CPointerEventsForHover: () => false, }; -module.exports = ReactNativeFeatureFlags; +export default ReactNativeFeatureFlags; diff --git a/packages/react-native/Libraries/ReactNative/ReactNativeRuntimeDiagnostics.js b/packages/react-native/Libraries/ReactNative/ReactNativeRuntimeDiagnostics.js index 82bea13749cb74..d95811f39a506e 100644 --- a/packages/react-native/Libraries/ReactNative/ReactNativeRuntimeDiagnostics.js +++ b/packages/react-native/Libraries/ReactNative/ReactNativeRuntimeDiagnostics.js @@ -65,4 +65,4 @@ const ReactNativeRuntimeDiagnostics: RuntimeDiagnostics = { }, }; -module.exports = ReactNativeRuntimeDiagnostics; +export default ReactNativeRuntimeDiagnostics; diff --git a/packages/react-native/Libraries/ReactNative/UIManager.d.ts b/packages/react-native/Libraries/ReactNative/UIManager.d.ts index 749015f1827495..773419bccf9c05 100644 --- a/packages/react-native/Libraries/ReactNative/UIManager.d.ts +++ b/packages/react-native/Libraries/ReactNative/UIManager.d.ts @@ -111,3 +111,4 @@ export interface UIManagerStatic { export const UIManager: UIManagerStatic; export type UIManager = UIManagerStatic; +export default UIManager; diff --git a/packages/react-native/Libraries/ReactNative/UIManager.js b/packages/react-native/Libraries/ReactNative/UIManager.js index 4c0e4d1ce94bdd..0910b6d3883f72 100644 --- a/packages/react-native/Libraries/ReactNative/UIManager.js +++ b/packages/react-native/Libraries/ReactNative/UIManager.js @@ -20,8 +20,8 @@ function isFabricReactTag(reactTag: number): boolean { const UIManagerImpl: UIManagerJSInterface = global.RN$Bridgeless === true - ? require('./BridgelessUIManager') - : require('./PaperUIManager'); + ? require('./BridgelessUIManager').default + : require('./PaperUIManager').default; // $FlowFixMe[cannot-spread-interface] const UIManager: UIManagerJSInterface = { @@ -190,4 +190,4 @@ const UIManager: UIManagerJSInterface = { }, }; -module.exports = UIManager; +export default UIManager; diff --git a/packages/react-native/Libraries/ReactNative/UIManagerProperties.js b/packages/react-native/Libraries/ReactNative/UIManagerProperties.js index b71073423ebe78..f91b3f15736391 100644 --- a/packages/react-native/Libraries/ReactNative/UIManagerProperties.js +++ b/packages/react-native/Libraries/ReactNative/UIManagerProperties.js @@ -60,4 +60,4 @@ const UIManagerProperties: $ReadOnlyArray = [ 'lazilyLoadView', ]; -module.exports = UIManagerProperties; +export default UIManagerProperties; diff --git a/packages/react-native/Libraries/ReactNative/getNativeComponentAttributes.js b/packages/react-native/Libraries/ReactNative/getNativeComponentAttributes.js index aae2d4d6796700..6aa53d35652ee4 100644 --- a/packages/react-native/Libraries/ReactNative/getNativeComponentAttributes.js +++ b/packages/react-native/Libraries/ReactNative/getNativeComponentAttributes.js @@ -23,7 +23,7 @@ const insetsDiffer = require('../Utilities/differ/insetsDiffer'); const matricesDiffer = require('../Utilities/differ/matricesDiffer'); const pointsDiffer = require('../Utilities/differ/pointsDiffer'); const sizesDiffer = require('../Utilities/differ/sizesDiffer'); -const UIManager = require('./UIManager'); +const UIManager = require('./UIManager').default; const nullthrows = require('nullthrows'); function getNativeComponentAttributes(uiViewClassName: string): any { @@ -209,4 +209,4 @@ function getProcessorForType(typeName: string): ?(nextProp: any) => any { return null; } -module.exports = getNativeComponentAttributes; +export default getNativeComponentAttributes; diff --git a/packages/react-native/Libraries/ReactNative/requireNativeComponent.js b/packages/react-native/Libraries/ReactNative/requireNativeComponent.js index 28f0a76acce7b5..44bfa0aa93f3dc 100644 --- a/packages/react-native/Libraries/ReactNative/requireNativeComponent.js +++ b/packages/react-native/Libraries/ReactNative/requireNativeComponent.js @@ -14,7 +14,8 @@ import type {HostComponent} from '../Renderer/shims/ReactNativeTypes'; const createReactNativeComponentClass = require('../Renderer/shims/createReactNativeComponentClass').default; -const getNativeComponentAttributes = require('./getNativeComponentAttributes'); +const getNativeComponentAttributes = + require('./getNativeComponentAttributes').default; /** * Creates values that can be used like React components which represent native diff --git a/packages/react-native/Libraries/ReactPrivate/ReactNativePrivateInterface.js b/packages/react-native/Libraries/ReactPrivate/ReactNativePrivateInterface.js index 206b826f14ed0d..5fd8eb2cc78701 100644 --- a/packages/react-native/Libraries/ReactPrivate/ReactNativePrivateInterface.js +++ b/packages/react-native/Libraries/ReactPrivate/ReactNativePrivateInterface.js @@ -56,7 +56,7 @@ module.exports = { return require('../Components/TextInput/TextInputState'); }, get UIManager(): UIManager { - return require('../ReactNative/UIManager'); + return require('../ReactNative/UIManager').default; }, // TODO: Remove when React has migrated to `createAttributePayload` and `diffAttributePayloads` get deepDiffer(): deepDiffer { diff --git a/packages/react-native/Libraries/Utilities/__tests__/codegenNativeComponent-test.js b/packages/react-native/Libraries/Utilities/__tests__/codegenNativeComponent-test.js index a53a0a4658e9b2..1ec0c457e29f7b 100644 --- a/packages/react-native/Libraries/Utilities/__tests__/codegenNativeComponent-test.js +++ b/packages/react-native/Libraries/Utilities/__tests__/codegenNativeComponent-test.js @@ -10,7 +10,7 @@ 'use strict'; -const UIManager = require('../../ReactNative/UIManager'); +const UIManager = require('../../ReactNative/UIManager').default; const codegenNativeComponent = require('../codegenNativeComponent').default; // We need to unmock requireNativeComponent since it's under test. diff --git a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap index 1123b59a8bef1f..cba007f9a30fa0 100644 --- a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap +++ b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap @@ -7345,7 +7345,7 @@ exports[`public API should not change unintentionally Libraries/ReactNative/AppC internal_excludeInspector?: boolean, |}>; declare const AppContainer: component(...Props); -declare module.exports: AppContainer; +declare export default typeof AppContainer; " `; @@ -7445,13 +7445,13 @@ declare const AppRegistry: { startHeadlessTask(taskId: number, taskKey: string, data: any): void, cancelHeadlessTask(taskId: number, taskKey: string): void, }; -declare module.exports: AppRegistry; +declare export default typeof AppRegistry; " `; exports[`public API should not change unintentionally Libraries/ReactNative/BridgelessUIManager.js 1`] = ` "declare const UIManagerJS: UIManagerJSInterface & { [string]: any }; -declare module.exports: UIManagerJS; +declare export default typeof UIManagerJS; " `; @@ -7528,7 +7528,7 @@ exports[`public API should not change unintentionally Libraries/ReactNative/Head `; exports[`public API should not change unintentionally Libraries/ReactNative/I18nManager.js 1`] = ` -"declare module.exports: { +"declare export default { getConstants: () => I18nManagerConstants, allowRTL: (shouldAllow: boolean) => void, forceRTL: (shouldForce: boolean) => void, @@ -7559,7 +7559,7 @@ declare export default typeof NativeUIManager; exports[`public API should not change unintentionally Libraries/ReactNative/PaperUIManager.js 1`] = ` "declare const UIManagerJS: UIManagerJSInterface; -declare module.exports: UIManagerJS; +declare export default typeof UIManagerJS; " `; @@ -7643,7 +7643,7 @@ exports[`public API should not change unintentionally Libraries/ReactNative/Reac shouldPressibilityUseW3CPointerEventsForHover: () => boolean, |}; declare const ReactNativeFeatureFlags: FeatureFlags; -declare module.exports: ReactNativeFeatureFlags; +declare export default typeof ReactNativeFeatureFlags; " `; @@ -7654,7 +7654,7 @@ exports[`public API should not change unintentionally Libraries/ReactNative/Reac |}; export type RuntimeDiagnosticFlag = \\"early_js_errors\\" | \\"all\\"; declare const ReactNativeRuntimeDiagnostics: RuntimeDiagnostics; -declare module.exports: ReactNativeRuntimeDiagnostics; +declare export default typeof ReactNativeRuntimeDiagnostics; " `; @@ -7715,13 +7715,13 @@ declare export function createRootTag(rootTag: number | RootTag): RootTag; exports[`public API should not change unintentionally Libraries/ReactNative/UIManager.js 1`] = ` "declare const UIManager: UIManagerJSInterface; -declare module.exports: UIManager; +declare export default typeof UIManager; " `; exports[`public API should not change unintentionally Libraries/ReactNative/UIManagerProperties.js 1`] = ` "declare const UIManagerProperties: $ReadOnlyArray; -declare module.exports: UIManagerProperties; +declare export default typeof UIManagerProperties; " `; @@ -7735,7 +7735,7 @@ declare export default function getCachedComponentWithDisplayName( exports[`public API should not change unintentionally Libraries/ReactNative/getNativeComponentAttributes.js 1`] = ` "declare function getNativeComponentAttributes(uiViewClassName: string): any; -declare module.exports: getNativeComponentAttributes; +declare export default typeof getNativeComponentAttributes; " `; diff --git a/packages/react-native/index.js b/packages/react-native/index.js index 31f568698f6f2a..91b1e2b0ea5f3a 100644 --- a/packages/react-native/index.js +++ b/packages/react-native/index.js @@ -227,7 +227,7 @@ module.exports = { return require('./Libraries/Utilities/Appearance'); }, get AppRegistry(): AppRegistry { - return require('./Libraries/ReactNative/AppRegistry'); + return require('./Libraries/ReactNative/AppRegistry').default; }, get AppState(): AppState { return require('./Libraries/AppState/AppState'); @@ -263,7 +263,7 @@ module.exports = { return require('./Libraries/ReactNative/RendererProxy').findNodeHandle; }, get I18nManager(): I18nManager { - return require('./Libraries/ReactNative/I18nManager'); + return require('./Libraries/ReactNative/I18nManager').default; }, get InteractionManager(): InteractionManager { return require('./Libraries/Interaction/InteractionManager'); @@ -328,7 +328,7 @@ module.exports = { return require('./Libraries/TurboModule/TurboModuleRegistry'); }, get UIManager(): UIManager { - return require('./Libraries/ReactNative/UIManager'); + return require('./Libraries/ReactNative/UIManager').default; }, get unstable_batchedUpdates(): $PropertyType< ReactNative,