Update Module Generators to follow new NativeModuleSchema

Summary:
NOTE: Flow and Jest won't pass on this diff. Sandcastle, should, however, be green on D24236405 (i.e: the tip of this stack).

## Changes
1. NativeModule generators now use the new RN Codegen NativeModule schema.
2. Tangential: We're no longer removing the `Native` prefix from the NativeModule filename, assuming that that's the module name (problem: wrong), and prefixing again with Native (problem: redundant), when we're generating code. Instead, like the internal codegen, we simply pass the filename to the Codegen output. Our linters enforce that all NativeModule specs are contained with files that start off with `Native`.
3. `GenerateModuleCpp` was fixed to use the actual module name as opposed to the spec name. I added a comment inline.

Changelog: [Internal]

(Note: this ignores all push blocking failures!)

Reviewed By: PeteTheHeat

Differential Revision: D24236405

fbshipit-source-id: ccd6b5674d252c350be0ec8a86e7ca5f2f614778
This commit is contained in:
Ramanpreet Nara
2020-10-15 22:50:19 -07:00
committed by Facebook GitHub Bot
parent ee177f6cba
commit c4f23354fd
21 changed files with 434 additions and 729 deletions
@@ -16,6 +16,8 @@ import type {
NativeModulePropertySchema,
NativeModuleMethodParamSchema,
NativeModuleReturnTypeAnnotation,
NativeModuleFunctionTypeAnnotation,
NativeModuleParamTypeAnnotation,
} from '../../CodegenSchema';
import type {AliasResolver} from './Utils';
@@ -56,7 +58,10 @@ function translateFunctionParamToJavaType(
imports: Set<string>,
): string {
const {optional, typeAnnotation: nullableTypeAnnotation} = param;
const [typeAnnotation, nullable] = unwrapNullable(nullableTypeAnnotation);
const [
typeAnnotation,
nullable,
] = unwrapNullable<NativeModuleParamTypeAnnotation>(nullableTypeAnnotation);
const isRequired = !optional && !nullable;
function wrapIntoNullableIfNeeded(generatedType: string) {
@@ -122,7 +127,10 @@ function translateFunctionReturnTypeToJavaType(
resolveAlias: AliasResolver,
imports: Set<string>,
): string {
const [returnTypeAnnotation, nullable] = unwrapNullable(
const [
returnTypeAnnotation,
nullable,
] = unwrapNullable<NativeModuleReturnTypeAnnotation>(
nullableReturnTypeAnnotation,
);
@@ -184,7 +192,9 @@ function buildGetConstantsMethod(
method: NativeModulePropertySchema,
imports: Set<string>,
): string {
const [methodTypeAnnotation] = unwrapNullable(method.typeAnnotation);
const [
methodTypeAnnotation,
] = unwrapNullable<NativeModuleFunctionTypeAnnotation>(method.typeAnnotation);
if (
methodTypeAnnotation.returnTypeAnnotation.type === 'ObjectTypeAnnotation'
) {
@@ -269,10 +279,13 @@ module.exports = {
const packageName = 'com.facebook.fbreact.specs.beta';
const nativeModules = getModules(schema);
Object.keys(nativeModules).forEach(name => {
const {aliases, properties} = nativeModules[name];
Object.keys(nativeModules).forEach(codegenModuleName => {
const {
aliases,
spec: {properties},
} = nativeModules[codegenModuleName];
const resolveAlias = createAliasResolver(aliases);
const className = `Native${name}Spec`;
const className = `${codegenModuleName}Spec`;
const imports: Set<string> = new Set([
// Always required.
@@ -288,7 +301,11 @@ module.exports = {
return buildGetConstantsMethod(method, imports);
}
const [methodTypeAnnotation] = unwrapNullable(method.typeAnnotation);
const [
methodTypeAnnotation,
] = unwrapNullable<NativeModuleFunctionTypeAnnotation>(
method.typeAnnotation,
);
// Handle return type
const translatedReturnType = translateFunctionReturnTypeToJavaType(