Refactor `buildPropertySchema` fn of (Flow, TS) into common fn (#35288)

Summary:
This PR is a task of https://github.com/facebook/react-native/issues/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: https://github.com/facebook/react-native/pull/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
This commit is contained in:
Pranav Yadav 2022-11-16 07:45:20 -08:00 коммит произвёл Facebook GitHub Bot
Родитель 85ceff529f
Коммит fb37dea38e
3 изменённых файлов: 81 добавлений и 102 удалений

Просмотреть файл

@ -15,17 +15,18 @@ import type {
NativeModuleAliasMap,
NativeModuleArrayTypeAnnotation,
NativeModuleBaseTypeAnnotation,
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,
@ -33,8 +34,9 @@ const {
parseObjectProperty,
emitUnionTypeAnnotation,
translateDefault,
translateFunctionTypeAnnotation,
buildPropertySchema,
} = require('../../parsers-commons');
const {
emitBoolean,
emitDouble,
@ -66,7 +68,6 @@ const {
throwIfMoreThanOneModuleRegistryCalls,
throwIfIncorrectModuleRegistryCallTypeParameterParserError,
throwIfUntypedModule,
throwIfModuleTypeIsUnsupported,
throwIfMoreThanOneModuleInterfaceParserError,
} = require('../../error-utils');
@ -326,52 +327,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' &&
@ -510,6 +465,9 @@ function buildModuleSchema(
aliasMap,
tryParse,
cxxOnly,
language,
resolveTypeAnnotation,
translateTypeAnnotation,
),
}));
})

Просмотреть файл

@ -11,6 +11,7 @@
'use strict';
import type {
Nullable,
NamedShape,
NativeModuleAliasMap,
NativeModuleBaseTypeAnnotation,
@ -19,19 +20,22 @@ import type {
NativeModuleTypeAnnotation,
NativeModuleFunctionTypeAnnotation,
NativeModuleParamTypeAnnotation,
NativeModulePropertyShape,
NativeModuleUnionTypeAnnotation,
Nullable,
SchemaType,
} 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,
throwIfUnsupportedFunctionParamTypeAnnotationParserError,
throwIfUnsupportedFunctionReturnTypeAnnotationParserError,
throwIfModuleTypeIsUnsupported,
} = require('./error-utils');
const {
MissingTypeParameterGenericParserError,
MoreThanOneTypeParameterGenericParserError,
@ -402,6 +406,62 @@ function translateFunctionTypeAnnotation(
};
}
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: Boolean(property.optional),
typeAnnotation: wrapNullable(
nullable,
translateFunctionTypeAnnotation(
hasteModuleName,
value,
types,
aliasMap,
tryParse,
cxxOnly,
translateTypeAnnotation,
language,
),
),
};
}
module.exports = {
wrapModuleSchema,
unwrapNullable,
@ -412,4 +472,5 @@ module.exports = {
emitUnionTypeAnnotation,
translateDefault,
translateFunctionTypeAnnotation,
buildPropertySchema,
};

Просмотреть файл

@ -22,9 +22,11 @@ import type {
} 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,
@ -32,8 +34,9 @@ const {
parseObjectProperty,
emitUnionTypeAnnotation,
translateDefault,
translateFunctionTypeAnnotation,
buildPropertySchema,
} = require('../../parsers-commons');
const {
emitBoolean,
emitDouble,
@ -50,6 +53,7 @@ const {
emitMixedTypeAnnotation,
typeAliasResolution,
} = require('../../parsers-primitives');
const {
UnsupportedArrayElementTypeAnnotationParserError,
UnsupportedGenericParserError,
@ -57,11 +61,8 @@ const {
IncorrectModuleRegistryCallArgumentTypeParserError,
} = require('../../errors.js');
const {verifyPlatforms} = require('../../utils');
const {
throwIfUntypedModule,
throwIfModuleTypeIsUnsupported,
throwIfUnusedModuleInterfaceParserError,
throwIfModuleInterfaceNotFound,
throwIfModuleInterfaceIsMisnamed,
@ -349,50 +350,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' &&
@ -535,6 +492,9 @@ function buildModuleSchema(
aliasMap,
tryParse,
cxxOnly,
language,
resolveTypeAnnotation,
translateTypeAnnotation,
),
}));
})