Skip to content

Commit 29a0791

Browse files
Antoine Doubovetzkyfacebook-github-bot
Antoine Doubovetzky
authored andcommitted
Move language ternaries logic to FlowParser and TypeScriptParser (#35742)
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
1 parent ba6a05b commit 29a0791

File tree

5 files changed

+147
-49
lines changed

5 files changed

+147
-49
lines changed

packages/react-native-codegen/src/parsers/flow/parser.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
import type {
1414
UnionTypeAnnotationMemberType,
1515
SchemaType,
16+
NamedShape,
17+
Nullable,
18+
NativeModuleParamTypeAnnotation,
1619
} from '../../CodegenSchema';
1720
import type {ParserType} from '../errors';
1821
import type {Parser} from '../parser';
@@ -88,6 +91,32 @@ class FlowParser implements Parser {
8891

8992
return buildSchema(contents, filename, this);
9093
}
94+
95+
getFunctionTypeAnnotationParameters(
96+
functionTypeAnnotation: $FlowFixMe,
97+
): $ReadOnlyArray<$FlowFixMe> {
98+
return functionTypeAnnotation.params;
99+
}
100+
101+
getFunctionNameFromParameter(
102+
parameter: NamedShape<Nullable<NativeModuleParamTypeAnnotation>>,
103+
): $FlowFixMe {
104+
return parameter.name;
105+
}
106+
107+
getParameterName(parameter: $FlowFixMe): string {
108+
return parameter.name.name;
109+
}
110+
111+
getParameterTypeAnnotation(parameter: $FlowFixMe): $FlowFixMe {
112+
return parameter.typeAnnotation;
113+
}
114+
115+
getFunctionTypeAnnotationReturnType(
116+
functionTypeAnnotation: $FlowFixMe,
117+
): $FlowFixMe {
118+
return functionTypeAnnotation.returnType;
119+
}
91120
}
92121

93122
module.exports = {

packages/react-native-codegen/src/parsers/parser.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010

1111
'use strict';
1212

13-
import type {UnionTypeAnnotationMemberType, SchemaType} from '../CodegenSchema';
13+
import type {
14+
UnionTypeAnnotationMemberType,
15+
SchemaType,
16+
NamedShape,
17+
Nullable,
18+
NativeModuleParamTypeAnnotation,
19+
} from '../CodegenSchema';
1420
import type {ParserType} from './errors';
1521

1622
/**
@@ -77,4 +83,45 @@ export interface Parser {
7783
* @returns: the AST of the file (given in program property for typescript).
7884
*/
7985
parseFile(filename: string): SchemaType;
86+
87+
/**
88+
* Given a FunctionTypeAnnotation, it returns an array of its parameters.
89+
* @parameter functionTypeAnnotation: a FunctionTypeAnnotation
90+
* @returns: the parameters of the FunctionTypeAnnotation.
91+
*/
92+
getFunctionTypeAnnotationParameters(
93+
functionTypeAnnotation: $FlowFixMe,
94+
): $ReadOnlyArray<$FlowFixMe>;
95+
96+
/**
97+
* Given a parameter, it returns the function name of the parameter.
98+
* @parameter parameter: a parameter of a FunctionTypeAnnotation.
99+
* @returns: the function name of the parameter.
100+
*/
101+
getFunctionNameFromParameter(
102+
parameter: NamedShape<Nullable<NativeModuleParamTypeAnnotation>>,
103+
): $FlowFixMe;
104+
105+
/**
106+
* Given a parameter, it returns its name.
107+
* @parameter parameter: a parameter of a FunctionTypeAnnotation.
108+
* @returns: the name of the parameter.
109+
*/
110+
getParameterName(parameter: $FlowFixMe): string;
111+
112+
/**
113+
* Given a parameter, it returns its typeAnnotation.
114+
* @parameter parameter: a parameter of a FunctionTypeAnnotation.
115+
* @returns: the typeAnnotation of the parameter.
116+
*/
117+
getParameterTypeAnnotation(param: $FlowFixMe): $FlowFixMe;
118+
119+
/**
120+
* Given a FunctionTypeAnnotation, it returns its returnType.
121+
* @parameter functionTypeAnnotation: a FunctionTypeAnnotation
122+
* @returns: the returnType of the FunctionTypeAnnotation.
123+
*/
124+
getFunctionTypeAnnotationReturnType(
125+
functionTypeAnnotation: $FlowFixMe,
126+
): $FlowFixMe;
80127
}

packages/react-native-codegen/src/parsers/parserMock.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212

1313
import type {Parser} from './parser';
1414
import type {ParserType} from './errors';
15-
import type {UnionTypeAnnotationMemberType, SchemaType} from '../CodegenSchema';
15+
import type {
16+
UnionTypeAnnotationMemberType,
17+
SchemaType,
18+
NamedShape,
19+
Nullable,
20+
NativeModuleParamTypeAnnotation,
21+
} from '../CodegenSchema';
1622

1723
const {
1824
UnsupportedObjectPropertyTypeAnnotationParserError,
@@ -82,4 +88,30 @@ export class MockedParser implements Parser {
8288
},
8389
};
8490
}
91+
92+
getFunctionTypeAnnotationParameters(
93+
functionTypeAnnotation: $FlowFixMe,
94+
): $ReadOnlyArray<$FlowFixMe> {
95+
return functionTypeAnnotation.params;
96+
}
97+
98+
getFunctionNameFromParameter(
99+
parameter: NamedShape<Nullable<NativeModuleParamTypeAnnotation>>,
100+
): $FlowFixMe {
101+
return parameter.name;
102+
}
103+
104+
getParameterName(parameter: $FlowFixMe): string {
105+
return parameter.name.name;
106+
}
107+
108+
getParameterTypeAnnotation(parameter: $FlowFixMe): $FlowFixMe {
109+
return parameter.typeAnnotation;
110+
}
111+
112+
getFunctionTypeAnnotationReturnType(
113+
functionTypeAnnotation: $FlowFixMe,
114+
): $FlowFixMe {
115+
return functionTypeAnnotation.returnType;
116+
}
85117
}

packages/react-native-codegen/src/parsers/parsers-commons.js

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -213,49 +213,11 @@ function translateDefault(
213213
);
214214
}
215215

216-
function getTypeAnnotationParameters(
217-
typeAnnotation: $FlowFixMe,
218-
language: ParserType,
219-
): $ReadOnlyArray<$FlowFixMe> {
220-
return language === 'Flow'
221-
? typeAnnotation.params
222-
: typeAnnotation.parameters;
223-
}
224-
225-
function getFunctionNameFromParameter(
226-
param: NamedShape<Nullable<NativeModuleParamTypeAnnotation>>,
227-
language: ParserType,
228-
): $FlowFixMe {
229-
return language === 'Flow' ? param.name : param.typeAnnotation;
230-
}
231-
232-
function getParameterName(param: $FlowFixMe, language: ParserType): string {
233-
return language === 'Flow' ? param.name.name : param.name;
234-
}
235-
236-
function getParameterTypeAnnotation(
237-
param: $FlowFixMe,
238-
language: ParserType,
239-
): $FlowFixMe {
240-
return language === 'Flow'
241-
? param.typeAnnotation
242-
: param.typeAnnotation.typeAnnotation;
243-
}
244-
245-
function getTypeAnnotationReturnType(
246-
typeAnnotation: $FlowFixMe,
247-
language: ParserType,
248-
): $FlowFixMe {
249-
return language === 'Flow'
250-
? typeAnnotation.returnType
251-
: typeAnnotation.typeAnnotation.typeAnnotation;
252-
}
253-
254216
function translateFunctionTypeAnnotation(
255217
hasteModuleName: string,
256218
// TODO(T108222691): Use flow-types for @babel/parser
257219
// TODO(T71778680): This is a FunctionTypeAnnotation. Type this.
258-
typeAnnotation: $FlowFixMe,
220+
functionTypeAnnotation: $FlowFixMe,
259221
types: TypeDeclarationMap,
260222
aliasMap: {...NativeModuleAliasMap},
261223
tryParse: ParserErrorCapturer,
@@ -266,22 +228,21 @@ function translateFunctionTypeAnnotation(
266228
type Param = NamedShape<Nullable<NativeModuleParamTypeAnnotation>>;
267229
const params: Array<Param> = [];
268230

269-
for (const param of getTypeAnnotationParameters(
270-
typeAnnotation,
271-
parser.language(),
231+
for (const param of parser.getFunctionTypeAnnotationParameters(
232+
functionTypeAnnotation,
272233
)) {
273234
const parsedParam = tryParse(() => {
274-
if (getFunctionNameFromParameter(param, parser.language()) == null) {
235+
if (parser.getFunctionNameFromParameter(param) == null) {
275236
throw new UnnamedFunctionParamParserError(param, hasteModuleName);
276237
}
277238

278-
const paramName = getParameterName(param, parser.language());
239+
const paramName = parser.getParameterName(param);
279240

280241
const [paramTypeAnnotation, isParamTypeAnnotationNullable] =
281242
unwrapNullable<$FlowFixMe>(
282243
translateTypeAnnotation(
283244
hasteModuleName,
284-
getParameterTypeAnnotation(param, parser.language()),
245+
parser.getParameterTypeAnnotation(param),
285246
types,
286247
aliasMap,
287248
tryParse,
@@ -321,7 +282,7 @@ function translateFunctionTypeAnnotation(
321282
unwrapNullable<$FlowFixMe>(
322283
translateTypeAnnotation(
323284
hasteModuleName,
324-
getTypeAnnotationReturnType(typeAnnotation, parser.language()),
285+
parser.getFunctionTypeAnnotationReturnType(functionTypeAnnotation),
325286
types,
326287
aliasMap,
327288
tryParse,
@@ -332,7 +293,7 @@ function translateFunctionTypeAnnotation(
332293

333294
throwIfUnsupportedFunctionReturnTypeAnnotationParserError(
334295
hasteModuleName,
335-
typeAnnotation,
296+
functionTypeAnnotation,
336297
'FunctionTypeAnnotation',
337298
cxxOnly,
338299
returnTypeAnnotation.type,

packages/react-native-codegen/src/parsers/typescript/parser.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
import type {
1414
UnionTypeAnnotationMemberType,
1515
SchemaType,
16+
NamedShape,
17+
Nullable,
18+
NativeModuleParamTypeAnnotation,
1619
} from '../../CodegenSchema';
1720
import type {ParserType} from '../errors';
1821
import type {Parser} from '../parser';
@@ -94,6 +97,32 @@ class TypeScriptParser implements Parser {
9497

9598
return buildSchema(contents, filename, this);
9699
}
100+
101+
getFunctionTypeAnnotationParameters(
102+
functionTypeAnnotation: $FlowFixMe,
103+
): $ReadOnlyArray<$FlowFixMe> {
104+
return functionTypeAnnotation.parameters;
105+
}
106+
107+
getFunctionNameFromParameter(
108+
parameter: NamedShape<Nullable<NativeModuleParamTypeAnnotation>>,
109+
): $FlowFixMe {
110+
return parameter.typeAnnotation;
111+
}
112+
113+
getParameterName(parameter: $FlowFixMe): string {
114+
return parameter.name;
115+
}
116+
117+
getParameterTypeAnnotation(parameter: $FlowFixMe): $FlowFixMe {
118+
return parameter.typeAnnotation.typeAnnotation;
119+
}
120+
121+
getFunctionTypeAnnotationReturnType(
122+
functionTypeAnnotation: $FlowFixMe,
123+
): $FlowFixMe {
124+
return functionTypeAnnotation.typeAnnotation.typeAnnotation;
125+
}
97126
}
98127
module.exports = {
99128
TypeScriptParser,

0 commit comments

Comments
 (0)