Skip to content

Commit efc6e14

Browse files
Pranav-yadavfacebook-github-bot
authored andcommitted
merge getExtendsProps & getProps fns - Flow (#36891)
Summary: [Codegen 102] This PR is subtask of umbrella #34872. It extracts the code to compute the `extendsProps` and the props properties in Flow in a `getProps() -> {extendsProps, props}` function into the same `index.js` file. This will help unifying the `buildComponentSchema` functions between Flow and TS so we can factor it out in a later step. ## Changelog: [INTERNAL][CHANGED] - merge `getExtendsProps` & `getProps` fns into `getProps` fn - Flow. Pull Request resolved: #36891 Test Plan: - `yarn flow && yarn test react-native-codegen` --> *should be green.* Reviewed By: rshest Differential Revision: D45044653 Pulled By: cipolleschi fbshipit-source-id: 9fcdaef60dfbc3332d880b19c6e575d948d21986
1 parent 8812f35 commit efc6e14

File tree

3 files changed

+74
-77
lines changed

3 files changed

+74
-77
lines changed

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

Lines changed: 0 additions & 66 deletions
This file was deleted.

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import type {ComponentSchemaBuilderConfig} from '../../schema.js';
1414

1515
const {getCommands} = require('./commands');
1616
const {getEvents} = require('./events');
17-
const {getExtendsProps, removeKnownExtends} = require('./extends');
1817
const {getProps} = require('./props');
1918
const {getProperties} = require('./componentsUtils.js');
2019
const {throwIfMoreThanOneCodegenNativecommands} = require('../../error-utils');
@@ -126,14 +125,10 @@ function buildComponentSchema(
126125
const types = parser.getTypes(ast);
127126

128127
const propProperties = getProperties(propsTypeName, types);
129-
130128
const commandProperties = getCommandProperties(ast, parser);
129+
const {extendsProps, props} = getProps(propProperties, types);
131130

132-
const extendsProps = getExtendsProps(propProperties, types);
133131
const options = getOptions(optionsExpression);
134-
135-
const nonExtendsProps = removeKnownExtends(propProperties, types);
136-
const props = getProps(nonExtendsProps, types);
137132
const events = getEvents(propProperties, types);
138133
const commands = getCommands(commandProperties, types);
139134

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

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,27 @@
1010

1111
'use strict';
1212

13+
import type {
14+
ExtendsPropsShape,
15+
NamedShape,
16+
PropTypeAnnotation,
17+
} from '../../../CodegenSchema.js';
18+
import type {TypeDeclarationMap} from '../../utils';
19+
1320
const {
1421
flattenProperties,
1522
getSchemaInfo,
1623
getTypeAnnotation,
1724
} = require('./componentsUtils.js');
1825

19-
import type {NamedShape, PropTypeAnnotation} from '../../../CodegenSchema.js';
20-
import type {TypeDeclarationMap} from '../../utils';
21-
2226
// $FlowFixMe[unclear-type] there's no flowtype for ASTs
2327
type PropAST = Object;
2428

29+
type ExtendsForProp = null | {
30+
type: 'ReactNativeBuiltInType',
31+
knownTypeName: 'ReactNativeCoreViewProps',
32+
};
33+
2534
function buildPropSchema(
2635
property: PropAST,
2736
types: TypeDeclarationMap,
@@ -46,13 +55,72 @@ function buildPropSchema(
4655
};
4756
}
4857

58+
function extendsForProp(
59+
prop: PropAST,
60+
types: TypeDeclarationMap,
61+
): ExtendsForProp {
62+
if (!prop.argument) {
63+
console.log('null', prop);
64+
}
65+
const name = prop.argument.id.name;
66+
67+
if (types[name] != null) {
68+
// This type is locally defined in the file
69+
return null;
70+
}
71+
72+
switch (name) {
73+
case 'ViewProps':
74+
return {
75+
type: 'ReactNativeBuiltInType',
76+
knownTypeName: 'ReactNativeCoreViewProps',
77+
};
78+
default: {
79+
throw new Error(`Unable to handle prop spread: ${name}`);
80+
}
81+
}
82+
}
83+
84+
function removeKnownExtends(
85+
typeDefinition: $ReadOnlyArray<PropAST>,
86+
types: TypeDeclarationMap,
87+
): $ReadOnlyArray<PropAST> {
88+
return typeDefinition.filter(
89+
prop =>
90+
prop.type !== 'ObjectTypeSpreadProperty' ||
91+
extendsForProp(prop, types) === null,
92+
);
93+
}
94+
95+
function getExtendsProps(
96+
typeDefinition: $ReadOnlyArray<PropAST>,
97+
types: TypeDeclarationMap,
98+
): $ReadOnlyArray<ExtendsPropsShape> {
99+
return typeDefinition
100+
.filter(prop => prop.type === 'ObjectTypeSpreadProperty')
101+
.map(prop => extendsForProp(prop, types))
102+
.filter(Boolean);
103+
}
104+
/**
105+
* Extracts the `props` and `extendsProps` (props with `extends` syntax)
106+
* from a type definition AST.
107+
*/
49108
function getProps(
50109
typeDefinition: $ReadOnlyArray<PropAST>,
51110
types: TypeDeclarationMap,
52-
): $ReadOnlyArray<NamedShape<PropTypeAnnotation>> {
53-
return flattenProperties(typeDefinition, types)
111+
): {
112+
props: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
113+
extendsProps: $ReadOnlyArray<ExtendsPropsShape>,
114+
} {
115+
const nonExtendsProps = removeKnownExtends(typeDefinition, types);
116+
const props = flattenProperties(nonExtendsProps, types)
54117
.map(property => buildPropSchema(property, types))
55118
.filter(Boolean);
119+
120+
return {
121+
props,
122+
extendsProps: getExtendsProps(typeDefinition, types),
123+
};
56124
}
57125

58126
module.exports = {

0 commit comments

Comments
 (0)