Skip to content

Commit b8a6e10

Browse files
chore: Extract logic to compute codegen TS extendsProps and props in getProps function
1 parent 9f24df0 commit b8a6e10

File tree

3 files changed

+72
-68
lines changed

3 files changed

+72
-68
lines changed

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

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,8 @@
1010

1111
'use strict';
1212

13-
import type {ExtendsPropsShape} from '../../../CodegenSchema.js';
1413
import type {TypeDeclarationMap} from '../../utils';
1514
const {parseTopLevelType} = require('../parseTopLevelType');
16-
const {flattenProperties} = require('./componentsUtils.js');
17-
18-
function extendsForProp(prop: PropsAST, types: TypeDeclarationMap) {
19-
if (!prop.expression) {
20-
console.log('null', prop);
21-
}
22-
const name = prop.expression.name;
23-
24-
if (types[name] != null) {
25-
// This type is locally defined in the file
26-
return null;
27-
}
28-
29-
switch (name) {
30-
case 'ViewProps':
31-
return {
32-
type: 'ReactNativeBuiltInType',
33-
knownTypeName: 'ReactNativeCoreViewProps',
34-
};
35-
default: {
36-
throw new Error(`Unable to handle prop spread: ${name}`);
37-
}
38-
}
39-
}
4015

4116
function isEvent(typeAnnotation: $FlowFixMe): boolean {
4217
if (typeAnnotation.type !== 'TSTypeReference') {
@@ -46,43 +21,16 @@ function isEvent(typeAnnotation: $FlowFixMe): boolean {
4621
return eventNames.has(typeAnnotation.typeName.name);
4722
}
4823

49-
function isProp(name: string, typeAnnotation: $FlowFixMe): boolean {
50-
if (typeAnnotation.type !== 'TSTypeReference') {
51-
return true;
52-
}
53-
const isStyle =
54-
name === 'style' &&
55-
typeAnnotation.type === 'GenericTypeAnnotation' &&
56-
typeAnnotation.typeName.name === 'ViewStyleProp';
57-
return !isStyle;
58-
}
59-
6024
// $FlowFixMe[unclear-type] TODO(T108222691): Use flow-types for @babel/parser
6125
type PropsAST = Object;
6226

6327
function categorizeProps(
6428
typeDefinition: $ReadOnlyArray<PropsAST>,
6529
types: TypeDeclarationMap,
66-
extendsProps: Array<ExtendsPropsShape>,
67-
props: Array<PropsAST>,
6830
events: Array<PropsAST>,
6931
): void {
70-
const remaining: Array<PropsAST> = [];
32+
// find events
7133
for (const prop of typeDefinition) {
72-
// find extends
73-
if (prop.type === 'TSExpressionWithTypeArguments') {
74-
const extend = extendsForProp(prop, types);
75-
if (extend) {
76-
extendsProps.push(extend);
77-
continue;
78-
}
79-
}
80-
81-
remaining.push(prop);
82-
}
83-
84-
// find events and props
85-
for (const prop of flattenProperties(remaining, types)) {
8634
if (prop.type === 'TSPropertySignature') {
8735
const topLevelType = parseTopLevelType(
8836
prop.typeAnnotation.typeAnnotation,
@@ -91,8 +39,6 @@ function categorizeProps(
9139

9240
if (isEvent(topLevelType.type)) {
9341
events.push(prop);
94-
} else if (isProp(prop.key.name, prop)) {
95-
props.push(prop);
9642
}
9743
}
9844
}

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010

1111
'use strict';
12-
import type {ExtendsPropsShape} from '../../../CodegenSchema.js';
1312
import type {Parser} from '../../parser';
1413
import type {ComponentSchemaBuilderConfig} from '../../schema.js';
1514

@@ -134,17 +133,9 @@ function buildComponentSchema(
134133

135134
const options = getOptions(optionsExpression);
136135

137-
const extendsProps: Array<ExtendsPropsShape> = [];
138-
const componentPropAsts: Array<PropsAST> = [];
139136
const componentEventAsts: Array<PropsAST> = [];
140-
categorizeProps(
141-
propProperties,
142-
types,
143-
extendsProps,
144-
componentPropAsts,
145-
componentEventAsts,
146-
);
147-
const props = getProps(componentPropAsts, types);
137+
categorizeProps(propProperties, types, componentEventAsts);
138+
const {props, extendsProps} = getProps(propProperties, types);
148139
const events = getEvents(componentEventAsts, types);
149140
const commands = getCommands(commandProperties, types);
150141

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

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const {getSchemaInfo, getTypeAnnotation} = require('./componentsUtils.js');
1313

1414
import type {NamedShape, PropTypeAnnotation} from '../../../CodegenSchema.js';
1515
import type {TypeDeclarationMap} from '../../utils';
16+
import type {ExtendsPropsShape} from '../../../CodegenSchema.js';
17+
18+
const {flattenProperties} = require('./componentsUtils.js');
1619

1720
// $FlowFixMe[unclear-type] there's no flowtype for ASTs
1821
type PropAST = Object;
@@ -36,11 +39,75 @@ function buildPropSchema(
3639
};
3740
}
3841

42+
function isProp(name: string, typeAnnotation: $FlowFixMe): boolean {
43+
if (typeAnnotation.type !== 'TSTypeReference') {
44+
return true;
45+
}
46+
const isStyle =
47+
name === 'style' &&
48+
typeAnnotation.type === 'GenericTypeAnnotation' &&
49+
typeAnnotation.typeName.name === 'ViewStyleProp';
50+
return !isStyle;
51+
}
52+
53+
function extendsForProp(prop: PropAST, types: TypeDeclarationMap) {
54+
if (!prop.expression) {
55+
console.log('null', prop);
56+
}
57+
const name = prop.expression.name;
58+
59+
if (types[name] != null) {
60+
// This type is locally defined in the file
61+
return null;
62+
}
63+
64+
switch (name) {
65+
case 'ViewProps':
66+
return {
67+
type: 'ReactNativeBuiltInType',
68+
knownTypeName: 'ReactNativeCoreViewProps',
69+
};
70+
default: {
71+
throw new Error(`Unable to handle prop spread: ${name}`);
72+
}
73+
}
74+
}
75+
3976
function getProps(
4077
typeDefinition: $ReadOnlyArray<PropAST>,
4178
types: TypeDeclarationMap,
42-
): $ReadOnlyArray<NamedShape<PropTypeAnnotation>> {
43-
return typeDefinition.map(property => buildPropSchema(property, types));
79+
): {
80+
props: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
81+
extendsProps: $ReadOnlyArray<ExtendsPropsShape>,
82+
} {
83+
const extendsProps: Array<ExtendsPropsShape> = [];
84+
const componentPropAsts: Array<PropAST> = [];
85+
const remaining: Array<PropAST> = [];
86+
87+
for (const prop of typeDefinition) {
88+
// find extends
89+
if (prop.type === 'TSExpressionWithTypeArguments') {
90+
const extend = extendsForProp(prop, types);
91+
if (extend) {
92+
extendsProps.push(extend);
93+
continue;
94+
}
95+
}
96+
97+
remaining.push(prop);
98+
}
99+
100+
// find events and props
101+
for (const prop of flattenProperties(remaining, types)) {
102+
if (prop.type === 'TSPropertySignature' && isProp(prop.key.name, prop)) {
103+
componentPropAsts.push(prop);
104+
}
105+
}
106+
107+
return {
108+
props: componentPropAsts.map(property => buildPropSchema(property, types)),
109+
extendsProps,
110+
};
44111
}
45112

46113
module.exports = {

0 commit comments

Comments
 (0)