mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-03-06 17:34:07 +08:00
Support hyphen symbols in enums
Summary: This diff adds support for kebab-case enum properties by transforming them to KebabCase in cpp Reviewed By: mdvacca Differential Revision: D15218781 fbshipit-source-id: 0ec6d28f3ca0e5b8187fc7026e12a8d76be73a7c
This commit is contained in:
committed by
Facebook Github Bot
parent
a5c57b4ed4
commit
43357df5cd
19
packages/react-native-codegen/src/Helpers.js
vendored
19
packages/react-native-codegen/src/Helpers.js
vendored
@@ -1,19 +0,0 @@
|
||||
/**
|
||||
* 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';
|
||||
|
||||
function upperCaseFirst(inString: string): string {
|
||||
return inString[0].toUpperCase() + inString.slice(1);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
upperCaseFirst,
|
||||
};
|
||||
@@ -11,6 +11,17 @@
|
||||
'use strict';
|
||||
import type {ComponentShape} from '../CodegenSchema';
|
||||
|
||||
function upperCaseFirst(inString: string): string {
|
||||
return inString[0].toUpperCase() + inString.slice(1);
|
||||
}
|
||||
|
||||
function toSafeCppString(input: string): string {
|
||||
return input
|
||||
.split('-')
|
||||
.map(upperCaseFirst)
|
||||
.join('');
|
||||
}
|
||||
|
||||
function getCppTypeForAnnotation(
|
||||
type:
|
||||
| 'BooleanTypeAnnotation'
|
||||
@@ -72,4 +83,5 @@ function getImports(component: ComponentShape): Set<string> {
|
||||
module.exports = {
|
||||
getCppTypeForAnnotation,
|
||||
getImports,
|
||||
toSafeCppString,
|
||||
};
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
const {upperCaseFirst} = require('../Helpers.js');
|
||||
const {toSafeCppString} = require('./CppHelpers.js');
|
||||
|
||||
// import type {EventTypeShape} from './CodegenSchema';
|
||||
|
||||
@@ -18,7 +18,7 @@ function generateStructName(
|
||||
componentName: string,
|
||||
parts: $ReadOnlyArray<string> = [],
|
||||
) {
|
||||
const additional = parts.map(upperCaseFirst).join('');
|
||||
const additional = parts.map(toSafeCppString).join('');
|
||||
return `${componentName}${additional}Struct`;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,8 +10,7 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
const {getCppTypeForAnnotation} = require('./CppHelpers.js');
|
||||
const {upperCaseFirst} = require('../Helpers.js');
|
||||
const {getCppTypeForAnnotation, toSafeCppString} = require('./CppHelpers.js');
|
||||
|
||||
import type {PropTypeShape, SchemaType} from '../CodegenSchema';
|
||||
|
||||
@@ -165,7 +164,7 @@ function convertDefaultTypeToString(componentName: string, prop): string {
|
||||
return '';
|
||||
}
|
||||
case 'StringEnumTypeAnnotation':
|
||||
return `${getEnumName(componentName, prop.name)}::${upperCaseFirst(
|
||||
return `${getEnumName(componentName, prop.name)}::${toSafeCppString(
|
||||
typeAnnotation.default,
|
||||
)}`;
|
||||
default:
|
||||
@@ -175,12 +174,12 @@ function convertDefaultTypeToString(componentName: string, prop): string {
|
||||
}
|
||||
|
||||
function getEnumName(componentName, propName): string {
|
||||
const uppercasedPropName = upperCaseFirst(propName);
|
||||
const uppercasedPropName = toSafeCppString(propName);
|
||||
return `${componentName}${uppercasedPropName}`;
|
||||
}
|
||||
|
||||
function convertValueToEnumOption(value: string): string {
|
||||
return upperCaseFirst(value);
|
||||
return toSafeCppString(value);
|
||||
}
|
||||
|
||||
function generateEnumString(componentName: string, component): string {
|
||||
@@ -213,7 +212,7 @@ function generateEnumString(componentName: string, component): string {
|
||||
|
||||
return enumTemplate
|
||||
.replace(/::_ENUM_NAME_::/g, enumName)
|
||||
.replace('::_VALUES_::', values.map(upperCaseFirst).join(', '))
|
||||
.replace('::_VALUES_::', values.map(toSafeCppString).join(', '))
|
||||
.replace('::_FROM_CASES_::', fromCases)
|
||||
.replace('::_TO_CASES_::', toCases);
|
||||
})
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
'use strict';
|
||||
|
||||
import type {SchemaType} from '../CodegenSchema';
|
||||
const {getImports} = require('./CppHelpers');
|
||||
const {getImports, toSafeCppString} = require('./CppHelpers');
|
||||
|
||||
type FilesOutput = Map<string, string>;
|
||||
type TestCase = $ReadOnly<{|
|
||||
@@ -52,7 +52,7 @@ function getTestCasesForProp(propName, typeAnnotation) {
|
||||
typeAnnotation.options.forEach(option =>
|
||||
cases.push({
|
||||
propName,
|
||||
testName: `${propName}_${option.name}`,
|
||||
testName: `${propName}_${toSafeCppString(option.name)}`,
|
||||
propValue: option.name,
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -484,7 +484,7 @@ const ENUM_PROP: SchemaType = {
|
||||
name: 'center',
|
||||
},
|
||||
{
|
||||
name: 'bottom',
|
||||
name: 'bottom-right',
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
@@ -123,13 +123,13 @@ Map {
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
enum class EnumPropsNativeComponentAlignment { Top, Center, Bottom };
|
||||
enum class EnumPropsNativeComponentAlignment { Top, Center, BottomRight };
|
||||
|
||||
static inline void fromRawValue(const RawValue &value, EnumPropsNativeComponentAlignment &result) {
|
||||
auto string = (std::string)value;
|
||||
if (string == \\"top\\") { result = EnumPropsNativeComponentAlignment::Top; return; }
|
||||
if (string == \\"center\\") { result = EnumPropsNativeComponentAlignment::Center; return; }
|
||||
if (string == \\"bottom\\") { result = EnumPropsNativeComponentAlignment::Bottom; return; }
|
||||
if (string == \\"bottom-right\\") { result = EnumPropsNativeComponentAlignment::BottomRight; return; }
|
||||
abort();
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ static inline std::string toString(const EnumPropsNativeComponentAlignment &valu
|
||||
switch (value) {
|
||||
case EnumPropsNativeComponentAlignment::Top: return \\"top\\";
|
||||
case EnumPropsNativeComponentAlignment::Center: return \\"center\\";
|
||||
case EnumPropsNativeComponentAlignment::Bottom: return \\"bottom\\";
|
||||
case EnumPropsNativeComponentAlignment::BottomRight: return \\"bottom-right\\";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -109,23 +109,23 @@ TEST(EnumPropsNativeComponentProps_DoesNotDie, etc) {
|
||||
EnumPropsNativeComponentProps(sourceProps, rawProps);
|
||||
}
|
||||
|
||||
TEST(EnumPropsNativeComponentProps_alignment_top, etc) {
|
||||
TEST(EnumPropsNativeComponentProps_alignment_Top, etc) {
|
||||
auto const &sourceProps = EnumPropsNativeComponentProps();
|
||||
auto const &rawProps = RawProps(folly::dynamic::object(\\"alignment\\", \\"top\\"));
|
||||
|
||||
EnumPropsNativeComponentProps(sourceProps, rawProps);
|
||||
}
|
||||
|
||||
TEST(EnumPropsNativeComponentProps_alignment_center, etc) {
|
||||
TEST(EnumPropsNativeComponentProps_alignment_Center, etc) {
|
||||
auto const &sourceProps = EnumPropsNativeComponentProps();
|
||||
auto const &rawProps = RawProps(folly::dynamic::object(\\"alignment\\", \\"center\\"));
|
||||
|
||||
EnumPropsNativeComponentProps(sourceProps, rawProps);
|
||||
}
|
||||
|
||||
TEST(EnumPropsNativeComponentProps_alignment_bottom, etc) {
|
||||
TEST(EnumPropsNativeComponentProps_alignment_BottomRight, etc) {
|
||||
auto const &sourceProps = EnumPropsNativeComponentProps();
|
||||
auto const &rawProps = RawProps(folly::dynamic::object(\\"alignment\\", \\"bottom\\"));
|
||||
auto const &rawProps = RawProps(folly::dynamic::object(\\"alignment\\", \\"bottom-right\\"));
|
||||
|
||||
EnumPropsNativeComponentProps(sourceProps, rawProps);
|
||||
}",
|
||||
|
||||
Reference in New Issue
Block a user