mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
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 https://github.com/facebook/react-native/issues/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: https://github.com/facebook/react-native/issues/34872#issuecomment-1517519254 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: https://github.com/facebook/react-native/pull/37043 Test Plan: - [ ] `yarn jest react-native-codegen` pass Reviewed By: rshest Differential Revision: D45209982 Pulled By: cipolleschi fbshipit-source-id: c241bc0542ba662c965d70d1dc283f48541e14ea
277 lines
6.4 KiB
JavaScript
277 lines
6.4 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow strict
|
|
* @format
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
import type {GetSchemaInfoFN, GetTypeAnnotationFN, Parser} from './parser';
|
|
import type {ParserType} from './errors';
|
|
import type {
|
|
UnionTypeAnnotationMemberType,
|
|
SchemaType,
|
|
NamedShape,
|
|
Nullable,
|
|
NativeModuleParamTypeAnnotation,
|
|
NativeModuleEnumMemberType,
|
|
NativeModuleEnumMembers,
|
|
NativeModuleAliasMap,
|
|
NativeModuleEnumMap,
|
|
} from '../CodegenSchema';
|
|
import type {ParserErrorCapturer, PropAST, TypeDeclarationMap} from './utils';
|
|
|
|
// $FlowFixMe[untyped-import] there's no flowtype flow-parser
|
|
const flowParser = require('flow-parser');
|
|
const {
|
|
UnsupportedObjectPropertyTypeAnnotationParserError,
|
|
} = require('./errors');
|
|
|
|
const schemaMock = {
|
|
modules: {
|
|
StringPropNativeComponentView: {
|
|
type: 'Component',
|
|
components: {
|
|
StringPropNativeComponentView: {
|
|
extendsProps: [],
|
|
events: [],
|
|
props: [],
|
|
commands: [],
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
export class MockedParser implements Parser {
|
|
typeParameterInstantiation: string = 'TypeParameterInstantiation';
|
|
|
|
isProperty(property: $FlowFixMe): boolean {
|
|
return property.type === 'ObjectTypeProperty';
|
|
}
|
|
|
|
getKeyName(property: $FlowFixMe, hasteModuleName: string): string {
|
|
if (!this.isProperty(property)) {
|
|
throw new UnsupportedObjectPropertyTypeAnnotationParserError(
|
|
hasteModuleName,
|
|
property,
|
|
property.type,
|
|
this.language(),
|
|
);
|
|
}
|
|
return property.key.name;
|
|
}
|
|
|
|
language(): ParserType {
|
|
return 'Flow';
|
|
}
|
|
|
|
nameForGenericTypeAnnotation(typeAnnotation: $FlowFixMe): string {
|
|
return typeAnnotation.id.name;
|
|
}
|
|
|
|
checkIfInvalidModule(typeArguments: $FlowFixMe): boolean {
|
|
return false;
|
|
}
|
|
|
|
remapUnionTypeAnnotationMemberNames(
|
|
membersTypes: $FlowFixMe[],
|
|
): UnionTypeAnnotationMemberType[] {
|
|
return [];
|
|
}
|
|
|
|
parseFile(filename: string): SchemaType {
|
|
return schemaMock;
|
|
}
|
|
|
|
parseString(contents: string, filename: ?string): SchemaType {
|
|
return schemaMock;
|
|
}
|
|
|
|
parseModuleFixture(filename: string): SchemaType {
|
|
return schemaMock;
|
|
}
|
|
|
|
getAst(contents: string): $FlowFixMe {
|
|
return flowParser.parse(contents, {
|
|
enums: true,
|
|
});
|
|
}
|
|
|
|
getFunctionTypeAnnotationParameters(
|
|
functionTypeAnnotation: $FlowFixMe,
|
|
): $ReadOnlyArray<$FlowFixMe> {
|
|
return functionTypeAnnotation.params;
|
|
}
|
|
|
|
getFunctionNameFromParameter(
|
|
parameter: NamedShape<Nullable<NativeModuleParamTypeAnnotation>>,
|
|
): $FlowFixMe {
|
|
return parameter.name;
|
|
}
|
|
|
|
getParameterName(parameter: $FlowFixMe): string {
|
|
return parameter.name.name;
|
|
}
|
|
|
|
getParameterTypeAnnotation(parameter: $FlowFixMe): $FlowFixMe {
|
|
return parameter.typeAnnotation;
|
|
}
|
|
|
|
getFunctionTypeAnnotationReturnType(
|
|
functionTypeAnnotation: $FlowFixMe,
|
|
): $FlowFixMe {
|
|
return functionTypeAnnotation.returnType;
|
|
}
|
|
|
|
parseEnumMembersType(typeAnnotation: $FlowFixMe): NativeModuleEnumMemberType {
|
|
return typeAnnotation.type;
|
|
}
|
|
|
|
validateEnumMembersSupported(
|
|
typeAnnotation: $FlowFixMe,
|
|
enumMembersType: NativeModuleEnumMemberType,
|
|
): void {
|
|
return;
|
|
}
|
|
|
|
parseEnumMembers(typeAnnotation: $FlowFixMe): NativeModuleEnumMembers {
|
|
return typeAnnotation.type === 'StringTypeAnnotation'
|
|
? [
|
|
{
|
|
name: 'Hello',
|
|
value: 'hello',
|
|
},
|
|
{
|
|
name: 'Goodbye',
|
|
value: 'goodbye',
|
|
},
|
|
]
|
|
: [
|
|
{
|
|
name: 'On',
|
|
value: '1',
|
|
},
|
|
{
|
|
name: 'Off',
|
|
value: '0',
|
|
},
|
|
];
|
|
}
|
|
|
|
isModuleInterface(node: $FlowFixMe): boolean {
|
|
return (
|
|
node.type === 'InterfaceDeclaration' &&
|
|
node.extends.length === 1 &&
|
|
node.extends[0].type === 'InterfaceExtends' &&
|
|
node.extends[0].id.name === 'TurboModule'
|
|
);
|
|
}
|
|
|
|
isGenericTypeAnnotation(type: $FlowFixMe): boolean {
|
|
return true;
|
|
}
|
|
|
|
extractAnnotatedElement(
|
|
typeAnnotation: $FlowFixMe,
|
|
types: TypeDeclarationMap,
|
|
): $FlowFixMe {
|
|
return types[typeAnnotation.typeParameters.params[0].id.name];
|
|
}
|
|
|
|
getTypes(ast: $FlowFixMe): TypeDeclarationMap {
|
|
return {};
|
|
}
|
|
|
|
callExpressionTypeParameters(callExpression: $FlowFixMe): $FlowFixMe | null {
|
|
return callExpression.typeArguments || null;
|
|
}
|
|
|
|
computePartialProperties(
|
|
properties: Array<$FlowFixMe>,
|
|
hasteModuleName: string,
|
|
types: TypeDeclarationMap,
|
|
aliasMap: {...NativeModuleAliasMap},
|
|
enumMap: {...NativeModuleEnumMap},
|
|
tryParse: ParserErrorCapturer,
|
|
cxxOnly: boolean,
|
|
): Array<$FlowFixMe> {
|
|
return [
|
|
{
|
|
name: 'a',
|
|
optional: true,
|
|
typeAnnotation: {type: 'StringTypeAnnotation'},
|
|
},
|
|
{
|
|
name: 'b',
|
|
optional: true,
|
|
typeAnnotation: {type: 'BooleanTypeAnnotation'},
|
|
},
|
|
];
|
|
}
|
|
|
|
functionTypeAnnotation(propertyValueType: string): boolean {
|
|
return propertyValueType === 'FunctionTypeAnnotation';
|
|
}
|
|
|
|
getTypeArgumentParamsFromDeclaration(declaration: $FlowFixMe): $FlowFixMe {
|
|
return declaration.typeArguments.params;
|
|
}
|
|
|
|
getNativeComponentType(
|
|
typeArgumentParams: $FlowFixMe,
|
|
funcArgumentParams: $FlowFixMe,
|
|
): {[string]: string} {
|
|
return {
|
|
propsTypeName: typeArgumentParams[0].id.name,
|
|
componentName: funcArgumentParams[0].value,
|
|
};
|
|
}
|
|
|
|
getAnnotatedElementProperties(annotatedElement: $FlowFixMe): $FlowFixMe {
|
|
return annotatedElement.right.properties;
|
|
}
|
|
|
|
bodyProperties(typeAlias: $FlowFixMe): $ReadOnlyArray<$FlowFixMe> {
|
|
return typeAlias.body.properties;
|
|
}
|
|
|
|
convertKeywordToTypeAnnotation(keyword: string): string {
|
|
return keyword;
|
|
}
|
|
|
|
argumentForProp(prop: PropAST): $FlowFixMe {
|
|
return prop.expression;
|
|
}
|
|
|
|
nameForArgument(prop: PropAST): $FlowFixMe {
|
|
return prop.expression.name;
|
|
}
|
|
|
|
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,
|
|
};
|
|
};
|
|
}
|
|
}
|