Force property to be optional if value has WithDefault

Summary: It's pointless to handle non optional key if value has withDefaul so I'm adding a rule into parser preventing from such cases

Reviewed By: lunaleaps

Differential Revision: D16166709

fbshipit-source-id: 38cef522b217917a3a4886d857720932f2ebb475
This commit is contained in:
Michał Osadnik
2019-07-09 14:44:43 -07:00
committed by Facebook Github Bot
parent a162554f5d
commit dc52f8c2e6
5 changed files with 44 additions and 101 deletions
@@ -45,7 +45,7 @@ type NativeProps = $ReadOnly<{|
thumbImage?: ?ImageSource,
thumbTintColor?: ?ColorValue,
trackImage?: ?ImageSource,
value: WithDefault<Float, 0>,
value?: WithDefault<Float, 0>,
// Events
onChange?: ?BubblingEventHandler<Event>,
@@ -396,7 +396,39 @@ export type ModuleProps = $ReadOnly<{|
export default codegenNativeComponent<ModuleProps>('Module');
`;
const NON_OPTIONAL_KEY_WITH_DEFAULT_VALUE = `
/**
* Copyright (c) Facebook, Inc. and its 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
*/
'use strict';
const codegenNativeComponent = require('codegenNativeComponent');
import type {
WithDefault,
Float,
} from 'CodegenTypes';
import type {ViewProps} from 'ViewPropTypes';
export type ModuleProps = $ReadOnly<{|
...ViewProps,
required_key_with_default: WithDefault<Float, 1.0>,
|}>;
export default codegenNativeComponent<ModuleProps>('Module');
`;
module.exports = {
NON_OPTIONAL_KEY_WITH_DEFAULT_VALUE,
NATIVE_MODULES_WITH_PROMISE_WITHOUT_TYPE,
NATIVE_MODULES_WITH_ARRAY_WITH_NO_TYPE_FOR_CONTENT_AS_PARAM,
NATIVE_MODULES_WITH_ARRAY_WITH_NO_TYPE_FOR_CONTENT,
@@ -449,46 +449,38 @@ type ModuleProps = $ReadOnly<{|
// Boolean props
boolean_required: boolean,
boolean_optional_key?: WithDefault<boolean, true>,
boolean_optional_value: WithDefault<boolean, true>,
boolean_optional_both?: WithDefault<boolean, true>,
// String props
string_required: string,
string_optional_key?: WithDefault<string, ''>,
string_optional_value: WithDefault<string, ''>,
string_optional_both?: WithDefault<string, ''>,
// String props, null default
string_null_optional_key?: WithDefault<string, null>,
string_null_optional_value: WithDefault<string, null>,
string_null_optional_both?: WithDefault<string, null>,
// Stringish props
stringish_required: Stringish,
stringish_optional_key?: WithDefault<Stringish, ''>,
stringish_optional_value: WithDefault<Stringish, ''>,
stringish_optional_both?: WithDefault<Stringish, ''>,
// Stringish props, null default
stringish_null_optional_key?: WithDefault<Stringish, null>,
stringish_null_optional_value: WithDefault<Stringish, null>,
stringish_null_optional_both?: WithDefault<Stringish, null>,
// Float props
float_required: Float,
float_optional_key?: WithDefault<Float, 1.1>,
float_optional_value: WithDefault<Float, 1.1>,
float_optional_both?: WithDefault<Float, 1.1>,
// Int32 props
int32_required: Int32,
int32_optional_key?: WithDefault<Int32, 1>,
int32_optional_value: WithDefault<Int32, 1>,
int32_optional_both?: WithDefault<Int32, 1>,
// String enum props
enum_optional_key?: WithDefault<('small' | 'large'), 'small'>,
enum_optional_value: WithDefault<('small' | 'large'), 'small'>,
enum_optional_both?: WithDefault<('small' | 'large'), 'small'>,
// ImageSource props
@@ -573,7 +565,6 @@ type ModuleProps = $ReadOnly<{|
// String enum props
array_enum_optional_key?: WithDefault<$ReadOnlyArray<('small' | 'large')>, 'small'>,
array_enum_optional_value: WithDefault<$ReadOnlyArray<('small' | 'large')>, 'small'>,
array_enum_optional_both?: WithDefault<$ReadOnlyArray<('small' | 'large')>, 'small'>,
// ImageSource props
@@ -24,6 +24,8 @@ exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_NOT
exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_PROMISE_WITHOUT_TYPE 1`] = `"Unsupported return promise type for getBool: expected to find annotation for type of promise content"`;
exports[`RN Codegen Flow Parser Fails with error message NON_OPTIONAL_KEY_WITH_DEFAULT_VALUE 1`] = `"key required_key_with_default must be optional if used with WithDefault<> annotation"`;
exports[`RN Codegen Flow Parser Fails with error message NULLABLE_WITH_DEFAULT 1`] = `"WithDefault<> is optional and does not need to be marked as optional. Please remove the ? annotation in front of it."`;
exports[`RN Codegen Flow Parser Fails with error message TWO_NATIVE_MODULES_EXPORTED_WITH_DEFAULT 1`] = `"File should contain only one default export."`;
@@ -59,14 +61,6 @@ Object {
"type": "BooleanTypeAnnotation",
},
},
Object {
"name": "boolean_optional_value",
"optional": true,
"typeAnnotation": Object {
"default": true,
"type": "BooleanTypeAnnotation",
},
},
Object {
"name": "boolean_optional_both",
"optional": true,
@@ -91,14 +85,6 @@ Object {
"type": "StringTypeAnnotation",
},
},
Object {
"name": "string_optional_value",
"optional": true,
"typeAnnotation": Object {
"default": "",
"type": "StringTypeAnnotation",
},
},
Object {
"name": "string_optional_both",
"optional": true,
@@ -115,14 +101,6 @@ Object {
"type": "StringTypeAnnotation",
},
},
Object {
"name": "string_null_optional_value",
"optional": true,
"typeAnnotation": Object {
"default": null,
"type": "StringTypeAnnotation",
},
},
Object {
"name": "string_null_optional_both",
"optional": true,
@@ -147,14 +125,6 @@ Object {
"type": "StringTypeAnnotation",
},
},
Object {
"name": "stringish_optional_value",
"optional": true,
"typeAnnotation": Object {
"default": "",
"type": "StringTypeAnnotation",
},
},
Object {
"name": "stringish_optional_both",
"optional": true,
@@ -171,14 +141,6 @@ Object {
"type": "StringTypeAnnotation",
},
},
Object {
"name": "stringish_null_optional_value",
"optional": true,
"typeAnnotation": Object {
"default": null,
"type": "StringTypeAnnotation",
},
},
Object {
"name": "stringish_null_optional_both",
"optional": true,
@@ -203,14 +165,6 @@ Object {
"type": "FloatTypeAnnotation",
},
},
Object {
"name": "float_optional_value",
"optional": true,
"typeAnnotation": Object {
"default": 1.1,
"type": "FloatTypeAnnotation",
},
},
Object {
"name": "float_optional_both",
"optional": true,
@@ -235,14 +189,6 @@ Object {
"type": "Int32TypeAnnotation",
},
},
Object {
"name": "int32_optional_value",
"optional": true,
"typeAnnotation": Object {
"default": 1,
"type": "Int32TypeAnnotation",
},
},
Object {
"name": "int32_optional_both",
"optional": true,
@@ -267,22 +213,6 @@ Object {
"type": "StringEnumTypeAnnotation",
},
},
Object {
"name": "enum_optional_value",
"optional": true,
"typeAnnotation": Object {
"default": "small",
"options": Array [
Object {
"name": "small",
},
Object {
"name": "large",
},
],
"type": "StringEnumTypeAnnotation",
},
},
Object {
"name": "enum_optional_both",
"optional": true,
@@ -633,25 +563,6 @@ Object {
"type": "ArrayTypeAnnotation",
},
},
Object {
"name": "array_enum_optional_value",
"optional": true,
"typeAnnotation": Object {
"elementType": Object {
"default": "small",
"options": Array [
Object {
"name": "small",
},
Object {
"name": "large",
},
],
"type": "StringEnumTypeAnnotation",
},
"type": "ArrayTypeAnnotation",
},
},
Object {
"name": "array_enum_optional_both",
"optional": true,
@@ -187,6 +187,15 @@ function buildPropSchema(property): ?PropTypeShape {
(value.type === 'GenericTypeAnnotation' &&
typeAnnotation.id.name === 'WithDefault');
if (
!property.optional &&
value.type === 'GenericTypeAnnotation' &&
typeAnnotation.id.name === 'WithDefault'
) {
throw new Error(
`key ${name} must be optional if used with WithDefault<> annotation`,
);
}
if (
value.type === 'NullableTypeAnnotation' &&
(typeAnnotation.type === 'GenericTypeAnnotation' &&