Skip to content

Commit 9eec55a

Browse files
committed
revert: undo codegen changes (schema version mismatch)
1 parent 0c1981d commit 9eec55a

3 files changed

Lines changed: 85 additions & 18 deletions

File tree

packages/@react-native-windows/codegen/src/generators/ObjectTypes.ts

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
NativeModuleUnionTypeAnnotation,
1313
NativeModuleStringTypeAnnotation,
1414
NativeModuleFunctionTypeAnnotation,
15+
StringLiteralTypeAnnotation,
1516
StringLiteralUnionTypeAnnotation,
1617
UnsafeAnyTypeAnnotation,
1718
Nullable,
@@ -32,14 +33,44 @@ function translateUnionReturnType(
3233
type: NativeModuleEnumDeclaration | NativeModuleUnionTypeAnnotation,
3334
options: CppCodegenOptions,
3435
): string {
35-
switch ((type as NativeModuleEnumDeclaration).memberType) {
36-
case 'StringTypeAnnotation':
37-
return options.cppStringType;
38-
case 'NumberTypeAnnotation':
39-
return 'double';
40-
default:
41-
return '::React::JSValue';
36+
if (type.type === 'EnumDeclaration') {
37+
switch (type.memberType) {
38+
case 'StringTypeAnnotation':
39+
return options.cppStringType;
40+
case 'NumberTypeAnnotation':
41+
return 'double';
42+
default:
43+
throw new Error(
44+
`Unknown enum member type in translateReturnType: ${type.memberType}`,
45+
);
46+
}
47+
}
48+
49+
// UnionTypeAnnotation: determine C++ type from the types array
50+
const types = type.types;
51+
if (types.length === 0) {
52+
return '::React::JSValue';
53+
}
54+
55+
const allString = types.every(
56+
t =>
57+
t.type === 'StringTypeAnnotation' ||
58+
t.type === 'StringLiteralTypeAnnotation',
59+
);
60+
if (allString) {
61+
return options.cppStringType;
4262
}
63+
64+
const allNumber = types.every(
65+
t =>
66+
t.type === 'NumberTypeAnnotation' ||
67+
t.type === 'NumberLiteralTypeAnnotation',
68+
);
69+
if (allNumber) {
70+
return 'double';
71+
}
72+
73+
return '::React::JSValue';
4374
}
4475

4576
// eslint-disable-next-line complexity
@@ -49,6 +80,7 @@ export function translateFieldOrReturnType(
4980
| NativeModuleBaseTypeAnnotation
5081
| NativeModuleStringTypeAnnotation
5182
| NativeModuleFunctionTypeAnnotation
83+
| StringLiteralTypeAnnotation
5284
| StringLiteralUnionTypeAnnotation
5385
>
5486
| UnsafeAnyTypeAnnotation,
@@ -61,7 +93,7 @@ export function translateFieldOrReturnType(
6193
const returnType = type.type;
6294
switch (type.type) {
6395
case 'StringTypeAnnotation':
64-
case 'StringLiteralUnionTypeAnnotation':
96+
case 'StringLiteralTypeAnnotation':
6597
return options.cppStringType;
6698
case 'NumberTypeAnnotation':
6799
case 'FloatTypeAnnotation':
@@ -88,7 +120,12 @@ export function translateFieldOrReturnType(
88120
case 'ObjectTypeAnnotation':
89121
return getAnonymousAliasCppName(aliases, baseAliasName, type);
90122
case 'ReservedTypeAnnotation': {
91-
// ReservedTypeAnnotation.name is always 'RootTag' (#6597)
123+
// avoid: Property 'name' does not exist on type 'never'
124+
const name = type.name;
125+
// (#6597)
126+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
127+
if (name !== 'RootTag')
128+
throw new Error(`Unknown reserved function: ${name} in ${callerName}`);
92129
return 'double';
93130
}
94131
case 'TypeAliasTypeAnnotation':

packages/@react-native-windows/codegen/src/generators/ParamTypes.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,44 @@ function translateUnionReturnType(
4848
target: ParamTarget,
4949
options: CppCodegenOptions,
5050
): string {
51-
switch ((type as NativeModuleEnumDeclaration).memberType) {
52-
case 'StringTypeAnnotation':
53-
return options.cppStringType;
54-
case 'NumberTypeAnnotation':
55-
return 'double';
56-
default:
57-
return decorateType('::React::JSValue', target);
51+
if (type.type === 'EnumDeclaration') {
52+
switch (type.memberType) {
53+
case 'StringTypeAnnotation':
54+
return options.cppStringType;
55+
case 'NumberTypeAnnotation':
56+
return 'double';
57+
default:
58+
throw new Error(
59+
`Unknown enum member type in translateReturnType: ${type.memberType}`,
60+
);
61+
}
5862
}
63+
64+
// UnionTypeAnnotation: determine C++ type from the types array
65+
const types = type.types;
66+
if (types.length === 0) {
67+
return decorateType('::React::JSValue', target);
68+
}
69+
70+
const allString = types.every(
71+
t =>
72+
t.type === 'StringTypeAnnotation' ||
73+
t.type === 'StringLiteralTypeAnnotation',
74+
);
75+
if (allString) {
76+
return options.cppStringType;
77+
}
78+
79+
const allNumber = types.every(
80+
t =>
81+
t.type === 'NumberTypeAnnotation' ||
82+
t.type === 'NumberLiteralTypeAnnotation',
83+
);
84+
if (allNumber) {
85+
return 'double';
86+
}
87+
88+
return decorateType('::React::JSValue', target);
5989
}
6090

6191
function translateFunction(

packages/@react-native-windows/codegen/src/generators/PropObjectTypes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export function translateComponentEventType(type: EventTypeAnnotation,
142142
case 'ObjectTypeAnnotation':
143143
arrayTemplateArg = translateComponentEventType(type.elementType, aliases, baseAliasName, options).type;
144144
break;
145-
case 'StringLiteralUnionTypeAnnotation':
145+
case 'UnionTypeAnnotation':
146146
arrayTemplateArg = options.cppStringType; // TODO - better enum type handling than just passing a string
147147
break;
148148
default:
@@ -157,7 +157,7 @@ export function translateComponentEventType(type: EventTypeAnnotation,
157157
case 'MixedTypeAnnotation': {
158158
return { type: 'winrt::Microsoft::ReactNative::JSValue', initializer: '{nullptr}', alreadySupportsOptionalOrHasDefault: true };
159159
}
160-
case 'StringLiteralUnionTypeAnnotation':
160+
case 'UnionTypeAnnotation':
161161
return { type: options.cppStringType, initializer: '' }; // TODO - better enum type handling than just passing a string
162162
default:
163163
throw new Error(`Unhandled type: ${(type as any).type}`);

0 commit comments

Comments
 (0)