From 09700327f7294ed5242fb6af7804e124183ebc64 Mon Sep 17 00:00:00 2001 From: Iwo Plaza Date: Tue, 21 Jan 2025 01:49:27 -0800 Subject: [PATCH] Migrated `BugReporting`, `ErrorUtils`, `Vibration` & `YellowBox` to use `export` syntax. (#48763) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48763 ## Motivation Modernising the react-native codebase to allow for ingestion by modern Flow tooling. ## This diff - Updates files in `Libraries/BugReporting`, `Libraries/vendor`, `Libraries/Vibration` and `Libraries/YellowBox` 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/BugReporting`, `Libraries/vendor`, `Libraries/Vibration` and `Libraries/YellowBox` use `export` syntax, which requires the addition of `.default` when imported with the CJS `require` syntax. Reviewed By: cortinico Differential Revision: D68329075 fbshipit-source-id: 7079a54ce3631171f8d7559bc33cab014df1d16d --- .../Libraries/BatchedBridge/MessageQueue.js | 2 +- .../Libraries/BugReporting/BugReporting.js | 4 ++-- .../Libraries/BugReporting/dumpReactTree.js | 4 ++-- .../Libraries/BugReporting/getReactData.js | 2 +- .../Libraries/Core/setUpErrorHandling.js | 2 +- .../react-native/Libraries/Vibration/Vibration.js | 2 +- .../Libraries/YellowBox/YellowBoxDeprecated.js | 2 +- .../YellowBox/__tests__/YellowBoxDeprecated-test.js | 2 +- .../__tests__/__snapshots__/public-api-test.js.snap | 12 ++++++------ .../react-native/Libraries/vendor/core/ErrorUtils.js | 2 +- packages/react-native/index.js | 4 ++-- packages/react-native/jest/setup.js | 7 +++++-- 12 files changed, 24 insertions(+), 21 deletions(-) diff --git a/packages/react-native/Libraries/BatchedBridge/MessageQueue.js b/packages/react-native/Libraries/BatchedBridge/MessageQueue.js index e22485f2a5e3a7..719d33ad8ae628 100644 --- a/packages/react-native/Libraries/BatchedBridge/MessageQueue.js +++ b/packages/react-native/Libraries/BatchedBridge/MessageQueue.js @@ -14,7 +14,7 @@ const Systrace = require('../Performance/Systrace'); const deepFreezeAndThrowOnMutationInDev = require('../Utilities/deepFreezeAndThrowOnMutationInDev'); const stringifySafe = require('../Utilities/stringifySafe').default; const warnOnce = require('../Utilities/warnOnce'); -const ErrorUtils = require('../vendor/core/ErrorUtils'); +const ErrorUtils = require('../vendor/core/ErrorUtils').default; const invariant = require('invariant'); export type SpyData = { diff --git a/packages/react-native/Libraries/BugReporting/BugReporting.js b/packages/react-native/Libraries/BugReporting/BugReporting.js index 2ee616af829c50..a11ad8084840b7 100644 --- a/packages/react-native/Libraries/BugReporting/BugReporting.js +++ b/packages/react-native/Libraries/BugReporting/BugReporting.js @@ -23,7 +23,7 @@ type DebugData = { function defaultExtras() { BugReporting.addFileSource('react_hierarchy.txt', () => - require('./dumpReactTree')(), + require('./dumpReactTree').default(), ); } @@ -137,4 +137,4 @@ class BugReporting { } } -module.exports = BugReporting; +export default BugReporting; diff --git a/packages/react-native/Libraries/BugReporting/dumpReactTree.js b/packages/react-native/Libraries/BugReporting/dumpReactTree.js index 6673657d42a894..e5a2b770d3db75 100644 --- a/packages/react-native/Libraries/BugReporting/dumpReactTree.js +++ b/packages/react-native/Libraries/BugReporting/dumpReactTree.js @@ -11,7 +11,7 @@ 'use strict'; /* -const getReactData = require('getReactData'); +const getReactData = require('getReactData').default; const INDENTATION_SIZE = 2; const MAX_DEPTH = 2; @@ -148,4 +148,4 @@ function indent(size: number) { } */ -module.exports = dumpReactTree; +export default dumpReactTree; diff --git a/packages/react-native/Libraries/BugReporting/getReactData.js b/packages/react-native/Libraries/BugReporting/getReactData.js index cc988ced109854..ea0da5e15eff34 100644 --- a/packages/react-native/Libraries/BugReporting/getReactData.js +++ b/packages/react-native/Libraries/BugReporting/getReactData.js @@ -184,4 +184,4 @@ function copyWithSet( return copyWithSetImpl(obj, path, 0, value); } -module.exports = getData; +export default getData; diff --git a/packages/react-native/Libraries/Core/setUpErrorHandling.js b/packages/react-native/Libraries/Core/setUpErrorHandling.js index b2518260edf276..aa2c368d7dc2ef 100644 --- a/packages/react-native/Libraries/Core/setUpErrorHandling.js +++ b/packages/react-native/Libraries/Core/setUpErrorHandling.js @@ -34,7 +34,7 @@ if (global.RN$useAlwaysAvailableJSErrorHandling !== true) { } }; - const ErrorUtils = require('../vendor/core/ErrorUtils'); + const ErrorUtils = require('../vendor/core/ErrorUtils').default; ErrorUtils.setGlobalHandler(handleError); } } diff --git a/packages/react-native/Libraries/Vibration/Vibration.js b/packages/react-native/Libraries/Vibration/Vibration.js index db124c63235c42..cee97dc1bd52fe 100644 --- a/packages/react-native/Libraries/Vibration/Vibration.js +++ b/packages/react-native/Libraries/Vibration/Vibration.js @@ -110,4 +110,4 @@ const Vibration = { }, }; -module.exports = Vibration; +export default Vibration; diff --git a/packages/react-native/Libraries/YellowBox/YellowBoxDeprecated.js b/packages/react-native/Libraries/YellowBox/YellowBoxDeprecated.js index c6a0f0d964cc16..dfa5757ef0bdcc 100644 --- a/packages/react-native/Libraries/YellowBox/YellowBoxDeprecated.js +++ b/packages/react-native/Libraries/YellowBox/YellowBoxDeprecated.js @@ -68,7 +68,7 @@ if (__DEV__) { } // $FlowFixMe[method-unbinding] -module.exports = (YellowBox: Class> & { +export default (YellowBox: Class> & { ignoreWarnings($ReadOnlyArray): void, install(): void, uninstall(): void, diff --git a/packages/react-native/Libraries/YellowBox/__tests__/YellowBoxDeprecated-test.js b/packages/react-native/Libraries/YellowBox/__tests__/YellowBoxDeprecated-test.js index a97e46ea67b659..5e972213afe742 100644 --- a/packages/react-native/Libraries/YellowBox/__tests__/YellowBoxDeprecated-test.js +++ b/packages/react-native/Libraries/YellowBox/__tests__/YellowBoxDeprecated-test.js @@ -12,7 +12,7 @@ 'use strict'; const LogBox = require('../../LogBox/LogBox').default; -const YellowBox = require('../YellowBoxDeprecated'); +const YellowBox = require('../YellowBoxDeprecated').default; describe('YellowBox', () => { beforeEach(() => { 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 78fc677b1cb083..597b32390b7baa 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 @@ -1540,7 +1540,7 @@ declare class BugReporting { ): { remove: () => void, ... }; static collectExtraData(): DebugData; } -declare module.exports: BugReporting; +declare export default typeof BugReporting; " `; @@ -1552,13 +1552,13 @@ declare export default typeof NativeBugReporting; exports[`public API should not change unintentionally Libraries/BugReporting/dumpReactTree.js 1`] = ` "declare function dumpReactTree(): string; -declare module.exports: dumpReactTree; +declare export default typeof dumpReactTree; " `; exports[`public API should not change unintentionally Libraries/BugReporting/getReactData.js 1`] = ` "declare function getData(element: Object): Object; -declare module.exports: getData; +declare export default typeof getData; " `; @@ -9406,7 +9406,7 @@ exports[`public API should not change unintentionally Libraries/Vibration/Vibrat vibrate: (pattern: number | Array, repeat: boolean) => void, cancel: () => void, }; -declare module.exports: Vibration; +declare export default typeof Vibration; " `; @@ -9510,7 +9510,7 @@ declare module.exports: WebSocketInterceptor; exports[`public API should not change unintentionally Libraries/YellowBox/YellowBoxDeprecated.js 1`] = ` "type Props = $ReadOnly<{}>; -declare module.exports: Class> & { +declare export default Class> & { ignoreWarnings($ReadOnlyArray): void, install(): void, uninstall(): void, @@ -9526,7 +9526,7 @@ declare export default typeof rejectionTrackingOptions; `; exports[`public API should not change unintentionally Libraries/vendor/core/ErrorUtils.js 1`] = ` -"declare module.exports: ErrorUtilsT; +"declare export default ErrorUtilsT; " `; diff --git a/packages/react-native/Libraries/vendor/core/ErrorUtils.js b/packages/react-native/Libraries/vendor/core/ErrorUtils.js index 37b0da17cd1881..3d331a5166bb2b 100644 --- a/packages/react-native/Libraries/vendor/core/ErrorUtils.js +++ b/packages/react-native/Libraries/vendor/core/ErrorUtils.js @@ -22,4 +22,4 @@ import type {ErrorUtilsT} from '@react-native/js-polyfills/error-guard'; * that use it aren't just using a global variable, so simply export the global * variable here. ErrorUtils is originally defined in a file named error-guard.js. */ -module.exports = (global.ErrorUtils: ErrorUtilsT); +export default (global.ErrorUtils: ErrorUtilsT); diff --git a/packages/react-native/index.js b/packages/react-native/index.js index dd02a72040cd95..3086f22a316767 100644 --- a/packages/react-native/index.js +++ b/packages/react-native/index.js @@ -350,10 +350,10 @@ module.exports = { return require('./Libraries/UTFSequence').default; }, get Vibration(): Vibration { - return require('./Libraries/Vibration/Vibration'); + return require('./Libraries/Vibration/Vibration').default; }, get YellowBox(): YellowBox { - return require('./Libraries/YellowBox/YellowBoxDeprecated'); + return require('./Libraries/YellowBox/YellowBoxDeprecated').default; }, // Plugins diff --git a/packages/react-native/jest/setup.js b/packages/react-native/jest/setup.js index d45743164190a6..94a9f1ab4d2230 100644 --- a/packages/react-native/jest/setup.js +++ b/packages/react-native/jest/setup.js @@ -430,8 +430,11 @@ jest return jest.requireActual('./mockNativeComponent'); }) .mock('../Libraries/Vibration/Vibration', () => ({ - vibrate: jest.fn(), - cancel: jest.fn(), + __esModule: true, + default: { + vibrate: jest.fn(), + cancel: jest.fn(), + }, })) .mock('../Libraries/Components/View/ViewNativeComponent', () => { const React = require('react');