Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators #36030

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions packages/react-native-codegen/src/generators/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,27 @@ function indent(nice: string, spaces: number): string {
.join('\n');
}

function toPascalCase(inString: string): string {
if (inString.length === 0) {
return inString;
}

return inString[0].toUpperCase() + inString.slice(1);
}

function toSafeCppString(input: string): string {
return input.split('-').map(toPascalCase).join('');
}

function getEnumName(moduleName: string, origEnumName: string): string {
const uppercasedPropName = toSafeCppString(origEnumName);
return `${moduleName}${uppercasedPropName}`;
}

module.exports = {
capitalize,
indent,
toPascalCase,
toSafeCppString,
getEnumName,
};
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import type {
const {
getCppTypeForAnnotation,
getEnumMaskName,
getEnumName,
generateStructName,
getImports,
} = require('./CppHelpers.js');

const {getEnumName} = require('../Utils');

function getNativeTypeFromAnnotation(
componentName: string,
prop:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@
'use strict';
import type {NamedShape, PropTypeAnnotation} from '../../CodegenSchema';

function upperCaseFirst(inString: string): string {
if (inString.length === 0) {
return inString;
}

return inString[0].toUpperCase() + inString.slice(1);
}

function toSafeCppString(input: string): string {
return input.split('-').map(upperCaseFirst).join('');
}
const {getEnumName, toSafeCppString} = require('../Utils');

function toIntEnumValueName(propName: string, value: number): string {
return `${toSafeCppString(propName)}${value}`;
Expand Down Expand Up @@ -124,11 +114,6 @@ function generateStructName(
return `${componentName}${additional}Struct`;
}

function getEnumName(componentName: string, propName: string): string {
const uppercasedPropName = toSafeCppString(propName);
return `${componentName}${uppercasedPropName}`;
}

function getEnumMaskName(enumName: string): string {
return `${enumName}Mask`;
}
Expand Down Expand Up @@ -224,10 +209,8 @@ function convertDefaultTypeToString(
module.exports = {
convertDefaultTypeToString,
getCppTypeForAnnotation,
getEnumName,
getEnumMaskName,
getImports,
toSafeCppString,
toIntEnumValueName,
generateStructName,
generateEventStructName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ const nullthrows = require('nullthrows');

const {
getCppTypeForAnnotation,
toSafeCppString,
generateEventStructName,
} = require('./CppHelpers');
const {indent} = require('../Utils');
const {indent, toSafeCppString} = require('../Utils');

import type {
ComponentShape,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ const {
const {
convertDefaultTypeToString,
getEnumMaskName,
getEnumName,
toSafeCppString,
generateStructName,
toIntEnumValueName,
} = require('./CppHelpers.js');

const {getEnumName, toSafeCppString} = require('../Utils');

import type {
ExtendsPropsShape,
NamedShape,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import type {ComponentShape, PropTypeAnnotation} from '../../CodegenSchema';
import type {SchemaType} from '../../CodegenSchema';

const {getImports, toSafeCppString} = require('./CppHelpers');
const {getImports} = require('./CppHelpers');

const {toSafeCppString} = require('../Utils');

type FilesOutput = Map<string, string>;
type PropValueType = string | number | boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
NativeModuleFunctionTypeAnnotation,
NativeModuleParamTypeAnnotation,
NativeModuleTypeAnnotation,
NativeModuleEnumMap,
} from '../../CodegenSchema';

import type {AliasResolver} from './Utils';
Expand Down Expand Up @@ -114,9 +115,11 @@ ${modules}
type Param = NamedShape<Nullable<NativeModuleParamTypeAnnotation>>;

function serializeArg(
moduleName: string,
arg: Param,
index: number,
resolveAlias: AliasResolver,
enumMap: NativeModuleEnumMap,
): string {
const {typeAnnotation: nullableTypeAnnotation, optional} = arg;
const [typeAnnotation, nullable] =
Expand Down Expand Up @@ -208,9 +211,11 @@ function serializeArg(
}

function serializePropertyIntoHostFunction(
moduleName: string,
hasteModuleName: string,
property: NativeModulePropertyShape,
resolveAlias: AliasResolver,
enumMap: NativeModuleEnumMap,
): string {
const [propertyTypeAnnotation] =
unwrapNullable<NativeModuleFunctionTypeAnnotation>(property.typeAnnotation);
Expand All @@ -220,7 +225,7 @@ function serializePropertyIntoHostFunction(
methodName: property.name,
returnTypeAnnotation: propertyTypeAnnotation.returnTypeAnnotation,
args: propertyTypeAnnotation.params.map((p, i) =>
serializeArg(p, i, resolveAlias),
serializeArg(moduleName, p, i, resolveAlias, enumMap),
),
});
}
Expand All @@ -239,15 +244,18 @@ module.exports = {
const nativeModule = nativeModules[hasteModuleName];
const {
aliasMap,
enumMap,
spec: {properties},
moduleName,
} = nativeModule;
const resolveAlias = createAliasResolver(aliasMap);
const hostFunctions = properties.map(property =>
serializePropertyIntoHostFunction(
moduleName,
hasteModuleName,
property,
resolveAlias,
enumMap,
),
);

Expand Down
Loading