Skip to content

Commit eb1b42f

Browse files
christophpurrerfacebook-github-bot
authored andcommittedMay 3, 2024
Sort spec members
Summary: The motiviation of this change is to produce sorted / stable native module schemas which members are alphabetically sorted. The benefit is mainly for verifying test fixtures as now new test cases will be inserted at predicatable spots. Changelog: [Internal] Reviewed By: rshest Differential Revision: D56741776 fbshipit-source-id: 842af73cac3b4859d2074e6a5206015924e87201
1 parent abb7070 commit eb1b42f

File tree

5 files changed

+125
-103
lines changed

5 files changed

+125
-103
lines changed
 

‎packages/react-native-codegen/e2e/__tests__/modules/__snapshots__/GenerateModuleH-test.js.snap

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,38 @@ private:
192192
};
193193

194194

195+
#pragma mark - NativeEnumTurboModuleStatusNumEnum
196+
197+
enum class NativeEnumTurboModuleStatusNumEnum { Active, Paused, Off };
198+
199+
template <>
200+
struct Bridging<NativeEnumTurboModuleStatusNumEnum> {
201+
static NativeEnumTurboModuleStatusNumEnum fromJs(jsi::Runtime &rt, const jsi::Value &rawValue) {
202+
double value = (double)rawValue.asNumber();
203+
if (value == 2) {
204+
return NativeEnumTurboModuleStatusNumEnum::Active;
205+
} else if (value == 1) {
206+
return NativeEnumTurboModuleStatusNumEnum::Paused;
207+
} else if (value == 0) {
208+
return NativeEnumTurboModuleStatusNumEnum::Off;
209+
} else {
210+
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
211+
}
212+
}
213+
214+
static jsi::Value toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusNumEnum value) {
215+
if (value == NativeEnumTurboModuleStatusNumEnum::Active) {
216+
return bridging::toJs(rt, 2);
217+
} else if (value == NativeEnumTurboModuleStatusNumEnum::Paused) {
218+
return bridging::toJs(rt, 1);
219+
} else if (value == NativeEnumTurboModuleStatusNumEnum::Off) {
220+
return bridging::toJs(rt, 0);
221+
} else {
222+
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
223+
}
224+
}
225+
};
226+
195227
#pragma mark - NativeEnumTurboModuleStatusRegularEnum
196228

197229
enum class NativeEnumTurboModuleStatusRegularEnum { Active, Paused, Off };
@@ -255,38 +287,6 @@ struct Bridging<NativeEnumTurboModuleStatusStrEnum> {
255287
}
256288
}
257289
};
258-
259-
#pragma mark - NativeEnumTurboModuleStatusNumEnum
260-
261-
enum class NativeEnumTurboModuleStatusNumEnum { Active, Paused, Off };
262-
263-
template <>
264-
struct Bridging<NativeEnumTurboModuleStatusNumEnum> {
265-
static NativeEnumTurboModuleStatusNumEnum fromJs(jsi::Runtime &rt, const jsi::Value &rawValue) {
266-
double value = (double)rawValue.asNumber();
267-
if (value == 2) {
268-
return NativeEnumTurboModuleStatusNumEnum::Active;
269-
} else if (value == 1) {
270-
return NativeEnumTurboModuleStatusNumEnum::Paused;
271-
} else if (value == 0) {
272-
return NativeEnumTurboModuleStatusNumEnum::Off;
273-
} else {
274-
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
275-
}
276-
}
277-
278-
static jsi::Value toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusNumEnum value) {
279-
if (value == NativeEnumTurboModuleStatusNumEnum::Active) {
280-
return bridging::toJs(rt, 2);
281-
} else if (value == NativeEnumTurboModuleStatusNumEnum::Paused) {
282-
return bridging::toJs(rt, 1);
283-
} else if (value == NativeEnumTurboModuleStatusNumEnum::Off) {
284-
return bridging::toJs(rt, 0);
285-
} else {
286-
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
287-
}
288-
}
289-
};
290290

291291
#pragma mark - NativeEnumTurboModuleStateType
292292

@@ -2146,6 +2146,38 @@ private:
21462146
};
21472147

21482148

2149+
#pragma mark - NativeEnumTurboModuleStatusNumEnum
2150+
2151+
enum class NativeEnumTurboModuleStatusNumEnum { Active, Paused, Off };
2152+
2153+
template <>
2154+
struct Bridging<NativeEnumTurboModuleStatusNumEnum> {
2155+
static NativeEnumTurboModuleStatusNumEnum fromJs(jsi::Runtime &rt, const jsi::Value &rawValue) {
2156+
double value = (double)rawValue.asNumber();
2157+
if (value == 2) {
2158+
return NativeEnumTurboModuleStatusNumEnum::Active;
2159+
} else if (value == 1) {
2160+
return NativeEnumTurboModuleStatusNumEnum::Paused;
2161+
} else if (value == 0) {
2162+
return NativeEnumTurboModuleStatusNumEnum::Off;
2163+
} else {
2164+
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
2165+
}
2166+
}
2167+
2168+
static jsi::Value toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusNumEnum value) {
2169+
if (value == NativeEnumTurboModuleStatusNumEnum::Active) {
2170+
return bridging::toJs(rt, 2);
2171+
} else if (value == NativeEnumTurboModuleStatusNumEnum::Paused) {
2172+
return bridging::toJs(rt, 1);
2173+
} else if (value == NativeEnumTurboModuleStatusNumEnum::Off) {
2174+
return bridging::toJs(rt, 0);
2175+
} else {
2176+
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
2177+
}
2178+
}
2179+
};
2180+
21492181
#pragma mark - NativeEnumTurboModuleStatusRegularEnum
21502182

21512183
enum class NativeEnumTurboModuleStatusRegularEnum { Active, Paused, Off };
@@ -2209,38 +2241,6 @@ struct Bridging<NativeEnumTurboModuleStatusStrEnum> {
22092241
}
22102242
}
22112243
};
2212-
2213-
#pragma mark - NativeEnumTurboModuleStatusNumEnum
2214-
2215-
enum class NativeEnumTurboModuleStatusNumEnum { Active, Paused, Off };
2216-
2217-
template <>
2218-
struct Bridging<NativeEnumTurboModuleStatusNumEnum> {
2219-
static NativeEnumTurboModuleStatusNumEnum fromJs(jsi::Runtime &rt, const jsi::Value &rawValue) {
2220-
double value = (double)rawValue.asNumber();
2221-
if (value == 2) {
2222-
return NativeEnumTurboModuleStatusNumEnum::Active;
2223-
} else if (value == 1) {
2224-
return NativeEnumTurboModuleStatusNumEnum::Paused;
2225-
} else if (value == 0) {
2226-
return NativeEnumTurboModuleStatusNumEnum::Off;
2227-
} else {
2228-
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
2229-
}
2230-
}
2231-
2232-
static jsi::Value toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusNumEnum value) {
2233-
if (value == NativeEnumTurboModuleStatusNumEnum::Active) {
2234-
return bridging::toJs(rt, 2);
2235-
} else if (value == NativeEnumTurboModuleStatusNumEnum::Paused) {
2236-
return bridging::toJs(rt, 1);
2237-
} else if (value == NativeEnumTurboModuleStatusNumEnum::Off) {
2238-
return bridging::toJs(rt, 0);
2239-
} else {
2240-
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
2241-
}
2242-
}
2243-
};
22442244

22452245
#pragma mark - NativeEnumTurboModuleStateType
22462246

‎packages/react-native-codegen/src/parsers/flow/modules/__tests__/__snapshots__/module-parser-snapshot-test.js.snap

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,51 +80,51 @@ exports[`RN Codegen Flow Parser can generate fixture CXX_ONLY_NATIVE_MODULE 1`]
8080
}
8181
]
8282
},
83-
'GraphNode': {
83+
'CustomDeviceEvent': {
8484
'type': 'ObjectTypeAnnotation',
8585
'properties': [
8686
{
87-
'name': 'label',
87+
'name': 'type',
8888
'optional': false,
8989
'typeAnnotation': {
9090
'type': 'StringTypeAnnotation'
9191
}
9292
},
9393
{
94-
'name': 'neighbors',
94+
'name': 'level',
95+
'optional': false,
96+
'typeAnnotation': {
97+
'type': 'NumberTypeAnnotation'
98+
}
99+
},
100+
{
101+
'name': 'degree',
95102
'optional': true,
96103
'typeAnnotation': {
97-
'type': 'ArrayTypeAnnotation',
98-
'elementType': {
99-
'type': 'TypeAliasTypeAnnotation',
100-
'name': 'GraphNode'
101-
}
104+
'type': 'NumberTypeAnnotation'
102105
}
103106
}
104107
]
105108
},
106-
'CustomDeviceEvent': {
109+
'GraphNode': {
107110
'type': 'ObjectTypeAnnotation',
108111
'properties': [
109112
{
110-
'name': 'type',
113+
'name': 'label',
111114
'optional': false,
112115
'typeAnnotation': {
113116
'type': 'StringTypeAnnotation'
114117
}
115118
},
116119
{
117-
'name': 'level',
118-
'optional': false,
119-
'typeAnnotation': {
120-
'type': 'NumberTypeAnnotation'
121-
}
122-
},
123-
{
124-
'name': 'degree',
120+
'name': 'neighbors',
125121
'optional': true,
126122
'typeAnnotation': {
127-
'type': 'NumberTypeAnnotation'
123+
'type': 'ArrayTypeAnnotation',
124+
'elementType': {
125+
'type': 'TypeAliasTypeAnnotation',
126+
'name': 'GraphNode'
127+
}
128128
}
129129
}
130130
]

‎packages/react-native-codegen/src/parsers/parsers-commons.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const {
6767
createParserErrorCapturer,
6868
extractNativeModuleName,
6969
getConfigType,
70+
getSortedObject,
7071
isModuleRegistryCall,
7172
verifyPlatforms,
7273
visit,
@@ -719,7 +720,7 @@ const buildModuleSchema = (
719720
language === 'Flow' ? moduleSpec.body.properties : moduleSpec.body.body;
720721

721722
// $FlowFixMe[missing-type-arg]
722-
return properties
723+
const nativeModuleSchema = properties
723724
.filter(
724725
property =>
725726
property.type === 'ObjectTypeProperty' ||
@@ -770,6 +771,15 @@ const buildModuleSchema = (
770771
excludedPlatforms.length !== 0 ? [...excludedPlatforms] : undefined,
771772
},
772773
);
774+
775+
return {
776+
type: 'NativeModule',
777+
aliasMap: getSortedObject(nativeModuleSchema.aliasMap),
778+
enumMap: getSortedObject(nativeModuleSchema.enumMap),
779+
spec: {properties: nativeModuleSchema.spec.properties.sort()},
780+
moduleName,
781+
excludedPlatforms: nativeModuleSchema.excludedPlatforms,
782+
};
773783
};
774784

775785
/**

‎packages/react-native-codegen/src/parsers/typescript/modules/__tests__/__snapshots__/typescript-module-parser-snapshot-test.js.snap

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,51 +71,51 @@ exports[`RN Codegen TypeScript Parser can generate fixture CXX_ONLY_NATIVE_MODUL
7171
}
7272
]
7373
},
74-
'GraphNode': {
74+
'CustomDeviceEvent': {
7575
'type': 'ObjectTypeAnnotation',
7676
'properties': [
7777
{
78-
'name': 'label',
78+
'name': 'type',
7979
'optional': false,
8080
'typeAnnotation': {
8181
'type': 'StringTypeAnnotation'
8282
}
8383
},
8484
{
85-
'name': 'neighbors',
85+
'name': 'level',
86+
'optional': false,
87+
'typeAnnotation': {
88+
'type': 'NumberTypeAnnotation'
89+
}
90+
},
91+
{
92+
'name': 'degree',
8693
'optional': true,
8794
'typeAnnotation': {
88-
'type': 'ArrayTypeAnnotation',
89-
'elementType': {
90-
'type': 'TypeAliasTypeAnnotation',
91-
'name': 'GraphNode'
92-
}
95+
'type': 'NumberTypeAnnotation'
9396
}
9497
}
9598
]
9699
},
97-
'CustomDeviceEvent': {
100+
'GraphNode': {
98101
'type': 'ObjectTypeAnnotation',
99102
'properties': [
100103
{
101-
'name': 'type',
104+
'name': 'label',
102105
'optional': false,
103106
'typeAnnotation': {
104107
'type': 'StringTypeAnnotation'
105108
}
106109
},
107110
{
108-
'name': 'level',
109-
'optional': false,
110-
'typeAnnotation': {
111-
'type': 'NumberTypeAnnotation'
112-
}
113-
},
114-
{
115-
'name': 'degree',
111+
'name': 'neighbors',
116112
'optional': true,
117113
'typeAnnotation': {
118-
'type': 'NumberTypeAnnotation'
114+
'type': 'ArrayTypeAnnotation',
115+
'elementType': {
116+
'type': 'TypeAliasTypeAnnotation',
117+
'name': 'GraphNode'
118+
}
119119
}
120120
}
121121
]

‎packages/react-native-codegen/src/parsers/utils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,23 @@ function isModuleRegistryCall(node: $FlowFixMe): boolean {
203203
return true;
204204
}
205205

206+
function getSortedObject<T>(unsortedObject: {[key: string]: T}): {
207+
[key: string]: T,
208+
} {
209+
return Object.keys(unsortedObject)
210+
.sort()
211+
.reduce((sortedObject: {[key: string]: T}, key: string) => {
212+
sortedObject[key] = unsortedObject[key];
213+
return sortedObject;
214+
}, {});
215+
}
216+
206217
module.exports = {
207218
getConfigType,
208219
extractNativeModuleName,
209220
createParserErrorCapturer,
210221
verifyPlatforms,
211222
visit,
212223
isModuleRegistryCall,
224+
getSortedObject,
213225
};

0 commit comments

Comments
 (0)