Skip to content

Commit

Permalink
Use buildPropSchema from parser-commons (#37043)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ken0nek authored and facebook-github-bot committed May 11, 2023
1 parent 3de44e7 commit 0212179
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

'use strict';

import type {ASTNode} from '../utils';
import type {NamedShape} from '../../../CodegenSchema.js';
const {getValueFromTypes} = require('../utils.js');
import type {TypeDeclarationMap, PropAST} from '../../utils';
import type {TypeDeclarationMap, PropAST, ASTNode} from '../../utils';
import type {BuildSchemaFN, Parser} from '../../parser';

function getProperties(
typeName: string,
Expand All @@ -34,7 +34,8 @@ function getTypeAnnotationForArray<+T>(
typeAnnotation: $FlowFixMe,
defaultValue: $FlowFixMe | null,
types: TypeDeclarationMap,
buildSchema: (property: PropAST, types: TypeDeclarationMap) => ?NamedShape<T>,
parser: Parser,
buildSchema: BuildSchemaFN<T>,
): $FlowFixMe {
const extractedTypeAnnotation = getValueFromTypes(typeAnnotation, types);
if (extractedTypeAnnotation.type === 'NullableTypeAnnotation') {
Expand Down Expand Up @@ -63,7 +64,7 @@ function getTypeAnnotationForArray<+T>(
objectType.typeParameters.params[0].properties,
types,
)
.map(prop => buildSchema(prop, types))
.map(prop => buildSchema(prop, types, parser))
.filter(Boolean),
};
}
Expand All @@ -84,7 +85,7 @@ function getTypeAnnotationForArray<+T>(
nestedObjectType.typeParameters.params[0].properties,
types,
)
.map(prop => buildSchema(prop, types))
.map(prop => buildSchema(prop, types, parser))
.filter(Boolean),
},
};
Expand Down Expand Up @@ -233,7 +234,8 @@ function getTypeAnnotation<+T>(
defaultValue: $FlowFixMe | null,
withNullDefault: boolean,
types: TypeDeclarationMap,
buildSchema: (property: PropAST, types: TypeDeclarationMap) => ?NamedShape<T>,
parser: Parser,
buildSchema: BuildSchemaFN<T>,
): $FlowFixMe {
const typeAnnotation = getValueFromTypes(annotation, types);

Expand All @@ -248,6 +250,7 @@ function getTypeAnnotation<+T>(
typeAnnotation.typeParameters.params[0],
defaultValue,
types,
parser,
buildSchema,
),
};
Expand All @@ -263,7 +266,7 @@ function getTypeAnnotation<+T>(
typeAnnotation.typeParameters.params[0].properties,
types,
)
.map(prop => buildSchema(prop, types))
.map(prop => buildSchema(prop, types, parser))
.filter(Boolean),
};
}
Expand Down
33 changes: 3 additions & 30 deletions packages/react-native-codegen/src/parsers/flow/components/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,14 @@ import type {
import type {TypeDeclarationMap, PropAST} from '../../utils';
import type {Parser} from '../../parser';

const {
flattenProperties,
getSchemaInfo,
getTypeAnnotation,
} = require('./componentsUtils.js');
const {flattenProperties} = require('./componentsUtils.js');
const {buildPropSchema} = require('../../parsers-commons');

type ExtendsForProp = null | {
type: 'ReactNativeBuiltInType',
knownTypeName: 'ReactNativeCoreViewProps',
};

function buildPropSchema(
property: PropAST,
types: TypeDeclarationMap,
): ?NamedShape<PropTypeAnnotation> {
const info = getSchemaInfo(property, types);
if (info == null) {
return null;
}
const {name, optional, typeAnnotation, defaultValue, withNullDefault} = info;

return {
name,
optional,
typeAnnotation: getTypeAnnotation(
name,
typeAnnotation,
defaultValue,
withNullDefault,
types,
buildPropSchema,
),
};
}

function extendsForProp(
prop: PropAST,
types: TypeDeclarationMap,
Expand Down Expand Up @@ -117,7 +90,7 @@ function getProps(
} {
const nonExtendsProps = removeKnownExtends(typeDefinition, types, parser);
const props = flattenProperties(nonExtendsProps, types)
.map(property => buildPropSchema(property, types))
.map(property => buildPropSchema(property, types, parser))
.filter(Boolean);

return {
Expand Down
15 changes: 14 additions & 1 deletion packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ import type {
NativeModuleEnumMap,
} from '../../CodegenSchema';
import type {ParserType} from '../errors';
import type {Parser} from '../parser';
import type {GetSchemaInfoFN, GetTypeAnnotationFN, Parser} from '../parser';
import type {ParserErrorCapturer, TypeDeclarationMap, PropAST} from '../utils';

const {
getSchemaInfo,
getTypeAnnotation,
} = require('./components/componentsUtils');

const {flowTranslateTypeAnnotation} = require('./modules');

// $FlowFixMe[untyped-import] there's no flowtype flow-parser
Expand Down Expand Up @@ -343,6 +348,14 @@ class FlowParser implements Parser {
property.value.type === 'NullableTypeAnnotation' || property.optional
);
}

getGetSchemaInfoFN(): GetSchemaInfoFN {
return getSchemaInfo;
}

getGetTypeAnnotationFN(): GetTypeAnnotationFN {
return getTypeAnnotation;
}
}

module.exports = {
Expand Down
5 changes: 1 addition & 4 deletions packages/react-native-codegen/src/parsers/flow/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

'use strict';

import type {TypeResolutionStatus, TypeDeclarationMap} from '../utils';

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

const invariant = require('invariant');

Expand Down
44 changes: 43 additions & 1 deletion packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,45 @@ import type {
NativeModuleEnumMap,
} from '../CodegenSchema';
import type {ParserType} from './errors';
import type {ParserErrorCapturer, TypeDeclarationMap, PropAST} from './utils';
import type {
ParserErrorCapturer,
TypeDeclarationMap,
PropAST,
ASTNode,
} from './utils';

export type GetTypeAnnotationFN = (
name: string,
annotation: $FlowFixMe | ASTNode,
defaultValue: $FlowFixMe | void,
withNullDefault: boolean,
types: TypeDeclarationMap,
parser: Parser,
buildSchema: (
property: PropAST,
types: TypeDeclarationMap,
parser: Parser,
) => $FlowFixMe,
) => $FlowFixMe;

export type SchemaInfo = {
name: string,
optional: boolean,
typeAnnotation: $FlowFixMe,
defaultValue: $FlowFixMe,
withNullDefault: boolean,
};

export type GetSchemaInfoFN = (
property: PropAST,
types: TypeDeclarationMap,
) => ?SchemaInfo;

export type BuildSchemaFN<T> = (
property: PropAST,
types: TypeDeclarationMap,
parser: Parser,
) => ?NamedShape<T>;

/**
* This is the main interface for Parsers of various languages.
Expand Down Expand Up @@ -276,4 +314,8 @@ export interface Parser {
* @returns: a boolean specifying if the Property is optional
*/
isOptionalProperty(property: $FlowFixMe): boolean;

getGetTypeAnnotationFN(): GetTypeAnnotationFN;

getGetSchemaInfoFN(): GetSchemaInfoFN;
}
20 changes: 19 additions & 1 deletion packages/react-native-codegen/src/parsers/parserMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

'use strict';

import type {Parser} from './parser';
import type {GetSchemaInfoFN, GetTypeAnnotationFN, Parser} from './parser';
import type {ParserType} from './errors';
import type {
UnionTypeAnnotationMemberType,
Expand Down Expand Up @@ -255,4 +255,22 @@ export class MockedParser implements Parser {
isOptionalProperty(property: $FlowFixMe): boolean {
return property.optional || false;
}

getGetTypeAnnotationFN(): GetTypeAnnotationFN {
return () => {
return {};
};
}

getGetSchemaInfoFN(): GetSchemaInfoFN {
return () => {
return {
name: 'MockedSchema',
optional: false,
typeAnnotation: 'BooleanTypeAnnotation',
defaultValue: false,
withNullDefault: false,
};
};
}
}
31 changes: 31 additions & 0 deletions packages/react-native-codegen/src/parsers/parsers-commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
SchemaType,
NativeModuleEnumMap,
OptionsShape,
PropTypeAnnotation,
EventTypeAnnotation,
ObjectTypeAnnotation,
} from '../CodegenSchema.js';
Expand Down Expand Up @@ -854,6 +855,35 @@ function extendsForProp(
}
}

function buildPropSchema(
property: PropAST,
types: TypeDeclarationMap,
parser: Parser,
): ?NamedShape<PropTypeAnnotation> {
const getSchemaInfoFN = parser.getGetSchemaInfoFN();
const info = getSchemaInfoFN(property, types);
if (info == null) {
return null;
}
const {name, optional, typeAnnotation, defaultValue, withNullDefault} = info;

const getTypeAnnotationFN = parser.getGetTypeAnnotationFN();

return {
name,
optional,
typeAnnotation: getTypeAnnotationFN(
name,
typeAnnotation,
defaultValue,
withNullDefault,
types,
parser,
buildPropSchema,
),
};
}

/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
* LTI update could not be added via codemod */
function getEventArgument(
Expand Down Expand Up @@ -892,5 +922,6 @@ module.exports = {
getOptions,
getCommandTypeNameAndOptionsExpression,
extendsForProp,
buildPropSchema,
getEventArgument,
};
Loading

0 comments on commit 0212179

Please sign in to comment.