@@ -16,22 +16,18 @@ import type {
1616 NativeModuleEnumMap ,
1717 NativeModuleBaseTypeAnnotation ,
1818 NativeModuleTypeAnnotation ,
19- NativeModulePropertyShape ,
20- NativeModuleSchema ,
2119 Nullable ,
2220} from '../../../CodegenSchema' ;
2321
2422import type { Parser } from '../../parser' ;
2523import type { ParserErrorCapturer , TypeDeclarationMap } from '../../utils' ;
2624
27- const { visit, isModuleRegistryCall, verifyPlatforms} = require ( '../../utils' ) ;
28- const { resolveTypeAnnotation, getTypes} = require ( '../utils' ) ;
25+ const { resolveTypeAnnotation} = require ( '../utils' ) ;
2926const {
3027 unwrapNullable,
3128 wrapNullable,
3229 assertGenericTypeAnnotationHasExactlyOneTypeParameter,
3330 parseObjectProperty,
34- buildPropertySchema,
3531} = require ( '../../parsers-commons' ) ;
3632const {
3733 emitArrayType,
@@ -60,15 +56,6 @@ const {
6056} = require ( '../../errors' ) ;
6157
6258const {
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-
488320module . exports = {
489- buildModuleSchema,
490321 flowTranslateTypeAnnotation : translateTypeAnnotation ,
491322} ;
0 commit comments