Skip to content

Commit 62244d4

Browse files
gabrieldonadelfacebook-github-bot
authored andcommitted
chore: Extract codegen translateFunctionTypeAnnotation into a common function (#35182)
Summary: This PR extracts the codegen `translateFunctionTypeAnnotation` Flow and TypeScript functions into a single common function in the `parsers-primitives.js` file that can be used by both parsers as requested on #34872. ## Changelog [Internal] [Changed] - Extract codegen `translateFunctionTypeAnnotation` into a common function in the` parsers-primitives.js` file Pull Request resolved: #35182 Test Plan: Run `yarn jest react-native-codegen` and ensure CI is green ![image](https://user-images.githubusercontent.com/11707729/199625849-e89b647f-63fb-40f8-b643-a59dedb4c59a.png) Reviewed By: cortinico Differential Revision: D41030544 Pulled By: rshest fbshipit-source-id: bc93c21e31ed4e8c3293cafe3d808d9f36cf8ecc
1 parent 6db3995 commit 62244d4

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

+5-92
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const {
5151
emitStringish,
5252
emitMixedTypeAnnotation,
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

+147-1
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,25 @@ import type {
2828
StringTypeAnnotation,
2929
VoidTypeAnnotation,
3030
NativeModuleFloatTypeAnnotation,
31+
NativeModuleParamTypeAnnotation,
32+
NamedShape,
3133
} from '../CodegenSchema';
3234
import type {ParserType} from './errors';
33-
import type {TypeAliasResolutionStatus} from './utils';
35+
import type {
36+
ParserErrorCapturer,
37+
TypeAliasResolutionStatus,
38+
TypeDeclarationMap,
39+
} from './utils';
40+
41+
const {UnnamedFunctionParamParserError} = require('./errors');
3442

3543
const {
44+
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
45+
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
46+
} = require('./error-utils');
47+
48+
const {
49+
unwrapNullable,
3650
wrapNullable,
3751
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
3852
} = require('./parsers-commons');
@@ -190,6 +204,137 @@ function emitFloat(
190204
});
191205
}
192206

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

0 commit comments

Comments
 (0)