Skip to content

Commit ff7c875

Browse files
chore: Extract translateFunctionTypeAnnotation into a common function
1 parent 46de03a commit ff7c875

File tree

3 files changed

+158
-192
lines changed

3 files changed

+158
-192
lines changed

packages/react-native-codegen/src/parsers/flow/modules/index.js

Lines changed: 5 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const {
5151
emitString,
5252
emitStringish,
5353
typeAliasResolution,
54+
translateFunctionTypeAnnotation,
5455
} = require('../../parsers-primitives');
5556

5657
const {
@@ -355,6 +356,8 @@ function translateTypeAnnotation(
355356
aliasMap,
356357
tryParse,
357358
cxxOnly,
359+
translateTypeAnnotation,
360+
language,
358361
);
359362
return emitFunction(nullable, translateFunctionTypeAnnotationValue);
360363
}
@@ -385,98 +388,6 @@ function translateTypeAnnotation(
385388
}
386389
}
387390

388-
function translateFunctionTypeAnnotation(
389-
hasteModuleName: string,
390-
// TODO(T71778680): This is a FunctionTypeAnnotation. Type this.
391-
flowFunctionTypeAnnotation: $FlowFixMe,
392-
types: TypeDeclarationMap,
393-
aliasMap: {...NativeModuleAliasMap},
394-
tryParse: ParserErrorCapturer,
395-
cxxOnly: boolean,
396-
): NativeModuleFunctionTypeAnnotation {
397-
type Param = NamedShape<Nullable<NativeModuleParamTypeAnnotation>>;
398-
const params: Array<Param> = [];
399-
400-
for (const flowParam of (flowFunctionTypeAnnotation.params: $ReadOnlyArray<$FlowFixMe>)) {
401-
const parsedParam = tryParse(() => {
402-
if (flowParam.name == null) {
403-
throw new UnnamedFunctionParamParserError(
404-
flowParam,
405-
hasteModuleName,
406-
language,
407-
);
408-
}
409-
410-
const paramName = flowParam.name.name;
411-
const [paramTypeAnnotation, isParamTypeAnnotationNullable] =
412-
unwrapNullable(
413-
translateTypeAnnotation(
414-
hasteModuleName,
415-
flowParam.typeAnnotation,
416-
types,
417-
aliasMap,
418-
tryParse,
419-
cxxOnly,
420-
),
421-
);
422-
423-
if (
424-
paramTypeAnnotation.type === 'VoidTypeAnnotation' ||
425-
paramTypeAnnotation.type === 'PromiseTypeAnnotation'
426-
) {
427-
return throwIfUnsupportedFunctionParamTypeAnnotationParserError(
428-
hasteModuleName,
429-
flowParam.typeAnnotation,
430-
paramName,
431-
paramTypeAnnotation.type,
432-
);
433-
}
434-
435-
return {
436-
name: flowParam.name.name,
437-
optional: flowParam.optional,
438-
typeAnnotation: wrapNullable(
439-
isParamTypeAnnotationNullable,
440-
paramTypeAnnotation,
441-
),
442-
};
443-
});
444-
445-
if (parsedParam != null) {
446-
params.push(parsedParam);
447-
}
448-
}
449-
450-
const [returnTypeAnnotation, isReturnTypeAnnotationNullable] = unwrapNullable(
451-
translateTypeAnnotation(
452-
hasteModuleName,
453-
flowFunctionTypeAnnotation.returnType,
454-
types,
455-
aliasMap,
456-
tryParse,
457-
cxxOnly,
458-
),
459-
);
460-
461-
throwIfUnsupportedFunctionReturnTypeAnnotationParserError(
462-
hasteModuleName,
463-
flowFunctionTypeAnnotation,
464-
'FunctionTypeAnnotation',
465-
language,
466-
cxxOnly,
467-
returnTypeAnnotation.type,
468-
);
469-
470-
return {
471-
type: 'FunctionTypeAnnotation',
472-
returnTypeAnnotation: wrapNullable(
473-
isReturnTypeAnnotationNullable,
474-
returnTypeAnnotation,
475-
),
476-
params,
477-
};
478-
}
479-
480391
function buildPropertySchema(
481392
hasteModuleName: string,
482393
// TODO(T71778680): This is an ObjectTypeProperty containing either:
@@ -516,6 +427,8 @@ function buildPropertySchema(
516427
aliasMap,
517428
tryParse,
518429
cxxOnly,
430+
translateTypeAnnotation,
431+
language,
519432
),
520433
),
521434
};

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

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,25 @@ import type {
2727
StringTypeAnnotation,
2828
VoidTypeAnnotation,
2929
NativeModuleFloatTypeAnnotation,
30+
NativeModuleParamTypeAnnotation,
31+
NamedShape,
3032
} from '../CodegenSchema';
3133
import type {ParserType} from './errors';
32-
import type {TypeAliasResolutionStatus} from './utils';
34+
import type {
35+
ParserErrorCapturer,
36+
TypeAliasResolutionStatus,
37+
TypeDeclarationMap,
38+
} from './utils';
39+
40+
const {UnnamedFunctionParamParserError} = require('./errors');
3341

3442
const {
43+
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
44+
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
45+
} = require('./error-utils');
46+
47+
const {
48+
unwrapNullable,
3549
wrapNullable,
3650
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
3751
} = require('./parsers-commons');
@@ -180,6 +194,137 @@ function emitFloat(
180194
});
181195
}
182196

197+
function getTypeAnnotationParameters(
198+
typeAnnotation: $FlowFixMe,
199+
language: ParserType,
200+
): $ReadOnlyArray<$FlowFixMe> {
201+
return language === 'Flow'
202+
? typeAnnotation.params
203+
: typeAnnotation.parameters;
204+
}
205+
206+
function getFunctionNameFromParameter(
207+
param: NamedShape<Nullable<NativeModuleParamTypeAnnotation>>,
208+
language: ParserType,
209+
) {
210+
return language === 'Flow' ? param.name : param.typeAnnotation;
211+
}
212+
213+
function getParameterName(param: $FlowFixMe, language: ParserType): string {
214+
return language === 'Flow' ? param.name.name : param.name;
215+
}
216+
217+
function getParameterTypeAnnotation(param: $FlowFixMe, language: ParserType) {
218+
return language === 'Flow'
219+
? param.typeAnnotation
220+
: param.typeAnnotation.typeAnnotation;
221+
}
222+
223+
function getTypeAnnotationReturnType(
224+
typeAnnotation: $FlowFixMe,
225+
language: ParserType,
226+
) {
227+
return language === 'Flow'
228+
? typeAnnotation.returnType
229+
: typeAnnotation.typeAnnotation.typeAnnotation;
230+
}
231+
232+
function translateFunctionTypeAnnotation(
233+
hasteModuleName: string,
234+
// TODO(T108222691): Use flow-types for @babel/parser
235+
// TODO(T71778680): This is a FunctionTypeAnnotation. Type this.
236+
typeAnnotation: $FlowFixMe,
237+
types: TypeDeclarationMap,
238+
aliasMap: {...NativeModuleAliasMap},
239+
tryParse: ParserErrorCapturer,
240+
cxxOnly: boolean,
241+
translateTypeAnnotation: $FlowFixMe,
242+
language: ParserType,
243+
): NativeModuleFunctionTypeAnnotation {
244+
type Param = NamedShape<Nullable<NativeModuleParamTypeAnnotation>>;
245+
const params: Array<Param> = [];
246+
247+
for (const param of getTypeAnnotationParameters(typeAnnotation, language)) {
248+
const parsedParam = tryParse(() => {
249+
if (getFunctionNameFromParameter(param, language) == null) {
250+
throw new UnnamedFunctionParamParserError(
251+
param,
252+
hasteModuleName,
253+
language,
254+
);
255+
}
256+
257+
const paramName = getParameterName(param, language);
258+
259+
const [paramTypeAnnotation, isParamTypeAnnotationNullable] =
260+
unwrapNullable(
261+
translateTypeAnnotation(
262+
hasteModuleName,
263+
getParameterTypeAnnotation(param, language),
264+
types,
265+
aliasMap,
266+
tryParse,
267+
cxxOnly,
268+
),
269+
);
270+
271+
if (
272+
paramTypeAnnotation.type === 'VoidTypeAnnotation' ||
273+
paramTypeAnnotation.type === 'PromiseTypeAnnotation'
274+
) {
275+
return throwIfUnsupportedFunctionParamTypeAnnotationParserError(
276+
hasteModuleName,
277+
param.typeAnnotation,
278+
paramName,
279+
paramTypeAnnotation.type,
280+
);
281+
}
282+
283+
return {
284+
name: paramName,
285+
optional: Boolean(param.optional),
286+
typeAnnotation: wrapNullable(
287+
isParamTypeAnnotationNullable,
288+
paramTypeAnnotation,
289+
),
290+
};
291+
});
292+
293+
if (parsedParam != null) {
294+
params.push(parsedParam);
295+
}
296+
}
297+
298+
const [returnTypeAnnotation, isReturnTypeAnnotationNullable] = unwrapNullable(
299+
translateTypeAnnotation(
300+
hasteModuleName,
301+
getTypeAnnotationReturnType(typeAnnotation, language),
302+
types,
303+
aliasMap,
304+
tryParse,
305+
cxxOnly,
306+
),
307+
);
308+
309+
throwIfUnsupportedFunctionReturnTypeAnnotationParserError(
310+
hasteModuleName,
311+
typeAnnotation,
312+
'FunctionTypeAnnotation',
313+
language,
314+
cxxOnly,
315+
returnTypeAnnotation.type,
316+
);
317+
318+
return {
319+
type: 'FunctionTypeAnnotation',
320+
returnTypeAnnotation: wrapNullable(
321+
isReturnTypeAnnotationNullable,
322+
returnTypeAnnotation,
323+
),
324+
params,
325+
};
326+
}
327+
183328
module.exports = {
184329
emitBoolean,
185330
emitDouble,
@@ -194,4 +339,5 @@ module.exports = {
194339
emitString,
195340
emitStringish,
196341
typeAliasResolution,
342+
translateFunctionTypeAnnotation,
197343
};

0 commit comments

Comments
 (0)