Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,10 @@ type ModuleNativeState = $ReadOnly<{|
float_null_optional_key?: WithDefault<Float, null>,
float_null_optional_both?: WithDefault<Float, null>,

// Float props, negative default
negative_float_optional_key?: WithDefault<Float, -1.0>;
negative_float_optional_both?: WithDefault<Float, -1.0>;

// Int32 props
int32_required: Int32,
int32_optional_key?: WithDefault<Int32, 1>,
Expand Down Expand Up @@ -1131,6 +1135,43 @@ export default (codegenNativeComponent<ModuleProps, Options>(
): HostComponent<ModuleProps>);
`;

const STATE_NEGATIVE_DEFAULTS = `
/**
* 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
*/

'use strict';

const codegenNativeComponent = require('codegenNativeComponent');

import type {Int32, Double, Float, WithDefault} from 'CodegenTypes';
import type {ImageSource} from 'ImageSource';
import type {ColorValue, PointValue, ProcessColorValue, EdgeInsetsValue} from 'StyleSheetTypes';
import type {ViewProps} from 'ViewPropTypes';
import type {HostComponent} from 'react-native';

type ModuleProps = $ReadOnly<{|
...ViewProps,
|}>;

type ModuleNativeState = $ReadOnly<{|

// Negative numbers default
negative_float_optional_key?: WithDefault<Float, -1.0>;
negative_int_optional_key?: WithDefault<Int32, -1>;
negative_double_optional_key?: WithDefault<Double, -1.0>;
|}>

export default codegenNativeComponent<ModuleProps>(
'Module',
) as HostComponent<ModuleProps>;
`;

const ARRAY_STATE_TYPES = `
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
Expand Down Expand Up @@ -1446,5 +1487,6 @@ module.exports = {
ALL_STATE_TYPES,
ARRAY_STATE_TYPES,
OBJECT_STATE_TYPES,
STATE_NEGATIVE_DEFAULTS,
// STATE_ALIASED_LOCALLY,
};
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,22 @@ exports[`RN Codegen Flow Parser can generate fixture ALL_STATE_TYPES 1`] = `
'default': null
}
},
{
'name': 'negative_float_optional_key',
'optional': true,
'typeAnnotation': {
'type': 'FloatTypeAnnotation',
'default': -1
}
},
{
'name': 'negative_float_optional_both',
'optional': true,
'typeAnnotation': {
'type': 'FloatTypeAnnotation',
'default': -1
}
},
{
'name': 'int32_required',
'optional': false,
Expand Down Expand Up @@ -11357,3 +11373,52 @@ exports[`RN Codegen Flow Parser can generate fixture PROPS_AS_EXTERNAL_TYPES 1`]
}
}"
`;

exports[`RN Codegen Flow Parser can generate fixture STATE_NEGATIVE_DEFAULTS 1`] = `
"{
'modules': {
'Module': {
'type': 'Component',
'components': {
'Module': {
'extendsProps': [
{
'type': 'ReactNativeBuiltInType',
'knownTypeName': 'ReactNativeCoreViewProps'
}
],
'events': [],
'props': [],
'commands': [],
'state': [
{
'name': 'negative_float_optional_key',
'optional': true,
'typeAnnotation': {
'type': 'FloatTypeAnnotation',
'default': -1
}
},
{
'name': 'negative_int_optional_key',
'optional': true,
'typeAnnotation': {
'type': 'Int32TypeAnnotation',
'default': -1
}
},
{
'name': 'negative_double_optional_key',
'optional': true,
'typeAnnotation': {
'type': 'DoubleTypeAnnotation',
'default': -1
}
}
]
}
}
}
}
}"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,10 @@ export interface ModuleNativeState {
float_null_optional_key?: WithDefault<Float, null>;
float_null_optional_both?: WithDefault<Float, null>;

// Float props, negative default
negative_float_optional_key?: WithDefault<Float, -1.0>;
negative_float_optional_both?: WithDefault<Float, -1.0>;

// Int32 props
int32_required: Int32;
int32_optional_key?: WithDefault<Int32, 1>;
Expand Down Expand Up @@ -1267,6 +1271,46 @@ export default codegenNativeComponent<ModuleProps>(
) as HostComponent<ModuleProps>;
`;

const STATE_NEGATIVE_DEFAULTS = `
/**
* 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
*/

'use strict';

const codegenNativeComponent = require('codegenNativeComponent');

import type {Int32, Double, Float, WithDefault} from 'CodegenTypes';
import type {ImageSource} from 'ImageSource';
import type {
ColorValue,
ColorArrayValue,
PointValue,
EdgeInsetsValue,
} from 'StyleSheetTypes';
import type {ViewProps} from 'ViewPropTypes';
import type {HostComponent} from 'react-native';

export interface ModuleProps extends ViewProps { }

export interface ModuleNativeState {

// Negative numbers default
negative_float_optional_key?: WithDefault<Float, -1.0>;
negative_int_optional_key?: WithDefault<Int32, -1>;
negative_double_optional_key?: WithDefault<Double, -1.0>;
}

export default codegenNativeComponent<ModuleProps>(
'Module',
) as HostComponent<ModuleProps>;
`;

const ARRAY_STATE_TYPES = `
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
Expand Down Expand Up @@ -1678,4 +1722,5 @@ module.exports = {
ARRAY_STATE_TYPES,
ARRAY2_STATE_TYPES,
OBJECT_STATE_TYPES,
STATE_NEGATIVE_DEFAULTS,
};
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,22 @@ exports[`RN Codegen TypeScript Parser can generate fixture ALL_STATE_TYPES 1`] =
'default': null
}
},
{
'name': 'negative_float_optional_key',
'optional': true,
'typeAnnotation': {
'type': 'FloatTypeAnnotation',
'default': -1
}
},
{
'name': 'negative_float_optional_both',
'optional': true,
'typeAnnotation': {
'type': 'FloatTypeAnnotation',
'default': -1
}
},
{
'name': 'int32_required',
'optional': false,
Expand Down Expand Up @@ -12987,3 +13003,52 @@ exports[`RN Codegen TypeScript Parser can generate fixture PROPS_AS_EXTERNAL_TYP
}
}"
`;

exports[`RN Codegen TypeScript Parser can generate fixture STATE_NEGATIVE_DEFAULTS 1`] = `
"{
'modules': {
'Module': {
'type': 'Component',
'components': {
'Module': {
'extendsProps': [
{
'type': 'ReactNativeBuiltInType',
'knownTypeName': 'ReactNativeCoreViewProps'
}
],
'events': [],
'props': [],
'commands': [],
'state': [
{
'name': 'negative_float_optional_key',
'optional': true,
'typeAnnotation': {
'type': 'FloatTypeAnnotation',
'default': -1
}
},
{
'name': 'negative_int_optional_key',
'optional': true,
'typeAnnotation': {
'type': 'Int32TypeAnnotation',
'default': -1
}
},
{
'name': 'negative_double_optional_key',
'optional': true,
'typeAnnotation': {
'type': 'DoubleTypeAnnotation',
'default': -1
}
}
]
}
}
}
}
}"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,15 @@ function getSchemaInfo(
if (defaultValueType === 'TSLiteralType') {
defaultValueType = typeAnnotation.typeParameters.params[1].literal.type;
defaultValue = typeAnnotation.typeParameters.params[1].literal.value;
if (
defaultValueType === 'UnaryExpression' &&
typeAnnotation.typeParameters.params[1].literal.argument.type ===
'NumericLiteral' &&
typeAnnotation.typeParameters.params[1].literal.operator === '-'
) {
defaultValue =
-1 * typeAnnotation.typeParameters.params[1].literal.argument.value;
}
}

if (defaultValueType === 'TSNullKeyword') {
Expand Down