Skip to content

Commit

Permalink
[tmp] Use buildPropSchema from parsers-commons.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ken0nek committed Apr 22, 2023
1 parent a0e9e87 commit 55e12d6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 49 deletions.
29 changes: 4 additions & 25 deletions packages/react-native-codegen/src/parsers/flow/components/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,22 @@ const {
getSchemaInfo,
getTypeAnnotation,
} = require('./componentsUtils.js');
const {buildPropSchema} = require('../../parsers-commons');

import type {NamedShape, PropTypeAnnotation} from '../../../CodegenSchema.js';
import type {TypeDeclarationMap} from '../../utils';

// $FlowFixMe[unclear-type] there's no flowtype for ASTs
type PropAST = Object;

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 getProps(
typeDefinition: $ReadOnlyArray<PropAST>,
types: TypeDeclarationMap,
): $ReadOnlyArray<NamedShape<PropTypeAnnotation>> {
return flattenProperties(typeDefinition, types)
.map(property => buildPropSchema(property, types))
.map(property =>
buildPropSchema(property, types, getSchemaInfo, getTypeAnnotation),
)
.filter(Boolean);
}

Expand Down
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,
} from '../CodegenSchema.js';

import type {Parser} from './parser';
Expand Down Expand Up @@ -817,6 +818,35 @@ function propertyNames(
.filter(Boolean);
}

// $FlowFixMe[unclear-type] there's no flowtype for ASTs
type PropAST = Object;

function buildPropSchema(
property: PropAST,
types: TypeDeclarationMap,
getSchemaInfo: $FlowFixMe, // TODO:
getTypeAnnotation: $FlowFixMe, // TODO:
): ?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,
),
};
}

module.exports = {
wrapModuleSchema,
unwrapNullable,
Expand All @@ -836,4 +866,5 @@ module.exports = {
getCommandOptions,
getOptions,
getCommandTypeNameAndOptionsExpression,
buildPropSchema,
};
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ function getTypeAnnotation<T>(
name: string,
annotation: $FlowFixMe | ASTNode,
defaultValue: $FlowFixMe | void,
withNullDefault: boolean,
withNullDefault: boolean, // Just to make `getTypeAnnotation` signature match with the one from Flow
types: TypeDeclarationMap,
buildSchema: (property: PropAST, types: TypeDeclarationMap) => ?NamedShape<T>,
): $FlowFixMe {
Expand Down Expand Up @@ -436,6 +436,7 @@ type SchemaInfo = {
optional: boolean,
typeAnnotation: $FlowFixMe,
defaultValue: $FlowFixMe,
withNullDefault: boolean, // Just to make `getTypeAnnotation` signature match with the one from Flow
};

function getSchemaInfo(
Expand All @@ -461,6 +462,7 @@ function getSchemaInfo(
optional: property.optional || topLevelType.optional,
typeAnnotation: topLevelType.type,
defaultValue: topLevelType.defaultValue,
withNullDefault: false, // Just to make `getTypeAnnotation` signature match with the one from Flow
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,22 @@
*/

'use strict';
const {getSchemaInfo, getTypeAnnotation} = require('./componentsUtils.js');

import type {NamedShape, PropTypeAnnotation} from '../../../CodegenSchema.js';
import type {TypeDeclarationMap} from '../../utils';
import type {ExtendsPropsShape} from '../../../CodegenSchema.js';

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

// $FlowFixMe[unclear-type] there's no flowtype for ASTs
type PropAST = Object;

function buildPropSchema(
property: PropAST,
types: TypeDeclarationMap,
): NamedShape<PropTypeAnnotation> {
const info = getSchemaInfo(property, types);
const {name, optional, typeAnnotation, defaultValue} = info;
return {
name,
optional,
typeAnnotation: getTypeAnnotation(
name,
typeAnnotation,
defaultValue,
false, // Just to make `getTypeAnnotation` signature match with the one from Flow
types,
buildPropSchema,
),
};
}

function isEvent(typeAnnotation: $FlowFixMe): boolean {
if (typeAnnotation.type !== 'TSTypeReference') {
return false;
Expand Down Expand Up @@ -124,7 +108,9 @@ function getProps(
}

return {
props: componentPropAsts.map(property => buildPropSchema(property, types)),
props: componentPropAsts.map(property =>
buildPropSchema(property, types, getSchemaInfo, getTypeAnnotation),
),
extendsProps,
};
}
Expand Down

0 comments on commit 55e12d6

Please sign in to comment.