Skip to content

Commit

Permalink
Move language ternaries logic to FlowParser and TypeScriptParser (#35742
Browse files Browse the repository at this point in the history
)

Summary:
This is not a task from #34872 but it goes towards the same goal: removing language-specific logic from shared code and moving it to the FlowParser and TypeScriptParser.

I'm not sure about the documentation of the functions. It's been quite difficult for me to understand what the arguments are and what is returned by the functions because I find the naming quite confusing and nothing is typed.

## 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] - [Codegen] Move language ternaries logic to FlowParser and TypeScriptParser

Pull Request resolved: #35742

Test Plan: I tested using Flow and Jest commands. I made sure that tests were broken when Flow and TypeScript implementations were reversed.

Reviewed By: NickGerleman

Differential Revision: D42280934

Pulled By: rshest

fbshipit-source-id: 580ebdf424a5c179c9928ac5f9441899fb04d0c7
  • Loading branch information
AntoineDoubovetzky authored and facebook-github-bot committed Dec 30, 2022
1 parent ba6a05b commit 29a0791
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 49 deletions.
29 changes: 29 additions & 0 deletions packages/react-native-codegen/src/parsers/flow/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import type {
UnionTypeAnnotationMemberType,
SchemaType,
NamedShape,
Nullable,
NativeModuleParamTypeAnnotation,
} from '../../CodegenSchema';
import type {ParserType} from '../errors';
import type {Parser} from '../parser';
Expand Down Expand Up @@ -88,6 +91,32 @@ class FlowParser implements Parser {

return buildSchema(contents, filename, this);
}

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

module.exports = {
Expand Down
49 changes: 48 additions & 1 deletion packages/react-native-codegen/src/parsers/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@

'use strict';

import type {UnionTypeAnnotationMemberType, SchemaType} from '../CodegenSchema';
import type {
UnionTypeAnnotationMemberType,
SchemaType,
NamedShape,
Nullable,
NativeModuleParamTypeAnnotation,
} from '../CodegenSchema';
import type {ParserType} from './errors';

/**
Expand Down Expand Up @@ -77,4 +83,45 @@ export interface Parser {
* @returns: the AST of the file (given in program property for typescript).
*/
parseFile(filename: string): SchemaType;

/**
* Given a FunctionTypeAnnotation, it returns an array of its parameters.
* @parameter functionTypeAnnotation: a FunctionTypeAnnotation
* @returns: the parameters of the FunctionTypeAnnotation.
*/
getFunctionTypeAnnotationParameters(
functionTypeAnnotation: $FlowFixMe,
): $ReadOnlyArray<$FlowFixMe>;

/**
* Given a parameter, it returns the function name of the parameter.
* @parameter parameter: a parameter of a FunctionTypeAnnotation.
* @returns: the function name of the parameter.
*/
getFunctionNameFromParameter(
parameter: NamedShape<Nullable<NativeModuleParamTypeAnnotation>>,
): $FlowFixMe;

/**
* Given a parameter, it returns its name.
* @parameter parameter: a parameter of a FunctionTypeAnnotation.
* @returns: the name of the parameter.
*/
getParameterName(parameter: $FlowFixMe): string;

/**
* Given a parameter, it returns its typeAnnotation.
* @parameter parameter: a parameter of a FunctionTypeAnnotation.
* @returns: the typeAnnotation of the parameter.
*/
getParameterTypeAnnotation(param: $FlowFixMe): $FlowFixMe;

/**
* Given a FunctionTypeAnnotation, it returns its returnType.
* @parameter functionTypeAnnotation: a FunctionTypeAnnotation
* @returns: the returnType of the FunctionTypeAnnotation.
*/
getFunctionTypeAnnotationReturnType(
functionTypeAnnotation: $FlowFixMe,
): $FlowFixMe;
}
34 changes: 33 additions & 1 deletion packages/react-native-codegen/src/parsers/parserMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@

import type {Parser} from './parser';
import type {ParserType} from './errors';
import type {UnionTypeAnnotationMemberType, SchemaType} from '../CodegenSchema';
import type {
UnionTypeAnnotationMemberType,
SchemaType,
NamedShape,
Nullable,
NativeModuleParamTypeAnnotation,
} from '../CodegenSchema';

const {
UnsupportedObjectPropertyTypeAnnotationParserError,
Expand Down Expand Up @@ -82,4 +88,30 @@ export class MockedParser implements Parser {
},
};
}

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;
}
}
55 changes: 8 additions & 47 deletions packages/react-native-codegen/src/parsers/parsers-commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,49 +213,11 @@ function translateDefault(
);
}

function getTypeAnnotationParameters(
typeAnnotation: $FlowFixMe,
language: ParserType,
): $ReadOnlyArray<$FlowFixMe> {
return language === 'Flow'
? typeAnnotation.params
: typeAnnotation.parameters;
}

function getFunctionNameFromParameter(
param: NamedShape<Nullable<NativeModuleParamTypeAnnotation>>,
language: ParserType,
): $FlowFixMe {
return language === 'Flow' ? param.name : param.typeAnnotation;
}

function getParameterName(param: $FlowFixMe, language: ParserType): string {
return language === 'Flow' ? param.name.name : param.name;
}

function getParameterTypeAnnotation(
param: $FlowFixMe,
language: ParserType,
): $FlowFixMe {
return language === 'Flow'
? param.typeAnnotation
: param.typeAnnotation.typeAnnotation;
}

function getTypeAnnotationReturnType(
typeAnnotation: $FlowFixMe,
language: ParserType,
): $FlowFixMe {
return language === 'Flow'
? typeAnnotation.returnType
: typeAnnotation.typeAnnotation.typeAnnotation;
}

function translateFunctionTypeAnnotation(
hasteModuleName: string,
// TODO(T108222691): Use flow-types for @babel/parser
// TODO(T71778680): This is a FunctionTypeAnnotation. Type this.
typeAnnotation: $FlowFixMe,
functionTypeAnnotation: $FlowFixMe,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
tryParse: ParserErrorCapturer,
Expand All @@ -266,22 +228,21 @@ function translateFunctionTypeAnnotation(
type Param = NamedShape<Nullable<NativeModuleParamTypeAnnotation>>;
const params: Array<Param> = [];

for (const param of getTypeAnnotationParameters(
typeAnnotation,
parser.language(),
for (const param of parser.getFunctionTypeAnnotationParameters(
functionTypeAnnotation,
)) {
const parsedParam = tryParse(() => {
if (getFunctionNameFromParameter(param, parser.language()) == null) {
if (parser.getFunctionNameFromParameter(param) == null) {
throw new UnnamedFunctionParamParserError(param, hasteModuleName);
}

const paramName = getParameterName(param, parser.language());
const paramName = parser.getParameterName(param);

const [paramTypeAnnotation, isParamTypeAnnotationNullable] =
unwrapNullable<$FlowFixMe>(
translateTypeAnnotation(
hasteModuleName,
getParameterTypeAnnotation(param, parser.language()),
parser.getParameterTypeAnnotation(param),
types,
aliasMap,
tryParse,
Expand Down Expand Up @@ -321,7 +282,7 @@ function translateFunctionTypeAnnotation(
unwrapNullable<$FlowFixMe>(
translateTypeAnnotation(
hasteModuleName,
getTypeAnnotationReturnType(typeAnnotation, parser.language()),
parser.getFunctionTypeAnnotationReturnType(functionTypeAnnotation),
types,
aliasMap,
tryParse,
Expand All @@ -332,7 +293,7 @@ function translateFunctionTypeAnnotation(

throwIfUnsupportedFunctionReturnTypeAnnotationParserError(
hasteModuleName,
typeAnnotation,
functionTypeAnnotation,
'FunctionTypeAnnotation',
cxxOnly,
returnTypeAnnotation.type,
Expand Down
29 changes: 29 additions & 0 deletions packages/react-native-codegen/src/parsers/typescript/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import type {
UnionTypeAnnotationMemberType,
SchemaType,
NamedShape,
Nullable,
NativeModuleParamTypeAnnotation,
} from '../../CodegenSchema';
import type {ParserType} from '../errors';
import type {Parser} from '../parser';
Expand Down Expand Up @@ -94,6 +97,32 @@ class TypeScriptParser implements Parser {

return buildSchema(contents, filename, this);
}

getFunctionTypeAnnotationParameters(
functionTypeAnnotation: $FlowFixMe,
): $ReadOnlyArray<$FlowFixMe> {
return functionTypeAnnotation.parameters;
}

getFunctionNameFromParameter(
parameter: NamedShape<Nullable<NativeModuleParamTypeAnnotation>>,
): $FlowFixMe {
return parameter.typeAnnotation;
}

getParameterName(parameter: $FlowFixMe): string {
return parameter.name;
}

getParameterTypeAnnotation(parameter: $FlowFixMe): $FlowFixMe {
return parameter.typeAnnotation.typeAnnotation;
}

getFunctionTypeAnnotationReturnType(
functionTypeAnnotation: $FlowFixMe,
): $FlowFixMe {
return functionTypeAnnotation.typeAnnotation.typeAnnotation;
}
}
module.exports = {
TypeScriptParser,
Expand Down

0 comments on commit 29a0791

Please sign in to comment.