Skip to content

Commit

Permalink
rf: buildPropertySchema Flow, TS fns > common fn
Browse files Browse the repository at this point in the history
- combined `Flow` and `TypeScript` `buildPropertySchema` fn 's into single common
- uses callback param `resolveTypeAnnotation`
- moved it to `parsers-commons.js`
- re-organize imports and exports
  • Loading branch information
Pranav-yadav committed Nov 14, 2022
1 parent c0f06e8 commit 76e01d4
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 112 deletions.
66 changes: 9 additions & 57 deletions packages/react-native-codegen/src/parsers/flow/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,28 @@ import type {
NativeModuleAliasMap,
NativeModuleArrayTypeAnnotation,
NativeModuleBaseTypeAnnotation,
NativeModuleFunctionTypeAnnotation,
NativeModuleParamTypeAnnotation,
NativeModuleTypeAnnotation,
NativeModulePropertyShape,
NativeModuleSchema,
Nullable,
} from '../../../CodegenSchema.js';

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

const {visit, verifyPlatforms, isModuleRegistryCall} = require('../../utils');
const {nullGuard} = require('../../parsers-utils');
const {visit, isModuleRegistryCall, verifyPlatforms} = require('../../utils');
const {resolveTypeAnnotation, getTypes} = require('../utils.js');

const {
unwrapNullable,
wrapNullable,
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
parseObjectProperty,
emitUnionTypeAnnotation,
translateDefault,
buildPropertySchema,
} = require('../../parsers-commons');

const {
emitBoolean,
emitDouble,
Expand All @@ -51,29 +52,23 @@ const {
emitStringish,
emitMixedTypeAnnotation,
typeAliasResolution,
translateFunctionTypeAnnotation,
} = require('../../parsers-primitives');

const {
UnnamedFunctionParamParserError,
UnsupportedArrayElementTypeAnnotationParserError,
UnsupportedTypeAnnotationParserError,
UnsupportedObjectPropertyTypeAnnotationParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');

const {
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
throwIfModuleInterfaceNotFound,
throwIfModuleInterfaceIsMisnamed,
throwIfUnusedModuleInterfaceParserError,
throwIfWrongNumberOfCallExpressionArgs,
throwIfMoreThanOneModuleRegistryCalls,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
throwIfUntypedModule,
throwIfModuleTypeIsUnsupported,
throwIfMoreThanOneModuleInterfaceParserError,
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
} = require('../../error-utils');

const {FlowParser} = require('../parser.js');
Expand Down Expand Up @@ -335,52 +330,6 @@ function translateTypeAnnotation(
}
}

function buildPropertySchema(
hasteModuleName: string,
// TODO(T71778680): This is an ObjectTypeProperty containing either:
// - a FunctionTypeAnnotation or GenericTypeAnnotation
// - a NullableTypeAnnoation containing a FunctionTypeAnnotation or GenericTypeAnnotation
// Flow type this node
property: $FlowFixMe,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
): NativeModulePropertyShape {
let nullable = false;
let {key, value} = property;

const methodName: string = key.name;

({nullable, typeAnnotation: value} = resolveTypeAnnotation(value, types));

throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
);

return {
name: methodName,
optional: property.optional,
typeAnnotation: wrapNullable(
nullable,
translateFunctionTypeAnnotation(
hasteModuleName,
value,
types,
aliasMap,
tryParse,
cxxOnly,
translateTypeAnnotation,
language,
),
),
};
}

function isModuleInterface(node: $FlowFixMe) {
return (
node.type === 'InterfaceDeclaration' &&
Expand Down Expand Up @@ -519,6 +468,9 @@ function buildModuleSchema(
aliasMap,
tryParse,
cxxOnly,
language,
resolveTypeAnnotation,
translateTypeAnnotation,
),
}));
})
Expand Down
73 changes: 69 additions & 4 deletions packages/react-native-codegen/src/parsers/parsers-commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,28 @@
'use strict';

import type {
Nullable,
NamedShape,
NativeModuleAliasMap,
NativeModuleBaseTypeAnnotation,
NativeModuleEnumDeclaration,
NativeModuleSchema,
NativeModuleTypeAnnotation,
NativeModulePropertyShape,
UnionTypeAnnotationMemberType,
NativeModuleUnionTypeAnnotation,
Nullable,
SchemaType,
UnionTypeAnnotationMemberType,
} from '../CodegenSchema.js';
import type {ParserType} from './errors';

import type {Parser} from './parser';
import type {ParserType} from './errors';
import type {ParserErrorCapturer, TypeDeclarationMap} from './utils';

const {throwIfPropertyValueTypeIsUnsupported} = require('./error-utils');
const {
throwIfModuleTypeIsUnsupported,
throwIfPropertyValueTypeIsUnsupported,
} = require('./error-utils');

const {
MissingTypeParameterGenericParserError,
MoreThanOneTypeParameterGenericParserError,
Expand All @@ -35,6 +41,8 @@ const {
UnsupportedObjectPropertyTypeAnnotationParserError,
UnsupportedUnionTypeAnnotationParserError,
} = require('./errors');

const {translateFunctionTypeAnnotation} = require('./parsers-primitives');
const invariant = require('invariant');

function wrapModuleSchema(
Expand Down Expand Up @@ -284,6 +292,62 @@ function translateDefault(
);
}

function buildPropertySchema(
hasteModuleName: string,
// TODO(T108222691): [TS] Use flow-types for @babel/parser
// TODO(T71778680): [Flow] This is an ObjectTypeProperty containing either:
// - a FunctionTypeAnnotation or GenericTypeAnnotation
// - a NullableTypeAnnoation containing a FunctionTypeAnnotation or GenericTypeAnnotation
// Flow type this node
property: $FlowFixMe,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
language: ParserType,
resolveTypeAnnotation: $FlowFixMe,
translateTypeAnnotation: $FlowFixMe,
): NativeModulePropertyShape {
let nullable: boolean = false;
let {key, value} = property;
const methodName: string = key.name;

if (language === 'TypeScript') {
value =
property.type === 'TSMethodSignature'
? property
: property.typeAnnotation;
}

({nullable, typeAnnotation: value} = resolveTypeAnnotation(value, types));

throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
key.name,
value.type,
language,
);

return {
name: methodName,
optional: property.optional,
typeAnnotation: wrapNullable(
nullable,
translateFunctionTypeAnnotation(
hasteModuleName,
value,
types,
aliasMap,
tryParse,
cxxOnly,
translateTypeAnnotation,
language,
),
),
};
}

module.exports = {
wrapModuleSchema,
unwrapNullable,
Expand All @@ -293,4 +357,5 @@ module.exports = {
parseObjectProperty,
emitUnionTypeAnnotation,
translateDefault,
buildPropertySchema,
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @flow strict
* @format
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,28 @@ import type {
NativeModuleAliasMap,
NativeModuleArrayTypeAnnotation,
NativeModuleBaseTypeAnnotation,
NativeModuleFunctionTypeAnnotation,
NativeModulePropertyShape,
NativeModuleTypeAnnotation,
NativeModuleSchema,
Nullable,
} from '../../../CodegenSchema.js';

import type {ParserErrorCapturer, TypeDeclarationMap} from '../../utils';

const {nullGuard} = require('../../parsers-utils');
const {visit, isModuleRegistryCall} = require('../../utils');
const {visit, isModuleRegistryCall, verifyPlatforms} = require('../../utils');
const {resolveTypeAnnotation, getTypes} = require('../utils.js');

const {
unwrapNullable,
wrapNullable,
assertGenericTypeAnnotationHasExactlyOneTypeParameter,
parseObjectProperty,
emitUnionTypeAnnotation,
translateDefault,
buildPropertySchema,
} = require('../../parsers-commons');

const {
emitBoolean,
emitDouble,
Expand All @@ -49,20 +52,17 @@ const {
emitStringish,
emitMixedTypeAnnotation,
typeAliasResolution,
translateFunctionTypeAnnotation,
} = require('../../parsers-primitives');

const {
UnsupportedArrayElementTypeAnnotationParserError,
UnsupportedGenericParserError,
UnsupportedTypeAnnotationParserError,
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');

const {verifyPlatforms} = require('../../utils');

const {
throwIfUntypedModule,
throwIfModuleTypeIsUnsupported,
throwIfUnusedModuleInterfaceParserError,
throwIfModuleInterfaceNotFound,
throwIfModuleInterfaceIsMisnamed,
Expand Down Expand Up @@ -341,50 +341,6 @@ function translateTypeAnnotation(
}
}

function buildPropertySchema(
hasteModuleName: string,
// TODO(T108222691): Use flow-types for @babel/parser
property: $FlowFixMe,
types: TypeDeclarationMap,
aliasMap: {...NativeModuleAliasMap},
tryParse: ParserErrorCapturer,
cxxOnly: boolean,
): NativeModulePropertyShape {
let nullable = false;
let {key} = property;
let value =
property.type === 'TSMethodSignature' ? property : property.typeAnnotation;

const methodName: string = key.name;

({nullable, typeAnnotation: value} = resolveTypeAnnotation(value, types));
throwIfModuleTypeIsUnsupported(
hasteModuleName,
property.value,
property.key.name,
value.type,
language,
);

return {
name: methodName,
optional: Boolean(property.optional),
typeAnnotation: wrapNullable(
nullable,
translateFunctionTypeAnnotation(
hasteModuleName,
value,
types,
aliasMap,
tryParse,
cxxOnly,
translateTypeAnnotation,
language,
),
),
};
}

function isModuleInterface(node: $FlowFixMe) {
return (
node.type === 'TSInterfaceDeclaration' &&
Expand Down Expand Up @@ -527,6 +483,9 @@ function buildModuleSchema(
aliasMap,
tryParse,
cxxOnly,
language,
resolveTypeAnnotation,
translateTypeAnnotation,
),
}));
})
Expand Down

0 comments on commit 76e01d4

Please sign in to comment.