Skip to content

Commit fb37dea

Browse files
Pranav-yadavfacebook-github-bot
authored andcommitted
Refactor buildPropertySchema fn of (Flow, TS) into common fn (#35288)
Summary: This PR is a task of #34872 - combined `Flow` and `TS` `buildPropertySchema` fn 's into common fn - added callback param `resolveTypeAnnotation` to the same - moved it to `parsers-commons.js` - re-organized imports and exports ## Changelog [INTERNAL] [CHANGED] - [Codegen]: Refactored `buildPropertySchema` fn of (Flow, TS) into common fn in `parsers-commons.js` Pull Request resolved: #35288 Test Plan: - ensure 👇 is `#00ff00` ```bash yarn lint && yarn flow && yarn test-ci ``` Reviewed By: christophpurrer Differential Revision: D41247738 Pulled By: cipolleschi fbshipit-source-id: aecc0ed8d07efa1c2c39e8a8e64b4ee73b720b8f
1 parent 85ceff5 commit fb37dea

File tree

3 files changed

+81
-102
lines changed

3 files changed

+81
-102
lines changed

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

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,28 @@ import type {
1515
NativeModuleAliasMap,
1616
NativeModuleArrayTypeAnnotation,
1717
NativeModuleBaseTypeAnnotation,
18+
NativeModuleTypeAnnotation,
1819
NativeModulePropertyShape,
1920
NativeModuleSchema,
2021
Nullable,
2122
} from '../../../CodegenSchema.js';
2223

2324
import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';
24-
import type {NativeModuleTypeAnnotation} from '../../../CodegenSchema.js';
25-
const {nullGuard} = require('../../parsers-utils');
2625

27-
const {visit, verifyPlatforms, isModuleRegistryCall} = require('../../utils');
26+
const {nullGuard} = require('../../parsers-utils');
27+
const {visit, isModuleRegistryCall, verifyPlatforms} = require('../../utils');
2828
const {resolveTypeAnnotation, getTypes} = require('../utils.js');
29+
2930
const {
3031
unwrapNullable,
3132
wrapNullable,
3233
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
3334
parseObjectProperty,
3435
emitUnionTypeAnnotation,
3536
translateDefault,
36-
translateFunctionTypeAnnotation,
37+
buildPropertySchema,
3738
} = require('../../parsers-commons');
39+
3840
const {
3941
emitBoolean,
4042
emitDouble,
@@ -66,7 +68,6 @@ const {
6668
throwIfMoreThanOneModuleRegistryCalls,
6769
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
6870
throwIfUntypedModule,
69-
throwIfModuleTypeIsUnsupported,
7071
throwIfMoreThanOneModuleInterfaceParserError,
7172
} = require('../../error-utils');
7273

@@ -326,52 +327,6 @@ function translateTypeAnnotation(
326327
}
327328
}
328329

329-
function buildPropertySchema(
330-
hasteModuleName: string,
331-
// TODO(T71778680): This is an ObjectTypeProperty containing either:
332-
// - a FunctionTypeAnnotation or GenericTypeAnnotation
333-
// - a NullableTypeAnnoation containing a FunctionTypeAnnotation or GenericTypeAnnotation
334-
// Flow type this node
335-
property: $FlowFixMe,
336-
types: TypeDeclarationMap,
337-
aliasMap: {...NativeModuleAliasMap},
338-
tryParse: ParserErrorCapturer,
339-
cxxOnly: boolean,
340-
): NativeModulePropertyShape {
341-
let nullable = false;
342-
let {key, value} = property;
343-
344-
const methodName: string = key.name;
345-
346-
({nullable, typeAnnotation: value} = resolveTypeAnnotation(value, types));
347-
348-
throwIfModuleTypeIsUnsupported(
349-
hasteModuleName,
350-
property.value,
351-
property.key.name,
352-
value.type,
353-
language,
354-
);
355-
356-
return {
357-
name: methodName,
358-
optional: property.optional,
359-
typeAnnotation: wrapNullable(
360-
nullable,
361-
translateFunctionTypeAnnotation(
362-
hasteModuleName,
363-
value,
364-
types,
365-
aliasMap,
366-
tryParse,
367-
cxxOnly,
368-
translateTypeAnnotation,
369-
language,
370-
),
371-
),
372-
};
373-
}
374-
375330
function isModuleInterface(node: $FlowFixMe) {
376331
return (
377332
node.type === 'InterfaceDeclaration' &&
@@ -510,6 +465,9 @@ function buildModuleSchema(
510465
aliasMap,
511466
tryParse,
512467
cxxOnly,
468+
language,
469+
resolveTypeAnnotation,
470+
translateTypeAnnotation,
513471
),
514472
}));
515473
})

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

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'use strict';
1212

1313
import type {
14+
Nullable,
1415
NamedShape,
1516
NativeModuleAliasMap,
1617
NativeModuleBaseTypeAnnotation,
@@ -19,19 +20,22 @@ import type {
1920
NativeModuleTypeAnnotation,
2021
NativeModuleFunctionTypeAnnotation,
2122
NativeModuleParamTypeAnnotation,
23+
NativeModulePropertyShape,
2224
NativeModuleUnionTypeAnnotation,
23-
Nullable,
2425
SchemaType,
2526
} from '../CodegenSchema.js';
26-
import type {ParserType} from './errors';
27+
2728
import type {Parser} from './parser';
29+
import type {ParserType} from './errors';
2830
import type {ParserErrorCapturer, TypeDeclarationMap} from './utils';
2931

3032
const {
3133
throwIfPropertyValueTypeIsUnsupported,
3234
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
3335
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
36+
throwIfModuleTypeIsUnsupported,
3437
} = require('./error-utils');
38+
3539
const {
3640
MissingTypeParameterGenericParserError,
3741
MoreThanOneTypeParameterGenericParserError,
@@ -402,6 +406,62 @@ function translateFunctionTypeAnnotation(
402406
};
403407
}
404408

409+
function buildPropertySchema(
410+
hasteModuleName: string,
411+
// TODO(T108222691): [TS] Use flow-types for @babel/parser
412+
// TODO(T71778680): [Flow] This is an ObjectTypeProperty containing either:
413+
// - a FunctionTypeAnnotation or GenericTypeAnnotation
414+
// - a NullableTypeAnnoation containing a FunctionTypeAnnotation or GenericTypeAnnotation
415+
// Flow type this node
416+
property: $FlowFixMe,
417+
types: TypeDeclarationMap,
418+
aliasMap: {...NativeModuleAliasMap},
419+
tryParse: ParserErrorCapturer,
420+
cxxOnly: boolean,
421+
language: ParserType,
422+
resolveTypeAnnotation: $FlowFixMe,
423+
translateTypeAnnotation: $FlowFixMe,
424+
): NativeModulePropertyShape {
425+
let nullable: boolean = false;
426+
let {key, value} = property;
427+
const methodName: string = key.name;
428+
429+
if (language === 'TypeScript') {
430+
value =
431+
property.type === 'TSMethodSignature'
432+
? property
433+
: property.typeAnnotation;
434+
}
435+
436+
({nullable, typeAnnotation: value} = resolveTypeAnnotation(value, types));
437+
438+
throwIfModuleTypeIsUnsupported(
439+
hasteModuleName,
440+
property.value,
441+
key.name,
442+
value.type,
443+
language,
444+
);
445+
446+
return {
447+
name: methodName,
448+
optional: Boolean(property.optional),
449+
typeAnnotation: wrapNullable(
450+
nullable,
451+
translateFunctionTypeAnnotation(
452+
hasteModuleName,
453+
value,
454+
types,
455+
aliasMap,
456+
tryParse,
457+
cxxOnly,
458+
translateTypeAnnotation,
459+
language,
460+
),
461+
),
462+
};
463+
}
464+
405465
module.exports = {
406466
wrapModuleSchema,
407467
unwrapNullable,
@@ -412,4 +472,5 @@ module.exports = {
412472
emitUnionTypeAnnotation,
413473
translateDefault,
414474
translateFunctionTypeAnnotation,
475+
buildPropertySchema,
415476
};

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

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,21 @@ import type {
2222
} from '../../../CodegenSchema.js';
2323

2424
import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';
25+
2526
const {nullGuard} = require('../../parsers-utils');
26-
const {visit, isModuleRegistryCall} = require('../../utils');
27+
const {visit, isModuleRegistryCall, verifyPlatforms} = require('../../utils');
2728
const {resolveTypeAnnotation, getTypes} = require('../utils.js');
29+
2830
const {
2931
unwrapNullable,
3032
wrapNullable,
3133
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
3234
parseObjectProperty,
3335
emitUnionTypeAnnotation,
3436
translateDefault,
35-
translateFunctionTypeAnnotation,
37+
buildPropertySchema,
3638
} = require('../../parsers-commons');
39+
3740
const {
3841
emitBoolean,
3942
emitDouble,
@@ -50,18 +53,16 @@ const {
5053
emitMixedTypeAnnotation,
5154
typeAliasResolution,
5255
} = require('../../parsers-primitives');
56+
5357
const {
5458
UnsupportedArrayElementTypeAnnotationParserError,
5559
UnsupportedGenericParserError,
5660
UnsupportedTypeAnnotationParserError,
5761
IncorrectModuleRegistryCallArgumentTypeParserError,
5862
} = require('../../errors.js');
5963

60-
const {verifyPlatforms} = require('../../utils');
61-
6264
const {
6365
throwIfUntypedModule,
64-
throwIfModuleTypeIsUnsupported,
6566
throwIfUnusedModuleInterfaceParserError,
6667
throwIfModuleInterfaceNotFound,
6768
throwIfModuleInterfaceIsMisnamed,
@@ -349,50 +350,6 @@ function translateTypeAnnotation(
349350
}
350351
}
351352

352-
function buildPropertySchema(
353-
hasteModuleName: string,
354-
// TODO(T108222691): Use flow-types for @babel/parser
355-
property: $FlowFixMe,
356-
types: TypeDeclarationMap,
357-
aliasMap: {...NativeModuleAliasMap},
358-
tryParse: ParserErrorCapturer,
359-
cxxOnly: boolean,
360-
): NativeModulePropertyShape {
361-
let nullable = false;
362-
let {key} = property;
363-
let value =
364-
property.type === 'TSMethodSignature' ? property : property.typeAnnotation;
365-
366-
const methodName: string = key.name;
367-
368-
({nullable, typeAnnotation: value} = resolveTypeAnnotation(value, types));
369-
throwIfModuleTypeIsUnsupported(
370-
hasteModuleName,
371-
property.value,
372-
property.key.name,
373-
value.type,
374-
language,
375-
);
376-
377-
return {
378-
name: methodName,
379-
optional: Boolean(property.optional),
380-
typeAnnotation: wrapNullable(
381-
nullable,
382-
translateFunctionTypeAnnotation(
383-
hasteModuleName,
384-
value,
385-
types,
386-
aliasMap,
387-
tryParse,
388-
cxxOnly,
389-
translateTypeAnnotation,
390-
language,
391-
),
392-
),
393-
};
394-
}
395-
396353
function isModuleInterface(node: $FlowFixMe) {
397354
return (
398355
node.type === 'TSInterfaceDeclaration' &&
@@ -535,6 +492,9 @@ function buildModuleSchema(
535492
aliasMap,
536493
tryParse,
537494
cxxOnly,
495+
language,
496+
resolveTypeAnnotation,
497+
translateTypeAnnotation,
538498
),
539499
}));
540500
})

0 commit comments

Comments
 (0)