Generate ObjC component protocols

Summary: We will be generating protocols for every component even if there are no commands. This is for consistency and our ability to add to them later without changing every native component that doesn't currently have a command. JoshuaGross and I figure this is okay as it appears that empty protocols are very cheap on app size

Reviewed By: zackargyle, JoshuaGross

Differential Revision: D16503773

fbshipit-source-id: 11b78fcd33b68926def909d3ce42f58b9bbee96a
This commit is contained in:
Eli White 2019-07-29 14:35:08 -07:00 коммит произвёл Facebook Github Bot
Родитель bd2b7d6c03
Коммит 47365da367
3 изменённых файлов: 590 добавлений и 0 удалений

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

@ -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<string, string>;
const protocolTemplate = `
@protocol ::_COMPONENT_NAME_::ViewProtocol <NSObject>
::_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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
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]]);
},
};

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

@ -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();
});
});
});

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

@ -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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol ArrayPropsNativeComponentViewProtocol <NSObject>
@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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol BooleanPropNativeComponentViewProtocol <NSObject>
@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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol ColorPropNativeComponentViewProtocol <NSObject>
@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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol CommandNativeComponentViewProtocol <NSObject>
- (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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol CommandNativeComponentViewProtocol <NSObject>
- (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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol EnumPropsNativeComponentViewProtocol <NSObject>
@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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol EventsNestedObjectNativeComponentViewProtocol <NSObject>
@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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol EventsNativeComponentViewProtocol <NSObject>
@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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol InterfaceOnlyComponentViewProtocol <NSObject>
@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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol FloatPropNativeComponentViewProtocol <NSObject>
@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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol ImagePropNativeComponentViewProtocol <NSObject>
@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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol IntegerPropNativeComponentViewProtocol <NSObject>
@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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol InterfaceOnlyComponentViewProtocol <NSObject>
@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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol ImageColorPropNativeComponentViewProtocol <NSObject>
@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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol NoPropsNoEventsComponentViewProtocol <NSObject>
@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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol PointPropNativeComponentViewProtocol <NSObject>
@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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol StringPropComponentViewProtocol <NSObject>
@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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol MultiFile1NativeComponentViewProtocol <NSObject>
@end
@protocol MultiFile2NativeComponentViewProtocol <NSObject>
@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 <Foundation/Foundation.h>
#import <React/RCTDefines.h>
#import <React/RCTLog.h>
NS_ASSUME_NONNULL_BEGIN
@protocol MultiComponent1NativeComponentViewProtocol <NSObject>
@end
@protocol MultiComponent2NativeComponentViewProtocol <NSObject>
@end
NS_ASSUME_NONNULL_END",
}
`;