From b5b6d1d69fbce83f18a62792783ceaa2d4f25262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Osadnik?= Date: Thu, 8 Aug 2019 11:09:30 -0700 Subject: [PATCH 1/7] Minor improvements in native modules codegens Summary: Add handling of `$ReadOnly`, $ReadOnlyArray`. Drop handling of params for callback (does not impact native generated node) and promises' types (does not impact native generated node). Remove typo from native codegen. Reviewed By: RSNara Differential Revision: D16686886 fbshipit-source-id: 26345978bbbba0cee14d00e7b5b9e5017c89a46c --- .../modules/NativeArrayTurboModule.js | 1 + .../modules/NativeObjectTurboModule.js | 1 + .../react-native-codegen/src/CodegenSchema.js | 19 +- .../modules/ObjCppUtils/GenerateStructs.js | 2 +- .../modules/__test_fixtures__/fixtures.js | 15 -- .../modules/__test_fixtures__/failures.js | 25 +++ .../modules/__test_fixtures__/fixtures.js | 9 + .../__snapshots__/module-parser-test.js.snap | 163 +++++++++++++----- .../src/parsers/flow/modules/methods.js | 80 +++++++-- 9 files changed, 229 insertions(+), 86 deletions(-) diff --git a/packages/react-native-codegen/e2e/__test_fixtures__/modules/NativeArrayTurboModule.js b/packages/react-native-codegen/e2e/__test_fixtures__/modules/NativeArrayTurboModule.js index bf8a330d297f..f421bafb6847 100644 --- a/packages/react-native-codegen/e2e/__test_fixtures__/modules/NativeArrayTurboModule.js +++ b/packages/react-native-codegen/e2e/__test_fixtures__/modules/NativeArrayTurboModule.js @@ -18,6 +18,7 @@ type AnotherArray = Array; export interface Spec extends TurboModule { +getArray: (a: Array) => Array; + +getReadOnlyArray: (a: Array) => $ReadOnlyArray; +getArrayWithAlias: (a: AnotherArray, b: Array) => AnotherArray; } diff --git a/packages/react-native-codegen/e2e/__test_fixtures__/modules/NativeObjectTurboModule.js b/packages/react-native-codegen/e2e/__test_fixtures__/modules/NativeObjectTurboModule.js index beafdf8f437c..2f9b3b936a6f 100644 --- a/packages/react-native-codegen/e2e/__test_fixtures__/modules/NativeObjectTurboModule.js +++ b/packages/react-native-codegen/e2e/__test_fixtures__/modules/NativeObjectTurboModule.js @@ -18,6 +18,7 @@ type AnotherGenericObject = GenericObject; export interface Spec extends TurboModule { +getGenericObject: (arg: Object) => Object; + +getGenericObjectReadOnly: (arg: Object) => $ReadOnly<{|a: string|}>; +getGenericObjectWithAlias: (arg: GenericObject) => AnotherGenericObject; +difficultObject: (A: {| D: boolean, diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index 8067ac5e65f5..9b77a50de6a8 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -149,7 +149,10 @@ export type PrimitiveTypeAnnotation = $ReadOnly<{| export type FunctionTypeAnnotationParamTypeAnnotation = | $ReadOnly<{| - type: 'AnyTypeAnnotation' | PrimitiveTypeAnnotationType, + type: + | 'AnyTypeAnnotation' + | 'FunctionTypeAnnotation' + | PrimitiveTypeAnnotationType, |}> | $ReadOnly<{| type: 'ArrayTypeAnnotation', @@ -158,11 +161,6 @@ export type FunctionTypeAnnotationParamTypeAnnotation = | $ReadOnly<{| type: 'ObjectTypeAnnotation', properties: ?$ReadOnlyArray, - |}> - | $ReadOnly<{| - type: 'FunctionTypeAnnotation', - params: $ReadOnlyArray, - returnTypeAnnotation: FunctionTypeAnnotationReturn, |}>; export type FunctionTypeAnnotationReturnArrayElementType = FunctionTypeAnnotationParamTypeAnnotation; @@ -175,16 +173,15 @@ export type ObjectParamTypeAnnotation = $ReadOnly<{| export type FunctionTypeAnnotationReturn = | $ReadOnly<{| - type: PrimitiveTypeAnnotationType | 'VoidTypeAnnotation', + type: + | PrimitiveTypeAnnotationType + | 'VoidTypeAnnotation' + | 'GenericPromiseTypeAnnotation', |}> | $ReadOnly<{| type: 'ArrayTypeAnnotation', elementType: ?FunctionTypeAnnotationReturnArrayElementType, |}> - | $ReadOnly<{| - type: 'GenericPromiseTypeAnnotation', - resolvedType: FunctionTypeAnnotationReturn, - |}> | $ReadOnly<{| type: 'ObjectTypeAnnotation', properties: ?$ReadOnlyArray, diff --git a/packages/react-native-codegen/src/generators/modules/ObjCppUtils/GenerateStructs.js b/packages/react-native-codegen/src/generators/modules/ObjCppUtils/GenerateStructs.js index 728574f96bef..6ac2ec230d91 100644 --- a/packages/react-native-codegen/src/generators/modules/ObjCppUtils/GenerateStructs.js +++ b/packages/react-native-codegen/src/generators/modules/ObjCppUtils/GenerateStructs.js @@ -119,7 +119,7 @@ function getInlineMethodImplementation( return inlineTemplate .replace( /::_RETURN_TYPE_::/, - 'facebook::react::LazyVector> +', + 'facebook::react::LazyVector> ', ) .replace( /::_RETURN_VALUE_::/, diff --git a/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js index 7866eefccb46..17155728821a 100644 --- a/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js @@ -222,18 +222,6 @@ const SIMPLE_NATIVE_MODULES: SchemaType = { nullable: false, typeAnnotation: { type: 'FunctionTypeAnnotation', - params: [ - { - nullable: false, - name: 'value', - typeAnnotation: { - type: 'StringTypeAnnotation', - }, - }, - ], - returnTypeAnnotation: { - type: 'VoidTypeAnnotation', - }, }, }, ], @@ -246,9 +234,6 @@ const SIMPLE_NATIVE_MODULES: SchemaType = { type: 'FunctionTypeAnnotation', returnTypeAnnotation: { type: 'GenericPromiseTypeAnnotation', - resolvedType: { - type: 'StringTypeAnnotation', - }, }, params: [ { diff --git a/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/failures.js b/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/failures.js index 6242d5b402ea..7529c597f319 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/failures.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/failures.js @@ -82,6 +82,30 @@ export default TurboModuleRegistry.getEnforcing('SampleTurboModule'); `; +const NATIVE_MODULES_WITH_READ_ONLY_OBJECT_NO_TYPE_FOR_CONTENT = ` +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + * @format + */ + +'use strict'; + +import type {TurboModule} from '../RCTExport'; +import * as TurboModuleRegistry from '../TurboModuleRegistry'; + +export interface Spec extends TurboModule { + getString: (arg : $ReadOnly<>) => string; +} + +export default TurboModuleRegistry.getEnforcing('SampleTurboModule'); + +`; + const NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_RETURN = ` /** * Copyright (c) Facebook, Inc. and its affiliates. @@ -209,6 +233,7 @@ export interface Spec2 extends TurboModule { `; module.exports = { + NATIVE_MODULES_WITH_READ_ONLY_OBJECT_NO_TYPE_FOR_CONTENT, NATIVE_MODULES_WITH_PROMISE_WITHOUT_TYPE, NATIVE_MODULES_WITH_ARRAY_WITH_NO_TYPE_FOR_CONTENT_AS_PARAM, NATIVE_MODULES_WITH_ARRAY_WITH_NO_TYPE_FOR_CONTENT, diff --git a/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/fixtures.js index d94178d74e45..348bde6a4d78 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/fixtures.js @@ -57,6 +57,9 @@ export interface Spec extends TurboModule { +getObject: (arg: {|const1: {|const1: boolean|}|}) => {| const1: {|const1: boolean|}, |}; + +getReadOnlyObject: (arg: $ReadOnly<{|const1: $ReadOnly<{|const1: boolean|}>|}>) => $ReadOnly<{| + const1: {|const1: boolean|}, + |}>; +getObject2: (arg: { a: String }) => Object; +getObjectInArray: (arg: {const1: {|const1: boolean|}}) => Array<{| const1: {const1: boolean}, @@ -255,6 +258,7 @@ import * as TurboModuleRegistry from '../TurboModuleRegistry'; export interface Spec extends TurboModule { +getArray: (arg: Array) => Array; + +getArray: (arg: $ReadOnlyArray) => $ReadOnlyArray; } export default TurboModuleRegistry.getEnforcing('SampleTurboModule'); @@ -287,6 +291,11 @@ export interface Spec extends TurboModule { windowPhysicalPixels: DisplayMetricsAndroid, }, |}; + +getConstants2: () => $ReadOnly<{| + +Dimensions: { + windowPhysicalPixels: DisplayMetricsAndroid, + }, + |}>; } export default TurboModuleRegistry.getEnforcing('SampleTurboModule'); diff --git a/packages/react-native-codegen/src/parsers/flow/modules/__tests__/__snapshots__/module-parser-test.js.snap b/packages/react-native-codegen/src/parsers/flow/modules/__tests__/__snapshots__/module-parser-test.js.snap index 41c15d7d2160..753c52591e0e 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/__tests__/__snapshots__/module-parser-test.js.snap +++ b/packages/react-native-codegen/src/parsers/flow/modules/__tests__/__snapshots__/module-parser-test.js.snap @@ -12,6 +12,8 @@ exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_NOT exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_PROMISE_WITHOUT_TYPE 1`] = `"Unsupported return promise type for getBool: expected to find annotation for type of promise content"`; +exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_READ_ONLY_OBJECT_NO_TYPE_FOR_CONTENT 1`] = `"Unsupported param for method \\"getString\\", param \\"arg\\". No type specified for $ReadOnly"`; + exports[`RN Codegen Flow Parser Fails with error message TWO_NATIVE_EXTENDING_TURBO_MODULE 1`] = `"Found two types extending \\"TurboModule\\" is one file. Split them into separated files."`; exports[`RN Codegen Flow Parser Fails with error message TWO_NATIVE_MODULES_EXPORTED_WITH_DEFAULT 1`] = ` @@ -141,6 +143,31 @@ Object { "type": "FunctionTypeAnnotation", }, }, + Object { + "name": "getArray", + "typeAnnotation": Object { + "optional": false, + "params": Array [ + Object { + "name": "arg", + "nullable": false, + "typeAnnotation": Object { + "elementType": Object { + "type": "StringTypeAnnotation", + }, + "type": "ArrayTypeAnnotation", + }, + }, + ], + "returnTypeAnnotation": Object { + "elementType": Object { + "type": "StringTypeAnnotation", + }, + "type": "ArrayTypeAnnotation", + }, + "type": "FunctionTypeAnnotation", + }, + }, ], }, }, @@ -256,31 +283,6 @@ Object { "name": "callback", "nullable": false, "typeAnnotation": Object { - "params": Array [ - Object { - "name": "value", - "nullable": false, - "typeAnnotation": Object { - "type": "StringTypeAnnotation", - }, - }, - Object { - "name": "arr", - "nullable": false, - "typeAnnotation": Object { - "elementType": Object { - "elementType": Object { - "type": "StringTypeAnnotation", - }, - "type": "ArrayTypeAnnotation", - }, - "type": "ArrayTypeAnnotation", - }, - }, - ], - "returnTypeAnnotation": Object { - "type": "VoidTypeAnnotation", - }, "type": "FunctionTypeAnnotation", }, }, @@ -419,6 +421,61 @@ Object { "type": "FunctionTypeAnnotation", }, }, + Object { + "name": "getReadOnlyObject", + "typeAnnotation": Object { + "optional": false, + "params": Array [ + Object { + "name": "arg", + "nullable": false, + "typeAnnotation": Object { + "properties": Array [ + Object { + "name": "const1", + "optional": false, + "typeAnnotation": Object { + "properties": Array [ + Object { + "name": "const1", + "optional": false, + "typeAnnotation": Object { + "type": "BooleanTypeAnnotation", + }, + }, + ], + "type": "ObjectTypeAnnotation", + }, + }, + ], + "type": "ObjectTypeAnnotation", + }, + }, + ], + "returnTypeAnnotation": Object { + "properties": Array [ + Object { + "name": "const1", + "optional": false, + "typeAnnotation": Object { + "properties": Array [ + Object { + "name": "const1", + "optional": false, + "typeAnnotation": Object { + "type": "BooleanTypeAnnotation", + }, + }, + ], + "type": "ObjectTypeAnnotation", + }, + }, + ], + "type": "ObjectTypeAnnotation", + }, + "type": "FunctionTypeAnnotation", + }, + }, Object { "name": "getObject2", "typeAnnotation": Object { @@ -692,6 +749,44 @@ Object { "type": "FunctionTypeAnnotation", }, }, + Object { + "name": "getConstants2", + "typeAnnotation": Object { + "optional": false, + "params": Array [], + "returnTypeAnnotation": Object { + "properties": Array [ + Object { + "name": "Dimensions", + "optional": false, + "typeAnnotation": Object { + "properties": Array [ + Object { + "name": "windowPhysicalPixels", + "optional": false, + "typeAnnotation": Object { + "properties": Array [ + Object { + "name": "width", + "optional": false, + "typeAnnotation": Object { + "type": "NumberTypeAnnotation", + }, + }, + ], + "type": "ObjectTypeAnnotation", + }, + }, + ], + "type": "ObjectTypeAnnotation", + }, + }, + ], + "type": "ObjectTypeAnnotation", + }, + "type": "FunctionTypeAnnotation", + }, + }, ], }, }, @@ -713,9 +808,6 @@ Object { "optional": false, "params": Array [], "returnTypeAnnotation": Object { - "resolvedType": Object { - "type": "StringTypeAnnotation", - }, "type": "GenericPromiseTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -727,9 +819,6 @@ Object { "optional": false, "params": Array [], "returnTypeAnnotation": Object { - "resolvedType": Object { - "type": "StringTypeAnnotation", - }, "type": "GenericPromiseTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -741,18 +830,6 @@ Object { "optional": false, "params": Array [], "returnTypeAnnotation": Object { - "resolvedType": Object { - "properties": Array [ - Object { - "name": "a", - "optional": false, - "typeAnnotation": Object { - "type": "StringTypeAnnotation", - }, - }, - ], - "type": "ObjectTypeAnnotation", - }, "type": "GenericPromiseTypeAnnotation", }, "type": "FunctionTypeAnnotation", diff --git a/packages/react-native-codegen/src/parsers/flow/modules/methods.js b/packages/react-native-codegen/src/parsers/flow/modules/methods.js index b66f52bbbdc5..232db74cb5ef 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/methods.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/methods.js @@ -64,6 +64,7 @@ function getElementTypeForArrayOrObject( switch (type) { case 'Array': + case '$ReadOnlyArray': if ( typeAnnotation.typeParameters && typeAnnotation.typeParameters.params[0] @@ -91,6 +92,25 @@ function getElementTypeForArrayOrObject( type: 'ObjectTypeAnnotation', properties: getObjectProperties(name, typeAnnotation, paramName, types), }; + case '$ReadOnly': + if ( + typeAnnotation.typeParameters.params && + typeAnnotation.typeParameters.params[0] + ) { + return { + type: 'ObjectTypeAnnotation', + properties: getObjectProperties( + name, + typeAnnotation.typeParameters.params[0], + paramName, + types, + ), + }; + } else { + throw new Error( + `Unsupported param for method "${name}", param "${paramName}". No type specified for $ReadOnly`, + ); + } case 'AnyTypeAnnotation': return { type, @@ -152,6 +172,7 @@ function getTypeAnnotationForParam( }, }; case 'Array': + case '$ReadOnlyArray': if ( typeAnnotation.typeParameters && typeAnnotation.typeParameters.params[0] @@ -188,22 +209,35 @@ function getTypeAnnotationForParam( ), }, }; + case '$ReadOnly': + if ( + typeAnnotation.typeParameters.params && + typeAnnotation.typeParameters.params[0] + ) { + return { + nullable, + name: paramName, + typeAnnotation: { + type: 'ObjectTypeAnnotation', + properties: getObjectProperties( + name, + typeAnnotation.typeParameters.params[0], + paramName, + types, + ), + }, + }; + } else { + throw new Error( + `Unsupported param for method "${name}", param "${paramName}". No type specified for $ReadOnly`, + ); + } case 'FunctionTypeAnnotation': - const params = typeAnnotation.params.map(callbackParam => - getTypeAnnotationForParam(name, callbackParam, types), - ); - const returnTypeAnnotation = getReturnTypeAnnotation( - name, - typeAnnotation.returnType, - types, - ); return { name: paramName, nullable, typeAnnotation: { type: 'FunctionTypeAnnotation', - params, - returnTypeAnnotation, }, }; case 'NumberTypeAnnotation': @@ -271,11 +305,6 @@ function getReturnTypeAnnotation( ) { return { type: 'GenericPromiseTypeAnnotation', - resolvedType: getReturnTypeAnnotation( - methodName, - typeAnnotation.typeParameters.params[0], - types, - ), }; } else { throw new Error( @@ -283,6 +312,7 @@ function getReturnTypeAnnotation( ); } case 'Array': + case '$ReadOnlyArray': if ( typeAnnotation.typeParameters && typeAnnotation.typeParameters.params[0] @@ -311,7 +341,25 @@ function getReturnTypeAnnotation( types, ), }; - + case '$ReadOnly': + if ( + typeAnnotation.typeParameters.params && + typeAnnotation.typeParameters.params[0] + ) { + return { + type: 'ObjectTypeAnnotation', + properties: getObjectProperties( + methodName, + typeAnnotation.typeParameters.params[0], + 'returning value', + types, + ), + }; + } else { + throw new Error( + `Unsupported return type for method "${methodName}", No type specified for $ReadOnly`, + ); + } case 'BooleanTypeAnnotation': case 'NumberTypeAnnotation': case 'VoidTypeAnnotation': From 35f81f62c2c9b4b915386accf62f45f711194676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Osadnik?= Date: Thu, 8 Aug 2019 11:09:30 -0700 Subject: [PATCH 2/7] Add handling of nullable return value Summary: Retuned value can be nullable and it need to be handled Reviewed By: RSNara Differential Revision: D16687359 fbshipit-source-id: 7869c4e2b1da69b680b6eade3c88e0558077b705 --- .../modules/NativeNullableTurboModule.js | 25 ++++++++++++ .../react-native-codegen/src/CodegenSchema.js | 3 ++ .../modules/GenerateModuleHObjCpp.js | 39 +++++++++++++++---- .../modules/__test_fixtures__/fixtures.js | 15 +++++++ .../__snapshots__/module-parser-test.js.snap | 27 +++++++++++++ .../src/parsers/flow/modules/methods.js | 18 ++++++++- 6 files changed, 118 insertions(+), 9 deletions(-) create mode 100644 packages/react-native-codegen/e2e/__test_fixtures__/modules/NativeNullableTurboModule.js diff --git a/packages/react-native-codegen/e2e/__test_fixtures__/modules/NativeNullableTurboModule.js b/packages/react-native-codegen/e2e/__test_fixtures__/modules/NativeNullableTurboModule.js new file mode 100644 index 000000000000..b8ca7d2635a6 --- /dev/null +++ b/packages/react-native-codegen/e2e/__test_fixtures__/modules/NativeNullableTurboModule.js @@ -0,0 +1,25 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + * @format + */ + +'use strict'; + +import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport'; +import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry'; + +export interface Spec extends TurboModule { + +getBool: () => ?boolean; + +getNumber: () => ?number; + +getString: () => ?string; + +getArray: () => ?Array; + +getObject: () => ?Object; + +getValueWithPromise: () => ?Promise; +} + +export default TurboModuleRegistry.getEnforcing('SampleTurboModule'); diff --git a/packages/react-native-codegen/src/CodegenSchema.js b/packages/react-native-codegen/src/CodegenSchema.js index 9b77a50de6a8..cf17f07061e0 100644 --- a/packages/react-native-codegen/src/CodegenSchema.js +++ b/packages/react-native-codegen/src/CodegenSchema.js @@ -173,16 +173,19 @@ export type ObjectParamTypeAnnotation = $ReadOnly<{| export type FunctionTypeAnnotationReturn = | $ReadOnly<{| + nullable: boolean, type: | PrimitiveTypeAnnotationType | 'VoidTypeAnnotation' | 'GenericPromiseTypeAnnotation', |}> | $ReadOnly<{| + nullable: boolean, type: 'ArrayTypeAnnotation', elementType: ?FunctionTypeAnnotationReturnArrayElementType, |}> | $ReadOnly<{| + nullable: boolean, type: 'ObjectTypeAnnotation', properties: ?$ReadOnlyArray, |}>; diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleHObjCpp.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleHObjCpp.js index f2555ea6dcdf..52c2aa837157 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleHObjCpp.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleHObjCpp.js @@ -91,15 +91,10 @@ const constants = `- (facebook::react::ModuleConstants)getConstants;`; function translatePrimitiveJSTypeToObjCType( - type: - | FunctionTypeAnnotationParamTypeAnnotation - | FunctionTypeAnnotationReturn, + type: FunctionTypeAnnotationParamTypeAnnotation, error: string, ) { switch (type.type) { - case 'VoidTypeAnnotation': - case 'GenericPromiseTypeAnnotation': - return 'void'; case 'StringTypeAnnotation': return 'NSString *'; case 'NumberTypeAnnotation': @@ -120,6 +115,36 @@ function translatePrimitiveJSTypeToObjCType( throw new Error(error); } } + +function translatePrimitiveJSTypeToObjCTypeForReturn( + type: FunctionTypeAnnotationReturn, + error: string, +) { + function wrapIntoNullableIfNeeded(generatedType: string) { + return type.nullable ? `${generatedType} _Nullable` : generatedType; + } + switch (type.type) { + case 'VoidTypeAnnotation': + case 'GenericPromiseTypeAnnotation': + return 'void'; + case 'StringTypeAnnotation': + return wrapIntoNullableIfNeeded('NSString *'); + case 'NumberTypeAnnotation': + case 'FloatTypeAnnotation': + case 'Int32TypeAnnotation': + return wrapIntoNullableIfNeeded('NSNumber *'); + case 'BooleanTypeAnnotation': + return type.nullable ? 'NSNumber * _Nullable' : 'BOOL'; + case 'GenericObjectTypeAnnotation': + return wrapIntoNullableIfNeeded('NSDictionary *'); + case 'ArrayTypeAnnotation': + return wrapIntoNullableIfNeeded('NSArray> *'); + case 'ObjectTypeAnnotation': + return wrapIntoNullableIfNeeded('NSDictionary *'); + default: + throw new Error(error); + } +} const methodImplementationTemplate = '- (::_RETURN_VALUE_::) ::_PROPERTY_NAME_::::_ARGS_::;'; @@ -193,7 +218,7 @@ module.exports = { .replace('::_PROPERTY_NAME_::', prop.name) .replace( '::_RETURN_VALUE_::', - translatePrimitiveJSTypeToObjCType( + translatePrimitiveJSTypeToObjCTypeForReturn( returnTypeAnnotation, `Unspopported return type for ${prop.name}. Found: ${ prop.typeAnnotation.returnTypeAnnotation.type diff --git a/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js index 17155728821a..a3f6affdb218 100644 --- a/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js @@ -35,6 +35,7 @@ const SIMPLE_NATIVE_MODULES: SchemaType = { typeAnnotation: { type: 'FunctionTypeAnnotation', returnTypeAnnotation: { + nullable: false, type: 'ObjectTypeAnnotation', properties: [ { @@ -69,6 +70,7 @@ const SIMPLE_NATIVE_MODULES: SchemaType = { typeAnnotation: { type: 'FunctionTypeAnnotation', returnTypeAnnotation: { + nullable: false, type: 'VoidTypeAnnotation', }, params: [], @@ -80,6 +82,7 @@ const SIMPLE_NATIVE_MODULES: SchemaType = { typeAnnotation: { type: 'FunctionTypeAnnotation', returnTypeAnnotation: { + nullable: false, type: 'BooleanTypeAnnotation', }, params: [ @@ -99,6 +102,7 @@ const SIMPLE_NATIVE_MODULES: SchemaType = { typeAnnotation: { type: 'FunctionTypeAnnotation', returnTypeAnnotation: { + nullable: false, type: 'NumberTypeAnnotation', }, params: [ @@ -118,6 +122,7 @@ const SIMPLE_NATIVE_MODULES: SchemaType = { typeAnnotation: { type: 'FunctionTypeAnnotation', returnTypeAnnotation: { + nullable: false, type: 'StringTypeAnnotation', }, params: [ @@ -137,6 +142,7 @@ const SIMPLE_NATIVE_MODULES: SchemaType = { typeAnnotation: { type: 'FunctionTypeAnnotation', returnTypeAnnotation: { + nullable: false, type: 'ArrayTypeAnnotation', elementType: { type: 'AnyTypeAnnotation', @@ -162,6 +168,7 @@ const SIMPLE_NATIVE_MODULES: SchemaType = { typeAnnotation: { type: 'FunctionTypeAnnotation', returnTypeAnnotation: { + nullable: false, type: 'GenericObjectTypeAnnotation', }, params: [ @@ -181,6 +188,7 @@ const SIMPLE_NATIVE_MODULES: SchemaType = { typeAnnotation: { type: 'FunctionTypeAnnotation', returnTypeAnnotation: { + nullable: false, type: 'GenericObjectTypeAnnotation', }, params: [ @@ -214,6 +222,7 @@ const SIMPLE_NATIVE_MODULES: SchemaType = { typeAnnotation: { type: 'FunctionTypeAnnotation', returnTypeAnnotation: { + nullable: false, type: 'VoidTypeAnnotation', }, params: [ @@ -233,6 +242,7 @@ const SIMPLE_NATIVE_MODULES: SchemaType = { typeAnnotation: { type: 'FunctionTypeAnnotation', returnTypeAnnotation: { + nullable: false, type: 'GenericPromiseTypeAnnotation', }, params: [ @@ -265,6 +275,7 @@ const TWO_MODULES_SAME_FILE: SchemaType = { typeAnnotation: { type: 'FunctionTypeAnnotation', returnTypeAnnotation: { + nullable: false, type: 'VoidTypeAnnotation', }, params: [], @@ -280,6 +291,7 @@ const TWO_MODULES_SAME_FILE: SchemaType = { typeAnnotation: { type: 'FunctionTypeAnnotation', returnTypeAnnotation: { + nullable: false, type: 'VoidTypeAnnotation', }, params: [], @@ -304,6 +316,7 @@ const TWO_MODULES_DIFFERENT_FILES: SchemaType = { typeAnnotation: { type: 'FunctionTypeAnnotation', returnTypeAnnotation: { + nullable: false, type: 'VoidTypeAnnotation', }, params: [], @@ -323,6 +336,7 @@ const TWO_MODULES_DIFFERENT_FILES: SchemaType = { typeAnnotation: { type: 'FunctionTypeAnnotation', returnTypeAnnotation: { + nullable: false, type: 'VoidTypeAnnotation', }, params: [], @@ -347,6 +361,7 @@ const COMPLEX_OBJECTS: SchemaType = { typeAnnotation: { type: 'FunctionTypeAnnotation', returnTypeAnnotation: { + nullable: false, type: 'ObjectTypeAnnotation', properties: [ { diff --git a/packages/react-native-codegen/src/parsers/flow/modules/__tests__/__snapshots__/module-parser-test.js.snap b/packages/react-native-codegen/src/parsers/flow/modules/__tests__/__snapshots__/module-parser-test.js.snap index 753c52591e0e..9386b7aa364c 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/__tests__/__snapshots__/module-parser-test.js.snap +++ b/packages/react-native-codegen/src/parsers/flow/modules/__tests__/__snapshots__/module-parser-test.js.snap @@ -62,6 +62,7 @@ Object { "elementType": Object { "type": "StringTypeAnnotation", }, + "nullable": false, "type": "ArrayTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -98,6 +99,7 @@ Object { ], "returnTypeAnnotation": Object { "elementType": undefined, + "nullable": false, "type": "ArrayTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -138,6 +140,7 @@ Object { "elementType": Object { "type": "StringTypeAnnotation", }, + "nullable": false, "type": "ArrayTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -163,6 +166,7 @@ Object { "elementType": Object { "type": "StringTypeAnnotation", }, + "nullable": false, "type": "ArrayTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -197,6 +201,7 @@ Object { }, ], "returnTypeAnnotation": Object { + "nullable": false, "type": "VoidTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -216,6 +221,7 @@ Object { }, ], "returnTypeAnnotation": Object { + "nullable": false, "type": "VoidTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -235,6 +241,7 @@ Object { }, ], "returnTypeAnnotation": Object { + "nullable": false, "type": "VoidTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -254,6 +261,7 @@ Object { }, ], "returnTypeAnnotation": Object { + "nullable": false, "type": "VoidTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -288,6 +296,7 @@ Object { }, ], "returnTypeAnnotation": Object { + "nullable": false, "type": "VoidTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -346,6 +355,7 @@ Object { }, "type": "ArrayTypeAnnotation", }, + "nullable": false, "type": "ArrayTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -398,6 +408,7 @@ Object { }, ], "returnTypeAnnotation": Object { + "nullable": false, "properties": Array [ Object { "name": "const1", @@ -453,6 +464,7 @@ Object { }, ], "returnTypeAnnotation": Object { + "nullable": false, "properties": Array [ Object { "name": "const1", @@ -499,6 +511,7 @@ Object { }, ], "returnTypeAnnotation": Object { + "nullable": false, "type": "GenericObjectTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -557,6 +570,7 @@ Object { ], "type": "ObjectTypeAnnotation", }, + "nullable": false, "type": "ArrayTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -583,6 +597,7 @@ Object { "optional": false, "params": Array [], "returnTypeAnnotation": Object { + "nullable": false, "properties": Array [ Object { "name": "isTesting", @@ -691,6 +706,7 @@ Object { }, ], "returnTypeAnnotation": Object { + "nullable": false, "type": "VoidTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -717,6 +733,7 @@ Object { "optional": false, "params": Array [], "returnTypeAnnotation": Object { + "nullable": false, "properties": Array [ Object { "name": "Dimensions", @@ -755,6 +772,7 @@ Object { "optional": false, "params": Array [], "returnTypeAnnotation": Object { + "nullable": false, "properties": Array [ Object { "name": "Dimensions", @@ -808,6 +826,7 @@ Object { "optional": false, "params": Array [], "returnTypeAnnotation": Object { + "nullable": false, "type": "GenericPromiseTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -819,6 +838,7 @@ Object { "optional": false, "params": Array [], "returnTypeAnnotation": Object { + "nullable": false, "type": "GenericPromiseTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -830,6 +850,7 @@ Object { "optional": false, "params": Array [], "returnTypeAnnotation": Object { + "nullable": false, "type": "GenericPromiseTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -864,6 +885,7 @@ Object { }, ], "returnTypeAnnotation": Object { + "nullable": false, "type": "GenericObjectTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -898,6 +920,7 @@ Object { }, ], "returnTypeAnnotation": Object { + "nullable": false, "type": "VoidTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -909,6 +932,7 @@ Object { "optional": false, "params": Array [], "returnTypeAnnotation": Object { + "nullable": false, "type": "VoidTypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -931,6 +955,7 @@ Object { }, ], "returnTypeAnnotation": Object { + "nullable": false, "properties": Array [ Object { "name": "a", @@ -974,6 +999,7 @@ Object { }, ], "returnTypeAnnotation": Object { + "nullable": false, "type": "Int32TypeAnnotation", }, "type": "FunctionTypeAnnotation", @@ -993,6 +1019,7 @@ Object { }, ], "returnTypeAnnotation": Object { + "nullable": false, "type": "FloatTypeAnnotation", }, "type": "FunctionTypeAnnotation", diff --git a/packages/react-native-codegen/src/parsers/flow/modules/methods.js b/packages/react-native-codegen/src/parsers/flow/modules/methods.js index 232db74cb5ef..13e3955c216c 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/methods.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/methods.js @@ -287,8 +287,13 @@ function getReturnTypeAnnotation( returnType, types: TypeMap, ): FunctionTypeAnnotationReturn { - const typeAnnotation = getValueFromTypes(returnType, types); - const type = + let typeAnnotation = getValueFromTypes(returnType, types); + let nullable = false; + if (typeAnnotation.type === 'NullableTypeAnnotation') { + nullable = true; + typeAnnotation = typeAnnotation.typeAnnotation; + } + let type = typeAnnotation.type === 'GenericTypeAnnotation' ? typeAnnotation.id.name : typeAnnotation.type; @@ -297,6 +302,7 @@ function getReturnTypeAnnotation( case 'Object': return { type: 'GenericObjectTypeAnnotation', + nullable, }; case 'Promise': if ( @@ -305,6 +311,7 @@ function getReturnTypeAnnotation( ) { return { type: 'GenericPromiseTypeAnnotation', + nullable, }; } else { throw new Error( @@ -318,6 +325,7 @@ function getReturnTypeAnnotation( typeAnnotation.typeParameters.params[0] ) { return { + nullable, type: 'ArrayTypeAnnotation', elementType: getElementTypeForArrayOrObject( methodName, @@ -333,6 +341,7 @@ function getReturnTypeAnnotation( } case 'ObjectTypeAnnotation': return { + nullable, type: 'ObjectTypeAnnotation', properties: getObjectProperties( methodName, @@ -347,6 +356,7 @@ function getReturnTypeAnnotation( typeAnnotation.typeParameters.params[0] ) { return { + nullable, type: 'ObjectTypeAnnotation', properties: getObjectProperties( methodName, @@ -364,20 +374,24 @@ function getReturnTypeAnnotation( case 'NumberTypeAnnotation': case 'VoidTypeAnnotation': return { + nullable, type, }; case 'StringTypeAnnotation': case 'Stringish': return { + nullable, type: 'StringTypeAnnotation', }; case 'Int32': return { + nullable, type: 'Int32TypeAnnotation', }; case 'Float': return { + nullable, type: 'FloatTypeAnnotation', }; default: From 52c86a96b859c8010dbdf2569bf493807705e846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Osadnik?= Date: Thu, 8 Aug 2019 11:09:30 -0700 Subject: [PATCH 3/7] fallback not understandable types from methods to object Summary: We don't want to make our codegen breaking if type is not existing. In order to it we can always fallback to Object. That's how it currently works in old codegen #Facebook Thare're few places in our internal code where we use `Map` or tuple in these cases Reviewed By: RSNara Differential Revision: D16687360 fbshipit-source-id: bf8aafd3254fc7e18ad0d58ad1a29e2beeb15bf0 --- .../modules/__test_fixtures__/failures.js | 50 ------------------- .../__snapshots__/module-parser-test.js.snap | 4 -- .../src/parsers/flow/modules/methods.js | 41 ++++++--------- 3 files changed, 14 insertions(+), 81 deletions(-) diff --git a/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/failures.js b/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/failures.js index 7529c597f319..4f22450a7e49 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/failures.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/__test_fixtures__/failures.js @@ -10,30 +10,6 @@ 'use strict'; -const NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_PARAM = ` -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow - * @format - */ - -'use strict'; - -import type {TurboModule} from '../RCTExport'; -import * as TurboModuleRegistry from '../TurboModuleRegistry'; - -export interface Spec extends TurboModule { - getString: (arg: NotString) => string; -} - -export default TurboModuleRegistry.getEnforcing('SampleTurboModule'); - -`; - const NATIVE_MODULES_WITH_ARRAY_WITH_NO_TYPE_FOR_CONTENT = ` /** * Copyright (c) Facebook, Inc. and its affiliates. @@ -106,30 +82,6 @@ export default TurboModuleRegistry.getEnforcing('SampleTurboModule'); `; -const NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_RETURN = ` -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow - * @format - */ - -'use strict'; - -import type {TurboModule} from '../RCTExport'; -import * as TurboModuleRegistry from '../TurboModuleRegistry'; - -export interface Spec extends TurboModule { - getString: (arg: NotString) => string; -} - -export default TurboModuleRegistry.getEnforcing('SampleTurboModule'); - -`; - const NATIVE_MODULES_WITH_NOT_ONLY_METHODS = ` /** * Copyright (c) Facebook, Inc. and its affiliates. @@ -238,8 +190,6 @@ module.exports = { NATIVE_MODULES_WITH_ARRAY_WITH_NO_TYPE_FOR_CONTENT_AS_PARAM, NATIVE_MODULES_WITH_ARRAY_WITH_NO_TYPE_FOR_CONTENT, TWO_NATIVE_MODULES_EXPORTED_WITH_DEFAULT, - NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_PARAM, - NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_RETURN, NATIVE_MODULES_WITH_NOT_ONLY_METHODS, TWO_NATIVE_EXTENDING_TURBO_MODULE, }; diff --git a/packages/react-native-codegen/src/parsers/flow/modules/__tests__/__snapshots__/module-parser-test.js.snap b/packages/react-native-codegen/src/parsers/flow/modules/__tests__/__snapshots__/module-parser-test.js.snap index 9386b7aa364c..5b97201c8af8 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/__tests__/__snapshots__/module-parser-test.js.snap +++ b/packages/react-native-codegen/src/parsers/flow/modules/__tests__/__snapshots__/module-parser-test.js.snap @@ -4,10 +4,6 @@ exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_ARR exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_ARRAY_WITH_NO_TYPE_FOR_CONTENT_AS_PARAM 1`] = `"Unsupported type for getString, param: \\"arg\\": expected to find annotation for type of array contents"`; -exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_PARAM 1`] = `"Unsupported param type for method \\"getString\\", param \\"arg\\". Found NotString"`; - -exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_NOT_EXISTING_TYPE_AS_RETURN 1`] = `"Unsupported param type for method \\"getString\\", param \\"arg\\". Found NotString"`; - exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_NOT_ONLY_METHODS 1`] = `"Only methods are supported as module properties. Found BooleanTypeAnnotation in sampleBool"`; exports[`RN Codegen Flow Parser Fails with error message NATIVE_MODULES_WITH_PROMISE_WITHOUT_TYPE 1`] = `"Unsupported return promise type for getBool: expected to find annotation for type of promise content"`; diff --git a/packages/react-native-codegen/src/parsers/flow/modules/methods.js b/packages/react-native-codegen/src/parsers/flow/modules/methods.js index 13e3955c216c..420d6d74fa98 100644 --- a/packages/react-native-codegen/src/parsers/flow/modules/methods.js +++ b/packages/react-native-codegen/src/parsers/flow/modules/methods.js @@ -83,10 +83,6 @@ function getElementTypeForArrayOrObject( `Unsupported type for ${name}, param: "${paramName}": expected to find annotation for type of nested array contents`, ); } - case 'Object': - return { - type: 'GenericObjectTypeAnnotation', - }; case 'ObjectTypeAnnotation': return { type: 'ObjectTypeAnnotation', @@ -137,9 +133,9 @@ function getElementTypeForArrayOrObject( case 'UnionTypeAnnotation': return undefined; default: - throw new Error( - `Unsupported param type for method "${name}", param "${paramName}". Found ${type}`, - ); + return { + type: 'GenericObjectTypeAnnotation', + }; } } @@ -163,14 +159,6 @@ function getTypeAnnotationForParam( : typeAnnotation.type; switch (type) { - case 'Object': - return { - nullable, - name: paramName, - typeAnnotation: { - type: 'GenericObjectTypeAnnotation', - }, - }; case 'Array': case '$ReadOnlyArray': if ( @@ -276,9 +264,13 @@ function getTypeAnnotationForParam( }, }; default: - throw new Error( - `Unsupported param type for method "${name}", param "${paramName}". Found ${type}`, - ); + return { + nullable, + name: paramName, + typeAnnotation: { + type: 'GenericObjectTypeAnnotation', + }, + }; } } @@ -299,11 +291,6 @@ function getReturnTypeAnnotation( : typeAnnotation.type; switch (type) { - case 'Object': - return { - type: 'GenericObjectTypeAnnotation', - nullable, - }; case 'Promise': if ( typeAnnotation.typeParameters && @@ -395,10 +382,10 @@ function getReturnTypeAnnotation( type: 'FloatTypeAnnotation', }; default: - (type: empty); - throw new Error( - `Unsupported return type for method "${methodName}", Found ${type}`, - ); + return { + type: 'GenericObjectTypeAnnotation', + nullable, + }; } } From 0da4612f039325f007ff7c8df4b09b1aec4e5622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Osadnik?= Date: Thu, 8 Aug 2019 11:09:30 -0700 Subject: [PATCH 4/7] change name convention for modules Summary: Following our internal discussion we want to change previously used name convention. Now it looks like: ``` #import ``` Name is a param of `rn_codegen` and `rn_library`. Also, I found it the easiest to move replacing `::_IMPORT_::` into buck rule Reviewed By: fkgozali Differential Revision: D16646616 fbshipit-source-id: 2c33c5b4d1c42b0e6f5a42d9a318bd8bda9745f4 --- packages/react-native-codegen/BUCK | 1 + packages/react-native-codegen/DEFS.bzl | 17 ++++++++-------- .../buck_tests/emptyFile.mm | 4 ++-- .../buck_tests/generate-tests.js | 7 ++++--- .../src/generators/RNCodegen.js | 9 ++++----- .../GenerateComponentDescriptorH.js | 6 +++++- .../components/GenerateComponentHObjCpp.js | 6 +++++- .../components/GenerateEventEmitterCpp.js | 6 +++++- .../components/GenerateEventEmitterH.js | 6 +++++- .../generators/components/GeneratePropsCpp.js | 6 +++++- .../generators/components/GeneratePropsH.js | 6 +++++- .../components/GeneratePropsJavaDelegate.js | 6 +++++- .../components/GeneratePropsJavaInterface.js | 6 +++++- .../components/GenerateShadowNodeCpp.js | 6 +++++- .../components/GenerateShadowNodeH.js | 6 +++++- .../generators/components/GenerateTests.js | 6 +++++- .../GenerateComponentDescriptorH-test.js | 4 +++- .../GenerateComponentHObjCpp-test.js | 4 +++- .../__tests__/GenerateEventEmitterCpp-test.js | 4 +++- .../__tests__/GenerateEventEmitterH-test.js | 4 +++- .../__tests__/GeneratePropsCpp-test.js | 4 +++- .../__tests__/GeneratePropsH-test.js | 4 +++- .../GeneratePropsJavaDelegate-test.js | 4 +++- .../GeneratePropsJavaInterface-test.js | 4 +++- .../__tests__/GenerateShadowNodeCpp-test.js | 4 +++- .../__tests__/GenerateShadowNodeH-test.js | 4 +++- .../__tests__/GenerateTests-test.js | 4 +++- .../generators/modules/GenerateModuleCpp.js | 6 +++++- .../src/generators/modules/GenerateModuleH.js | 6 +++++- .../modules/GenerateModuleHObjCpp.js | 8 ++++++-- .../generators/modules/GenerateModuleMm.js | 13 ++++++++---- .../__tests__/GenerateModuleCpp-test.js | 4 +++- .../modules/__tests__/GenerateModuleH-test.js | 4 +++- .../__tests__/GenerateModuleHObjCpp-test.js | 4 +++- .../__tests__/GenerateModuleMm-test.js | 4 +++- .../GenerateModuleHObjCpp-test.js.snap | 10 +++++----- .../GenerateModuleMm-test.js.snap | 20 +++++++++---------- 37 files changed, 160 insertions(+), 67 deletions(-) diff --git a/packages/react-native-codegen/BUCK b/packages/react-native-codegen/BUCK index 99873ba7ca87..079267dbefe4 100644 --- a/packages/react-native-codegen/BUCK +++ b/packages/react-native-codegen/BUCK @@ -48,6 +48,7 @@ fb_native.genrule( rn_codegen( name = "codegen_tests", + native_module_spec_name = "FBReactNativeTestSpec", schema_target = ":codegen_tests_schema", ) diff --git a/packages/react-native-codegen/DEFS.bzl b/packages/react-native-codegen/DEFS.bzl index 45ba4594c8ca..38d7bd175532 100644 --- a/packages/react-native-codegen/DEFS.bzl +++ b/packages/react-native-codegen/DEFS.bzl @@ -18,6 +18,7 @@ load( ) def rn_codegen( + native_module_spec_name, name = "", schema_target = ""): generate_fixtures_rule_name = "generate_fixtures-{}".format(name) @@ -40,7 +41,7 @@ def rn_codegen( fb_native.genrule( name = generate_fixtures_rule_name, srcs = native.glob(["src/generators/**/*.js"]), - cmd = "$(exe fbsource//xplat/js/react-native-github/packages/react-native-codegen:rn_codegen) $(location {}) {} $OUT".format(schema_target, name), + cmd = "$(exe fbsource//xplat/js/react-native-github/packages/react-native-codegen:rn_codegen) $(location {}) {} $OUT {}".format(schema_target, name, native_module_spec_name), out = "codegenfiles-{}".format(name), ) @@ -125,14 +126,14 @@ def rn_codegen( fb_native.genrule( name = generate_module_hobjcpp_name, - cmd = "cp $(location :{})/RCTNativeModules.h $OUT".format(generate_fixtures_rule_name), - out = "RCTNativeModules.h", + cmd = "cp $(location :{})/{}.h $OUT".format(generate_fixtures_rule_name, native_module_spec_name), + out = "{}.h".format(native_module_spec_name), ) fb_native.genrule( name = generate_module_mm_name, - cmd = "cp $(location :{})/RCTNativeModules.mm $OUT".format(generate_fixtures_rule_name), - out = "RCTNativeModules.mm", + cmd = "cp $(location :{})/{}-generated.mm $OUT".format(generate_fixtures_rule_name, native_module_spec_name), + out = "{}-generated.mm".format(native_module_spec_name), ) # libs @@ -236,10 +237,10 @@ def rn_codegen( ":{}".format(generate_module_hobjcpp_name), ], ios_exported_headers = { - "RCTNativeModules.h": ":{}".format(generate_module_hobjcpp_name), - "RCTNativeModules.mm": ":{}".format(generate_module_mm_name), + "{}.h".format(native_module_spec_name): ":{}".format(generate_module_hobjcpp_name), + "{}-generated.mm".format(native_module_spec_name): ":{}".format(generate_module_mm_name), }, - header_namespace = "react/modules/{}".format(name), + header_namespace = native_module_spec_name, compiler_flags = [ "-fexceptions", "-frtti", diff --git a/packages/react-native-codegen/buck_tests/emptyFile.mm b/packages/react-native-codegen/buck_tests/emptyFile.mm index 32188a252c88..d36f586fc7ed 100644 --- a/packages/react-native-codegen/buck_tests/emptyFile.mm +++ b/packages/react-native-codegen/buck_tests/emptyFile.mm @@ -1,7 +1,7 @@ #import #import -#import -#import +#import +#import // TODO: Import every prop and event to asset they're generated diff --git a/packages/react-native-codegen/buck_tests/generate-tests.js b/packages/react-native-codegen/buck_tests/generate-tests.js index e84ebfa9a1af..2681bddc64a9 100644 --- a/packages/react-native-codegen/buck_tests/generate-tests.js +++ b/packages/react-native-codegen/buck_tests/generate-tests.js @@ -15,9 +15,9 @@ const fs = require('fs'); const mkdirp = require('mkdirp'); const args = process.argv.slice(2); -if (args.length !== 3) { +if (args.length !== 4) { throw new Error( - `Expected to receive path to schema, library name, and output directory. Received ${args.join( + `Expected to receive path to schema, library name, output directory and module spec name. Received ${args.join( ', ', )}`, ); @@ -26,6 +26,7 @@ if (args.length !== 3) { const schemaPath = args[0]; const libraryName = args[1]; const outputDirectory = args[2]; +const moduleSpecName = args[3]; const schemaText = fs.readFileSync(schemaPath, 'utf-8'); @@ -43,7 +44,7 @@ try { } RNCodegen.generate( - {libraryName, schema, outputDirectory}, + {libraryName, schema, outputDirectory, moduleSpecName}, { generators: [ 'descriptors', diff --git a/packages/react-native-codegen/src/generators/RNCodegen.js b/packages/react-native-codegen/src/generators/RNCodegen.js index 2e863ff278d5..0de0f2ec1d10 100644 --- a/packages/react-native-codegen/src/generators/RNCodegen.js +++ b/packages/react-native-codegen/src/generators/RNCodegen.js @@ -42,6 +42,7 @@ type Options = $ReadOnly<{| libraryName: string, schema: SchemaType, outputDirectory: string, + moduleSpecName: string, |}>; type Generators = @@ -50,8 +51,7 @@ type Generators = | 'props' | 'tests' | 'shadow-nodes' - | 'modules' - | 'view-configs'; + | 'modules'; type Config = $ReadOnly<{| generators: Array, @@ -79,7 +79,6 @@ const GENERATORS = { generateShadowNodeCpp.generate, generateShadowNodeH.generate, ], - 'view-configs': [generateViewConfigJs.generate], }; function writeMapToFiles(map: Map, outputDir: string) { @@ -118,7 +117,7 @@ function checkFilesForChanges( module.exports = { generate( - {libraryName, schema, outputDirectory}: Options, + {libraryName, schema, outputDirectory, moduleSpecName}: Options, {generators, test}: Config, ): boolean { schemaValidator.validate(schema); @@ -126,7 +125,7 @@ module.exports = { const generatedFiles = []; for (const name of generators) { for (const generator of GENERATORS[name]) { - generatedFiles.push(...generator(libraryName, schema)); + generatedFiles.push(...generator(libraryName, schema, moduleSpecName)); } } diff --git a/packages/react-native-codegen/src/generators/components/GenerateComponentDescriptorH.js b/packages/react-native-codegen/src/generators/components/GenerateComponentDescriptorH.js index 2656abac66bb..2bd7a7817d2f 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateComponentDescriptorH.js +++ b/packages/react-native-codegen/src/generators/components/GenerateComponentDescriptorH.js @@ -42,7 +42,11 @@ using ::_CLASSNAME_::ComponentDescriptor = ConcreteComponentDescriptor<::_CLASSN `.trim(); module.exports = { - generate(libraryName: string, schema: SchemaType): FilesOutput { + generate( + libraryName: string, + schema: SchemaType, + moduleSpecName: string, + ): FilesOutput { const fileName = 'ComponentDescriptors.h'; const componentDescriptors = Object.keys(schema.modules) diff --git a/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js b/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js index 0f48703efd06..1d8c8e36e4b2 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js +++ b/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js @@ -263,7 +263,11 @@ function generateCommandHandler( } module.exports = { - generate(libraryName: string, schema: SchemaType): FilesOutput { + generate( + libraryName: string, + schema: SchemaType, + moduleSpecName: string, + ): FilesOutput { const fileName = 'RCTComponentViewHelpers.h'; const componentContent = Object.keys(schema.modules) diff --git a/packages/react-native-codegen/src/generators/components/GenerateEventEmitterCpp.js b/packages/react-native-codegen/src/generators/components/GenerateEventEmitterCpp.js index ce967b2adfc0..58ea16790067 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateEventEmitterCpp.js +++ b/packages/react-native-codegen/src/generators/components/GenerateEventEmitterCpp.js @@ -166,7 +166,11 @@ function generateEvent(componentName: string, event): string { } module.exports = { - generate(libraryName: string, schema: SchemaType): FilesOutput { + generate( + libraryName: string, + schema: SchemaType, + moduleSpecName: string, + ): FilesOutput { const moduleComponents: ComponentCollection = Object.keys(schema.modules) .map(moduleName => { const components = schema.modules[moduleName].components; diff --git a/packages/react-native-codegen/src/generators/components/GenerateEventEmitterH.js b/packages/react-native-codegen/src/generators/components/GenerateEventEmitterH.js index 653ed4157549..7444a724874d 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateEventEmitterH.js +++ b/packages/react-native-codegen/src/generators/components/GenerateEventEmitterH.js @@ -221,7 +221,11 @@ function generateEvents(componentName: string, component): string { } module.exports = { - generate(libraryName: string, schema: SchemaType): FilesOutput { + generate( + libraryName: string, + schema: SchemaType, + moduleSpecName: string, + ): FilesOutput { const moduleComponents: ComponentCollection = Object.keys(schema.modules) .map(moduleName => { const components = schema.modules[moduleName].components; diff --git a/packages/react-native-codegen/src/generators/components/GeneratePropsCpp.js b/packages/react-native-codegen/src/generators/components/GeneratePropsCpp.js index ec818ee8ed66..7032b6c1efaf 100644 --- a/packages/react-native-codegen/src/generators/components/GeneratePropsCpp.js +++ b/packages/react-native-codegen/src/generators/components/GeneratePropsCpp.js @@ -81,7 +81,11 @@ function getClassExtendString(component): string { } module.exports = { - generate(libraryName: string, schema: SchemaType): FilesOutput { + generate( + libraryName: string, + schema: SchemaType, + moduleSpecName: string, + ): FilesOutput { const fileName = 'Props.cpp'; const allImports: Set = new Set([ '#include ', diff --git a/packages/react-native-codegen/src/generators/components/GeneratePropsH.js b/packages/react-native-codegen/src/generators/components/GeneratePropsH.js index 94e4bf2a97fe..64ffc352ead9 100644 --- a/packages/react-native-codegen/src/generators/components/GeneratePropsH.js +++ b/packages/react-native-codegen/src/generators/components/GeneratePropsH.js @@ -418,7 +418,11 @@ function getImports(component): Set { } module.exports = { - generate(libraryName: string, schema: SchemaType): FilesOutput { + generate( + libraryName: string, + schema: SchemaType, + moduleSpecName: string, + ): FilesOutput { const fileName = 'Props.h'; const allImports: Set = new Set(); diff --git a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js index ad16a1f614b5..4f48c0cf5660 100644 --- a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js +++ b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaDelegate.js @@ -208,7 +208,11 @@ function generateMethods(propsString, commandsString): string { } module.exports = { - generate(libraryName: string, schema: SchemaType): FilesOutput { + generate( + libraryName: string, + schema: SchemaType, + moduleSpecName: string, + ): FilesOutput { const files = new Map(); Object.keys(schema.modules).forEach(moduleName => { const components = schema.modules[moduleName].components; diff --git a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaInterface.js b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaInterface.js index 27846e460a4a..0ee838f894ea 100644 --- a/packages/react-native-codegen/src/generators/components/GeneratePropsJavaInterface.js +++ b/packages/react-native-codegen/src/generators/components/GeneratePropsJavaInterface.js @@ -158,7 +158,11 @@ function getClassExtendString(component): string { } module.exports = { - generate(libraryName: string, schema: SchemaType): FilesOutput { + generate( + libraryName: string, + schema: SchemaType, + moduleSpecName: string, + ): FilesOutput { const files = new Map(); Object.keys(schema.modules).forEach(moduleName => { const components = schema.modules[moduleName].components; diff --git a/packages/react-native-codegen/src/generators/components/GenerateShadowNodeCpp.js b/packages/react-native-codegen/src/generators/components/GenerateShadowNodeCpp.js index b34401375d53..8784849619f5 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateShadowNodeCpp.js +++ b/packages/react-native-codegen/src/generators/components/GenerateShadowNodeCpp.js @@ -39,7 +39,11 @@ extern const char ::_CLASSNAME_::ComponentName[] = "::_CLASSNAME_::"; `.trim(); module.exports = { - generate(libraryName: string, schema: SchemaType): FilesOutput { + generate( + libraryName: string, + schema: SchemaType, + moduleSpecName: string, + ): FilesOutput { const fileName = 'ShadowNodes.cpp'; const componentNames = Object.keys(schema.modules) diff --git a/packages/react-native-codegen/src/generators/components/GenerateShadowNodeH.js b/packages/react-native-codegen/src/generators/components/GenerateShadowNodeH.js index a224c41324f3..84c22bc31902 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateShadowNodeH.js +++ b/packages/react-native-codegen/src/generators/components/GenerateShadowNodeH.js @@ -49,7 +49,11 @@ using ::_CLASSNAME_::ShadowNode = ConcreteViewShadowNode< `.trim(); module.exports = { - generate(libraryName: string, schema: SchemaType): FilesOutput { + generate( + libraryName: string, + schema: SchemaType, + moduleSpecName: string, + ): FilesOutput { const fileName = 'ShadowNodes.h'; let hasAnyEvents = false; diff --git a/packages/react-native-codegen/src/generators/components/GenerateTests.js b/packages/react-native-codegen/src/generators/components/GenerateTests.js index 62968fad1c72..0f9a8266bd7a 100644 --- a/packages/react-native-codegen/src/generators/components/GenerateTests.js +++ b/packages/react-native-codegen/src/generators/components/GenerateTests.js @@ -133,7 +133,11 @@ function generateTestsString(name, component) { } module.exports = { - generate(libraryName: string, schema: SchemaType): FilesOutput { + generate( + libraryName: string, + schema: SchemaType, + moduleSpecName: string, + ): FilesOutput { const fileName = 'Tests.cpp'; const allImports = new Set([ '#include ', diff --git a/packages/react-native-codegen/src/generators/components/__tests__/GenerateComponentDescriptorH-test.js b/packages/react-native-codegen/src/generators/components/__tests__/GenerateComponentDescriptorH-test.js index 02133c76f905..c73dd315c8bf 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/GenerateComponentDescriptorH-test.js +++ b/packages/react-native-codegen/src/generators/components/__tests__/GenerateComponentDescriptorH-test.js @@ -21,7 +21,9 @@ describe('GenerateComponentDescriptorH', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate(fixtureName, fixture, 'SampleSpec'), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/GenerateComponentHObjCpp-test.js b/packages/react-native-codegen/src/generators/components/__tests__/GenerateComponentHObjCpp-test.js index 52b638973aae..44528dbbb02a 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/GenerateComponentHObjCpp-test.js +++ b/packages/react-native-codegen/src/generators/components/__tests__/GenerateComponentHObjCpp-test.js @@ -21,7 +21,9 @@ describe('GenerateComponentHObjCpp', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate(fixtureName, fixture, 'SampleSpec'), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/GenerateEventEmitterCpp-test.js b/packages/react-native-codegen/src/generators/components/__tests__/GenerateEventEmitterCpp-test.js index 14ac106e959b..5a17690e597b 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/GenerateEventEmitterCpp-test.js +++ b/packages/react-native-codegen/src/generators/components/__tests__/GenerateEventEmitterCpp-test.js @@ -21,7 +21,9 @@ describe('GenerateEventEmitterCpp', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate(fixtureName, fixture, 'SampleSpec'), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/GenerateEventEmitterH-test.js b/packages/react-native-codegen/src/generators/components/__tests__/GenerateEventEmitterH-test.js index 2b6a26422aa3..3b1974f66539 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/GenerateEventEmitterH-test.js +++ b/packages/react-native-codegen/src/generators/components/__tests__/GenerateEventEmitterH-test.js @@ -21,7 +21,9 @@ describe('GenerateEventEmitterH', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate(fixtureName, fixture, 'SampleSpec'), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsCpp-test.js b/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsCpp-test.js index f9b9332a1259..284c5457acad 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsCpp-test.js +++ b/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsCpp-test.js @@ -21,7 +21,9 @@ describe('GeneratePropsCpp', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate(fixtureName, fixture, 'SampleSpec'), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsH-test.js b/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsH-test.js index 77305fc2c4a0..23f8769ebeb8 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsH-test.js +++ b/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsH-test.js @@ -21,7 +21,9 @@ describe('GeneratePropsH', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate(fixtureName, fixture, 'SampleSpec'), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsJavaDelegate-test.js b/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsJavaDelegate-test.js index 36f1f9e8a5ca..b9c5724c914e 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsJavaDelegate-test.js +++ b/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsJavaDelegate-test.js @@ -21,7 +21,9 @@ describe('GeneratePropsJavaDelegate', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate(fixtureName, fixture, 'SampleSpec'), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsJavaInterface-test.js b/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsJavaInterface-test.js index cc1eadab41ab..a14ac875cc7b 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsJavaInterface-test.js +++ b/packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsJavaInterface-test.js @@ -21,7 +21,9 @@ describe('GeneratePropsJavaInterface', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate(fixtureName, fixture, 'SampleSpec'), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/GenerateShadowNodeCpp-test.js b/packages/react-native-codegen/src/generators/components/__tests__/GenerateShadowNodeCpp-test.js index e023ed51a49f..b3cc7efdcab2 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/GenerateShadowNodeCpp-test.js +++ b/packages/react-native-codegen/src/generators/components/__tests__/GenerateShadowNodeCpp-test.js @@ -21,7 +21,9 @@ describe('GenerateShadowNodeCpp', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate(fixtureName, fixture, 'SampleSpec'), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/GenerateShadowNodeH-test.js b/packages/react-native-codegen/src/generators/components/__tests__/GenerateShadowNodeH-test.js index ea704d4773d6..3edd5e3695fd 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/GenerateShadowNodeH-test.js +++ b/packages/react-native-codegen/src/generators/components/__tests__/GenerateShadowNodeH-test.js @@ -21,7 +21,9 @@ describe('GenerateShadowNodeH', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate(fixtureName, fixture, 'SampleSpec'), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/GenerateTests-test.js b/packages/react-native-codegen/src/generators/components/__tests__/GenerateTests-test.js index 6961d0b1754e..8b9f066b0c18 100644 --- a/packages/react-native-codegen/src/generators/components/__tests__/GenerateTests-test.js +++ b/packages/react-native-codegen/src/generators/components/__tests__/GenerateTests-test.js @@ -21,7 +21,9 @@ describe('GenerateTests', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate(fixtureName, fixture, 'SampleSpec'), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleCpp.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleCpp.js index 00bfb6782aa0..52b611455dcf 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleCpp.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleCpp.js @@ -106,7 +106,11 @@ function traverseProprety(property): string { } module.exports = { - generate(libraryName: string, schema: SchemaType): FilesOutput { + generate( + libraryName: string, + schema: SchemaType, + moduleSpecName: string, + ): FilesOutput { const nativeModules = Object.keys(schema.modules) .map(moduleName => { const modules = schema.modules[moduleName].nativeModules; diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js index 4e9809b3172c..d72095939664 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleH.js @@ -84,7 +84,11 @@ const propertyTemplate = 'virtual ::_RETURN_VALUE_:: ::_PROPERTY_NAME_::(jsi::Runtime &rt::_ARGS_::) = 0;'; module.exports = { - generate(libraryName: string, schema: SchemaType): FilesOutput { + generate( + libraryName: string, + schema: SchemaType, + moduleSpecName: string, + ): FilesOutput { const nativeModules = Object.keys(schema.modules) .map(moduleName => { const modules = schema.modules[moduleName].nativeModules; diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleHObjCpp.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleHObjCpp.js index 52c2aa837157..221979e2d97b 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleHObjCpp.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleHObjCpp.js @@ -149,7 +149,11 @@ const methodImplementationTemplate = '- (::_RETURN_VALUE_::) ::_PROPERTY_NAME_::::_ARGS_::;'; module.exports = { - generate(libraryName: string, schema: SchemaType): FilesOutput { + generate( + libraryName: string, + schema: SchemaType, + moduleSpecName: string, + ): FilesOutput { const nativeModules = Object.keys(schema.modules) .map(moduleName => { const modules = schema.modules[moduleName].nativeModules; @@ -243,7 +247,7 @@ module.exports = { }) .join('\n'); - const fileName = 'RCTNativeModules.h'; + const fileName = `${moduleSpecName}.h`; const replacedTemplate = template .replace(/::_MODULES_::/g, modules) .replace(/::_PROTOCOLS_::/g, protocols); diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleMm.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleMm.js index 5acd161969ec..d56905285bed 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleMm.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleMm.js @@ -56,7 +56,7 @@ const template = ` * LICENSE file in the root directory of this source tree. */ -#include +#include <::_INCLUDE_::> ::_GETTERS_:: namespace facebook { namespace react { @@ -128,7 +128,11 @@ function tranlsateMethodForImplementation(property): string { } module.exports = { - generate(libraryName: string, schema: SchemaType): FilesOutput { + generate( + libraryName: string, + schema: SchemaType, + moduleSpecName: string, + ): FilesOutput { const nativeModules: {[name: string]: NativeModuleShape} = Object.keys( schema.modules, ) @@ -221,11 +225,12 @@ module.exports = { }) .join('\n'); - const fileName = 'RCTNativeModules.mm'; + const fileName = `${moduleSpecName}-generated.mm`; const replacedTemplate = template .replace(/::_GETTERS_::/g, gettersImplementations) .replace(/::_MODULES_::/g, modules) - .replace(/::_LIBRARY_NAME_::/g, libraryName); + .replace(/::_LIBRARY_NAME_::/g, libraryName) + .replace(/::_INCLUDE_::/g, `${moduleSpecName}/${moduleSpecName}.h`); return new Map([[fileName, replacedTemplate]]); }, }; diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/GenerateModuleCpp-test.js b/packages/react-native-codegen/src/generators/modules/__tests__/GenerateModuleCpp-test.js index f4a9e1edfa09..6dbe02f952f1 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/GenerateModuleCpp-test.js +++ b/packages/react-native-codegen/src/generators/modules/__tests__/GenerateModuleCpp-test.js @@ -21,7 +21,9 @@ describe('GenerateModuleCpp', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate(fixtureName, fixture, 'SampleSpec'), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/GenerateModuleH-test.js b/packages/react-native-codegen/src/generators/modules/__tests__/GenerateModuleH-test.js index 200eb955e649..918184b9ff50 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/GenerateModuleH-test.js +++ b/packages/react-native-codegen/src/generators/modules/__tests__/GenerateModuleH-test.js @@ -21,7 +21,9 @@ describe('GenerateModuleCpp', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate(fixtureName, fixture, 'SampleSpec'), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/GenerateModuleHObjCpp-test.js b/packages/react-native-codegen/src/generators/modules/__tests__/GenerateModuleHObjCpp-test.js index ae8da6f16ba4..b41441cb2daf 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/GenerateModuleHObjCpp-test.js +++ b/packages/react-native-codegen/src/generators/modules/__tests__/GenerateModuleHObjCpp-test.js @@ -21,7 +21,9 @@ describe('GenerateModuleHObjCpp', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate(fixtureName, fixture, 'SampleSpec'), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/GenerateModuleMm-test.js b/packages/react-native-codegen/src/generators/modules/__tests__/GenerateModuleMm-test.js index 9edb472a33b7..d6a59847b4a4 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/GenerateModuleMm-test.js +++ b/packages/react-native-codegen/src/generators/modules/__tests__/GenerateModuleMm-test.js @@ -21,7 +21,9 @@ describe('GenerateModuleHObjCpp', () => { const fixture = fixtures[fixtureName]; it(`can generate fixture ${fixtureName}`, () => { - expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + expect( + generator.generate(fixtureName, fixture, 'SampleSpec'), + ).toMatchSnapshot(); }); }); }); diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap index 0013895d8679..b46d05b4e447 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap +++ b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap @@ -2,7 +2,7 @@ exports[`GenerateModuleHObjCpp can generate fixture COMPLEX_OBJECTS 1`] = ` Map { - "RCTNativeModules.h" => " + "SampleSpec.h" => " /** * Copyright (c) Facebook, Inc. and its affiliates. * @@ -169,7 +169,7 @@ public: exports[`GenerateModuleHObjCpp can generate fixture EMPTY_NATIVE_MODULES 1`] = ` Map { - "RCTNativeModules.h" => " + "SampleSpec.h" => " /** * Copyright (c) Facebook, Inc. and its affiliates. * @@ -218,7 +218,7 @@ public: exports[`GenerateModuleHObjCpp can generate fixture SIMPLE_NATIVE_MODULES 1`] = ` Map { - "RCTNativeModules.h" => " + "SampleSpec.h" => " /** * Copyright (c) Facebook, Inc. and its affiliates. * @@ -324,7 +324,7 @@ public: exports[`GenerateModuleHObjCpp can generate fixture TWO_MODULES_DIFFERENT_FILES 1`] = ` Map { - "RCTNativeModules.h" => " + "SampleSpec.h" => " /** * Copyright (c) Facebook, Inc. and its affiliates. * @@ -388,7 +388,7 @@ public: exports[`GenerateModuleHObjCpp can generate fixture TWO_MODULES_SAME_FILE 1`] = ` Map { - "RCTNativeModules.h" => " + "SampleSpec.h" => " /** * Copyright (c) Facebook, Inc. and its affiliates. * diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleMm-test.js.snap b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleMm-test.js.snap index 36ee8c57fd65..7ce647467ad5 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleMm-test.js.snap +++ b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleMm-test.js.snap @@ -2,7 +2,7 @@ exports[`GenerateModuleHObjCpp can generate fixture COMPLEX_OBJECTS 1`] = ` Map { - "RCTNativeModules.mm" => " + "SampleSpec-generated.mm" => " /** * Copyright (c) Facebook, Inc. and its affiliates. * @@ -10,7 +10,7 @@ Map { * LICENSE file in the root directory of this source tree. */ -#include +#include @implementation RCTCxxConvert (NativeSampleTurboModule_SpecDifficultReturnType) + (RCTManagedPointer *)JS_NativeSampleTurboModule_SpecDifficultReturnType:(id)json { @@ -51,7 +51,7 @@ NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(id " + "SampleSpec-generated.mm" => " /** * Copyright (c) Facebook, Inc. and its affiliates. * @@ -59,7 +59,7 @@ Map { * LICENSE file in the root directory of this source tree. */ -#include +#include namespace facebook { namespace react { @@ -80,7 +80,7 @@ NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(id " + "SampleSpec-generated.mm" => " /** * Copyright (c) Facebook, Inc. and its affiliates. * @@ -88,7 +88,7 @@ Map { * LICENSE file in the root directory of this source tree. */ -#include +#include namespace facebook { namespace react { @@ -157,7 +157,7 @@ NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(id " + "SampleSpec-generated.mm" => " /** * Copyright (c) Facebook, Inc. and its affiliates. * @@ -165,7 +165,7 @@ Map { * LICENSE file in the root directory of this source tree. */ -#include +#include namespace facebook { @@ -199,7 +199,7 @@ NativeSample2TurboModuleSpecJSI::NativeSample2TurboModuleSpecJSI(id " + "SampleSpec-generated.mm" => " /** * Copyright (c) Facebook, Inc. and its affiliates. * @@ -207,7 +207,7 @@ Map { * LICENSE file in the root directory of this source tree. */ -#include +#include namespace facebook { From 28e668e56e27eb0328ec86ada5edcc9a8216c00d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Osadnik?= Date: Thu, 8 Aug 2019 11:09:30 -0700 Subject: [PATCH 5/7] Add pointer to generated id Summary: It should always be a pointer, sorry! Reviewed By: RSNara Differential Revision: D16689608 fbshipit-source-id: f67d2606b5bdc169d312c1c75748c390ee5e56ed --- .../src/generators/modules/ObjCppUtils/GenerateStructs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-native-codegen/src/generators/modules/ObjCppUtils/GenerateStructs.js b/packages/react-native-codegen/src/generators/modules/ObjCppUtils/GenerateStructs.js index 6ac2ec230d91..2672c762c816 100644 --- a/packages/react-native-codegen/src/generators/modules/ObjCppUtils/GenerateStructs.js +++ b/packages/react-native-codegen/src/generators/modules/ObjCppUtils/GenerateStructs.js @@ -99,7 +99,7 @@ function getInlineMethodImplementation( case 'GenericObjectTypeAnnotation': case 'AnyTypeAnnotation': return inlineTemplate - .replace(/::_RETURN_TYPE_::/, 'id *') + .replace(/::_RETURN_TYPE_::/, 'id ') .replace(/::_RETURN_VALUE_::/, 'p'); case 'ObjectTypeAnnotation': return inlineTemplate From 0dcd57c73c4d0d0368ad77ebde1f93587caa9118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Osadnik?= Date: Thu, 8 Aug 2019 11:09:30 -0700 Subject: [PATCH 6/7] Fix veryfying exports in module codegen Summary: If was breaking in cases like ``` export { x as default } ``` Reviewed By: RSNara Differential Revision: D16689606 fbshipit-source-id: 2583c73c5ac06ea0fa8666d219e739e68fc75b12 --- packages/react-native-codegen/src/parsers/flow/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-native-codegen/src/parsers/flow/index.js b/packages/react-native-codegen/src/parsers/flow/index.js index 11a1b2f33e48..4de02b009439 100644 --- a/packages/react-native-codegen/src/parsers/flow/index.js +++ b/packages/react-native-codegen/src/parsers/flow/index.js @@ -23,7 +23,7 @@ const {processModule} = require('./modules'); function getTypes(ast) { return ast.body.reduce((types, node) => { if (node.type === 'ExportNamedDeclaration') { - if (node.declaration.type !== 'VariableDeclaration') { + if (node.declaration && node.declaration.type !== 'VariableDeclaration') { types[node.declaration.id.name] = node.declaration; } } else if ( From 305b0a28142414d559d2d08795a5963716dc4b0f Mon Sep 17 00:00:00 2001 From: Eli White Date: Thu, 8 Aug 2019 11:49:19 -0700 Subject: [PATCH 7/7] DrawerLayoutAndroid drawerPosition now expects a string, number is deprecated Summary: The native change to support strings was made in D15912607 on June 21st. Migrating the JS callsites now to start passing strings instead of the constants. Reviewed By: zackargyle, mdvacca Differential Revision: D16703569 fbshipit-source-id: cb1d8698df55d2961cde1e2b1fbfcba086a03bb2 --- .../AndroidDrawerLayoutNativeComponent.js | 2 +- .../DrawerAndroid/DrawerLayoutAndroid.android.js | 16 +++++++++------- .../__tests__/DrawerAndroid-test.js | 8 ++++---- .../__snapshots__/DrawerAndroid-test.js.snap | 8 ++++---- RNTester/js/RNTesterApp.android.js | 2 +- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/Libraries/Components/DrawerAndroid/AndroidDrawerLayoutNativeComponent.js b/Libraries/Components/DrawerAndroid/AndroidDrawerLayoutNativeComponent.js index 55ef782ad9b5..9507b691b586 100644 --- a/Libraries/Components/DrawerAndroid/AndroidDrawerLayoutNativeComponent.js +++ b/Libraries/Components/DrawerAndroid/AndroidDrawerLayoutNativeComponent.js @@ -60,7 +60,7 @@ type NativeProps = $ReadOnly<{| /** * Specifies the side of the screen from which the drawer will slide in. */ - drawerPosition: ?Int32, + drawerPosition?: WithDefault<'left' | 'right', 'left'>, /** * Specifies the width of the drawer, more precisely the width of the view that be pulled in diff --git a/Libraries/Components/DrawerAndroid/DrawerLayoutAndroid.android.js b/Libraries/Components/DrawerAndroid/DrawerLayoutAndroid.android.js index f80846d25a03..5807013f55e1 100644 --- a/Libraries/Components/DrawerAndroid/DrawerLayoutAndroid.android.js +++ b/Libraries/Components/DrawerAndroid/DrawerLayoutAndroid.android.js @@ -12,15 +12,11 @@ const Platform = require('../../Utilities/Platform'); const React = require('react'); -const ReactNative = require('../../Renderer/shims/ReactNative'); const StatusBar = require('../StatusBar/StatusBar'); const StyleSheet = require('../../StyleSheet/StyleSheet'); -const UIManager = require('../../ReactNative/UIManager'); const View = require('../View/View'); const nullthrows = require('nullthrows'); -const DrawerConsts = UIManager.getViewManagerConfig('AndroidDrawerLayout') - .Constants; const dismissKeyboard = require('../../Utilities/dismissKeyboard'); import AndroidDrawerLayoutNativeComponent, { Commands, @@ -67,7 +63,7 @@ type Props = $ReadOnly<{| /** * Specifies the side of the screen from which the drawer will slide in. */ - drawerPosition: ?number, + drawerPosition: ?('left' | 'right'), /** * Specifies the width of the drawer, more precisely the width of the view that be pulled in @@ -148,7 +144,7 @@ type State = {| * return ( * navigationView}> * * Hello @@ -160,7 +156,13 @@ type State = {| * ``` */ class DrawerLayoutAndroid extends React.Component { - static positions = DrawerConsts.DrawerPosition; + static get positions(): mixed { + console.warn( + 'Setting DrawerLayoutAndroid drawerPosition using `DrawerLayoutAndroid.positions` is deprecated. Instead pass the string value "left" or "right"', + ); + + return {Left: 'left', Right: 'right'}; + } static defaultProps = { drawerBackgroundColor: 'white', }; diff --git a/Libraries/Components/DrawerAndroid/__tests__/DrawerAndroid-test.js b/Libraries/Components/DrawerAndroid/__tests__/DrawerAndroid-test.js index 514edf85cc44..91c800f59c34 100644 --- a/Libraries/Components/DrawerAndroid/__tests__/DrawerAndroid-test.js +++ b/Libraries/Components/DrawerAndroid/__tests__/DrawerAndroid-test.js @@ -25,7 +25,7 @@ describe('', () => { const instance = render.create( } />, ); @@ -36,7 +36,7 @@ describe('', () => { const output = render.shallow( } />, ); @@ -49,7 +49,7 @@ describe('', () => { const output = render.shallow( } />, ); @@ -62,7 +62,7 @@ describe('', () => { const instance = render.create( } />, ); diff --git a/Libraries/Components/DrawerAndroid/__tests__/__snapshots__/DrawerAndroid-test.js.snap b/Libraries/Components/DrawerAndroid/__tests__/__snapshots__/DrawerAndroid-test.js.snap index f647438e3a1e..88f8eeca1714 100644 --- a/Libraries/Components/DrawerAndroid/__tests__/__snapshots__/DrawerAndroid-test.js.snap +++ b/Libraries/Components/DrawerAndroid/__tests__/__snapshots__/DrawerAndroid-test.js.snap @@ -3,7 +3,7 @@ exports[` should render as when mocked 1`] = ` should render as when moc exports[` should render as when not mocked 1`] = ` should render as when not exports[` should shallow render as when mocked 1`] = ` @@ -116,7 +116,7 @@ exports[` should shallow render as exports[` should shallow render as when not mocked 1`] = ` diff --git a/RNTester/js/RNTesterApp.android.js b/RNTester/js/RNTesterApp.android.js index a7c79047de96..f23e5093a21f 100644 --- a/RNTester/js/RNTesterApp.android.js +++ b/RNTester/js/RNTesterApp.android.js @@ -105,7 +105,7 @@ class RNTesterApp extends React.Component { } return ( {