diff --git a/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js b/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js new file mode 100644 index 0000000000..878fa6b3ff --- /dev/null +++ b/packages/react-native-codegen/src/generators/components/GenerateComponentHObjCpp.js @@ -0,0 +1,115 @@ +/** + * 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 strict + * @format + */ + +'use strict'; + +import type { + ComponentShape, + SchemaType, + CommandsFunctionTypeParamAnnotation, +} from '../../CodegenSchema'; + +type FilesOutput = Map; + +const protocolTemplate = ` +@protocol ::_COMPONENT_NAME_::ViewProtocol +::_METHODS_:: +@end +`.trim(); + +const template = ` +/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +::_COMPONENT_CONTENT_:: + +NS_ASSUME_NONNULL_END +`.trim(); + +function getObjCParamType(param: CommandsFunctionTypeParamAnnotation): string { + switch (param.typeAnnotation.type) { + case 'BooleanTypeAnnotation': + return 'BOOL'; + case 'Int32TypeAnnotation': + return 'NSInteger'; + default: + (param.typeAnnotation.type: empty); + throw new Error('Received invalid param type annotation'); + } +} + +function generateProtocol( + component: ComponentShape, + componentName: string, +): string { + const commands = component.commands + .map(command => { + const params = command.typeAnnotation.params; + const paramString = + params.length === 0 + ? '' + : params + .map((param, index) => { + const objCType = getObjCParamType(param); + + return `${index === 0 ? '' : param.name}:(${objCType})${ + param.name + }`; + }) + .join(' '); + return `- (void)${command.name}${paramString};`; + }) + .join('\n') + .trim(); + + return protocolTemplate + .replace(/::_COMPONENT_NAME_::/g, componentName) + .replace('::_METHODS_::', commands); +} + +module.exports = { + generate(libraryName: string, schema: SchemaType): FilesOutput { + const fileName = 'ComponentViewHelpers.h'; + + const componentContent = Object.keys(schema.modules) + .map(moduleName => { + const components = schema.modules[moduleName].components; + // No components in this module + if (components == null) { + return null; + } + + return Object.keys(components) + .map(componentName => { + return generateProtocol(components[componentName], componentName); + }) + .join('\n\n'); + }) + .filter(Boolean) + .join('\n\n'); + + const replacedTemplate = template.replace( + '::_COMPONENT_CONTENT_::', + componentContent, + ); + + return new Map([[fileName, replacedTemplate]]); + }, +}; 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 new file mode 100644 index 0000000000..52b638973a --- /dev/null +++ b/packages/react-native-codegen/src/generators/components/__tests__/GenerateComponentHObjCpp-test.js @@ -0,0 +1,27 @@ +/** + * 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. + * + * @emails oncall+react_native + * @flow strict-local + * @format + */ + +'use strict'; + +const fixtures = require('../__test_fixtures__/fixtures.js'); +const generator = require('../GenerateComponentHObjCpp.js'); + +describe('GenerateComponentHObjCpp', () => { + Object.keys(fixtures) + .sort() + .forEach(fixtureName => { + const fixture = fixtures[fixtureName]; + + it(`can generate fixture ${fixtureName}`, () => { + expect(generator.generate(fixtureName, fixture)).toMatchSnapshot(); + }); + }); +}); diff --git a/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateComponentHObjCpp-test.js.snap b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateComponentHObjCpp-test.js.snap new file mode 100644 index 0000000000..bbebf6857e --- /dev/null +++ b/packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GenerateComponentHObjCpp-test.js.snap @@ -0,0 +1,448 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`GenerateComponentHObjCpp can generate fixture ARRAY_PROPS 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol ArrayPropsNativeComponentViewProtocol + +@end + +NS_ASSUME_NONNULL_END", +} +`; + +exports[`GenerateComponentHObjCpp can generate fixture BOOLEAN_PROP 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol BooleanPropNativeComponentViewProtocol + +@end + +NS_ASSUME_NONNULL_END", +} +`; + +exports[`GenerateComponentHObjCpp can generate fixture COLOR_PROP 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol ColorPropNativeComponentViewProtocol + +@end + +NS_ASSUME_NONNULL_END", +} +`; + +exports[`GenerateComponentHObjCpp can generate fixture COMMANDS 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol CommandNativeComponentViewProtocol +- (void)flashScrollIndicators; +- (void)hotspotUpdate:(NSInteger)x y:(NSInteger)y; +- (void)scrollTo:(NSInteger)y animated:(BOOL)animated; +@end + +NS_ASSUME_NONNULL_END", +} +`; + +exports[`GenerateComponentHObjCpp can generate fixture COMMANDS_AND_PROPS 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol CommandNativeComponentViewProtocol +- (void)hotspotUpdate:(NSInteger)x y:(NSInteger)y; +@end + +NS_ASSUME_NONNULL_END", +} +`; + +exports[`GenerateComponentHObjCpp can generate fixture ENUM_PROP 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol EnumPropsNativeComponentViewProtocol + +@end + +NS_ASSUME_NONNULL_END", +} +`; + +exports[`GenerateComponentHObjCpp can generate fixture EVENT_NESTED_OBJECT_PROPS 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol EventsNestedObjectNativeComponentViewProtocol + +@end + +NS_ASSUME_NONNULL_END", +} +`; + +exports[`GenerateComponentHObjCpp can generate fixture EVENT_PROPS 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol EventsNativeComponentViewProtocol + +@end + +NS_ASSUME_NONNULL_END", +} +`; + +exports[`GenerateComponentHObjCpp can generate fixture EVENTS_WITH_PAPER_NAME 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol InterfaceOnlyComponentViewProtocol + +@end + +NS_ASSUME_NONNULL_END", +} +`; + +exports[`GenerateComponentHObjCpp can generate fixture FLOAT_PROPS 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol FloatPropNativeComponentViewProtocol + +@end + +NS_ASSUME_NONNULL_END", +} +`; + +exports[`GenerateComponentHObjCpp can generate fixture IMAGE_PROP 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol ImagePropNativeComponentViewProtocol + +@end + +NS_ASSUME_NONNULL_END", +} +`; + +exports[`GenerateComponentHObjCpp can generate fixture INTEGER_PROPS 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol IntegerPropNativeComponentViewProtocol + +@end + +NS_ASSUME_NONNULL_END", +} +`; + +exports[`GenerateComponentHObjCpp can generate fixture INTERFACE_ONLY 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol InterfaceOnlyComponentViewProtocol + +@end + +NS_ASSUME_NONNULL_END", +} +`; + +exports[`GenerateComponentHObjCpp can generate fixture MULTI_NATIVE_PROP 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol ImageColorPropNativeComponentViewProtocol + +@end + +NS_ASSUME_NONNULL_END", +} +`; + +exports[`GenerateComponentHObjCpp can generate fixture NO_PROPS_NO_EVENTS 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol NoPropsNoEventsComponentViewProtocol + +@end + +NS_ASSUME_NONNULL_END", +} +`; + +exports[`GenerateComponentHObjCpp can generate fixture POINT_PROP 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol PointPropNativeComponentViewProtocol + +@end + +NS_ASSUME_NONNULL_END", +} +`; + +exports[`GenerateComponentHObjCpp can generate fixture STRING_PROP 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol StringPropComponentViewProtocol + +@end + +NS_ASSUME_NONNULL_END", +} +`; + +exports[`GenerateComponentHObjCpp can generate fixture TWO_COMPONENTS_DIFFERENT_FILES 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol MultiFile1NativeComponentViewProtocol + +@end + +@protocol MultiFile2NativeComponentViewProtocol + +@end + +NS_ASSUME_NONNULL_END", +} +`; + +exports[`GenerateComponentHObjCpp can generate fixture TWO_COMPONENTS_SAME_FILE 1`] = ` +Map { + "ComponentViewHelpers.h" => "/** +* 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. +*/ + +#import +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@protocol MultiComponent1NativeComponentViewProtocol + +@end + +@protocol MultiComponent2NativeComponentViewProtocol + +@end + +NS_ASSUME_NONNULL_END", +} +`;