Skip to content

Commit 43357df

Browse files
rickhanloniifacebook-github-bot
authored andcommitted
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
1 parent a5c57b4 commit 43357df

File tree

8 files changed

+29
-37
lines changed

8 files changed

+29
-37
lines changed

packages/react-native-codegen/src/Helpers.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

packages/react-native-codegen/src/generators/CppHelpers.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
'use strict';
1212
import type {ComponentShape} from '../CodegenSchema';
1313

14+
function upperCaseFirst(inString: string): string {
15+
return inString[0].toUpperCase() + inString.slice(1);
16+
}
17+
18+
function toSafeCppString(input: string): string {
19+
return input
20+
.split('-')
21+
.map(upperCaseFirst)
22+
.join('');
23+
}
24+
1425
function getCppTypeForAnnotation(
1526
type:
1627
| 'BooleanTypeAnnotation'
@@ -72,4 +83,5 @@ function getImports(component: ComponentShape): Set<string> {
7283
module.exports = {
7384
getCppTypeForAnnotation,
7485
getImports,
86+
toSafeCppString,
7587
};

packages/react-native-codegen/src/generators/EventEmitterHelpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010

1111
'use strict';
1212

13-
const {upperCaseFirst} = require('../Helpers.js');
13+
const {toSafeCppString} = require('./CppHelpers.js');
1414

1515
// import type {EventTypeShape} from './CodegenSchema';
1616

1717
function generateStructName(
1818
componentName: string,
1919
parts: $ReadOnlyArray<string> = [],
2020
) {
21-
const additional = parts.map(upperCaseFirst).join('');
21+
const additional = parts.map(toSafeCppString).join('');
2222
return `${componentName}${additional}Struct`;
2323
}
2424

packages/react-native-codegen/src/generators/GeneratePropsH.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
'use strict';
1212

13-
const {getCppTypeForAnnotation} = require('./CppHelpers.js');
14-
const {upperCaseFirst} = require('../Helpers.js');
13+
const {getCppTypeForAnnotation, toSafeCppString} = require('./CppHelpers.js');
1514

1615
import type {PropTypeShape, SchemaType} from '../CodegenSchema';
1716

@@ -165,7 +164,7 @@ function convertDefaultTypeToString(componentName: string, prop): string {
165164
return '';
166165
}
167166
case 'StringEnumTypeAnnotation':
168-
return `${getEnumName(componentName, prop.name)}::${upperCaseFirst(
167+
return `${getEnumName(componentName, prop.name)}::${toSafeCppString(
169168
typeAnnotation.default,
170169
)}`;
171170
default:
@@ -175,12 +174,12 @@ function convertDefaultTypeToString(componentName: string, prop): string {
175174
}
176175

177176
function getEnumName(componentName, propName): string {
178-
const uppercasedPropName = upperCaseFirst(propName);
177+
const uppercasedPropName = toSafeCppString(propName);
179178
return `${componentName}${uppercasedPropName}`;
180179
}
181180

182181
function convertValueToEnumOption(value: string): string {
183-
return upperCaseFirst(value);
182+
return toSafeCppString(value);
184183
}
185184

186185
function generateEnumString(componentName: string, component): string {
@@ -213,7 +212,7 @@ function generateEnumString(componentName: string, component): string {
213212

214213
return enumTemplate
215214
.replace(/::_ENUM_NAME_::/g, enumName)
216-
.replace('::_VALUES_::', values.map(upperCaseFirst).join(', '))
215+
.replace('::_VALUES_::', values.map(toSafeCppString).join(', '))
217216
.replace('::_FROM_CASES_::', fromCases)
218217
.replace('::_TO_CASES_::', toCases);
219218
})

packages/react-native-codegen/src/generators/GenerateTests.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
'use strict';
1212

1313
import type {SchemaType} from '../CodegenSchema';
14-
const {getImports} = require('./CppHelpers');
14+
const {getImports, toSafeCppString} = require('./CppHelpers');
1515

1616
type FilesOutput = Map<string, string>;
1717
type TestCase = $ReadOnly<{|
@@ -52,7 +52,7 @@ function getTestCasesForProp(propName, typeAnnotation) {
5252
typeAnnotation.options.forEach(option =>
5353
cases.push({
5454
propName,
55-
testName: `${propName}_${option.name}`,
55+
testName: `${propName}_${toSafeCppString(option.name)}`,
5656
propValue: option.name,
5757
}),
5858
);

packages/react-native-codegen/src/generators/__test_fixtures__/fixtures.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ const ENUM_PROP: SchemaType = {
484484
name: 'center',
485485
},
486486
{
487-
name: 'bottom',
487+
name: 'bottom-right',
488488
},
489489
],
490490
},

packages/react-native-codegen/src/generators/__tests__/__snapshots__/GeneratePropsH-test.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,21 @@ Map {
123123
namespace facebook {
124124
namespace react {
125125
126-
enum class EnumPropsNativeComponentAlignment { Top, Center, Bottom };
126+
enum class EnumPropsNativeComponentAlignment { Top, Center, BottomRight };
127127
128128
static inline void fromRawValue(const RawValue &value, EnumPropsNativeComponentAlignment &result) {
129129
auto string = (std::string)value;
130130
if (string == \\"top\\") { result = EnumPropsNativeComponentAlignment::Top; return; }
131131
if (string == \\"center\\") { result = EnumPropsNativeComponentAlignment::Center; return; }
132-
if (string == \\"bottom\\") { result = EnumPropsNativeComponentAlignment::Bottom; return; }
132+
if (string == \\"bottom-right\\") { result = EnumPropsNativeComponentAlignment::BottomRight; return; }
133133
abort();
134134
}
135135
136136
static inline std::string toString(const EnumPropsNativeComponentAlignment &value) {
137137
switch (value) {
138138
case EnumPropsNativeComponentAlignment::Top: return \\"top\\";
139139
case EnumPropsNativeComponentAlignment::Center: return \\"center\\";
140-
case EnumPropsNativeComponentAlignment::Bottom: return \\"bottom\\";
140+
case EnumPropsNativeComponentAlignment::BottomRight: return \\"bottom-right\\";
141141
}
142142
}
143143

packages/react-native-codegen/src/generators/__tests__/__snapshots__/GenerateTests-test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,23 +109,23 @@ TEST(EnumPropsNativeComponentProps_DoesNotDie, etc) {
109109
EnumPropsNativeComponentProps(sourceProps, rawProps);
110110
}
111111
112-
TEST(EnumPropsNativeComponentProps_alignment_top, etc) {
112+
TEST(EnumPropsNativeComponentProps_alignment_Top, etc) {
113113
auto const &sourceProps = EnumPropsNativeComponentProps();
114114
auto const &rawProps = RawProps(folly::dynamic::object(\\"alignment\\", \\"top\\"));
115115
116116
EnumPropsNativeComponentProps(sourceProps, rawProps);
117117
}
118118
119-
TEST(EnumPropsNativeComponentProps_alignment_center, etc) {
119+
TEST(EnumPropsNativeComponentProps_alignment_Center, etc) {
120120
auto const &sourceProps = EnumPropsNativeComponentProps();
121121
auto const &rawProps = RawProps(folly::dynamic::object(\\"alignment\\", \\"center\\"));
122122
123123
EnumPropsNativeComponentProps(sourceProps, rawProps);
124124
}
125125
126-
TEST(EnumPropsNativeComponentProps_alignment_bottom, etc) {
126+
TEST(EnumPropsNativeComponentProps_alignment_BottomRight, etc) {
127127
auto const &sourceProps = EnumPropsNativeComponentProps();
128-
auto const &rawProps = RawProps(folly::dynamic::object(\\"alignment\\", \\"bottom\\"));
128+
auto const &rawProps = RawProps(folly::dynamic::object(\\"alignment\\", \\"bottom-right\\"));
129129
130130
EnumPropsNativeComponentProps(sourceProps, rawProps);
131131
}",

0 commit comments

Comments
 (0)