forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate Custom Native State (facebook#34796)
Summary: Pull Request resolved: facebook#34796 This diff introduces the generation of custom native states using basic types as we do with the Props. To make it work, the custom types are already writte in the Props.h file, therefore the State.h file must import that other file to have access to the required types. This diff adds and updates the tests for the State. ## Changelog [General][Added] - Generate custom Native State Differential Revision: https://internalfb.com/D39816763 fbshipit-source-id: c369d4965ce092d081b6b69947f32d01a7bcfeb0
- Loading branch information
1 parent
7227bde
commit ab92f6c
Showing
22 changed files
with
8,243 additions
and
310 deletions.
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
packages/react-native-codegen/src/generators/components/ComponentsGeneratorUtils.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/** | ||
* 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. | ||
* | ||
* @flow strict | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import type { | ||
NamedShape, | ||
PropTypeAnnotation, | ||
StateTypeAnnotation, | ||
} from '../../CodegenSchema'; | ||
|
||
import type { | ||
StringTypeAnnotation, | ||
ReservedPropTypeAnnotation, | ||
ObjectTypeAnnotation, | ||
Int32TypeAnnotation, | ||
FloatTypeAnnotation, | ||
DoubleTypeAnnotation, | ||
BooleanTypeAnnotation, | ||
} from '../../CodegenSchema'; | ||
|
||
const { | ||
convertDefaultTypeToString, | ||
getCppTypeForAnnotation, | ||
getEnumMaskName, | ||
getEnumName, | ||
generateStructName, | ||
} = require('./CppHelpers.js'); | ||
|
||
function getNativeTypeFromAnnotation( | ||
componentName: string, | ||
prop: | ||
| NamedShape<PropTypeAnnotation> | ||
| NamedShape<StateTypeAnnotation> | ||
| { | ||
name: string, | ||
typeAnnotation: | ||
| $FlowFixMe | ||
| DoubleTypeAnnotation | ||
| FloatTypeAnnotation | ||
| BooleanTypeAnnotation | ||
| Int32TypeAnnotation | ||
| StringTypeAnnotation | ||
| ObjectTypeAnnotation<PropTypeAnnotation> | ||
| ReservedPropTypeAnnotation | ||
| { | ||
+default: string, | ||
+options: $ReadOnlyArray<string>, | ||
+type: 'StringEnumTypeAnnotation', | ||
} | ||
| { | ||
+elementType: ObjectTypeAnnotation<PropTypeAnnotation>, | ||
+type: 'ArrayTypeAnnotation', | ||
}, | ||
}, | ||
nameParts: $ReadOnlyArray<string>, | ||
): string { | ||
const typeAnnotation = prop.typeAnnotation; | ||
switch (typeAnnotation.type) { | ||
case 'BooleanTypeAnnotation': | ||
case 'StringTypeAnnotation': | ||
case 'Int32TypeAnnotation': | ||
case 'DoubleTypeAnnotation': | ||
case 'FloatTypeAnnotation': | ||
return getCppTypeForAnnotation(typeAnnotation.type); | ||
case 'ReservedPropTypeAnnotation': | ||
switch (typeAnnotation.name) { | ||
case 'ColorPrimitive': | ||
return 'SharedColor'; | ||
case 'ImageSourcePrimitive': | ||
return 'ImageSource'; | ||
case 'PointPrimitive': | ||
return 'Point'; | ||
case 'EdgeInsetsPrimitive': | ||
return 'EdgeInsets'; | ||
default: | ||
(typeAnnotation.name: empty); | ||
throw new Error('Received unknown ReservedPropTypeAnnotation'); | ||
} | ||
case 'ArrayTypeAnnotation': { | ||
const arrayType = typeAnnotation.elementType.type; | ||
if (arrayType === 'ArrayTypeAnnotation') { | ||
return `std::vector<${getNativeTypeFromAnnotation( | ||
componentName, | ||
{typeAnnotation: typeAnnotation.elementType, name: ''}, | ||
nameParts.concat([prop.name]), | ||
)}>`; | ||
} | ||
if (arrayType === 'ObjectTypeAnnotation') { | ||
const structName = generateStructName( | ||
componentName, | ||
nameParts.concat([prop.name]), | ||
); | ||
return `std::vector<${structName}>`; | ||
} | ||
if (arrayType === 'StringEnumTypeAnnotation') { | ||
const enumName = getEnumName(componentName, prop.name); | ||
return getEnumMaskName(enumName); | ||
} | ||
const itemAnnotation = getNativeTypeFromAnnotation( | ||
componentName, | ||
{ | ||
typeAnnotation: typeAnnotation.elementType, | ||
name: componentName, | ||
}, | ||
nameParts.concat([prop.name]), | ||
); | ||
return `std::vector<${itemAnnotation}>`; | ||
} | ||
case 'ObjectTypeAnnotation': { | ||
return generateStructName(componentName, nameParts.concat([prop.name])); | ||
} | ||
case 'StringEnumTypeAnnotation': | ||
return getEnumName(componentName, prop.name); | ||
case 'Int32EnumTypeAnnotation': | ||
return getEnumName(componentName, prop.name); | ||
default: | ||
(typeAnnotation: empty); | ||
throw new Error( | ||
`Received invalid typeAnnotation for ${componentName} prop ${prop.name}, received ${typeAnnotation.type}`, | ||
); | ||
} | ||
} | ||
|
||
function getStateConstituents( | ||
componentName: string, | ||
stateShape: NamedShape<StateTypeAnnotation>, | ||
): { | ||
name: string, | ||
varName: string, | ||
type: string, | ||
defaultValue: $FlowFixMe, | ||
} { | ||
const name = stateShape.name; | ||
const varName = `${name}_`; | ||
const type = getNativeTypeFromAnnotation(componentName, stateShape, []); | ||
const defaultValue = convertDefaultTypeToString(componentName, stateShape); | ||
|
||
return { | ||
name, | ||
varName, | ||
type, | ||
defaultValue, | ||
}; | ||
} | ||
|
||
module.exports = { | ||
getNativeTypeFromAnnotation, | ||
getStateConstituents, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.