Support spreading locally defined types deeply
Summary: We want to be able to spread props at any level, not just the top level Reviewed By: JoshuaGross Differential Revision: D16812884 fbshipit-source-id: 2e710141f833a7cc7ea25a91a1523a5c43b4e02c
This commit is contained in:
Родитель
0a3967480e
Коммит
9ae866c072
|
@ -450,6 +450,10 @@ export type ModuleProps = $ReadOnly<{|
|
|||
...ViewProps,
|
||||
|
||||
...PropsInFile
|
||||
|
||||
localType: $ReadOnly<{|
|
||||
...PropsInFile
|
||||
|}>
|
||||
|}>;
|
||||
|
||||
export default (codegenNativeComponent<ModuleProps>(
|
||||
|
|
|
@ -4926,6 +4926,23 @@ Object {
|
|||
"type": "BooleanTypeAnnotation",
|
||||
},
|
||||
},
|
||||
Object {
|
||||
"name": "localType",
|
||||
"optional": false,
|
||||
"typeAnnotation": Object {
|
||||
"properties": Array [
|
||||
Object {
|
||||
"name": "isEnabled",
|
||||
"optional": false,
|
||||
"typeAnnotation": Object {
|
||||
"default": false,
|
||||
"type": "BooleanTypeAnnotation",
|
||||
},
|
||||
},
|
||||
],
|
||||
"type": "ObjectTypeAnnotation",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
|
|
@ -123,9 +123,12 @@ function getTypeAnnotation(name, typeAnnotation, defaultValue, types) {
|
|||
) {
|
||||
return {
|
||||
type: 'ObjectTypeAnnotation',
|
||||
properties: typeAnnotation.typeParameters.params[0].properties.map(prop =>
|
||||
buildPropSchema(prop, types),
|
||||
),
|
||||
properties: flattenProperties(
|
||||
typeAnnotation.typeParameters.params[0].properties,
|
||||
types,
|
||||
)
|
||||
.map(prop => buildPropSchema(prop, types))
|
||||
.filter(Boolean),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -299,19 +302,16 @@ function buildPropSchema(property, types: TypeMap): ?PropTypeShape {
|
|||
// $FlowFixMe there's no flowtype for ASTs
|
||||
type PropAST = Object;
|
||||
|
||||
function getProps(
|
||||
function flattenProperties(
|
||||
typeDefinition: $ReadOnlyArray<PropAST>,
|
||||
types: TypeMap,
|
||||
): $ReadOnlyArray<PropTypeShape> {
|
||||
) {
|
||||
return typeDefinition
|
||||
.map(property => {
|
||||
if (property.type === 'ObjectTypeProperty') {
|
||||
return buildPropSchema(property, types);
|
||||
return property;
|
||||
} else if (property.type === 'ObjectTypeSpreadProperty') {
|
||||
return getProps(
|
||||
getPropProperties(property.argument.id.name, types),
|
||||
types[property.argument.id.name],
|
||||
);
|
||||
return getPropProperties(property.argument.id.name, types);
|
||||
}
|
||||
})
|
||||
.reduce((acc, item) => {
|
||||
|
@ -325,6 +325,15 @@ function getProps(
|
|||
.filter(Boolean);
|
||||
}
|
||||
|
||||
function getProps(
|
||||
typeDefinition: $ReadOnlyArray<PropAST>,
|
||||
types: TypeMap,
|
||||
): $ReadOnlyArray<PropTypeShape> {
|
||||
return flattenProperties(typeDefinition, types)
|
||||
.map(property => buildPropSchema(property, types))
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getProps,
|
||||
getPropProperties,
|
||||
|
|
Загрузка…
Ссылка в новой задаче