Skip to content

Commit 9ae866c

Browse files
elicwhitefacebook-github-bot
authored andcommitted
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
1 parent 0a39674 commit 9ae866c

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,10 @@ export type ModuleProps = $ReadOnly<{|
450450
...ViewProps,
451451
452452
...PropsInFile
453+
454+
localType: $ReadOnly<{|
455+
...PropsInFile
456+
|}>
453457
|}>;
454458
455459
export default (codegenNativeComponent<ModuleProps>(

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4926,6 +4926,23 @@ Object {
49264926
"type": "BooleanTypeAnnotation",
49274927
},
49284928
},
4929+
Object {
4930+
"name": "localType",
4931+
"optional": false,
4932+
"typeAnnotation": Object {
4933+
"properties": Array [
4934+
Object {
4935+
"name": "isEnabled",
4936+
"optional": false,
4937+
"typeAnnotation": Object {
4938+
"default": false,
4939+
"type": "BooleanTypeAnnotation",
4940+
},
4941+
},
4942+
],
4943+
"type": "ObjectTypeAnnotation",
4944+
},
4945+
},
49294946
],
49304947
},
49314948
},

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,12 @@ function getTypeAnnotation(name, typeAnnotation, defaultValue, types) {
123123
) {
124124
return {
125125
type: 'ObjectTypeAnnotation',
126-
properties: typeAnnotation.typeParameters.params[0].properties.map(prop =>
127-
buildPropSchema(prop, types),
128-
),
126+
properties: flattenProperties(
127+
typeAnnotation.typeParameters.params[0].properties,
128+
types,
129+
)
130+
.map(prop => buildPropSchema(prop, types))
131+
.filter(Boolean),
129132
};
130133
}
131134

@@ -299,19 +302,16 @@ function buildPropSchema(property, types: TypeMap): ?PropTypeShape {
299302
// $FlowFixMe there's no flowtype for ASTs
300303
type PropAST = Object;
301304

302-
function getProps(
305+
function flattenProperties(
303306
typeDefinition: $ReadOnlyArray<PropAST>,
304307
types: TypeMap,
305-
): $ReadOnlyArray<PropTypeShape> {
308+
) {
306309
return typeDefinition
307310
.map(property => {
308311
if (property.type === 'ObjectTypeProperty') {
309-
return buildPropSchema(property, types);
312+
return property;
310313
} else if (property.type === 'ObjectTypeSpreadProperty') {
311-
return getProps(
312-
getPropProperties(property.argument.id.name, types),
313-
types[property.argument.id.name],
314-
);
314+
return getPropProperties(property.argument.id.name, types);
315315
}
316316
})
317317
.reduce((acc, item) => {
@@ -325,6 +325,15 @@ function getProps(
325325
.filter(Boolean);
326326
}
327327

328+
function getProps(
329+
typeDefinition: $ReadOnlyArray<PropAST>,
330+
types: TypeMap,
331+
): $ReadOnlyArray<PropTypeShape> {
332+
return flattenProperties(typeDefinition, types)
333+
.map(property => buildPropSchema(property, types))
334+
.filter(Boolean);
335+
}
336+
328337
module.exports = {
329338
getProps,
330339
getPropProperties,

0 commit comments

Comments
 (0)