Skip to content

Commit

Permalink
Improve types, fix translator compatibility for version-related modul…
Browse files Browse the repository at this point in the history
…es (#42638)

Summary:
Pull Request resolved: #42638

Enables these modules to be covered by `public-api-test`.

- Standardise as CommonJS modules, fixing compatibility with [`flow-api-translator`](https://www.npmjs.com/package/flow-api-translator).
- Use explicit object type in generated file template.

Changelog: [Internal]

Reviewed By: NickGerleman

Differential Revision: D52963967

fbshipit-source-id: c9f3e35f70859c1b99b7297228ee2498f91d9041
  • Loading branch information
huntie authored and facebook-github-bot committed Jan 25, 2024
1 parent 0ea16fd commit 02e7244
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
9 changes: 8 additions & 1 deletion packages/react-native/Libraries/Core/ReactNativeVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@
* @flow strict
*/

exports.version = {
const version: $ReadOnly<{
major: number,
minor: number,
patch: number,
prerelease: string | null,
}> = {
major: 1000,
minor: 0,
patch: 0,
prerelease: null,
};

module.exports = {version};
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ const ReactNativeVersion = require('./ReactNativeVersion');
* implementations for other platforms (ex: Windows) may override this module
* and rely on its existence as a separate module.
*/
exports.checkVersions = function checkVersions(): void {
const checkVersions = function checkVersions(): void {
const nativeVersion = Platform.constants.reactNativeVersion;
if (
ReactNativeVersion.version.major !== nativeVersion.major ||
ReactNativeVersion.version.minor !== nativeVersion.minor
) {
console.error(
`React Native version mismatch.\n\nJavaScript version: ${_formatVersion(
ReactNativeVersion.version,
(ReactNativeVersion.version: $FlowFixMe),
)}\n` +
`Native version: ${_formatVersion(nativeVersion)}\n\n` +
'Make sure that you have rebuilt the native code. If the problem ' +
Expand All @@ -48,3 +48,5 @@ function _formatVersion(
(version.prerelease != undefined ? `-${version.prerelease}` : '')
);
}

module.exports = {checkVersions};
Original file line number Diff line number Diff line change
Expand Up @@ -3851,6 +3851,23 @@ declare export default typeof ReactFiberErrorDialog;
"
`;

exports[`public API should not change unintentionally Libraries/Core/ReactNativeVersion.js 1`] = `
"declare const version: $ReadOnly<{
major: number,
minor: number,
patch: number,
prerelease: string | null,
}>;
declare module.exports: { version: version };
"
`;

exports[`public API should not change unintentionally Libraries/Core/ReactNativeVersionCheck.js 1`] = `
"declare const checkVersions: () => void;
declare module.exports: { checkVersions: checkVersions };
"
`;

exports[`public API should not change unintentionally Libraries/Core/SegmentFetcher/NativeSegmentFetcher.js 1`] = `
"export * from \\"../../../src/private/specs/modules/NativeSegmentFetcher\\";
declare export default typeof NativeSegmentFetcher;
Expand Down
2 changes: 0 additions & 2 deletions packages/react-native/Libraries/__tests__/public-api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ const FILES_WITH_KNOWN_ERRORS = new Set([
'Libraries/Components/Touchable/TouchableNativeFeedback.js',
'Libraries/Components/Touchable/TouchableWithoutFeedback.js',
'Libraries/Components/UnimplementedViews/UnimplementedView.js',
'Libraries/Core/ReactNativeVersion.js',
'Libraries/Core/ReactNativeVersionCheck.js',
'Libraries/Image/ImageBackground.js',
'Libraries/Inspector/ElementProperties.js',
'Libraries/Inspector/BorderBox.js',
Expand Down
9 changes: 8 additions & 1 deletion scripts/versiontemplates/ReactNativeVersion.js.template
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@
* @flow strict
*/

exports.version = {
const version: $ReadOnly<{
major: number,
minor: number,
patch: number,
prerelease: string | null,
}> = {
major: ${major},
minor: ${minor},
patch: ${patch},
prerelease: ${prerelease},
};

module.exports = {version};

0 comments on commit 02e7244

Please sign in to comment.