Skip to content

Commit

Permalink
Wrap buildPropSchema to pass the right parameters based on @cipolle…
Browse files Browse the repository at this point in the history
…schi's suggestion
  • Loading branch information
ken0nek committed Apr 26, 2023
1 parent 5c96365 commit 4eed951
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@

'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';

function getProperties(
typeName: string,
Expand Down
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 @@ -23,36 +23,13 @@ const {
getSchemaInfo,
getTypeAnnotation,
} = 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 +94,9 @@ function getProps(
} {
const nonExtendsProps = removeKnownExtends(typeDefinition, types, parser);
const props = flattenProperties(nonExtendsProps, types)
.map(property => buildPropSchema(property, types))
.map(property =>
buildPropSchema(property, types, getSchemaInfo, getTypeAnnotation),
)
.filter(Boolean);

return {
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
57 changes: 56 additions & 1 deletion packages/react-native-codegen/src/parsers/parsers-commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ import type {
SchemaType,
NativeModuleEnumMap,
OptionsShape,
PropTypeAnnotation,
} from '../CodegenSchema.js';

import type {Parser} from './parser';
import type {ParserType} from './errors';
import type {ParserErrorCapturer, TypeDeclarationMap, PropAST} from './utils';
import type {
ParserErrorCapturer,
TypeDeclarationMap,
PropAST,
ASTNode,
} from './utils';
import type {ComponentSchemaBuilderConfig} from './schema.js';

const {
Expand Down Expand Up @@ -852,6 +858,54 @@ function extendsForProp(
}
}

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

function buildPropSchema(
property: PropAST,
types: TypeDeclarationMap,
getSchemaInfo: (property: PropAST, types: TypeDeclarationMap) => ?SchemaInfo,
getTypeAnnotation: (
name: string,
annotation: $FlowFixMe | ASTNode,
defaultValue: $FlowFixMe | void,
withNullDefault: boolean,
types: TypeDeclarationMap,
buildSchema: (property: PropAST, types: TypeDeclarationMap) => $FlowFixMe,
) => $FlowFixMe,
): ?NamedShape<PropTypeAnnotation> {
const info = getSchemaInfo(property, types);
if (info == null) {
return null;
}
const {name, optional, typeAnnotation, defaultValue, withNullDefault} = info;

const wrappedBuildSchema = (
passedProps: PropAST,
passedTypes: TypeDeclarationMap,
): $FlowFixMe => {
buildPropSchema(passedProps, passedTypes, getSchemaInfo, getTypeAnnotation);
};

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

module.exports = {
wrapModuleSchema,
unwrapNullable,
Expand All @@ -872,4 +926,5 @@ module.exports = {
getOptions,
getCommandTypeNameAndOptionsExpression,
extendsForProp,
buildPropSchema,
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
*/

'use strict';
import type {ASTNode} from '../utils';
import type {NamedShape} from '../../../CodegenSchema.js';
const {
parseTopLevelType,
flattenIntersectionType,
} = require('../parseTopLevelType');
import type {TypeDeclarationMap, PropAST} from '../../utils';
import type {TypeDeclarationMap, PropAST, ASTNode} from '../../utils';

function getProperties(
typeName: string,
Expand Down Expand Up @@ -370,7 +369,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 +435,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 +461,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 @@ -19,27 +19,7 @@ import type {Parser} from '../../parser';

const {flattenProperties} = require('./componentsUtils.js');
const {parseTopLevelType} = require('../parseTopLevelType');
const {extendsForProp} = require('../../parsers-commons');

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,
),
};
}
const {buildPropSchema, extendsForProp} = require('../../parsers-commons');

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

return {
props: componentPropAsts.map(property => buildPropSchema(property, types)),
props: componentPropAsts.map(property =>
buildPropSchema(property, types, getSchemaInfo, getTypeAnnotation),
),
extendsProps,
};
}
Expand Down
3 changes: 0 additions & 3 deletions packages/react-native-codegen/src/parsers/typescript/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import type {TypeResolutionStatus, TypeDeclarationMap} from '../utils';

const {parseTopLevelType} = require('./parseTopLevelType');

// $FlowFixMe[unclear-type] Use flow-types for @babel/parser
export type ASTNode = Object;

const invariant = require('invariant');

function resolveTypeAnnotation(
Expand Down
3 changes: 3 additions & 0 deletions packages/react-native-codegen/src/parsers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export type ParserErrorCapturer = <T>(fn: () => T) => ?T;
// $FlowFixMe[unclear-type] there's no flowtype for ASTs
export type PropAST = Object;

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

function createParserErrorCapturer(): [
Array<ParserError>,
ParserErrorCapturer,
Expand Down

0 comments on commit 4eed951

Please sign in to comment.