Skip to content

Commit e9c8278

Browse files
Riccardo Cipolleschifacebook-github-bot
authored andcommitted
Handle properly negative values in TS parser
Summary: The TypeScript parser was not handling negative default values properly. The reason why is because the AST for those values is structurally different and wraps them in a `UnaryExpression` with the `-` operator. This Diff adds the support for those default values and it also add some tests in both Flow and TS. ## Changed [General][Fixed] Properly parse negative values Differential Revision: D39847784 fbshipit-source-id: 6be3c62e79458e2cb6c7798914e72bc221fe0d25
1 parent e36a6fe commit e9c8278

File tree

5 files changed

+226
-0
lines changed

5 files changed

+226
-0
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,10 @@ type ModuleNativeState = $ReadOnly<{|
10731073
float_null_optional_key?: WithDefault<Float, null>,
10741074
float_null_optional_both?: WithDefault<Float, null>,
10751075
1076+
// Float props, negative default
1077+
negative_float_optional_key?: WithDefault<Float, -1.0>;
1078+
negative_float_optional_both?: WithDefault<Float, -1.0>;
1079+
10761080
// Int32 props
10771081
int32_required: Int32,
10781082
int32_optional_key?: WithDefault<Int32, 1>,
@@ -1131,6 +1135,43 @@ export default (codegenNativeComponent<ModuleProps, Options>(
11311135
): HostComponent<ModuleProps>);
11321136
`;
11331137

1138+
const STATE_NEGATIVE_DEFAULTS = `
1139+
/**
1140+
* Copyright (c) Meta Platforms, Inc. and affiliates.
1141+
*
1142+
* This source code is licensed under the MIT license found in the
1143+
* LICENSE file in the root directory of this source tree.
1144+
*
1145+
* @format
1146+
*/
1147+
1148+
'use strict';
1149+
1150+
const codegenNativeComponent = require('codegenNativeComponent');
1151+
1152+
import type {Int32, Double, Float, WithDefault} from 'CodegenTypes';
1153+
import type {ImageSource} from 'ImageSource';
1154+
import type {ColorValue, PointValue, ProcessColorValue, EdgeInsetsValue} from 'StyleSheetTypes';
1155+
import type {ViewProps} from 'ViewPropTypes';
1156+
import type {HostComponent} from 'react-native';
1157+
1158+
type ModuleProps = $ReadOnly<{|
1159+
...ViewProps,
1160+
|}>;
1161+
1162+
type ModuleNativeState = $ReadOnly<{|
1163+
1164+
// Negative numbers default
1165+
negative_float_optional_key?: WithDefault<Float, -1.0>;
1166+
negative_int_optional_key?: WithDefault<Int32, -1>;
1167+
negative_double_optional_key?: WithDefault<Double, -1.0>;
1168+
|}>
1169+
1170+
export default codegenNativeComponent<ModuleProps>(
1171+
'Module',
1172+
) as HostComponent<ModuleProps>;
1173+
`;
1174+
11341175
const ARRAY_STATE_TYPES = `
11351176
/**
11361177
* Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -1446,5 +1487,6 @@ module.exports = {
14461487
ALL_STATE_TYPES,
14471488
ARRAY_STATE_TYPES,
14481489
OBJECT_STATE_TYPES,
1490+
STATE_NEGATIVE_DEFAULTS,
14491491
// STATE_ALIASED_LOCALLY,
14501492
};

packages/react-native-codegen/src/parsers/flow/components/__tests__/__snapshots__/component-parser-test.js.snap

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,22 @@ exports[`RN Codegen Flow Parser can generate fixture ALL_STATE_TYPES 1`] = `
774774
'default': null
775775
}
776776
},
777+
{
778+
'name': 'negative_float_optional_key',
779+
'optional': true,
780+
'typeAnnotation': {
781+
'type': 'FloatTypeAnnotation',
782+
'default': -1
783+
}
784+
},
785+
{
786+
'name': 'negative_float_optional_both',
787+
'optional': true,
788+
'typeAnnotation': {
789+
'type': 'FloatTypeAnnotation',
790+
'default': -1
791+
}
792+
},
777793
{
778794
'name': 'int32_required',
779795
'optional': false,
@@ -11357,3 +11373,52 @@ exports[`RN Codegen Flow Parser can generate fixture PROPS_AS_EXTERNAL_TYPES 1`]
1135711373
}
1135811374
}"
1135911375
`;
11376+
11377+
exports[`RN Codegen Flow Parser can generate fixture STATE_NEGATIVE_DEFAULTS 1`] = `
11378+
"{
11379+
'modules': {
11380+
'Module': {
11381+
'type': 'Component',
11382+
'components': {
11383+
'Module': {
11384+
'extendsProps': [
11385+
{
11386+
'type': 'ReactNativeBuiltInType',
11387+
'knownTypeName': 'ReactNativeCoreViewProps'
11388+
}
11389+
],
11390+
'events': [],
11391+
'props': [],
11392+
'commands': [],
11393+
'state': [
11394+
{
11395+
'name': 'negative_float_optional_key',
11396+
'optional': true,
11397+
'typeAnnotation': {
11398+
'type': 'FloatTypeAnnotation',
11399+
'default': -1
11400+
}
11401+
},
11402+
{
11403+
'name': 'negative_int_optional_key',
11404+
'optional': true,
11405+
'typeAnnotation': {
11406+
'type': 'Int32TypeAnnotation',
11407+
'default': -1
11408+
}
11409+
},
11410+
{
11411+
'name': 'negative_double_optional_key',
11412+
'optional': true,
11413+
'typeAnnotation': {
11414+
'type': 'DoubleTypeAnnotation',
11415+
'default': -1
11416+
}
11417+
}
11418+
]
11419+
}
11420+
}
11421+
}
11422+
}
11423+
}"
11424+
`;

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,10 @@ export interface ModuleNativeState {
11661166
float_null_optional_key?: WithDefault<Float, null>;
11671167
float_null_optional_both?: WithDefault<Float, null>;
11681168
1169+
// Float props, negative default
1170+
negative_float_optional_key?: WithDefault<Float, -1.0>;
1171+
negative_float_optional_both?: WithDefault<Float, -1.0>;
1172+
11691173
// Int32 props
11701174
int32_required: Int32;
11711175
int32_optional_key?: WithDefault<Int32, 1>;
@@ -1224,6 +1228,46 @@ export default codegenNativeComponent<ModuleProps>(
12241228
) as HostComponent<ModuleProps>;
12251229
`;
12261230

1231+
const STATE_NEGATIVE_DEFAULTS = `
1232+
/**
1233+
* Copyright (c) Meta Platforms, Inc. and affiliates.
1234+
*
1235+
* This source code is licensed under the MIT license found in the
1236+
* LICENSE file in the root directory of this source tree.
1237+
*
1238+
* @format
1239+
*/
1240+
1241+
'use strict';
1242+
1243+
const codegenNativeComponent = require('codegenNativeComponent');
1244+
1245+
import type {Int32, Double, Float, WithDefault} from 'CodegenTypes';
1246+
import type {ImageSource} from 'ImageSource';
1247+
import type {
1248+
ColorValue,
1249+
ColorArrayValue,
1250+
PointValue,
1251+
EdgeInsetsValue,
1252+
} from 'StyleSheetTypes';
1253+
import type {ViewProps} from 'ViewPropTypes';
1254+
import type {HostComponent} from 'react-native';
1255+
1256+
export interface ModuleProps extends ViewProps { }
1257+
1258+
export interface ModuleNativeState {
1259+
1260+
// Negative numbers default
1261+
negative_float_optional_key?: WithDefault<Float, -1.0>;
1262+
negative_int_optional_key?: WithDefault<Int32, -1>;
1263+
negative_double_optional_key?: WithDefault<Double, -1.0>;
1264+
}
1265+
1266+
export default codegenNativeComponent<ModuleProps>(
1267+
'Module',
1268+
) as HostComponent<ModuleProps>;
1269+
`;
1270+
12271271
const ARRAY_STATE_TYPES = `
12281272
/**
12291273
* Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -1634,4 +1678,5 @@ module.exports = {
16341678
ARRAY_STATE_TYPES,
16351679
ARRAY2_STATE_TYPES,
16361680
OBJECT_STATE_TYPES,
1681+
STATE_NEGATIVE_DEFAULTS,
16371682
};

packages/react-native-codegen/src/parsers/typescript/components/__tests__/__snapshots__/typescript-component-parser-test.js.snap

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,22 @@ exports[`RN Codegen TypeScript Parser can generate fixture ALL_STATE_TYPES 1`] =
770770
'default': null
771771
}
772772
},
773+
{
774+
'name': 'negative_float_optional_key',
775+
'optional': true,
776+
'typeAnnotation': {
777+
'type': 'FloatTypeAnnotation',
778+
'default': -1
779+
}
780+
},
781+
{
782+
'name': 'negative_float_optional_both',
783+
'optional': true,
784+
'typeAnnotation': {
785+
'type': 'FloatTypeAnnotation',
786+
'default': -1
787+
}
788+
},
773789
{
774790
'name': 'int32_required',
775791
'optional': false,
@@ -12676,3 +12692,52 @@ exports[`RN Codegen TypeScript Parser can generate fixture PROPS_AS_EXTERNAL_TYP
1267612692
}
1267712693
}"
1267812694
`;
12695+
12696+
exports[`RN Codegen TypeScript Parser can generate fixture STATE_NEGATIVE_DEFAULTS 1`] = `
12697+
"{
12698+
'modules': {
12699+
'Module': {
12700+
'type': 'Component',
12701+
'components': {
12702+
'Module': {
12703+
'extendsProps': [
12704+
{
12705+
'type': 'ReactNativeBuiltInType',
12706+
'knownTypeName': 'ReactNativeCoreViewProps'
12707+
}
12708+
],
12709+
'events': [],
12710+
'props': [],
12711+
'commands': [],
12712+
'state': [
12713+
{
12714+
'name': 'negative_float_optional_key',
12715+
'optional': true,
12716+
'typeAnnotation': {
12717+
'type': 'FloatTypeAnnotation',
12718+
'default': -1
12719+
}
12720+
},
12721+
{
12722+
'name': 'negative_int_optional_key',
12723+
'optional': true,
12724+
'typeAnnotation': {
12725+
'type': 'Int32TypeAnnotation',
12726+
'default': -1
12727+
}
12728+
},
12729+
{
12730+
'name': 'negative_double_optional_key',
12731+
'optional': true,
12732+
'typeAnnotation': {
12733+
'type': 'DoubleTypeAnnotation',
12734+
'default': -1
12735+
}
12736+
}
12737+
]
12738+
}
12739+
}
12740+
}
12741+
}
12742+
}"
12743+
`;

packages/react-native-codegen/src/parsers/typescript/components/componentsUtils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,15 @@ function getSchemaInfo(
612612
if (defaultValueType === 'TSLiteralType') {
613613
defaultValueType = typeAnnotation.typeParameters.params[1].literal.type;
614614
defaultValue = typeAnnotation.typeParameters.params[1].literal.value;
615+
if (
616+
defaultValueType === 'UnaryExpression' &&
617+
typeAnnotation.typeParameters.params[1].literal.argument.type ===
618+
'NumericLiteral' &&
619+
typeAnnotation.typeParameters.params[1].literal.operator === '-'
620+
) {
621+
defaultValue =
622+
-1 * typeAnnotation.typeParameters.params[1].literal.argument.value;
623+
}
615624
}
616625

617626
if (defaultValueType === 'TSNullKeyword') {

0 commit comments

Comments
 (0)