Skip to content

Commit 0212179

Browse files
ken0nekfacebook-github-bot
authored andcommitted
Use buildPropSchema from parser-commons (#37043)
Summary: > The `buildPropSchema` function in `parsers/typescript/components/props.js` and `parsers/flow/components/props.js` is the same. move it to `parser-commons` and use it in the original files. part of #34872 - [x] Make the getTypeAnnotation signature from the Flow package to be equal to the typescript one. Specifically, the Typescript one needs an additional parameter withNullDefault that we can safely ignore in the implementation. - [x] buildPropSchema signature can be updated to accept those two functions in input as callbacks. Then, the getProps function can feed the right functions based on the language to the shared build prop schema. ref: #34872 (comment) bypass-github-export-checks ## Changelog: <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [Internal] [Changed] - Use `buildPropSchema` from parser-commons Pull Request resolved: #37043 Test Plan: - [ ] `yarn jest react-native-codegen` pass Reviewed By: rshest Differential Revision: D45209982 Pulled By: cipolleschi fbshipit-source-id: c241bc0542ba662c965d70d1dc283f48541e14ea
1 parent 3de44e7 commit 0212179

File tree

12 files changed

+172
-82
lines changed

12 files changed

+172
-82
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
'use strict';
1212

13-
import type {ASTNode} from '../utils';
1413
import type {NamedShape} from '../../../CodegenSchema.js';
1514
const {getValueFromTypes} = require('../utils.js');
16-
import type {TypeDeclarationMap, PropAST} from '../../utils';
15+
import type {TypeDeclarationMap, PropAST, ASTNode} from '../../utils';
16+
import type {BuildSchemaFN, Parser} from '../../parser';
1717

1818
function getProperties(
1919
typeName: string,
@@ -34,7 +34,8 @@ function getTypeAnnotationForArray<+T>(
3434
typeAnnotation: $FlowFixMe,
3535
defaultValue: $FlowFixMe | null,
3636
types: TypeDeclarationMap,
37-
buildSchema: (property: PropAST, types: TypeDeclarationMap) => ?NamedShape<T>,
37+
parser: Parser,
38+
buildSchema: BuildSchemaFN<T>,
3839
): $FlowFixMe {
3940
const extractedTypeAnnotation = getValueFromTypes(typeAnnotation, types);
4041
if (extractedTypeAnnotation.type === 'NullableTypeAnnotation') {
@@ -63,7 +64,7 @@ function getTypeAnnotationForArray<+T>(
6364
objectType.typeParameters.params[0].properties,
6465
types,
6566
)
66-
.map(prop => buildSchema(prop, types))
67+
.map(prop => buildSchema(prop, types, parser))
6768
.filter(Boolean),
6869
};
6970
}
@@ -84,7 +85,7 @@ function getTypeAnnotationForArray<+T>(
8485
nestedObjectType.typeParameters.params[0].properties,
8586
types,
8687
)
87-
.map(prop => buildSchema(prop, types))
88+
.map(prop => buildSchema(prop, types, parser))
8889
.filter(Boolean),
8990
},
9091
};
@@ -233,7 +234,8 @@ function getTypeAnnotation<+T>(
233234
defaultValue: $FlowFixMe | null,
234235
withNullDefault: boolean,
235236
types: TypeDeclarationMap,
236-
buildSchema: (property: PropAST, types: TypeDeclarationMap) => ?NamedShape<T>,
237+
parser: Parser,
238+
buildSchema: BuildSchemaFN<T>,
237239
): $FlowFixMe {
238240
const typeAnnotation = getValueFromTypes(annotation, types);
239241

@@ -248,6 +250,7 @@ function getTypeAnnotation<+T>(
248250
typeAnnotation.typeParameters.params[0],
249251
defaultValue,
250252
types,
253+
parser,
251254
buildSchema,
252255
),
253256
};
@@ -263,7 +266,7 @@ function getTypeAnnotation<+T>(
263266
typeAnnotation.typeParameters.params[0].properties,
264267
types,
265268
)
266-
.map(prop => buildSchema(prop, types))
269+
.map(prop => buildSchema(prop, types, parser))
267270
.filter(Boolean),
268271
};
269272
}

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

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,14 @@ import type {
1818
import type {TypeDeclarationMap, PropAST} from '../../utils';
1919
import type {Parser} from '../../parser';
2020

21-
const {
22-
flattenProperties,
23-
getSchemaInfo,
24-
getTypeAnnotation,
25-
} = require('./componentsUtils.js');
21+
const {flattenProperties} = require('./componentsUtils.js');
22+
const {buildPropSchema} = require('../../parsers-commons');
2623

2724
type ExtendsForProp = null | {
2825
type: 'ReactNativeBuiltInType',
2926
knownTypeName: 'ReactNativeCoreViewProps',
3027
};
3128

32-
function buildPropSchema(
33-
property: PropAST,
34-
types: TypeDeclarationMap,
35-
): ?NamedShape<PropTypeAnnotation> {
36-
const info = getSchemaInfo(property, types);
37-
if (info == null) {
38-
return null;
39-
}
40-
const {name, optional, typeAnnotation, defaultValue, withNullDefault} = info;
41-
42-
return {
43-
name,
44-
optional,
45-
typeAnnotation: getTypeAnnotation(
46-
name,
47-
typeAnnotation,
48-
defaultValue,
49-
withNullDefault,
50-
types,
51-
buildPropSchema,
52-
),
53-
};
54-
}
55-
5629
function extendsForProp(
5730
prop: PropAST,
5831
types: TypeDeclarationMap,
@@ -117,7 +90,7 @@ function getProps(
11790
} {
11891
const nonExtendsProps = removeKnownExtends(typeDefinition, types, parser);
11992
const props = flattenProperties(nonExtendsProps, types)
120-
.map(property => buildPropSchema(property, types))
93+
.map(property => buildPropSchema(property, types, parser))
12194
.filter(Boolean);
12295

12396
return {

packages/react-native-codegen/src/parsers/flow/parser.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ import type {
2222
NativeModuleEnumMap,
2323
} from '../../CodegenSchema';
2424
import type {ParserType} from '../errors';
25-
import type {Parser} from '../parser';
25+
import type {GetSchemaInfoFN, GetTypeAnnotationFN, Parser} from '../parser';
2626
import type {ParserErrorCapturer, TypeDeclarationMap, PropAST} from '../utils';
2727

28+
const {
29+
getSchemaInfo,
30+
getTypeAnnotation,
31+
} = require('./components/componentsUtils');
32+
2833
const {flowTranslateTypeAnnotation} = require('./modules');
2934

3035
// $FlowFixMe[untyped-import] there's no flowtype flow-parser
@@ -343,6 +348,14 @@ class FlowParser implements Parser {
343348
property.value.type === 'NullableTypeAnnotation' || property.optional
344349
);
345350
}
351+
352+
getGetSchemaInfoFN(): GetSchemaInfoFN {
353+
return getSchemaInfo;
354+
}
355+
356+
getGetTypeAnnotationFN(): GetTypeAnnotationFN {
357+
return getTypeAnnotation;
358+
}
346359
}
347360

348361
module.exports = {

packages/react-native-codegen/src/parsers/flow/utils.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010

1111
'use strict';
1212

13-
import type {TypeResolutionStatus, TypeDeclarationMap} from '../utils';
14-
15-
// $FlowFixMe[unclear-type] there's no flowtype for ASTs
16-
export type ASTNode = Object;
13+
import type {TypeResolutionStatus, TypeDeclarationMap, ASTNode} from '../utils';
1714

1815
const invariant = require('invariant');
1916

packages/react-native-codegen/src/parsers/parser.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,45 @@ import type {
2222
NativeModuleEnumMap,
2323
} from '../CodegenSchema';
2424
import type {ParserType} from './errors';
25-
import type {ParserErrorCapturer, TypeDeclarationMap, PropAST} from './utils';
25+
import type {
26+
ParserErrorCapturer,
27+
TypeDeclarationMap,
28+
PropAST,
29+
ASTNode,
30+
} from './utils';
31+
32+
export type GetTypeAnnotationFN = (
33+
name: string,
34+
annotation: $FlowFixMe | ASTNode,
35+
defaultValue: $FlowFixMe | void,
36+
withNullDefault: boolean,
37+
types: TypeDeclarationMap,
38+
parser: Parser,
39+
buildSchema: (
40+
property: PropAST,
41+
types: TypeDeclarationMap,
42+
parser: Parser,
43+
) => $FlowFixMe,
44+
) => $FlowFixMe;
45+
46+
export type SchemaInfo = {
47+
name: string,
48+
optional: boolean,
49+
typeAnnotation: $FlowFixMe,
50+
defaultValue: $FlowFixMe,
51+
withNullDefault: boolean,
52+
};
53+
54+
export type GetSchemaInfoFN = (
55+
property: PropAST,
56+
types: TypeDeclarationMap,
57+
) => ?SchemaInfo;
58+
59+
export type BuildSchemaFN<T> = (
60+
property: PropAST,
61+
types: TypeDeclarationMap,
62+
parser: Parser,
63+
) => ?NamedShape<T>;
2664

2765
/**
2866
* This is the main interface for Parsers of various languages.
@@ -276,4 +314,8 @@ export interface Parser {
276314
* @returns: a boolean specifying if the Property is optional
277315
*/
278316
isOptionalProperty(property: $FlowFixMe): boolean;
317+
318+
getGetTypeAnnotationFN(): GetTypeAnnotationFN;
319+
320+
getGetSchemaInfoFN(): GetSchemaInfoFN;
279321
}

packages/react-native-codegen/src/parsers/parserMock.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
'use strict';
1212

13-
import type {Parser} from './parser';
13+
import type {GetSchemaInfoFN, GetTypeAnnotationFN, Parser} from './parser';
1414
import type {ParserType} from './errors';
1515
import type {
1616
UnionTypeAnnotationMemberType,
@@ -255,4 +255,22 @@ export class MockedParser implements Parser {
255255
isOptionalProperty(property: $FlowFixMe): boolean {
256256
return property.optional || false;
257257
}
258+
259+
getGetTypeAnnotationFN(): GetTypeAnnotationFN {
260+
return () => {
261+
return {};
262+
};
263+
}
264+
265+
getGetSchemaInfoFN(): GetSchemaInfoFN {
266+
return () => {
267+
return {
268+
name: 'MockedSchema',
269+
optional: false,
270+
typeAnnotation: 'BooleanTypeAnnotation',
271+
defaultValue: false,
272+
withNullDefault: false,
273+
};
274+
};
275+
}
258276
}

packages/react-native-codegen/src/parsers/parsers-commons.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type {
2323
SchemaType,
2424
NativeModuleEnumMap,
2525
OptionsShape,
26+
PropTypeAnnotation,
2627
EventTypeAnnotation,
2728
ObjectTypeAnnotation,
2829
} from '../CodegenSchema.js';
@@ -854,6 +855,35 @@ function extendsForProp(
854855
}
855856
}
856857

858+
function buildPropSchema(
859+
property: PropAST,
860+
types: TypeDeclarationMap,
861+
parser: Parser,
862+
): ?NamedShape<PropTypeAnnotation> {
863+
const getSchemaInfoFN = parser.getGetSchemaInfoFN();
864+
const info = getSchemaInfoFN(property, types);
865+
if (info == null) {
866+
return null;
867+
}
868+
const {name, optional, typeAnnotation, defaultValue, withNullDefault} = info;
869+
870+
const getTypeAnnotationFN = parser.getGetTypeAnnotationFN();
871+
872+
return {
873+
name,
874+
optional,
875+
typeAnnotation: getTypeAnnotationFN(
876+
name,
877+
typeAnnotation,
878+
defaultValue,
879+
withNullDefault,
880+
types,
881+
parser,
882+
buildPropSchema,
883+
),
884+
};
885+
}
886+
857887
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
858888
* LTI update could not be added via codemod */
859889
function getEventArgument(
@@ -892,5 +922,6 @@ module.exports = {
892922
getOptions,
893923
getCommandTypeNameAndOptionsExpression,
894924
extendsForProp,
925+
buildPropSchema,
895926
getEventArgument,
896927
};

0 commit comments

Comments
 (0)