Skip to content

Commit 3d3a339

Browse files
authored
Default invalid pointerEvents values to auto
Summary: Invalid Android pointerEvents values fall back to `AUTO` and log a warning instead of throwing. Null still resets the default silently, and valid case/hyphen variants retain their existing behaviour. Changelog: [Android][Fixed] - Default invalid pointerEvents values to auto with a warning Differential Revision: D129603915 Pull Request resolved: #59794
1 parent 480fe97 commit 3d3a339

7 files changed

Lines changed: 181 additions & 25 deletions

File tree

‎packages/react-native-codegen/e2e/deep_imports/__tests__/components/__snapshots__/GeneratePropsH-test.js.snap‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
402402
if (string == \\"top\\") { result = EnumPropNativeComponentViewAlignment::Top; return; }
403403
if (string == \\"center\\") { result = EnumPropNativeComponentViewAlignment::Center; return; }
404404
if (string == \\"bottom-right\\") { result = EnumPropNativeComponentViewAlignment::BottomRight; return; }
405-
abort();
405+
result = EnumPropNativeComponentViewAlignment::Center;
406406
}
407407
408408
static inline std::string toString(const EnumPropNativeComponentViewAlignment &value) {
@@ -439,7 +439,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
439439
result = EnumPropNativeComponentViewIntervals::Intervals60;
440440
return;
441441
default:
442-
abort();
442+
result = EnumPropNativeComponentViewIntervals::Intervals0;
443443
}
444444
}
445445
@@ -902,7 +902,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
902902
auto string = (std::string)value;
903903
if (string == \\"small\\") { result = ObjectPropsNativeComponentStringEnumProp::Small; return; }
904904
if (string == \\"large\\") { result = ObjectPropsNativeComponentStringEnumProp::Large; return; }
905-
abort();
905+
result = ObjectPropsNativeComponentStringEnumProp::Small;
906906
}
907907
908908
static inline std::string toString(const ObjectPropsNativeComponentStringEnumProp &value) {
@@ -932,7 +932,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
932932
result = ObjectPropsNativeComponentIntEnumProp::IntEnumProp1;
933933
return;
934934
default:
935-
abort();
935+
result = ObjectPropsNativeComponentIntEnumProp::IntEnumProp0;
936936
}
937937
}
938938

‎packages/react-native-codegen/e2e/namespaced/__tests__/components/__snapshots__/GeneratePropsH-test.js.snap‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
402402
if (string == \\"top\\") { result = EnumPropNativeComponentViewAlignment::Top; return; }
403403
if (string == \\"center\\") { result = EnumPropNativeComponentViewAlignment::Center; return; }
404404
if (string == \\"bottom-right\\") { result = EnumPropNativeComponentViewAlignment::BottomRight; return; }
405-
abort();
405+
result = EnumPropNativeComponentViewAlignment::Center;
406406
}
407407
408408
static inline std::string toString(const EnumPropNativeComponentViewAlignment &value) {
@@ -439,7 +439,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
439439
result = EnumPropNativeComponentViewIntervals::Intervals60;
440440
return;
441441
default:
442-
abort();
442+
result = EnumPropNativeComponentViewIntervals::Intervals0;
443443
}
444444
}
445445
@@ -902,7 +902,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
902902
auto string = (std::string)value;
903903
if (string == \\"small\\") { result = ObjectPropsNativeComponentStringEnumProp::Small; return; }
904904
if (string == \\"large\\") { result = ObjectPropsNativeComponentStringEnumProp::Large; return; }
905-
abort();
905+
result = ObjectPropsNativeComponentStringEnumProp::Small;
906906
}
907907
908908
static inline std::string toString(const ObjectPropsNativeComponentStringEnumProp &value) {
@@ -932,7 +932,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
932932
result = ObjectPropsNativeComponentIntEnumProp::IntEnumProp1;
933933
return;
934934
default:
935-
abort();
935+
result = ObjectPropsNativeComponentIntEnumProp::IntEnumProp0;
936936
}
937937
}
938938

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const {
2323
getNativeTypeFromAnnotation,
2424
} = require('./ComponentsGeneratorUtils.js');
2525
const {
26+
convertDefaultTypeToString,
2627
generateStructName,
2728
getDefaultInitializerString,
2829
getEnumMaskName,
@@ -106,19 +107,21 @@ const EnumTemplate = ({
106107
values,
107108
fromCases,
108109
toCases,
110+
defaultValue,
109111
}: {
110112
enumName: string,
111113
values: string,
112114
fromCases: string,
113115
toCases: string,
116+
defaultValue: string,
114117
}) =>
115118
`
116119
enum class ${enumName} { ${values} };
117120
118121
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ${enumName} &result) {
119122
auto string = (std::string)value;
120123
${fromCases}
121-
abort();
124+
result = ${defaultValue};
122125
}
123126
124127
static inline std::string toString(const ${enumName} &value) {
@@ -142,12 +145,14 @@ const IntEnumTemplate = ({
142145
fromCases,
143146
toCases,
144147
toDynamicCases,
148+
defaultValue,
145149
}: {
146150
enumName: string,
147151
values: string,
148152
fromCases: string,
149153
toCases: string,
150154
toDynamicCases: string,
155+
defaultValue: string,
151156
}) =>
152157
`
153158
enum class ${enumName} { ${values} };
@@ -157,7 +162,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
157162
auto integerValue = (int)value;
158163
switch (integerValue) {${fromCases}
159164
default:
160-
abort();
165+
result = ${defaultValue};
161166
}
162167
}
163168
@@ -419,6 +424,7 @@ function generateStringEnum(
419424
values: values.map(toSafeCppString).join(', '),
420425
fromCases: fromCases,
421426
toCases: toCases,
427+
defaultValue: convertDefaultTypeToString(componentName, prop),
422428
});
423429
}
424430

@@ -474,6 +480,7 @@ function generateIntEnum(
474480
fromCases,
475481
toCases,
476482
toDynamicCases,
483+
defaultValue: convertDefaultTypeToString(componentName, prop),
477484
});
478485
}
479486

‎packages/react-native-codegen/src/generators/components/__tests__/GeneratePropsH-test.js‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010

1111
'use strict';
1212

13+
import type {
14+
NamedShape,
15+
PropTypeAnnotation,
16+
SchemaType,
17+
} from '../../../CodegenSchema';
18+
1319
const fixtures = require('../__test_fixtures__/fixtures.js');
1420
const generator = require('../GeneratePropsH.js');
1521

@@ -23,4 +29,74 @@ describe('GeneratePropsH', () => {
2329
expect(generator.generate(fixtureName, fixture)).toMatchSnapshot();
2430
});
2531
});
32+
33+
it.each(['auto', 'box-none'])(
34+
'uses the declared string enum default %s for unknown values',
35+
defaultValue => {
36+
const header = generatePropsHeader({
37+
name: 'pointerEvents',
38+
optional: true,
39+
typeAnnotation: {
40+
type: 'StringEnumTypeAnnotation',
41+
default: defaultValue,
42+
options: ['none', 'auto', 'box-none'],
43+
},
44+
});
45+
const conversion = header.match(
46+
/static inline void fromRawValue[\s\S]*?\n}/,
47+
)?.[0];
48+
49+
expect(conversion).toContain(
50+
`result = TestComponentPointerEvents::${defaultValue === 'auto' ? 'Auto' : 'BoxNone'};\n}`,
51+
);
52+
expect(conversion).not.toContain('abort()');
53+
},
54+
);
55+
56+
it.each([0, 2])(
57+
'uses the declared integer enum default %s for unknown values',
58+
defaultValue => {
59+
const header = generatePropsHeader({
60+
name: 'mode',
61+
optional: true,
62+
typeAnnotation: {
63+
type: 'Int32EnumTypeAnnotation',
64+
default: defaultValue,
65+
options: [0, 1, 2],
66+
},
67+
});
68+
const conversion = header.match(
69+
/static inline void fromRawValue[\s\S]*?\n}/,
70+
)?.[0];
71+
72+
expect(conversion).toContain(
73+
`default:\n result = TestComponentMode::Mode${defaultValue};`,
74+
);
75+
expect(conversion).not.toContain('abort()');
76+
},
77+
);
2678
});
79+
80+
function generatePropsHeader(prop: NamedShape<PropTypeAnnotation>): string {
81+
const schema: SchemaType = {
82+
modules: {
83+
TestModule: {
84+
type: 'Component',
85+
components: {
86+
TestComponent: {
87+
extendsProps: [
88+
{
89+
type: 'ReactNativeBuiltInType',
90+
knownTypeName: 'ReactNativeCoreViewProps',
91+
},
92+
],
93+
events: [],
94+
props: [prop],
95+
commands: [],
96+
},
97+
},
98+
},
99+
},
100+
};
101+
return generator.generate('TestModule', schema).get('Props.h') ?? '';
102+
}

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ enum class AllPropStringEnumProp { Option1 };
2727
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, AllPropStringEnumProp &result) {
2828
auto string = (std::string)value;
2929
if (string == \\"option1\\") { result = AllPropStringEnumProp::Option1; return; }
30-
abort();
30+
result = AllPropStringEnumProp::Option1;
3131
}
3232
3333
static inline std::string toString(const AllPropStringEnumProp &value) {
@@ -53,7 +53,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
5353
result = AllPropIntEnumProp::IntEnumProp0;
5454
return;
5555
default:
56-
abort();
56+
result = AllPropIntEnumProp::IntEnumProp0;
5757
}
5858
}
5959
@@ -401,7 +401,7 @@ enum class AllPropWithOptionalGenStringEnumProp { Option1 };
401401
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, AllPropWithOptionalGenStringEnumProp &result) {
402402
auto string = (std::string)value;
403403
if (string == \\"option1\\") { result = AllPropWithOptionalGenStringEnumProp::Option1; return; }
404-
abort();
404+
result = AllPropWithOptionalGenStringEnumProp::Option1;
405405
}
406406
407407
static inline std::string toString(const AllPropWithOptionalGenStringEnumProp &value) {
@@ -427,7 +427,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
427427
result = AllPropWithOptionalGenIntEnumProp::IntEnumProp0;
428428
return;
429429
default:
430-
abort();
430+
result = AllPropWithOptionalGenIntEnumProp::IntEnumProp0;
431431
}
432432
}
433433
@@ -775,7 +775,7 @@ enum class AllPropWithOptionalObjectGenStringEnumProp { Option1 };
775775
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, AllPropWithOptionalObjectGenStringEnumProp &result) {
776776
auto string = (std::string)value;
777777
if (string == \\"option1\\") { result = AllPropWithOptionalObjectGenStringEnumProp::Option1; return; }
778-
abort();
778+
result = AllPropWithOptionalObjectGenStringEnumProp::Option1;
779779
}
780780
781781
static inline std::string toString(const AllPropWithOptionalObjectGenStringEnumProp &value) {
@@ -801,7 +801,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
801801
result = AllPropWithOptionalObjectGenIntEnumProp::IntEnumProp0;
802802
return;
803803
default:
804-
abort();
804+
result = AllPropWithOptionalObjectGenIntEnumProp::IntEnumProp0;
805805
}
806806
}
807807
@@ -3013,7 +3013,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
30133013
result = Int32EnumPropsNativeComponentMaxInterval::MaxInterval2;
30143014
return;
30153015
default:
3016-
abort();
3016+
result = Int32EnumPropsNativeComponentMaxInterval::MaxInterval0;
30173017
}
30183018
}
30193019
@@ -3301,7 +3301,7 @@ enum class ObjectPropsStringEnumProp { Option1 };
33013301
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsStringEnumProp &result) {
33023302
auto string = (std::string)value;
33033303
if (string == \\"option1\\") { result = ObjectPropsStringEnumProp::Option1; return; }
3304-
abort();
3304+
result = ObjectPropsStringEnumProp::Option1;
33053305
}
33063306
33073307
static inline std::string toString(const ObjectPropsStringEnumProp &value) {
@@ -3327,7 +3327,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
33273327
result = ObjectPropsIntEnumProp::IntEnumProp0;
33283328
return;
33293329
default:
3330-
abort();
3330+
result = ObjectPropsIntEnumProp::IntEnumProp0;
33313331
}
33323332
}
33333333
@@ -3735,7 +3735,7 @@ enum class ObjectPropsWithOptionalGenStringEnumProp { Option1 };
37353735
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsWithOptionalGenStringEnumProp &result) {
37363736
auto string = (std::string)value;
37373737
if (string == \\"option1\\") { result = ObjectPropsWithOptionalGenStringEnumProp::Option1; return; }
3738-
abort();
3738+
result = ObjectPropsWithOptionalGenStringEnumProp::Option1;
37393739
}
37403740
37413741
static inline std::string toString(const ObjectPropsWithOptionalGenStringEnumProp &value) {
@@ -3761,7 +3761,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
37613761
result = ObjectPropsWithOptionalGenIntEnumProp::IntEnumProp0;
37623762
return;
37633763
default:
3764-
abort();
3764+
result = ObjectPropsWithOptionalGenIntEnumProp::IntEnumProp0;
37653765
}
37663766
}
37673767
@@ -4169,7 +4169,7 @@ enum class ObjectPropsWithOptionalObjectGenStringEnumProp { Option1 };
41694169
static inline void fromRawValue(const PropsParserContext& context, const RawValue &value, ObjectPropsWithOptionalObjectGenStringEnumProp &result) {
41704170
auto string = (std::string)value;
41714171
if (string == \\"option1\\") { result = ObjectPropsWithOptionalObjectGenStringEnumProp::Option1; return; }
4172-
abort();
4172+
result = ObjectPropsWithOptionalObjectGenStringEnumProp::Option1;
41734173
}
41744174
41754175
static inline std::string toString(const ObjectPropsWithOptionalObjectGenStringEnumProp &value) {
@@ -4195,7 +4195,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
41954195
result = ObjectPropsWithOptionalObjectGenIntEnumProp::IntEnumProp0;
41964196
return;
41974197
default:
4198-
abort();
4198+
result = ObjectPropsWithOptionalObjectGenIntEnumProp::IntEnumProp0;
41994199
}
42004200
}
42014201
@@ -4651,7 +4651,7 @@ static inline void fromRawValue(const PropsParserContext& context, const RawValu
46514651
if (string == \\"top\\") { result = StringEnumPropsNativeComponentAlignment::Top; return; }
46524652
if (string == \\"center\\") { result = StringEnumPropsNativeComponentAlignment::Center; return; }
46534653
if (string == \\"bottom-right\\") { result = StringEnumPropsNativeComponentAlignment::BottomRight; return; }
4654-
abort();
4654+
result = StringEnumPropsNativeComponentAlignment::Center;
46554655
}
46564656
46574657
static inline std::string toString(const StringEnumPropsNativeComponentAlignment &value) {

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/PointerEvents.kt‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
package com.facebook.react.uimanager
99

10+
import com.facebook.common.logging.FLog
11+
import com.facebook.react.common.ReactConstants
1012
import java.util.Locale
1113

1214
/**
@@ -28,12 +30,21 @@ public enum class PointerEvents {
2830

2931
public companion object {
3032

33+
/**
34+
* Parses pointer events case-insensitively, treating hyphens as underscores. Null and invalid
35+
* values return [AUTO]; invalid values also log a warning.
36+
*/
3137
@JvmStatic
3238
public fun parsePointerEvents(pointerEventsStr: String?): PointerEvents =
3339
if (pointerEventsStr == null) {
3440
AUTO
3541
} else {
36-
PointerEvents.valueOf(pointerEventsStr.uppercase(Locale.US).replace("-", "_"))
42+
try {
43+
PointerEvents.valueOf(pointerEventsStr.uppercase(Locale.US).replace("-", "_"))
44+
} catch (e: IllegalArgumentException) {
45+
FLog.w(ReactConstants.TAG, "Invalid pointerEvents value: $pointerEventsStr")
46+
AUTO
47+
}
3748
}
3849

3950
@JvmStatic

0 commit comments

Comments
 (0)