diff --git a/packages/react-native/Libraries/Alert/Alert.js b/packages/react-native/Libraries/Alert/Alert.js index be65d0d2b3f3f4..a66d6da9cc5a5b 100644 --- a/packages/react-native/Libraries/Alert/Alert.js +++ b/packages/react-native/Libraries/Alert/Alert.js @@ -9,18 +9,18 @@ */ import type {DialogOptions} from '../NativeModules/specs/NativeDialogManagerAndroid'; -import type {AlertOptions, AlertType, Buttons} from './Alert.flow'; +import type {AlertButtons, AlertOptions, AlertType} from './AlertTypes.flow'; import Platform from '../Utilities/Platform'; import RCTAlertManager from './RCTAlertManager'; -export type * from './Alert.flow'; +export * from './AlertTypes.flow'; class Alert { static alert( title: ?string, message?: ?string, - buttons?: Buttons, + buttons?: AlertButtons, options?: AlertOptions, ): void { if (Platform.OS === 'ios') { @@ -53,7 +53,7 @@ class Alert { // At most three buttons (neutral, negative, positive). Ignore rest. // The text 'OK' should be probably localized. iOS Alert does that in native. const defaultPositiveText = 'OK'; - const validButtons: Buttons = buttons + const validButtons: AlertButtons = buttons ? buttons.slice(0, 3) : [{text: defaultPositiveText}]; const buttonPositive = validButtons.pop(); @@ -96,7 +96,7 @@ class Alert { static prompt( title: ?string, message?: ?string, - callbackOrButtons?: ?(((text: string) => void) | Buttons), + callbackOrButtons?: ?(((text: string) => void) | AlertButtons), type?: ?AlertType = 'plain-text', defaultValue?: string, keyboardType?: string, diff --git a/packages/react-native/Libraries/Alert/Alert.flow.js b/packages/react-native/Libraries/Alert/AlertTypes.flow.js similarity index 56% rename from packages/react-native/Libraries/Alert/Alert.flow.js rename to packages/react-native/Libraries/Alert/AlertTypes.flow.js index 97c18ef07c4cf3..0a2f85b780e53d 100644 --- a/packages/react-native/Libraries/Alert/Alert.flow.js +++ b/packages/react-native/Libraries/Alert/AlertTypes.flow.js @@ -24,7 +24,7 @@ export type AlertButton = { ... }; -export type Buttons = Array; +export type AlertButtons = Array; export type AlertOptions = { /** @platform android */ @@ -34,29 +34,3 @@ export type AlertOptions = { onDismiss?: ?() => void, ... }; - -/** - * Launches an alert dialog with the specified title and message. - * - * See https://reactnative.dev/docs/alert - */ -declare class Alert { - static alert( - title: ?string, - message?: ?string, - buttons?: Buttons, - options?: AlertOptions, - ): void; - - static prompt( - title: ?string, - message?: ?string, - callbackOrButtons?: ?(((text: string) => void) | Buttons), - type?: ?AlertType, - defaultValue?: string, - keyboardType?: string, - options?: AlertOptions, - ): void; -} - -export default Alert; diff --git a/packages/react-native/Libraries/Alert/RCTAlertManager.flow.js b/packages/react-native/Libraries/Alert/RCTAlertManager.flow.js deleted file mode 100644 index a2fb268a3d1902..00000000000000 --- a/packages/react-native/Libraries/Alert/RCTAlertManager.flow.js +++ /dev/null @@ -1,20 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - * @flow strict-local - */ - -import type {Args} from './NativeAlertManager'; - -declare const RCTAlertManager: { - alertWithArgs( - args: Args, - callback: (id: number, value: string) => void, - ): void, -}; - -export default RCTAlertManager; 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 37fd09f70b308b..33a3e0a938df1e 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 @@ -53,28 +53,8 @@ declare export default typeof NativeActionSheetManager; " `; -exports[`public API should not change unintentionally Libraries/Alert/Alert.flow.js 1`] = ` -"export type AlertType = - | \\"default\\" - | \\"plain-text\\" - | \\"secure-text\\" - | \\"login-password\\"; -export type AlertButtonStyle = \\"default\\" | \\"cancel\\" | \\"destructive\\"; -export type AlertButton = { - text?: string, - onPress?: ?((value?: string) => any) | ?Function, - isPreferred?: boolean, - style?: AlertButtonStyle, - ... -}; -export type Buttons = Array; -export type AlertOptions = { - cancelable?: ?boolean, - userInterfaceStyle?: \\"unspecified\\" | \\"light\\" | \\"dark\\", - onDismiss?: ?() => void, - ... -}; -declare class Alert { +exports[`public API should not change unintentionally Libraries/Alert/Alert.js 1`] = ` +"declare class Alert { static alert( title: ?string, message?: ?string, @@ -95,8 +75,8 @@ declare export default typeof Alert; " `; -exports[`public API should not change unintentionally Libraries/Alert/Alert.js 1`] = ` -"export type * from \\"./Alert.flow\\"; +exports[`public API should not change unintentionally Libraries/Alert/Alert.js.flow 1`] = ` +"export type * from \\"./AlertTypes.flow\\"; declare class Alert { static alert( title: ?string, @@ -118,20 +98,33 @@ declare export default typeof Alert; " `; -exports[`public API should not change unintentionally Libraries/Alert/NativeAlertManager.js 1`] = ` -"export * from \\"../../src/private/specs_DEPRECATED/modules/NativeAlertManager\\"; -declare export default typeof NativeAlertManager; +exports[`public API should not change unintentionally Libraries/Alert/AlertTypes.flow.js 1`] = ` +"export type AlertType = + | \\"default\\" + | \\"plain-text\\" + | \\"secure-text\\" + | \\"login-password\\"; +export type AlertButtonStyle = \\"default\\" | \\"cancel\\" | \\"destructive\\"; +export type AlertButton = { + text?: string, + onPress?: ?((value?: string) => any) | ?Function, + isPreferred?: boolean, + style?: AlertButtonStyle, + ... +}; +export type Buttons = Array; +export type AlertOptions = { + cancelable?: ?boolean, + userInterfaceStyle?: \\"unspecified\\" | \\"light\\" | \\"dark\\", + onDismiss?: ?() => void, + ... +}; " `; -exports[`public API should not change unintentionally Libraries/Alert/RCTAlertManager.flow.js 1`] = ` -"declare const RCTAlertManager: { - alertWithArgs( - args: Args, - callback: (id: number, value: string) => void - ): void, -}; -declare export default typeof RCTAlertManager; +exports[`public API should not change unintentionally Libraries/Alert/NativeAlertManager.js 1`] = ` +"export * from \\"../../src/private/specs_DEPRECATED/modules/NativeAlertManager\\"; +declare export default typeof NativeAlertManager; " `; diff --git a/scripts/build/build-types.js b/scripts/build/build-types.js index b83c0af74768a6..91dce846e22137 100644 --- a/scripts/build/build-types.js +++ b/scripts/build/build-types.js @@ -21,11 +21,11 @@ const path = require('path'); const {parseArgs} = require('util'); const TYPES_DIR = 'types_generated'; -const IGNORE_PATTERN = '**/__{tests,mocks,fixtures}__/**'; +const IGNORE_PATTERN = '**/__{tests,mocks,fixtures,flowtests}__/**'; const SOURCE_PATTERNS = [ // Start with Animated only - 'react-native/Libraries/Alert/**/*.js', + 'react-native/Libraries/Alert/**/*.{js,flow}', 'react-native/Libraries/ActionSheetIOS/**/*.js', 'react-native/Libraries/TurboModule/RCTExport.js', 'react-native/Libraries/Types/RootTagTypes.js', @@ -115,7 +115,7 @@ function getBuildPath(file /*: string */) /*: string */ { packageDir, file .replace(packageDir, TYPES_DIR) - .replace(/\.flow\.js$/, '.js') + .replace(/\.js\.flow$/, '.js') .replace(/\.js$/, '.d.ts'), ); } @@ -126,7 +126,7 @@ function ignoreShadowedFiles(files /*: Array */) /*: Array */ { // Find all flow definition files that shadow other files for (const file of files) { - if (/\.flow\.js$/.test(file)) { + if (/\.js\.flow$/.test(file)) { shadowedPrefixes[file.substring(0, file.length - 8)] = true; } } @@ -134,7 +134,7 @@ function ignoreShadowedFiles(files /*: Array */) /*: Array */ { // Filter out all files shadowed by flow definition files for (const file of files) { const prefix = file.split('.')[0]; - if (/\.flow\.js$/.test(file) || !shadowedPrefixes[prefix]) { + if (/\.js\.flow$/.test(file) || !shadowedPrefixes[prefix]) { result.push(file); } }