Files
react-native/packages/react-native-codegen/src/parsers/parserMock.js
T
Pranav Yadav 05454fa866 Merge Parse-Module-Name anon fn of Flow & TS parsers' (#36297)
Summary:
Part of Umbrella https://github.com/facebook/react-native/issues/34872
> [**Codegen 84** - assigned to Pranav-yadav] It depends on [Codegen 83] export the parseModuleName anonymous function (Flow, TypeScript) in a common parseModuleName function in the parsers-commons.js file.

- merged Parse Module-Name _**anon**_ fn of `Flow` & `TS` parsers; into a common `parseModuleName` fn in the `parsers-commons.js`
- added **tests** for `parseModuleName` fn from `parsers-commons.js`
- added `callExpressionTypeParameters` method to **_parsers_**
- added **tests** for `callExpressionTypeParameters` method of _parsers_
- used `parser.callExpressionTypeParameters` method in `parseModuleName` fn

PS: fixed merge conflicts several times :(

Overall :)

## Changelog

[INTERNAL] [CHANGED] - Merge Parse-Module-Name anon fn of `Flow` & `TS` and add `callExpressionTypeParameters` method to **_parsers_**

Pull Request resolved: https://github.com/facebook/react-native/pull/36297

Test Plan: - `yarn lint && yarn run flow && yarn test react-native-codegen`  ==> ✅

Reviewed By: rshest

Differential Revision: D43694563

Pulled By: cipolleschi

fbshipit-source-id: 99cf40ada0a567cd9ff91078f66fd4ac3684f7cc
2023-03-02 08:28:49 -08:00

188 lines
4.3 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 {Parser} from './parser';
import type {ParserType} from './errors';
import type {
UnionTypeAnnotationMemberType,
SchemaType,
NamedShape,
Nullable,
NativeModuleParamTypeAnnotation,
NativeModuleEnumMemberType,
NativeModuleEnumMembers,
} from '../CodegenSchema';
import type {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'
);
}
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;
}
}