Skip to content

Commit

Permalink
Handle properly negative values in TS parser (#34800)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #34800

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.

## Changelog
[General][Fixed] - Properly parse negative values

Reviewed By: cortinico

Differential Revision: D39847784

fbshipit-source-id: 95fc5768987477c540a54a7c4e4ff785d7a1e5d7
  • Loading branch information
Riccardo Cipolleschi authored and facebook-github-bot committed Sep 30, 2022
1 parent 3037190 commit f3c98c5
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 0 deletions.
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

0 comments on commit f3c98c5

Please sign in to comment.