Skip to content

Commit 50de956

Browse files
committed
refactor(codegen): extract buildModuleSchema to parsers-commons
1 parent 4b8b9cc commit 50de956

File tree

6 files changed

+117
-351
lines changed

6 files changed

+117
-351
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import type {ParserType} from '../errors';
2525
const {Visitor} = require('../flow/Visitor');
2626
const {wrapComponentSchema} = require('../schema.js');
2727
const {buildComponentSchema} = require('../flow/components');
28-
const {buildModuleSchema} = require('../flow/modules');
28+
const {buildModuleSchema} = require('../parsers-commons.js');
2929
const {isModuleRegistryCall} = require('../utils.js');
3030
const {
3131
ParserError,

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

Lines changed: 1 addition & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,18 @@ import type {
1616
NativeModuleEnumMap,
1717
NativeModuleBaseTypeAnnotation,
1818
NativeModuleTypeAnnotation,
19-
NativeModulePropertyShape,
20-
NativeModuleSchema,
2119
Nullable,
2220
} from '../../../CodegenSchema';
2321

2422
import type {Parser} from '../../parser';
2523
import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';
2624

27-
const {visit, isModuleRegistryCall, verifyPlatforms} = require('../../utils');
28-
const {resolveTypeAnnotation, getTypes} = require('../utils');
25+
const {resolveTypeAnnotation} = require('../utils');
2926
const {
3027
unwrapNullable,
3128
wrapNullable,
3229
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
3330
parseObjectProperty,
34-
buildPropertySchema,
3531
} = require('../../parsers-commons');
3632
const {
3733
emitArrayType,
@@ -60,15 +56,6 @@ const {
6056
} = require('../../errors');
6157

6258
const {
63-
throwIfModuleInterfaceNotFound,
64-
throwIfModuleInterfaceIsMisnamed,
65-
throwIfUnusedModuleInterfaceParserError,
66-
throwIfWrongNumberOfCallExpressionArgs,
67-
throwIfMoreThanOneModuleRegistryCalls,
68-
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
69-
throwIfIncorrectModuleRegistryCallArgument,
70-
throwIfUntypedModule,
71-
throwIfMoreThanOneModuleInterfaceParserError,
7259
throwIfPartialNotAnnotatingTypeParameter,
7360
throwIfPartialWithMoreParameter,
7461
} = require('../../error-utils');
@@ -330,162 +317,6 @@ function translateTypeAnnotation(
330317
}
331318
}
332319

333-
function buildModuleSchema(
334-
hasteModuleName: string,
335-
/**
336-
* TODO(T71778680): Flow-type this node.
337-
*/
338-
ast: $FlowFixMe,
339-
tryParse: ParserErrorCapturer,
340-
parser: Parser,
341-
): NativeModuleSchema {
342-
const types = getTypes(ast);
343-
const moduleSpecs = (Object.values(types): $ReadOnlyArray<$FlowFixMe>).filter(
344-
t => parser.isModuleInterface(t),
345-
);
346-
347-
throwIfModuleInterfaceNotFound(
348-
moduleSpecs.length,
349-
hasteModuleName,
350-
ast,
351-
language,
352-
);
353-
354-
throwIfMoreThanOneModuleInterfaceParserError(
355-
hasteModuleName,
356-
moduleSpecs,
357-
language,
358-
);
359-
360-
const [moduleSpec] = moduleSpecs;
361-
362-
throwIfModuleInterfaceIsMisnamed(hasteModuleName, moduleSpec.id, language);
363-
364-
// Parse Module Name
365-
const moduleName = ((): string => {
366-
const callExpressions = [];
367-
visit(ast, {
368-
CallExpression(node) {
369-
if (isModuleRegistryCall(node)) {
370-
callExpressions.push(node);
371-
}
372-
},
373-
});
374-
375-
throwIfUnusedModuleInterfaceParserError(
376-
hasteModuleName,
377-
moduleSpec,
378-
callExpressions,
379-
);
380-
381-
throwIfMoreThanOneModuleRegistryCalls(
382-
hasteModuleName,
383-
callExpressions,
384-
callExpressions.length,
385-
);
386-
387-
const [callExpression] = callExpressions;
388-
const {typeArguments} = callExpression;
389-
const methodName = callExpression.callee.property.name;
390-
391-
throwIfWrongNumberOfCallExpressionArgs(
392-
hasteModuleName,
393-
callExpression,
394-
methodName,
395-
callExpression.arguments.length,
396-
);
397-
398-
throwIfIncorrectModuleRegistryCallArgument(
399-
hasteModuleName,
400-
callExpression.arguments[0],
401-
methodName,
402-
);
403-
404-
const $moduleName = callExpression.arguments[0].value;
405-
406-
throwIfUntypedModule(
407-
typeArguments,
408-
hasteModuleName,
409-
callExpression,
410-
methodName,
411-
$moduleName,
412-
);
413-
414-
throwIfIncorrectModuleRegistryCallTypeParameterParserError(
415-
hasteModuleName,
416-
typeArguments,
417-
methodName,
418-
$moduleName,
419-
parser,
420-
);
421-
422-
return $moduleName;
423-
})();
424-
425-
// Some module names use platform suffix to indicate platform-exclusive modules.
426-
// Eventually this should be made explicit in the Flow type itself.
427-
// Also check the hasteModuleName for platform suffix.
428-
// Note: this shape is consistent with ComponentSchema.
429-
const {cxxOnly, excludedPlatforms} = verifyPlatforms(
430-
hasteModuleName,
431-
moduleName,
432-
);
433-
434-
// $FlowFixMe[missing-type-arg]
435-
return (moduleSpec.body.properties: $ReadOnlyArray<$FlowFixMe>)
436-
.filter(property => property.type === 'ObjectTypeProperty')
437-
.map<?{
438-
aliasMap: NativeModuleAliasMap,
439-
enumMap: NativeModuleEnumMap,
440-
propertyShape: NativeModulePropertyShape,
441-
}>(property => {
442-
const aliasMap: {...NativeModuleAliasMap} = {};
443-
const enumMap: {...NativeModuleEnumMap} = {};
444-
return tryParse(() => ({
445-
aliasMap: aliasMap,
446-
enumMap: enumMap,
447-
propertyShape: buildPropertySchema(
448-
hasteModuleName,
449-
property,
450-
types,
451-
aliasMap,
452-
enumMap,
453-
tryParse,
454-
cxxOnly,
455-
resolveTypeAnnotation,
456-
translateTypeAnnotation,
457-
parser,
458-
),
459-
}));
460-
})
461-
.filter(Boolean)
462-
.reduce(
463-
(
464-
moduleSchema: NativeModuleSchema,
465-
{aliasMap, enumMap, propertyShape},
466-
) => ({
467-
type: 'NativeModule',
468-
aliasMap: {...moduleSchema.aliasMap, ...aliasMap},
469-
enumMap: {...moduleSchema.enumMap, ...enumMap},
470-
spec: {
471-
properties: [...moduleSchema.spec.properties, propertyShape],
472-
},
473-
moduleName: moduleSchema.moduleName,
474-
excludedPlatforms: moduleSchema.excludedPlatforms,
475-
}),
476-
{
477-
type: 'NativeModule',
478-
aliasMap: {},
479-
enumMap: {},
480-
spec: {properties: []},
481-
moduleName,
482-
excludedPlatforms:
483-
excludedPlatforms.length !== 0 ? [...excludedPlatforms] : undefined,
484-
},
485-
);
486-
}
487-
488320
module.exports = {
489-
buildModuleSchema,
490321
flowTranslateTypeAnnotation: translateTypeAnnotation,
491322
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const {buildSchema} = require('../parsers-commons');
3030
const {Visitor} = require('./Visitor');
3131
const {buildComponentSchema} = require('./components');
3232
const {wrapComponentSchema} = require('../schema.js');
33-
const {buildModuleSchema} = require('./modules');
33+
const {buildModuleSchema} = require('../parsers-commons.js');
3434

3535
const fs = require('fs');
3636

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

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type {
2121
NativeModuleParamTypeAnnotation,
2222
NativeModulePropertyShape,
2323
SchemaType,
24+
NativeModuleEnumMap,
2425
} from '../CodegenSchema.js';
2526

2627
import type {Parser} from './parser';
@@ -39,6 +40,9 @@ const {
3940
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
4041
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
4142
throwIfModuleTypeIsUnsupported,
43+
throwIfModuleInterfaceNotFound,
44+
throwIfMoreThanOneModuleInterfaceParserError,
45+
throwIfModuleInterfaceIsMisnamed,
4246
} = require('./error-utils');
4347

4448
const {
@@ -48,7 +52,6 @@ const {
4852
} = require('./errors');
4953

5054
const invariant = require('invariant');
51-
import type {NativeModuleEnumMap} from '../CodegenSchema';
5255

5356
function wrapModuleSchema(
5457
nativeModuleSchema: NativeModuleSchema,
@@ -432,6 +435,113 @@ function buildSchema(
432435
);
433436
}
434437

438+
function buildModuleSchema(
439+
hasteModuleName: string,
440+
/**
441+
* TODO(T71778680): Flow-type this node.
442+
*/
443+
ast: $FlowFixMe,
444+
tryParse: ParserErrorCapturer,
445+
parser: Parser,
446+
): NativeModuleSchema {
447+
const language = parser.language();
448+
const types = parser.getTypes(ast);
449+
const moduleSpecs = (Object.values(types): $ReadOnlyArray<$FlowFixMe>).filter(
450+
t => parser.isModuleInterface(t),
451+
);
452+
453+
throwIfModuleInterfaceNotFound(
454+
moduleSpecs.length,
455+
hasteModuleName,
456+
ast,
457+
language,
458+
);
459+
460+
throwIfMoreThanOneModuleInterfaceParserError(
461+
hasteModuleName,
462+
moduleSpecs,
463+
language,
464+
);
465+
466+
const [moduleSpec] = moduleSpecs;
467+
468+
throwIfModuleInterfaceIsMisnamed(hasteModuleName, moduleSpec.id, language);
469+
470+
// Parse Module Name
471+
const moduleName = parseModuleName(hasteModuleName, moduleSpec, ast, parser);
472+
473+
// Some module names use platform suffix to indicate platform-exclusive modules.
474+
// Eventually this should be made explicit in the Flow type itself.
475+
// Also check the hasteModuleName for platform suffix.
476+
// Note: this shape is consistent with ComponentSchema.
477+
const {cxxOnly, excludedPlatforms} = verifyPlatforms(
478+
hasteModuleName,
479+
moduleName,
480+
);
481+
482+
// $FlowFixMe[missing-type-arg]
483+
const properties: $ReadOnlyArray<$FlowFixMe> =
484+
language === 'Flow' ? moduleSpec.body.properties : moduleSpec.body.body;
485+
486+
return properties
487+
.filter(
488+
property =>
489+
property.type === 'ObjectTypeProperty' ||
490+
property.type === 'TSPropertySignature' ||
491+
property.type === 'TSMethodSignature',
492+
)
493+
.map<?{
494+
aliasMap: NativeModuleAliasMap,
495+
enumMap: NativeModuleEnumMap,
496+
propertyShape: NativeModulePropertyShape,
497+
}>(property => {
498+
const aliasMap: {...NativeModuleAliasMap} = {};
499+
const enumMap: {...NativeModuleEnumMap} = {};
500+
501+
return tryParse(() => ({
502+
aliasMap,
503+
enumMap,
504+
propertyShape: buildPropertySchema(
505+
hasteModuleName,
506+
property,
507+
types,
508+
aliasMap,
509+
enumMap,
510+
tryParse,
511+
cxxOnly,
512+
resolveTypeAnnotation,
513+
translateTypeAnnotation,
514+
parser,
515+
),
516+
}));
517+
})
518+
.filter(Boolean)
519+
.reduce(
520+
(
521+
moduleSchema: NativeModuleSchema,
522+
{aliasMap, enumMap, propertyShape},
523+
) => ({
524+
type: 'NativeModule',
525+
aliasMap: {...moduleSchema.aliasMap, ...aliasMap},
526+
enumMap: {...moduleSchema.enumMap, ...enumMap},
527+
spec: {
528+
properties: [...moduleSchema.spec.properties, propertyShape],
529+
},
530+
moduleName: moduleSchema.moduleName,
531+
excludedPlatforms: moduleSchema.excludedPlatforms,
532+
}),
533+
{
534+
type: 'NativeModule',
535+
aliasMap: {},
536+
enumMap: {},
537+
spec: {properties: []},
538+
moduleName,
539+
excludedPlatforms:
540+
excludedPlatforms.length !== 0 ? [...excludedPlatforms] : undefined,
541+
},
542+
);
543+
}
544+
435545
module.exports = {
436546
wrapModuleSchema,
437547
unwrapNullable,
@@ -443,4 +553,5 @@ module.exports = {
443553
buildPropertySchema,
444554
buildSchemaFromConfigType,
445555
buildSchema,
556+
buildModuleSchema,
446557
};

0 commit comments

Comments
 (0)