Skip to content

Commit 61e95e5

Browse files
osdnkfacebook-github-bot
authored andcommitted
Force WithDefault not to be an optional value
Summary: `WithDefault` appears not to be required to be prefixed with `?` because it's option value per se. Fixed tests, removed `?` where needed, updated snapshots and review them. Added mechanism fro throwing error when `?WithDefault` found. Add tests for it. Reviewed By: rubennorte Differential Revision: D16048463 fbshipit-source-id: f55ed7454aacf0b8c42944a9b5c1037ad1b360fe
1 parent b476ad7 commit 61e95e5

File tree

19 files changed

+110
-101
lines changed

19 files changed

+110
-101
lines changed

Libraries/Components/ActivityIndicator/ActivityIndicatorViewNativeComponent.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ type NativeProps = $ReadOnly<{|
2525
*
2626
* See http://facebook.github.io/react-native/docs/activityindicator.html#hideswhenstopped
2727
*/
28-
hidesWhenStopped?: ?WithDefault<boolean, false>,
28+
hidesWhenStopped?: WithDefault<boolean, false>,
2929

3030
/**
3131
* Whether to show the indicator (true, the default) or hide it (false).
3232
*
3333
* See http://facebook.github.io/react-native/docs/activityindicator.html#animating
3434
*/
35-
animating?: ?WithDefault<boolean, false>,
35+
animating?: WithDefault<boolean, false>,
3636

3737
/**
3838
* The foreground color of the spinner (default is gray).
@@ -47,7 +47,7 @@ type NativeProps = $ReadOnly<{|
4747
*
4848
* See http://facebook.github.io/react-native/docs/activityindicator.html#size
4949
*/
50-
size?: ?WithDefault<'small' | 'large', 'small'>,
50+
size?: WithDefault<'small' | 'large', 'small'>,
5151
|}>;
5252

5353
export default codegenNativeComponent<NativeProps>('ActivityIndicatorView', {

Libraries/Components/Picker/AndroidDialogPickerNativeComponent.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ type NativeProps = $ReadOnly<{|
3737

3838
// Props
3939
color?: ?ColorValue,
40-
enabled?: ?WithDefault<boolean, true>,
40+
enabled?: WithDefault<boolean, true>,
4141
items: $ReadOnlyArray<PickerItem>,
42-
prompt?: ?WithDefault<string, ''>,
42+
prompt?: WithDefault<string, ''>,
4343
selected: Int32,
4444

4545
// Events

Libraries/Components/Picker/AndroidDropdownPickerNativeComponent.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ type NativeProps = $ReadOnly<{|
3737

3838
// Props
3939
color?: ?ColorValue,
40-
enabled?: ?WithDefault<boolean, true>,
40+
enabled?: WithDefault<boolean, true>,
4141
items: $ReadOnlyArray<PickerItem>,
42-
prompt?: ?WithDefault<string, ''>,
42+
prompt?: WithDefault<string, ''>,
4343
selected: Int32,
4444

4545
// Events

Libraries/Components/ProgressBarAndroid/ProgressBarAndroidNativeComponent.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ type NativeProps = $ReadOnly<{|
2424
typeAttr?: string,
2525
indeterminate: boolean,
2626
progress?: WithDefault<Float, 0>,
27-
animating?: ?WithDefault<boolean, true>,
27+
animating?: WithDefault<boolean, true>,
2828
color?: ?ColorValue,
29-
testID?: ?WithDefault<string, ''>,
29+
testID?: WithDefault<string, ''>,
3030
|}>;
3131

3232
export default codegenNativeComponent<NativeProps>('AndroidProgressBar');

Libraries/Components/ProgressViewIOS/RCTProgressViewNativeComponent.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ type NativeProps = $ReadOnly<{|
2121
...ViewProps,
2222

2323
// Props
24-
progressViewStyle?: ?WithDefault<'default' | 'bar', 'default'>,
25-
progress?: ?WithDefault<Float, 0>,
24+
progressViewStyle?: WithDefault<'default' | 'bar', 'default'>,
25+
progress?: WithDefault<Float, 0>,
2626
progressTintColor?: ?ColorValue,
2727
trackTintColor?: ?ColorValue,
2828
progressImage?: ?ImageSource,

Libraries/Components/RefreshControl/AndroidSwipeRefreshLayoutNativeComponent.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type NativeProps = $ReadOnly<{|
2727
/**
2828
* Whether the pull to refresh functionality is enabled.
2929
*/
30-
enabled?: ?WithDefault<boolean, false>,
30+
enabled?: WithDefault<boolean, false>,
3131
/**
3232
* The colors (at least one) that will be used to draw the refresh indicator.
3333
*/
@@ -45,13 +45,13 @@ type NativeProps = $ReadOnly<{|
4545
* Also, 1 isn't actually a safe default. We are able to set this here
4646
* because native code isn't currently consuming the generated artifact.
4747
* This will end up being
48-
* size?: ?WithDefault<'default' | 'large', 'default'>,
48+
* size?: WithDefault<'default' | 'large', 'default'>,
4949
*/
50-
size?: ?WithDefault<Int32, 1>,
50+
size?: WithDefault<Int32, 1>,
5151
/**
5252
* Progress view top offset
5353
*/
54-
progressViewOffset?: ?WithDefault<Float, 0>,
54+
progressViewOffset?: WithDefault<Float, 0>,
5555

5656
/**
5757
* Called when the view starts refreshing.

Libraries/Components/RefreshControl/PullToRefreshViewNativeComponent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type NativeProps = $ReadOnly<{|
3030
/**
3131
* The title displayed under the refresh indicator.
3232
*/
33-
title?: ?WithDefault<string, null>,
33+
title?: WithDefault<string, null>,
3434

3535
/**
3636
* Called when the view starts refreshing.

Libraries/Components/SegmentedControlIOS/RCTSegmentedControlNativeComponent.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ type NativeProps = $ReadOnly<{|
2828

2929
// Props
3030
values?: $ReadOnlyArray<string>,
31-
selectedIndex?: ?WithDefault<Int32, 0>,
32-
enabled?: ?WithDefault<boolean, true>,
31+
selectedIndex?: WithDefault<Int32, 0>,
32+
enabled?: WithDefault<boolean, true>,
3333
tintColor?: ?ColorValue,
34-
momentary?: ?WithDefault<boolean, false>,
34+
momentary?: WithDefault<boolean, false>,
3535

3636
// Events
3737
onChange?: ?BubblingEventHandler<OnChangeEvent>,

Libraries/Components/Slider/SliderNativeComponent.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ type NativeProps = $ReadOnly<{|
3232
...ViewProps,
3333

3434
// Props
35-
disabled?: ?WithDefault<boolean, false>,
36-
enabled?: ?WithDefault<boolean, false>,
35+
disabled?: WithDefault<boolean, false>,
36+
enabled?: WithDefault<boolean, false>,
3737
maximumTrackImage?: ?ImageSource,
3838
maximumTrackTintColor?: ?ColorValue,
39-
maximumValue?: ?WithDefault<Float, 1>,
39+
maximumValue?: WithDefault<Float, 1>,
4040
minimumTrackImage?: ?ImageSource,
4141
minimumTrackTintColor?: ?ColorValue,
42-
minimumValue?: ?WithDefault<Float, 0>,
43-
step?: ?WithDefault<Float, 0>,
44-
testID?: ?WithDefault<string, ''>,
42+
minimumValue?: WithDefault<Float, 0>,
43+
step?: WithDefault<Float, 0>,
44+
testID?: WithDefault<string, ''>,
4545
thumbImage?: ?ImageSource,
4646
thumbTintColor?: ?ColorValue,
4747
trackImage?: ?ImageSource,
48-
value: ?WithDefault<Float, 0>,
48+
value: WithDefault<Float, 0>,
4949

5050
// Events
5151
onChange?: ?BubblingEventHandler<Event>,

Libraries/Components/Switch/SwitchNativeComponent.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ type NativeProps = $ReadOnly<{|
2424
...ViewProps,
2525

2626
// Props
27-
disabled?: ?WithDefault<boolean, false>,
28-
value?: ?WithDefault<boolean, false>,
27+
disabled?: WithDefault<boolean, false>,
28+
value?: WithDefault<boolean, false>,
2929
tintColor?: ?ColorValue,
3030
onTintColor?: ?ColorValue,
3131
thumbTintColor?: ?ColorValue,

Libraries/Components/UnimplementedViews/UnimplementedNativeViewNativeComponent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import codegenNativeComponent from '../../Utilities/codegenNativeComponent';
1717

1818
type NativeProps = $ReadOnly<{|
1919
...ViewProps,
20-
name?: ?WithDefault<string, ''>,
20+
name?: WithDefault<string, ''>,
2121
|}>;
2222

2323
// NOTE: This compoenent is not implemented in paper

Libraries/Modal/RCTModalHostViewNativeComponent.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ type NativeProps = $ReadOnly<{|
3232
*
3333
* See https://facebook.github.io/react-native/docs/modal.html#animationtype
3434
*/
35-
animationType?: ?WithDefault<'none' | 'slide' | 'fade', 'none'>,
35+
animationType?: WithDefault<'none' | 'slide' | 'fade', 'none'>,
3636

3737
/**
3838
* The `presentationStyle` prop controls how the modal appears.
3939
*
4040
* See https://facebook.github.io/react-native/docs/modal.html#presentationstyle
4141
*/
42-
presentationStyle?: ?WithDefault<
42+
presentationStyle?: WithDefault<
4343
'fullScreen' | 'pageSheet' | 'formSheet' | 'overFullScreen',
4444
'fullScreen',
4545
>,
@@ -50,22 +50,22 @@ type NativeProps = $ReadOnly<{|
5050
*
5151
* See https://facebook.github.io/react-native/docs/modal.html#transparent
5252
*/
53-
transparent?: ?WithDefault<boolean, false>,
53+
transparent?: WithDefault<boolean, false>,
5454

5555
/**
5656
* The `hardwareAccelerated` prop controls whether to force hardware
5757
* acceleration for the underlying window.
5858
*
5959
* See https://facebook.github.io/react-native/docs/modal.html#hardwareaccelerated
6060
*/
61-
hardwareAccelerated?: ?WithDefault<boolean, false>,
61+
hardwareAccelerated?: WithDefault<boolean, false>,
6262

6363
/**
6464
* The `visible` prop determines whether your modal is visible.
6565
*
6666
* See https://facebook.github.io/react-native/docs/modal.html#visible
6767
*/
68-
visible?: ?WithDefault<boolean, false>,
68+
visible?: WithDefault<boolean, false>,
6969

7070
/**
7171
* The `onRequestClose` callback is called when the user taps the hardware
@@ -96,14 +96,14 @@ type NativeProps = $ReadOnly<{|
9696
/**
9797
* Deprecated. Use the `animationType` prop instead.
9898
*/
99-
animated?: ?WithDefault<boolean, false>,
99+
animated?: WithDefault<boolean, false>,
100100

101101
/**
102102
* The `supportedOrientations` prop allows the modal to be rotated to any of the specified orientations.
103103
*
104104
* See https://facebook.github.io/react-native/docs/modal.html#supportedorientations
105105
*/
106-
supportedOrientations?: ?WithDefault<
106+
supportedOrientations?: WithDefault<
107107
$ReadOnlyArray<
108108
| 'portrait'
109109
| 'portrait-upside-down'
@@ -124,7 +124,7 @@ type NativeProps = $ReadOnly<{|
124124
/**
125125
* The `identifier` is the unique number for identifying Modal components.
126126
*/
127-
identifier?: ?WithDefault<Int32, 0>,
127+
identifier?: WithDefault<Int32, 0>,
128128
|}>;
129129

130130
export default codegenNativeComponent<NativeProps>('ModalHostView', {

Libraries/Types/CodegenTypes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ type DefaultTypes = number | boolean | string | $ReadOnlyArray<string>;
3636
// but that is currently not supported in the codegen since we require a default
3737
//
3838
// eslint-disable-next-line no-unused-vars
39-
export type WithDefault<Type: DefaultTypes, Value: ?Type | string> = Type;
39+
export type WithDefault<Type: DefaultTypes, Value: ?Type | string> = ?Type;

packages/babel-plugin-inline-view-configs/__test_fixtures__/fixtures.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type ModuleProps = $ReadOnly<{|
3939
...ViewProps,
4040
4141
// Props
42-
boolean_default_true_optional_both?: ?WithDefault<boolean, true>,
42+
boolean_default_true_optional_both?: WithDefault<boolean, true>,
4343
4444
// Events
4545
onDirectEventDefinedInlineNull: DirectEventHandler<null>,

packages/babel-plugin-inline-view-configs/__tests__/__snapshots__/index-test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface NativeCommands {
1414
}
1515
type ModuleProps = $ReadOnly<{| ...ViewProps,
1616
// Props
17-
boolean_default_true_optional_both?: ?WithDefault<boolean, true>,
17+
boolean_default_true_optional_both?: WithDefault<boolean, true>,
1818
// Events
1919
onDirectEventDefinedInlineNull: DirectEventHandler<null>,
2020
onBubblingEventDefinedInlineNull: BubblingEventHandler<null>,

packages/react-native-codegen/src/parsers/flow/__test_fixtures__/failures.js

+31
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,42 @@ export type ModuleProps = $ReadOnly<{|
120120
121121
export const Commands = codegenNativeCommands<NativeCommands>();
122122
123+
export default codegenNativeComponent<ModuleProps>('Module');
124+
`;
125+
const NULLABLE_WITH_DEFAULT = `
126+
/**
127+
* Copyright (c) Facebook, Inc. and its affiliates.
128+
*
129+
* This source code is licensed under the MIT license found in the
130+
* LICENSE file in the root directory of this source tree.
131+
*
132+
* @format
133+
* @flow
134+
*/
135+
136+
'use strict';
137+
138+
const codegenNativeComponent = require('codegenNativeComponent');
139+
140+
import type {
141+
WithDefault,
142+
Float,
143+
} from 'CodegenTypes';
144+
145+
import type {ViewProps} from 'ViewPropTypes';
146+
147+
148+
export type ModuleProps = $ReadOnly<{|
149+
...ViewProps,
150+
nullable_with_default: ?WithDefault<Float, 1.0>,
151+
|}>;
152+
123153
export default codegenNativeComponent<ModuleProps>('Module');
124154
`;
125155

126156
module.exports = {
127157
COMMANDS_DEFINED_INLINE,
128158
COMMANDS_DEFINED_MULTIPLE_TIMES,
129159
COMMANDS_DEFINED_WITHOUT_REF,
160+
NULLABLE_WITH_DEFAULT,
130161
};

packages/react-native-codegen/src/parsers/flow/__test_fixtures__/fixtures.js

+19-21
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ type ModuleProps = $ReadOnly<{|
8989
...ViewProps,
9090
9191
// Props
92-
boolean_default_true_optional_both?: ?WithDefault<boolean, true>,
92+
boolean_default_true_optional_both?: WithDefault<boolean, true>,
9393
9494
// Events
9595
onDirectEventDefinedInlineNull: DirectEventHandler<null>,
@@ -160,48 +160,47 @@ type ModuleProps = $ReadOnly<{|
160160
// Boolean props
161161
boolean_required: boolean,
162162
boolean_optional_key?: WithDefault<boolean, true>,
163-
boolean_optional_value: ?WithDefault<boolean, true>,
164-
boolean_optional_both?: ?WithDefault<boolean, true>,
163+
boolean_optional_value: WithDefault<boolean, true>,
164+
boolean_optional_both?: WithDefault<boolean, true>,
165165
166166
// String props
167167
string_required: string,
168168
string_optional_key?: WithDefault<string, ''>,
169-
string_optional_value: ?WithDefault<string, ''>,
170-
string_optional_both?: ?WithDefault<string, ''>,
169+
string_optional_value: WithDefault<string, ''>,
170+
string_optional_both?: WithDefault<string, ''>,
171171
172172
// String props, null default
173173
string_null_optional_key?: WithDefault<string, null>,
174-
string_null_optional_value: ?WithDefault<string, null>,
175-
string_null_optional_both?: ?WithDefault<string, null>,
174+
string_null_optional_value: WithDefault<string, null>,
175+
string_null_optional_both?: WithDefault<string, null>,
176176
177177
// Stringish props
178178
stringish_required: Stringish,
179179
stringish_optional_key?: WithDefault<Stringish, ''>,
180-
stringish_optional_value: ?WithDefault<Stringish, ''>,
181-
stringish_optional_both?: ?WithDefault<Stringish, ''>,
180+
stringish_optional_value: WithDefault<Stringish, ''>,
181+
stringish_optional_both?: WithDefault<Stringish, ''>,
182182
183183
// Stringish props, null default
184184
stringish_null_optional_key?: WithDefault<Stringish, null>,
185-
stringish_null_optional_value: ?WithDefault<Stringish, null>,
186-
stringish_null_optional_both?: ?WithDefault<Stringish, null>,
185+
stringish_null_optional_value: WithDefault<Stringish, null>,
186+
stringish_null_optional_both?: WithDefault<Stringish, null>,
187187
188188
// Float props
189189
float_required: Float,
190190
float_optional_key?: WithDefault<Float, 1.1>,
191-
float_optional_value: ?WithDefault<Float, 1.1>,
192-
float_optional_both?: ?WithDefault<Float, 1.1>,
191+
float_optional_value: WithDefault<Float, 1.1>,
192+
float_optional_both?: WithDefault<Float, 1.1>,
193193
194194
// Int32 props
195195
int32_required: Int32,
196196
int32_optional_key?: WithDefault<Int32, 1>,
197-
int32_optional_value: ?WithDefault<Int32, 1>,
198-
int32_optional_both?: ?WithDefault<Int32, 1>,
197+
int32_optional_value: WithDefault<Int32, 1>,
198+
int32_optional_both?: WithDefault<Int32, 1>,
199199
200200
// String enum props
201-
enum_required: WithDefault<('small' | 'large'), 'small'>,
202201
enum_optional_key?: WithDefault<('small' | 'large'), 'small'>,
203-
enum_optional_value: ?WithDefault<('small' | 'large'), 'small'>,
204-
enum_optional_both?: ?WithDefault<('small' | 'large'), 'small'>,
202+
enum_optional_value: WithDefault<('small' | 'large'), 'small'>,
203+
enum_optional_both?: WithDefault<('small' | 'large'), 'small'>,
205204
206205
// ImageSource props
207206
image_required: ImageSource,
@@ -284,10 +283,9 @@ type ModuleProps = $ReadOnly<{|
284283
array_int32_optional_both?: ?$ReadOnlyArray<Int32>,
285284
286285
// String enum props
287-
array_enum_required: WithDefault<$ReadOnlyArray<('small' | 'large')>, 'small'>,
288286
array_enum_optional_key?: WithDefault<$ReadOnlyArray<('small' | 'large')>, 'small'>,
289-
array_enum_optional_value: ?WithDefault<$ReadOnlyArray<('small' | 'large')>, 'small'>,
290-
array_enum_optional_both?: ?WithDefault<$ReadOnlyArray<('small' | 'large')>, 'small'>,
287+
array_enum_optional_value: WithDefault<$ReadOnlyArray<('small' | 'large')>, 'small'>,
288+
array_enum_optional_both?: WithDefault<$ReadOnlyArray<('small' | 'large')>, 'small'>,
291289
292290
// ImageSource props
293291
array_image_required: $ReadOnlyArray<ImageSource>,

0 commit comments

Comments
 (0)