Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
if (string == \\"top\\") { result = EnumPropNativeComponentViewAlignment::Top; return; }
if (string == \\"center\\") { result = EnumPropNativeComponentViewAlignment::Center; return; }
if (string == \\"bottom-right\\") { result = EnumPropNativeComponentViewAlignment::BottomRight; return; }
abort();
result = EnumPropNativeComponentViewAlignment::Center;
}

static inline std::string toString(const EnumPropNativeComponentViewAlignment &value) {
Expand Down Expand Up @@ -439,7 +439,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
result = EnumPropNativeComponentViewIntervals::Intervals60;
return;
default:
abort();
result = EnumPropNativeComponentViewIntervals::Intervals0;
}
}

Expand Down Expand Up @@ -902,7 +902,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
auto string = (std::string)value;
if (string == \\"small\\") { result = ObjectPropsNativeComponentStringEnumProp::Small; return; }
if (string == \\"large\\") { result = ObjectPropsNativeComponentStringEnumProp::Large; return; }
abort();
result = ObjectPropsNativeComponentStringEnumProp::Small;
}

static inline std::string toString(const ObjectPropsNativeComponentStringEnumProp &value) {
Expand Down Expand Up @@ -932,7 +932,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
result = ObjectPropsNativeComponentIntEnumProp::IntEnumProp1;
return;
default:
abort();
result = ObjectPropsNativeComponentIntEnumProp::IntEnumProp0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
if (string == \\"top\\") { result = EnumPropNativeComponentViewAlignment::Top; return; }
if (string == \\"center\\") { result = EnumPropNativeComponentViewAlignment::Center; return; }
if (string == \\"bottom-right\\") { result = EnumPropNativeComponentViewAlignment::BottomRight; return; }
abort();
result = EnumPropNativeComponentViewAlignment::Center;
}

static inline std::string toString(const EnumPropNativeComponentViewAlignment &value) {
Expand Down Expand Up @@ -439,7 +439,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
result = EnumPropNativeComponentViewIntervals::Intervals60;
return;
default:
abort();
result = EnumPropNativeComponentViewIntervals::Intervals0;
}
}

Expand Down Expand Up @@ -902,7 +902,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
auto string = (std::string)value;
if (string == \\"small\\") { result = ObjectPropsNativeComponentStringEnumProp::Small; return; }
if (string == \\"large\\") { result = ObjectPropsNativeComponentStringEnumProp::Large; return; }
abort();
result = ObjectPropsNativeComponentStringEnumProp::Small;
}

static inline std::string toString(const ObjectPropsNativeComponentStringEnumProp &value) {
Expand Down Expand Up @@ -932,7 +932,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
result = ObjectPropsNativeComponentIntEnumProp::IntEnumProp1;
return;
default:
abort();
result = ObjectPropsNativeComponentIntEnumProp::IntEnumProp0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const {
getNativeTypeFromAnnotation,
} = require('./ComponentsGeneratorUtils.js');
const {
convertDefaultTypeToString,
generateStructName,
getDefaultInitializerString,
getEnumMaskName,
Expand Down Expand Up @@ -106,19 +107,21 @@ const EnumTemplate = ({
values,
fromCases,
toCases,
defaultValue,
}: {
enumName: string,
values: string,
fromCases: string,
toCases: string,
defaultValue: string,
}) =>
`
enum class ${enumName} { ${values} };

static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ${enumName} &result) {
auto string = (std::string)value;
${fromCases}
abort();
result = ${defaultValue};
}

static inline std::string toString(const ${enumName} &value) {
Expand All @@ -142,12 +145,14 @@ const IntEnumTemplate = ({
fromCases,
toCases,
toDynamicCases,
defaultValue,
}: {
enumName: string,
values: string,
fromCases: string,
toCases: string,
toDynamicCases: string,
defaultValue: string,
}) =>
`
enum class ${enumName} { ${values} };
Expand All @@ -157,7 +162,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
auto integerValue = (int)value;
switch (integerValue) {${fromCases}
default:
abort();
result = ${defaultValue};
}
}

Expand Down Expand Up @@ -419,6 +424,7 @@ function generateStringEnum(
values: values.map(toSafeCppString).join(', '),
fromCases: fromCases,
toCases: toCases,
defaultValue: convertDefaultTypeToString(componentName, prop),
});
}

Expand Down Expand Up @@ -474,6 +480,7 @@ function generateIntEnum(
fromCases,
toCases,
toDynamicCases,
defaultValue: convertDefaultTypeToString(componentName, prop),
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

'use strict';

import type {
NamedShape,
PropTypeAnnotation,
SchemaType,
} from '../../../CodegenSchema';

const fixtures = require('../__test_fixtures__/fixtures.js');
const generator = require('../GeneratePropsH.js');

Expand All @@ -23,4 +29,74 @@ describe('GeneratePropsH', () => {
expect(generator.generate(fixtureName, fixture)).toMatchSnapshot();
});
});

it.each(['auto', 'box-none'])(
'uses the declared string enum default %s for unknown values',
defaultValue => {
const header = generatePropsHeader({
name: 'pointerEvents',
optional: true,
typeAnnotation: {
type: 'StringEnumTypeAnnotation',
default: defaultValue,
options: ['none', 'auto', 'box-none'],
},
});
const conversion = header.match(
/static inline void fromRawValue[\s\S]*?\n}/,
)?.[0];

expect(conversion).toContain(
`result = TestComponentPointerEvents::${defaultValue === 'auto' ? 'Auto' : 'BoxNone'};\n}`,
);
expect(conversion).not.toContain('abort()');
},
);

it.each([0, 2])(
'uses the declared integer enum default %s for unknown values',
defaultValue => {
const header = generatePropsHeader({
name: 'mode',
optional: true,
typeAnnotation: {
type: 'Int32EnumTypeAnnotation',
default: defaultValue,
options: [0, 1, 2],
},
});
const conversion = header.match(
/static inline void fromRawValue[\s\S]*?\n}/,
)?.[0];

expect(conversion).toContain(
`default:\n result = TestComponentMode::Mode${defaultValue};`,
);
expect(conversion).not.toContain('abort()');
},
);
});

function generatePropsHeader(prop: NamedShape<PropTypeAnnotation>): string {
const schema: SchemaType = {
modules: {
TestModule: {
type: 'Component',
components: {
TestComponent: {
extendsProps: [
{
type: 'ReactNativeBuiltInType',
knownTypeName: 'ReactNativeCoreViewProps',
},
],
events: [],
props: [prop],
commands: [],
},
},
},
},
};
return generator.generate('TestModule', schema).get('Props.h') ?? '';
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ enum class AllPropStringEnumProp { Option1 };
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, AllPropStringEnumProp &result) {
auto string = (std::string)value;
if (string == \\"option1\\") { result = AllPropStringEnumProp::Option1; return; }
abort();
result = AllPropStringEnumProp::Option1;
}

static inline std::string toString(const AllPropStringEnumProp &value) {
Expand All @@ -53,7 +53,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
result = AllPropIntEnumProp::IntEnumProp0;
return;
default:
abort();
result = AllPropIntEnumProp::IntEnumProp0;
}
}

Expand Down Expand Up @@ -401,7 +401,7 @@ enum class AllPropWithOptionalGenStringEnumProp { Option1 };
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, AllPropWithOptionalGenStringEnumProp &result) {
auto string = (std::string)value;
if (string == \\"option1\\") { result = AllPropWithOptionalGenStringEnumProp::Option1; return; }
abort();
result = AllPropWithOptionalGenStringEnumProp::Option1;
}

static inline std::string toString(const AllPropWithOptionalGenStringEnumProp &value) {
Expand All @@ -427,7 +427,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
result = AllPropWithOptionalGenIntEnumProp::IntEnumProp0;
return;
default:
abort();
result = AllPropWithOptionalGenIntEnumProp::IntEnumProp0;
}
}

Expand Down Expand Up @@ -775,7 +775,7 @@ enum class AllPropWithOptionalObjectGenStringEnumProp { Option1 };
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, AllPropWithOptionalObjectGenStringEnumProp &result) {
auto string = (std::string)value;
if (string == \\"option1\\") { result = AllPropWithOptionalObjectGenStringEnumProp::Option1; return; }
abort();
result = AllPropWithOptionalObjectGenStringEnumProp::Option1;
}

static inline std::string toString(const AllPropWithOptionalObjectGenStringEnumProp &value) {
Expand All @@ -801,7 +801,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
result = AllPropWithOptionalObjectGenIntEnumProp::IntEnumProp0;
return;
default:
abort();
result = AllPropWithOptionalObjectGenIntEnumProp::IntEnumProp0;
}
}

Expand Down Expand Up @@ -3013,7 +3013,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
result = Int32EnumPropsNativeComponentMaxInterval::MaxInterval2;
return;
default:
abort();
result = Int32EnumPropsNativeComponentMaxInterval::MaxInterval0;
}
}

Expand Down Expand Up @@ -3301,7 +3301,7 @@ enum class ObjectPropsStringEnumProp { Option1 };
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsStringEnumProp &result) {
auto string = (std::string)value;
if (string == \\"option1\\") { result = ObjectPropsStringEnumProp::Option1; return; }
abort();
result = ObjectPropsStringEnumProp::Option1;
}

static inline std::string toString(const ObjectPropsStringEnumProp &value) {
Expand All @@ -3327,7 +3327,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
result = ObjectPropsIntEnumProp::IntEnumProp0;
return;
default:
abort();
result = ObjectPropsIntEnumProp::IntEnumProp0;
}
}

Expand Down Expand Up @@ -3735,7 +3735,7 @@ enum class ObjectPropsWithOptionalGenStringEnumProp { Option1 };
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsWithOptionalGenStringEnumProp &result) {
auto string = (std::string)value;
if (string == \\"option1\\") { result = ObjectPropsWithOptionalGenStringEnumProp::Option1; return; }
abort();
result = ObjectPropsWithOptionalGenStringEnumProp::Option1;
}

static inline std::string toString(const ObjectPropsWithOptionalGenStringEnumProp &value) {
Expand All @@ -3761,7 +3761,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
result = ObjectPropsWithOptionalGenIntEnumProp::IntEnumProp0;
return;
default:
abort();
result = ObjectPropsWithOptionalGenIntEnumProp::IntEnumProp0;
}
}

Expand Down Expand Up @@ -4169,7 +4169,7 @@ enum class ObjectPropsWithOptionalObjectGenStringEnumProp { Option1 };
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsWithOptionalObjectGenStringEnumProp &result) {
auto string = (std::string)value;
if (string == \\"option1\\") { result = ObjectPropsWithOptionalObjectGenStringEnumProp::Option1; return; }
abort();
result = ObjectPropsWithOptionalObjectGenStringEnumProp::Option1;
}

static inline std::string toString(const ObjectPropsWithOptionalObjectGenStringEnumProp &value) {
Expand All @@ -4195,7 +4195,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
result = ObjectPropsWithOptionalObjectGenIntEnumProp::IntEnumProp0;
return;
default:
abort();
result = ObjectPropsWithOptionalObjectGenIntEnumProp::IntEnumProp0;
}
}

Expand Down Expand Up @@ -4651,7 +4651,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
if (string == \\"top\\") { result = StringEnumPropsNativeComponentAlignment::Top; return; }
if (string == \\"center\\") { result = StringEnumPropsNativeComponentAlignment::Center; return; }
if (string == \\"bottom-right\\") { result = StringEnumPropsNativeComponentAlignment::BottomRight; return; }
abort();
result = StringEnumPropsNativeComponentAlignment::Center;
}

static inline std::string toString(const StringEnumPropsNativeComponentAlignment &value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package com.facebook.react.uimanager

import com.facebook.common.logging.FLog
import com.facebook.react.common.ReactConstants
import java.util.Locale

/**
Expand All @@ -28,12 +30,21 @@ public enum class PointerEvents {

public companion object {

/**
* Parses pointer events case-insensitively, treating hyphens as underscores. Null and invalid
* values return [AUTO]; invalid values also log a warning.
*/
@JvmStatic
public fun parsePointerEvents(pointerEventsStr: String?): PointerEvents =
if (pointerEventsStr == null) {
AUTO
} else {
PointerEvents.valueOf(pointerEventsStr.uppercase(Locale.US).replace("-", "_"))
try {
PointerEvents.valueOf(pointerEventsStr.uppercase(Locale.US).replace("-", "_"))
} catch (e: IllegalArgumentException) {
FLog.w(ReactConstants.TAG, "Invalid pointerEvents value: $pointerEventsStr")
AUTO
}
}

@JvmStatic
Expand Down
Loading
Loading