Skip to content

Commit

Permalink
Migrated BugReporting, ErrorUtils, Vibration & YellowBox to u…
Browse files Browse the repository at this point in the history
…se `export` syntax. (facebook#48763)

Summary:
Pull Request resolved: facebook#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
  • Loading branch information
iwoplaza authored and facebook-github-bot committed Jan 21, 2025
1 parent 9a70bc0 commit 0970032
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/Libraries/BugReporting/BugReporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type DebugData = {

function defaultExtras() {
BugReporting.addFileSource('react_hierarchy.txt', () =>
require('./dumpReactTree')(),
require('./dumpReactTree').default(),
);
}

Expand Down Expand Up @@ -137,4 +137,4 @@ class BugReporting {
}
}

module.exports = BugReporting;
export default BugReporting;
4 changes: 2 additions & 2 deletions packages/react-native/Libraries/BugReporting/dumpReactTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
'use strict';

/*
const getReactData = require('getReactData');
const getReactData = require('getReactData').default;
const INDENTATION_SIZE = 2;
const MAX_DEPTH = 2;
Expand Down Expand Up @@ -148,4 +148,4 @@ function indent(size: number) {
}
*/

module.exports = dumpReactTree;
export default dumpReactTree;
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,4 @@ function copyWithSet(
return copyWithSetImpl(obj, path, 0, value);
}

module.exports = getData;
export default getData;
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/Core/setUpErrorHandling.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/Vibration/Vibration.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,4 @@ const Vibration = {
},
};

module.exports = Vibration;
export default Vibration;
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ if (__DEV__) {
}

// $FlowFixMe[method-unbinding]
module.exports = (YellowBox: Class<React.Component<Props>> & {
export default (YellowBox: Class<React.Component<Props>> & {
ignoreWarnings($ReadOnlyArray<IgnorePattern>): void,
install(): void,
uninstall(): void,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'use strict';

const LogBox = require('../../LogBox/LogBox').default;
const YellowBox = require('../YellowBoxDeprecated');
const YellowBox = require('../YellowBoxDeprecated').default;

describe('YellowBox', () => {
beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,7 @@ declare class BugReporting {
): { remove: () => void, ... };
static collectExtraData(): DebugData;
}
declare module.exports: BugReporting;
declare export default typeof BugReporting;
"
`;

Expand All @@ -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;
"
`;

Expand Down Expand Up @@ -9406,7 +9406,7 @@ exports[`public API should not change unintentionally Libraries/Vibration/Vibrat
vibrate: (pattern: number | Array<number>, repeat: boolean) => void,
cancel: () => void,
};
declare module.exports: Vibration;
declare export default typeof Vibration;
"
`;

Expand Down Expand Up @@ -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<React.Component<Props>> & {
declare export default Class<React.Component<Props>> & {
ignoreWarnings($ReadOnlyArray<IgnorePattern>): void,
install(): void,
uninstall(): void,
Expand All @@ -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;
"
`;

Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/vendor/core/ErrorUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
4 changes: 2 additions & 2 deletions packages/react-native/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions packages/react-native/jest/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 0970032

Please sign in to comment.