@react-native-windows/codegen 0.65.0 → 0.67.0-preview.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/CHANGELOG.json +305 -10
  2. package/CHANGELOG.md +121 -8
  3. package/lib-commonjs/Cli.d.ts +7 -0
  4. package/lib-commonjs/Cli.js +203 -0
  5. package/lib-commonjs/Cli.js.map +1 -0
  6. package/lib-commonjs/generators/AliasGen.d.ts +11 -0
  7. package/lib-commonjs/generators/AliasGen.js +72 -0
  8. package/lib-commonjs/generators/AliasGen.js.map +1 -0
  9. package/lib-commonjs/generators/AliasManaging.d.ts +15 -0
  10. package/lib-commonjs/generators/AliasManaging.js +49 -0
  11. package/lib-commonjs/generators/AliasManaging.js.map +1 -0
  12. package/lib-commonjs/generators/GenerateNM2.d.ts +11 -0
  13. package/lib-commonjs/generators/GenerateNM2.js +94 -0
  14. package/lib-commonjs/generators/GenerateNM2.js.map +1 -0
  15. package/lib-commonjs/generators/ObjectTypes.d.ts +8 -0
  16. package/lib-commonjs/generators/ObjectTypes.js +53 -0
  17. package/lib-commonjs/generators/ObjectTypes.js.map +1 -0
  18. package/lib-commonjs/generators/ParamTypes.d.ts +11 -0
  19. package/lib-commonjs/generators/ParamTypes.js +114 -0
  20. package/lib-commonjs/generators/ParamTypes.js.map +1 -0
  21. package/lib-commonjs/generators/ReturnTypes.d.ts +9 -0
  22. package/lib-commonjs/generators/ReturnTypes.js +63 -0
  23. package/lib-commonjs/generators/ReturnTypes.js.map +1 -0
  24. package/lib-commonjs/generators/ValidateConstants.d.ts +8 -0
  25. package/lib-commonjs/generators/ValidateConstants.js +38 -0
  26. package/lib-commonjs/generators/ValidateConstants.js.map +1 -0
  27. package/lib-commonjs/generators/ValidateMethods.d.ts +8 -0
  28. package/lib-commonjs/generators/ValidateMethods.js +70 -0
  29. package/lib-commonjs/generators/ValidateMethods.js.map +1 -0
  30. package/package.json +21 -11
  31. package/src/Cli.ts +102 -54
  32. package/src/generators/AliasGen.ts +105 -0
  33. package/src/generators/AliasManaging.ts +75 -0
  34. package/src/generators/GenerateNM2.ts +58 -290
  35. package/src/generators/ObjectTypes.ts +70 -0
  36. package/src/generators/ParamTypes.ts +220 -0
  37. package/src/generators/ReturnTypes.ts +92 -0
  38. package/src/generators/ValidateConstants.ts +50 -0
  39. package/src/generators/ValidateMethods.ts +135 -0
  40. package/.eslintrc.js +0 -4
  41. package/.vscode/launch.json +0 -23
  42. package/jest.config.js +0 -1
  43. package/tsconfig.json +0 -5
@@ -6,14 +6,11 @@
6
6
 
7
7
  'use strict';
8
8
 
9
- import {
10
- SchemaType,
11
- MethodTypeShape,
12
- // FunctionTypeAnnotation,
13
- FunctionTypeAnnotationParam,
14
- FunctionTypeAnnotationParamTypeAnnotation,
15
- FunctionTypeAnnotationReturn,
16
- } from 'react-native-tscodegen';
9
+ import {SchemaType} from 'react-native-tscodegen';
10
+ import {AliasMap, setPreferredModuleName} from './AliasManaging';
11
+ import {createAliasMap, generateAliases} from './AliasGen';
12
+ import {generateValidateConstants} from './ValidateConstants';
13
+ import {generateValidateMethods} from './ValidateMethods';
17
14
 
18
15
  type FilesOutput = Map<string, string>;
19
16
 
@@ -31,280 +28,21 @@ const moduleTemplate = `
31
28
  #include <tuple>
32
29
 
33
30
  namespace ::_NAMESPACE_:: {
34
-
31
+ ::_MODULE_ALIASED_STRUCTS_::
35
32
  struct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec {
36
- static constexpr auto methods = std::tuple{
37
- ::_MODULE_PROPERTIES_TUPLE_::
38
- };
33
+ ::_MODULE_MEMBERS_TUPLES_::
39
34
 
40
35
  template <class TModule>
41
36
  static constexpr void ValidateModule() noexcept {
42
- constexpr auto methodCheckResults = CheckMethods<TModule, ::_MODULE_NAME_::Spec>();
37
+ ::_MODULE_MEMBERS_CHECKS_::
43
38
 
44
- ::_MODULE_PROPERTIES_SPEC_ERRORS_::
39
+ ::_MODULE_MEMBERS_ERRORS_::
45
40
  }
46
41
  };
47
42
 
48
43
  } // namespace ::_NAMESPACE_::
49
44
  `;
50
45
 
51
- function translateSpecFunctionParam(
52
- param: FunctionTypeAnnotationParam,
53
- ): string {
54
- switch (param.typeAnnotation.type) {
55
- case 'StringTypeAnnotation':
56
- return 'std::string';
57
- case 'NumberTypeAnnotation':
58
- case 'FloatTypeAnnotation':
59
- return 'double';
60
- case 'Int32TypeAnnotation':
61
- return 'int';
62
- case 'BooleanTypeAnnotation':
63
- return 'bool';
64
- case 'FunctionTypeAnnotation': {
65
- // Ideally we'd get more information about the expected parameters of the callback
66
- // But the current schema doesn't seem to provide the necessary information.
67
- return 'Callback<React::JSValue>';
68
- }
69
- case 'ArrayTypeAnnotation':
70
- // Ideally we'd get more information about the expected type of the array
71
- // But the current schema doesn't seem to provide the necessary information.
72
- return 'React::JSValueArray';
73
- case 'GenericObjectTypeAnnotation':
74
- return 'React::JSValueObject';
75
- case 'ObjectTypeAnnotation':
76
- // TODO we have more information here, and could create a more specific type
77
- return 'React::JSValueObject';
78
- case 'ReservedFunctionValueTypeAnnotation':
79
- // (#6597)
80
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
81
- if (param.typeAnnotation.name !== 'RootTag')
82
- throw new Error(
83
- `Unknown reserved function: ${param.typeAnnotation.name} in translateSpecFunctionParam`,
84
- );
85
- return 'double';
86
- default:
87
- throw new Error(
88
- `Unhandled type in translateSpecFunctionParam: ${param.typeAnnotation.type}`,
89
- );
90
- }
91
- }
92
-
93
- function translateFunctionParam(param: FunctionTypeAnnotationParam): string {
94
- switch (param.typeAnnotation.type) {
95
- case 'StringTypeAnnotation':
96
- return 'std::string';
97
- case 'NumberTypeAnnotation':
98
- case 'FloatTypeAnnotation':
99
- return 'double';
100
- case 'Int32TypeAnnotation':
101
- return 'int';
102
- case 'BooleanTypeAnnotation':
103
- return 'bool';
104
- case 'FunctionTypeAnnotation': {
105
- // Ideally we'd get more information about the expected parameters of the callback
106
- // But the current schema doesn't seem to provide the necessary information.
107
- return 'std::function<void(React::JSValue const &)> const &';
108
- }
109
- case 'ArrayTypeAnnotation':
110
- // Ideally we'd get more information about the expected type of the array
111
- // But the current schema doesn't seem to provide the necessary information.
112
- return 'React::JSValueArray &&';
113
- case 'GenericObjectTypeAnnotation':
114
- return 'React::JSValueObject &&';
115
- case 'ObjectTypeAnnotation':
116
- // TODO we have more information here, and could create a more specific type
117
- return 'React::JSValueObject &&';
118
- case 'ReservedFunctionValueTypeAnnotation':
119
- // (#6597)
120
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
121
- if (param.typeAnnotation.name !== 'RootTag')
122
- throw new Error(
123
- `Unknown reserved function: ${param.typeAnnotation.name} in translateFunctionParam`,
124
- );
125
- return 'double';
126
- default:
127
- throw new Error(
128
- `Unhandled type in translateFunctionParam: ${param.typeAnnotation.type} in translateFunctionParam`,
129
- );
130
- }
131
- }
132
-
133
- function translateSpecReturnType(
134
- type:
135
- | FunctionTypeAnnotationParamTypeAnnotation
136
- | FunctionTypeAnnotationReturn,
137
- ) {
138
- switch (type.type) {
139
- case 'VoidTypeAnnotation':
140
- return 'void';
141
- case 'StringTypeAnnotation':
142
- return 'std::string';
143
- case 'NumberTypeAnnotation':
144
- case 'FloatTypeAnnotation':
145
- return 'double';
146
- case 'Int32TypeAnnotation':
147
- return 'int';
148
- case 'BooleanTypeAnnotation':
149
- return 'bool';
150
- case 'GenericPromiseTypeAnnotation':
151
- return 'void';
152
- case 'ArrayTypeAnnotation':
153
- // Ideally we'd get more information about the expected type of the array
154
- // But the current schema doesn't seem to provide the necessary information.
155
- return 'React::JSValueArray';
156
- case 'GenericObjectTypeAnnotation':
157
- return 'React::JSValueObject';
158
- case 'ReservedFunctionValueTypeAnnotation':
159
- // (#6597)
160
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
161
- if (type.name !== 'RootTag')
162
- throw new Error(
163
- `Unknown reserved function: ${type.name} in translateSpecReturnType`,
164
- );
165
- return 'double';
166
- default:
167
- throw new Error(
168
- `Unhandled type in translateSpecReturnType: ${type.type}`,
169
- );
170
- }
171
- }
172
-
173
- function translateImplReturnType(
174
- type:
175
- | FunctionTypeAnnotationParamTypeAnnotation
176
- | FunctionTypeAnnotationReturn,
177
- ) {
178
- switch (type.type) {
179
- case 'VoidTypeAnnotation':
180
- return 'void';
181
- case 'StringTypeAnnotation':
182
- return 'std::string';
183
- case 'NumberTypeAnnotation':
184
- case 'FloatTypeAnnotation':
185
- return 'double';
186
- case 'Int32TypeAnnotation':
187
- return 'int';
188
- case 'BooleanTypeAnnotation':
189
- return 'bool';
190
- case 'GenericPromiseTypeAnnotation':
191
- return 'void';
192
- case 'ArrayTypeAnnotation':
193
- // Ideally we'd get more information about the expected type of the array
194
- // But the current schema doesn't seem to provide the necessary information.
195
- return 'React::JSValueArray';
196
- case 'GenericObjectTypeAnnotation':
197
- return 'React::JSValueObject';
198
- case 'ReservedFunctionValueTypeAnnotation':
199
- // (#6597)
200
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
201
- if (type.name !== 'RootTag')
202
- throw new Error(
203
- `Unknown reserved function: ${type.name} in translateSpecReturnType`,
204
- );
205
- return 'double';
206
- default:
207
- throw new Error(
208
- `Unhandled type in translateImplReturnType: ${type.type}`,
209
- );
210
- }
211
- }
212
-
213
- function translateSpecArgs(params: ReadonlyArray<FunctionTypeAnnotationParam>) {
214
- return params.map(param => {
215
- const translatedParam = translateSpecFunctionParam(param);
216
- return `${translatedParam}`;
217
- });
218
- }
219
-
220
- function translateArgs(params: ReadonlyArray<FunctionTypeAnnotationParam>) {
221
- return params.map(param => {
222
- const translatedParam = translateFunctionParam(param);
223
- return `${translatedParam} ${param.name}`;
224
- });
225
- }
226
-
227
- function isMethodSync(prop: MethodTypeShape) {
228
- return (
229
- prop.typeAnnotation.returnTypeAnnotation.type !== 'VoidTypeAnnotation' &&
230
- prop.typeAnnotation.returnTypeAnnotation.type !==
231
- 'GenericPromiseTypeAnnotation'
232
- );
233
- }
234
-
235
- function isPromise(prop: MethodTypeShape) {
236
- return (
237
- prop.typeAnnotation.returnTypeAnnotation.type ===
238
- 'GenericPromiseTypeAnnotation'
239
- );
240
- }
241
-
242
- function getPossibleMethodSignatures(prop: MethodTypeShape): string[] {
243
- const args = translateArgs(prop.typeAnnotation.params);
244
- if (isPromise(prop)) {
245
- // Sadly, currently, the schema doesn't currently provide us information on the type of the promise.
246
- args.push('React::ReactPromise<React::JSValue> &&result');
247
- }
248
-
249
- // TODO be much more exhastive on the possible method signatures that can be used..
250
- const sig = `REACT_${isMethodSync(prop) ? 'SYNC_' : ''}METHOD(${
251
- prop.name
252
- }) ${translateImplReturnType(prop.typeAnnotation.returnTypeAnnotation)} ${
253
- prop.name
254
- }(${args.join(', ')}) noexcept { /* implementation */ }}`;
255
-
256
- const staticsig = `REACT_${isMethodSync(prop) ? 'SYNC_' : ''}METHOD(${
257
- prop.name
258
- }) static ${translateImplReturnType(
259
- prop.typeAnnotation.returnTypeAnnotation,
260
- )} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }}`;
261
-
262
- return [sig, staticsig];
263
- }
264
-
265
- function translatePossibleMethodSignatures(prop: MethodTypeShape): string {
266
- return getPossibleMethodSignatures(prop)
267
- .map(sig => `" ${sig}\\n"`)
268
- .join('\n ');
269
- }
270
-
271
- function renderProperties(
272
- properties: ReadonlyArray<MethodTypeShape>,
273
- tuple: boolean,
274
- ): string {
275
- // We skip the constants for now, since we dont have Spec file validation of them.
276
- return properties
277
- .filter(prop => prop.name !== 'getConstants')
278
- .map((prop, index) => {
279
- const params = prop.typeAnnotation.params;
280
-
281
- const traversedArgs = translateSpecArgs(params);
282
-
283
- const translatedReturnParam = translateSpecReturnType(
284
- prop.typeAnnotation.returnTypeAnnotation,
285
- );
286
-
287
- if (isPromise(prop)) {
288
- // Sadly, currently, the schema doesn't currently provide us information on the type of the promise.
289
- traversedArgs.push('Promise<React::JSValue>');
290
- }
291
-
292
- if (tuple) {
293
- return ` ${
294
- isMethodSync(prop) ? 'Sync' : ''
295
- }Method<${translatedReturnParam}(${traversedArgs.join(
296
- ', ',
297
- )}) noexcept>{${index}, L"${prop.name}"},`;
298
- } else {
299
- return ` REACT_SHOW_METHOD_SPEC_ERRORS(
300
- ${index},
301
- "${prop.name}",
302
- ${translatePossibleMethodSignatures(prop)});`;
303
- }
304
- })
305
- .join('\n');
306
- }
307
-
308
46
  export function createNM2Generator({namespace}: {namespace: string}) {
309
47
  return (
310
48
  _libraryName: string,
@@ -313,30 +51,60 @@ export function createNM2Generator({namespace}: {namespace: string}) {
313
51
  ): FilesOutput => {
314
52
  const files = new Map<string, string>();
315
53
 
316
- const nativeModules = Object.keys(schema.modules)
317
- .map(moduleName => schema.modules[moduleName].nativeModules)
318
- .filter(Boolean)
319
- .reduce((acc, components) => Object.assign(acc, components), {});
320
-
321
- if (nativeModules) {
322
- Object.keys(nativeModules).forEach(name => {
323
- console.log(`Generating Native${name}Spec.g.h`);
324
- const {properties} = nativeModules[name];
325
- const traversedProperties = renderProperties(properties, false);
326
- const traversedPropertyTuples = renderProperties(properties, true);
54
+ for (const moduleName of Object.keys(schema.modules)) {
55
+ const nativeModule = schema.modules[moduleName];
56
+ // from 0.65 facebook's react-native-codegen
57
+ // the module name has the Native prefix comparing to 0.63
58
+ // when reading files we provided
59
+ const preferredModuleName = moduleName.startsWith('Native')
60
+ ? moduleName.substr(6)
61
+ : moduleName;
62
+ setPreferredModuleName(preferredModuleName);
63
+
64
+ if (nativeModule.type === 'NativeModule') {
65
+ console.log(`Generating Native${preferredModuleName}Spec.g.h`);
66
+
67
+ // copy all explicit to a map
68
+ const aliases: AliasMap = createAliasMap(nativeModule.aliases);
69
+
70
+ // prepare methods
71
+ const methods = generateValidateMethods(nativeModule, aliases);
72
+ let tuples = `
73
+ static constexpr auto methods = std::tuple{
74
+ ${methods[0]}
75
+ };`;
76
+ let checks = `
77
+ constexpr auto methodCheckResults = CheckMethods<TModule, ::_MODULE_NAME_::Spec>();`;
78
+ let errors = methods[1];
79
+
80
+ // prepare constants
81
+ const constants = generateValidateConstants(nativeModule, aliases);
82
+ if (constants !== undefined) {
83
+ tuples = `
84
+ static constexpr auto constants = std::tuple{
85
+ ${constants[0]}
86
+ };${tuples}`;
87
+ checks = `
88
+ constexpr auto constantCheckResults = CheckConstants<TModule, ::_MODULE_NAME_::Spec>();${checks}`;
89
+ errors = `${constants[1]}
90
+
91
+ ${errors}`;
92
+ }
93
+
94
+ // generate code for structs
95
+ const traversedAliasedStructs = generateAliases(aliases);
327
96
 
328
97
  files.set(
329
- `Native${name}Spec.g.h`,
98
+ `Native${preferredModuleName}Spec.g.h`,
330
99
  moduleTemplate
331
- .replace(/::_MODULE_PROPERTIES_TUPLE_::/g, traversedPropertyTuples)
332
- .replace(
333
- /::_MODULE_PROPERTIES_SPEC_ERRORS_::/g,
334
- traversedProperties,
335
- )
336
- .replace(/::_MODULE_NAME_::/g, name)
100
+ .replace(/::_MODULE_ALIASED_STRUCTS_::/g, traversedAliasedStructs)
101
+ .replace(/::_MODULE_MEMBERS_TUPLES_::/g, tuples.substr(1))
102
+ .replace(/::_MODULE_MEMBERS_CHECKS_::/g, checks.substr(1))
103
+ .replace(/::_MODULE_MEMBERS_ERRORS_::/g, errors)
104
+ .replace(/::_MODULE_NAME_::/g, preferredModuleName)
337
105
  .replace(/::_NAMESPACE_::/g, namespace),
338
106
  );
339
- });
107
+ }
340
108
  }
341
109
 
342
110
  return files;
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ * Licensed under the MIT License.
4
+ * @format
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ import {NativeModuleBaseTypeAnnotation, Nullable} from 'react-native-tscodegen';
10
+ import {
11
+ AliasMap,
12
+ getAliasCppName,
13
+ getAnonymousAliasCppName,
14
+ } from './AliasManaging';
15
+
16
+ export function translateField(
17
+ type: Nullable<NativeModuleBaseTypeAnnotation>,
18
+ aliases: AliasMap,
19
+ baseAliasName: string,
20
+ ): string {
21
+ // avoid: Property 'type' does not exist on type 'never'
22
+ const returnType = type.type;
23
+ switch (type.type) {
24
+ case 'StringTypeAnnotation':
25
+ return 'std::string';
26
+ case 'NumberTypeAnnotation':
27
+ case 'FloatTypeAnnotation':
28
+ case 'DoubleTypeAnnotation':
29
+ return 'double';
30
+ case 'Int32TypeAnnotation':
31
+ return 'int';
32
+ case 'BooleanTypeAnnotation':
33
+ return 'bool';
34
+ case 'ArrayTypeAnnotation':
35
+ if (type.elementType) {
36
+ return `std::vector<${translateField(
37
+ type.elementType,
38
+ aliases,
39
+ `${baseAliasName}_element`,
40
+ )}>`;
41
+ } else {
42
+ return `React::JSValueArray`;
43
+ }
44
+ case 'GenericObjectTypeAnnotation':
45
+ return 'React::JSValue';
46
+ case 'ObjectTypeAnnotation':
47
+ return getAnonymousAliasCppName(aliases, baseAliasName, type);
48
+ case 'ReservedTypeAnnotation': {
49
+ // avoid: Property 'name' does not exist on type 'never'
50
+ const name = type.name;
51
+ // (#6597)
52
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
53
+ if (name !== 'RootTag')
54
+ throw new Error(
55
+ `Unknown reserved function: ${name} in translateReturnType`,
56
+ );
57
+ return 'double';
58
+ }
59
+ case 'TypeAliasTypeAnnotation':
60
+ return getAliasCppName(type.name);
61
+ case 'NullableTypeAnnotation':
62
+ return `std::optional<${translateField(
63
+ type.typeAnnotation,
64
+ aliases,
65
+ baseAliasName,
66
+ )}>`;
67
+ default:
68
+ throw new Error(`Unhandled type in translateReturnType: ${returnType}`);
69
+ }
70
+ }
@@ -0,0 +1,220 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ * Licensed under the MIT License.
4
+ * @format
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ import {
10
+ NamedShape,
11
+ NativeModuleParamTypeAnnotation,
12
+ Nullable,
13
+ } from 'react-native-tscodegen';
14
+ import {
15
+ AliasMap,
16
+ getAliasCppName,
17
+ getAnonymousAliasCppName,
18
+ } from './AliasManaging';
19
+
20
+ type NativeModuleParamShape = NamedShape<
21
+ Nullable<NativeModuleParamTypeAnnotation>
22
+ >;
23
+
24
+ type ParamTarget = 'spec' | 'template' | 'callback-arg' | 'method-arg';
25
+
26
+ function decorateType(type: string, target: ParamTarget): string {
27
+ switch (target) {
28
+ case 'method-arg':
29
+ return `${type} &&`;
30
+ case 'callback-arg':
31
+ return `${type} const &`;
32
+ default:
33
+ return type;
34
+ }
35
+ }
36
+
37
+ function translateParam(
38
+ param: NativeModuleParamTypeAnnotation,
39
+ aliases: AliasMap,
40
+ baseAliasName: string,
41
+ target: ParamTarget,
42
+ ): string {
43
+ // avoid: Property 'type' does not exist on type 'never'
44
+ const paramType = param.type;
45
+ switch (param.type) {
46
+ case 'StringTypeAnnotation':
47
+ return 'std::string';
48
+ case 'NumberTypeAnnotation':
49
+ case 'FloatTypeAnnotation':
50
+ case 'DoubleTypeAnnotation':
51
+ return 'double';
52
+ case 'Int32TypeAnnotation':
53
+ return 'int';
54
+ case 'BooleanTypeAnnotation':
55
+ return 'bool';
56
+ case 'FunctionTypeAnnotation': {
57
+ // TODO: type.returnTypeAnnotation
58
+ switch (target) {
59
+ case 'spec':
60
+ return `Callback<${param.params
61
+ .map((p: NativeModuleParamShape) =>
62
+ translateSpecFunctionParam(
63
+ p,
64
+ aliases,
65
+ `${baseAliasName}_${p.name}`,
66
+ ),
67
+ )
68
+ .join(', ')}>`;
69
+ case 'template':
70
+ return `std::function<void(${param.params
71
+ .map((p: NativeModuleParamShape) =>
72
+ translateCallbackParam(p, aliases, `${baseAliasName}_${p.name}`),
73
+ )
74
+ .join(', ')})>`;
75
+ default:
76
+ return `std::function<void(${param.params
77
+ .map((p: NativeModuleParamShape) =>
78
+ translateCallbackParam(p, aliases, `${baseAliasName}_${p.name}`),
79
+ )
80
+ .join(', ')})> const &`;
81
+ }
82
+ }
83
+ case 'ArrayTypeAnnotation':
84
+ if (param.elementType) {
85
+ switch (target) {
86
+ case 'spec':
87
+ case 'template':
88
+ return `std::vector<${translateNullableParamType(
89
+ param.elementType,
90
+ aliases,
91
+ `${baseAliasName}_element`,
92
+ 'template',
93
+ 'template',
94
+ )}>`;
95
+ default:
96
+ return `std::vector<${translateNullableParamType(
97
+ param.elementType,
98
+ aliases,
99
+ `${baseAliasName}_element`,
100
+ 'template',
101
+ 'template',
102
+ )}> const &`;
103
+ }
104
+ } else {
105
+ return decorateType('React::JSValueArray', target);
106
+ }
107
+ case 'GenericObjectTypeAnnotation':
108
+ return decorateType('React::JSValue', target);
109
+ case 'ObjectTypeAnnotation':
110
+ return decorateType(
111
+ getAnonymousAliasCppName(aliases, baseAliasName, param),
112
+ target,
113
+ );
114
+ case 'ReservedTypeAnnotation': {
115
+ // avoid: Property 'name' does not exist on type 'never'
116
+ const name = param.name;
117
+ // (#6597)
118
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
119
+ if (name !== 'RootTag')
120
+ throw new Error(`Unknown reserved function: ${name} in translateParam`);
121
+ return 'double';
122
+ }
123
+ case 'TypeAliasTypeAnnotation':
124
+ return decorateType(getAliasCppName(param.name), target);
125
+ default:
126
+ throw new Error(`Unhandled type in translateParam: ${paramType}`);
127
+ }
128
+ }
129
+
130
+ function translateNullableParamType(
131
+ paramType: Nullable<NativeModuleParamTypeAnnotation>,
132
+ aliases: AliasMap,
133
+ baseAliasName: string,
134
+ nullableTarget: ParamTarget,
135
+ target: ParamTarget,
136
+ ): string {
137
+ switch (paramType.type) {
138
+ case 'NullableTypeAnnotation':
139
+ return `std::optional<${translateParam(
140
+ paramType.typeAnnotation,
141
+ aliases,
142
+ baseAliasName,
143
+ nullableTarget,
144
+ )}>`;
145
+ default:
146
+ return translateParam(paramType, aliases, baseAliasName, target);
147
+ }
148
+ }
149
+
150
+ function translateSpecFunctionParam(
151
+ param: NativeModuleParamShape,
152
+ aliases: AliasMap,
153
+ baseAliasName: string,
154
+ ): string {
155
+ return translateNullableParamType(
156
+ param.typeAnnotation,
157
+ aliases,
158
+ baseAliasName,
159
+ 'spec',
160
+ 'spec',
161
+ );
162
+ }
163
+
164
+ function translateCallbackParam(
165
+ param: NativeModuleParamShape,
166
+ aliases: AliasMap,
167
+ baseAliasName: string,
168
+ ): string {
169
+ return translateNullableParamType(
170
+ param.typeAnnotation,
171
+ aliases,
172
+ baseAliasName,
173
+ 'template',
174
+ 'callback-arg',
175
+ );
176
+ }
177
+
178
+ function translateFunctionParam(
179
+ param: NativeModuleParamShape,
180
+ aliases: AliasMap,
181
+ baseAliasName: string,
182
+ ): string {
183
+ return translateNullableParamType(
184
+ param.typeAnnotation,
185
+ aliases,
186
+ baseAliasName,
187
+ 'template',
188
+ 'method-arg',
189
+ );
190
+ }
191
+
192
+ export function translateSpecArgs(
193
+ params: ReadonlyArray<NativeModuleParamShape>,
194
+ aliases: AliasMap,
195
+ baseAliasName: string,
196
+ ) {
197
+ return params.map(param => {
198
+ const translatedParam = translateSpecFunctionParam(
199
+ param,
200
+ aliases,
201
+ `${baseAliasName}_${param.name}`,
202
+ );
203
+ return `${translatedParam}`;
204
+ });
205
+ }
206
+
207
+ export function translateArgs(
208
+ params: ReadonlyArray<NativeModuleParamShape>,
209
+ aliases: AliasMap,
210
+ baseAliasName: string,
211
+ ) {
212
+ return params.map(param => {
213
+ const translatedParam = translateFunctionParam(
214
+ param,
215
+ aliases,
216
+ `${baseAliasName}_${param.name}`,
217
+ );
218
+ return `${translatedParam} ${param.name}`;
219
+ });
220
+ }