Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Codegen 86] Use buildPropSchema from parser-commons #37043

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error ----------------------------------- packages/react-native-codegen/src/parsers/typescript/components/props.js:112:7

Cannot return object literal because null or undefined [1] is incompatible with `NamedShape` [2] in array element of
property `props`. [incompatible-return]

   packages/react-native-codegen/src/parsers/typescript/components/props.js:112:7
   112|       buildPropSchema(property, types, getSchemaInfo, getTypeAnnotation),
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

References:
   packages/react-native-codegen/src/parsers/parsers-commons.js:845:4
   845| ): ?NamedShape<PropTypeAnnotation> {
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [1]
   packages/react-native-codegen/src/parsers/typescript/components/props.js:74:25
    74|   props: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [2]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const info = getSchemaInfo(property, types);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have to handle the nullability of getSchemaInfo

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,6 +369,7 @@ function getTypeAnnotation<T>(
name: string,
annotation: $FlowFixMe | ASTNode,
defaultValue: $FlowFixMe | void,
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 @@ -435,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 @@ -460,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,26 +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,
types,
buildPropSchema,
),
};
}
const {buildPropSchema, extendsForProp} = require('../../parsers-commons');

function isEvent(typeAnnotation: $FlowFixMe): boolean {
if (typeAnnotation.type !== 'TSTypeReference') {
Expand Down Expand Up @@ -101,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