@react-native-windows/codegen 0.0.0-canary.9 → 0.0.1-0

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 (53) hide show
  1. package/CHANGELOG.md +726 -15
  2. package/README.md +1 -1
  3. package/bin.js +0 -0
  4. package/lib-commonjs/Cli.d.ts +7 -0
  5. package/lib-commonjs/Cli.js +92 -0
  6. package/lib-commonjs/Cli.js.map +1 -0
  7. package/lib-commonjs/generators/AliasGen.d.ts +12 -0
  8. package/lib-commonjs/generators/AliasGen.js +88 -0
  9. package/lib-commonjs/generators/AliasGen.js.map +1 -0
  10. package/lib-commonjs/generators/AliasManaging.d.ts +15 -0
  11. package/lib-commonjs/generators/AliasManaging.js +49 -0
  12. package/lib-commonjs/generators/AliasManaging.js.map +1 -0
  13. package/lib-commonjs/generators/GenerateNM2.d.ts +15 -0
  14. package/lib-commonjs/generators/GenerateNM2.js +142 -0
  15. package/lib-commonjs/generators/GenerateNM2.js.map +1 -0
  16. package/lib-commonjs/generators/GenerateTypeScript.d.ts +11 -0
  17. package/lib-commonjs/generators/GenerateTypeScript.js +166 -0
  18. package/lib-commonjs/generators/GenerateTypeScript.js.map +1 -0
  19. package/lib-commonjs/generators/ObjectTypes.d.ts +13 -0
  20. package/lib-commonjs/generators/ObjectTypes.js +75 -0
  21. package/lib-commonjs/generators/ObjectTypes.js.map +1 -0
  22. package/lib-commonjs/generators/ParamTypes.d.ts +12 -0
  23. package/lib-commonjs/generators/ParamTypes.js +137 -0
  24. package/lib-commonjs/generators/ParamTypes.js.map +1 -0
  25. package/lib-commonjs/generators/ReturnTypes.d.ts +10 -0
  26. package/lib-commonjs/generators/ReturnTypes.js +29 -0
  27. package/lib-commonjs/generators/ReturnTypes.js.map +1 -0
  28. package/lib-commonjs/generators/ValidateConstants.d.ts +8 -0
  29. package/lib-commonjs/generators/ValidateConstants.js +38 -0
  30. package/lib-commonjs/generators/ValidateConstants.js.map +1 -0
  31. package/lib-commonjs/generators/ValidateMethods.d.ts +9 -0
  32. package/lib-commonjs/generators/ValidateMethods.js +75 -0
  33. package/lib-commonjs/generators/ValidateMethods.js.map +1 -0
  34. package/lib-commonjs/index.d.ts +37 -0
  35. package/lib-commonjs/index.js +224 -0
  36. package/lib-commonjs/index.js.map +1 -0
  37. package/package.json +37 -20
  38. package/src/Cli.ts +57 -190
  39. package/src/generators/AliasGen.ts +149 -0
  40. package/src/generators/AliasManaging.ts +75 -0
  41. package/src/generators/GenerateNM2.ts +138 -295
  42. package/src/generators/GenerateTypeScript.ts +247 -0
  43. package/src/generators/ObjectTypes.ts +130 -0
  44. package/src/generators/ParamTypes.ts +298 -0
  45. package/src/generators/ReturnTypes.ts +70 -0
  46. package/src/generators/ValidateConstants.ts +50 -0
  47. package/src/generators/ValidateMethods.ts +177 -0
  48. package/src/index.ts +393 -0
  49. package/.eslintrc.js +0 -4
  50. package/.vscode/launch.json +0 -23
  51. package/CHANGELOG.json +0 -583
  52. package/jest.config.js +0 -1
  53. package/tsconfig.json +0 -5
@@ -6,306 +6,89 @@
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 type {SchemaType} from '@react-native/codegen/lib/CodegenSchema';
10
+ import {AliasMap, setPreferredModuleName} from './AliasManaging';
11
+ import {createAliasMap, generateAliases} from './AliasGen';
12
+ import {generateValidateConstants} from './ValidateConstants';
13
+ import {generateValidateMethods} from './ValidateMethods';
14
+ import type {CppStringTypes} from './ObjectTypes';
15
+
16
+ export type {CppStringTypes} from './ObjectTypes';
17
17
 
18
18
  type FilesOutput = Map<string, string>;
19
19
 
20
- const moduleTemplate = `
21
- /*
20
+ const headerTemplate = `/*
22
21
  * This file is auto-generated from a NativeModule spec file in js.
23
22
  *
24
23
  * This is a C++ Spec class that should be used with MakeTurboModuleProvider to register native modules
25
24
  * in a way that also verifies at compile time that the native module matches the interface required
26
25
  * by the TurboModule JS spec.
27
26
  */
28
- #pragma once
29
-
30
- #include "NativeModules.h"
31
- #include <tuple>
32
-
33
- namespace ::_NAMESPACE_:: {
27
+ #pragma once`;
34
28
 
29
+ const specTemplate = `::_MODULE_CUSTPM_TYPES_REFLECTION_::
35
30
  struct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec {
36
- static constexpr auto methods = std::tuple{
37
- ::_MODULE_PROPERTIES_TUPLE_::
38
- };
31
+ ::_MODULE_MEMBERS_TUPLES_::
39
32
 
40
33
  template <class TModule>
41
34
  static constexpr void ValidateModule() noexcept {
42
- constexpr auto methodCheckResults = CheckMethods<TModule, ::_MODULE_NAME_::Spec>();
43
-
44
- ::_MODULE_PROPERTIES_SPEC_ERRORS_::
45
- }
46
- };
47
-
48
- } // namespace ::_NAMESPACE_::
49
- `;
50
-
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
- }
35
+ ::_MODULE_MEMBERS_CHECKS_::
226
36
 
227
- function isMethodSync(prop: MethodTypeShape) {
228
- return (
229
- prop.typeAnnotation.returnTypeAnnotation.type !== 'VoidTypeAnnotation' &&
230
- prop.typeAnnotation.returnTypeAnnotation.type !==
231
- 'GenericPromiseTypeAnnotation'
232
- );
233
- }
37
+ ::_MODULE_MEMBERS_ERRORS_::
38
+ }`;
234
39
 
235
- function isPromise(prop: MethodTypeShape) {
236
- return (
237
- prop.typeAnnotation.returnTypeAnnotation.type ===
238
- 'GenericPromiseTypeAnnotation'
239
- );
240
- }
40
+ const typeOnlyTemplate = `
41
+ ${headerTemplate}
241
42
 
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
- }
43
+ #include <string>
44
+ #include <optional>
45
+ #include <functional>
46
+ #include <vector>
248
47
 
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 */ }}`;
48
+ namespace ::_NAMESPACE_:: {
49
+ ::_MODULE_CUSTPM_TYPES_::
50
+ } // namespace ::_NAMESPACE_::
51
+ `;
255
52
 
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 */ }}`;
53
+ const moduleOnlyTemplate = `
54
+ ${headerTemplate}
261
55
 
262
- return [sig, staticsig];
263
- }
56
+ ::_TYPE_DEFINITION_INCLUDE_::
57
+ #include <NativeModules.h>
58
+ #include <tuple>
264
59
 
265
- function translatePossibleMethodSignatures(prop: MethodTypeShape): string {
266
- return getPossibleMethodSignatures(prop)
267
- .map(sig => `" ${sig}\\n"`)
268
- .join('\n ');
269
- }
60
+ namespace ::_NAMESPACE_:: {
61
+ ${specTemplate}
62
+ };
270
63
 
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;
64
+ } // namespace ::_NAMESPACE_::
65
+ `;
280
66
 
281
- const traversedArgs = translateSpecArgs(params);
67
+ const allInOneTemplate = `
68
+ ${headerTemplate}
282
69
 
283
- const translatedReturnParam = translateSpecReturnType(
284
- prop.typeAnnotation.returnTypeAnnotation,
285
- );
70
+ #include <NativeModules.h>
71
+ #include <tuple>
286
72
 
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
- }
73
+ namespace ::_NAMESPACE_:: {
74
+ ::_MODULE_CUSTPM_TYPES_::
75
+ ${specTemplate}
76
+ };
291
77
 
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
- }
78
+ } // namespace ::_NAMESPACE_::
79
+ `;
307
80
 
308
- export function createNM2Generator({namespace}: {namespace: string}) {
81
+ export function createNM2Generator({
82
+ methodOnly,
83
+ namespace,
84
+ cppStringType,
85
+ separateDataTypes,
86
+ }: {
87
+ methodOnly: boolean;
88
+ namespace: string;
89
+ cppStringType: CppStringTypes;
90
+ separateDataTypes: boolean;
91
+ }) {
309
92
  return (
310
93
  _libraryName: string,
311
94
  schema: SchemaType,
@@ -313,30 +96,90 @@ export function createNM2Generator({namespace}: {namespace: string}) {
313
96
  ): FilesOutput => {
314
97
  const files = new Map<string, string>();
315
98
 
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);
327
-
328
- files.set(
329
- `Native${name}Spec.g.h`,
330
- moduleTemplate
331
- .replace(/::_MODULE_PROPERTIES_TUPLE_::/g, traversedPropertyTuples)
99
+ for (const moduleName of Object.keys(schema.modules)) {
100
+ const nativeModule = schema.modules[moduleName];
101
+ // from 0.65 facebook's react-native-codegen
102
+ // the module name has the Native prefix comparing to 0.63
103
+ // when reading files we provided
104
+ const preferredModuleName = moduleName.startsWith('Native')
105
+ ? moduleName.substr(6)
106
+ : moduleName;
107
+ setPreferredModuleName(preferredModuleName);
108
+
109
+ if (nativeModule.type === 'NativeModule') {
110
+ console.log(`Generating Native${preferredModuleName}Spec.g.h`);
111
+
112
+ // copy all explicit to a map
113
+ const aliases: AliasMap = createAliasMap(nativeModule.aliasMap);
114
+
115
+ // prepare methods
116
+ const methods = generateValidateMethods(nativeModule, aliases, {
117
+ cppStringType,
118
+ });
119
+ let tuples = `
120
+ static constexpr auto methods = std::tuple{
121
+ ${methods[0]}
122
+ };`;
123
+ let checks = `
124
+ constexpr auto methodCheckResults = CheckMethods<TModule, ::_MODULE_NAME_::Spec>();`;
125
+ let errors = methods[1];
126
+
127
+ // prepare constants
128
+ const constants = generateValidateConstants(nativeModule, aliases);
129
+ if (constants !== undefined && !methodOnly) {
130
+ tuples = `
131
+ static constexpr auto constants = std::tuple{
132
+ ${constants[0]}
133
+ };${tuples}`;
134
+ checks = `
135
+ constexpr auto constantCheckResults = CheckConstants<TModule, ::_MODULE_NAME_::Spec>();${checks}`;
136
+ errors = `${constants[1]}
137
+
138
+ ${errors}`;
139
+ }
140
+
141
+ // generate code for structs
142
+ const [customTypes, customReflection] = generateAliases(aliases, {
143
+ cppStringType,
144
+ });
145
+
146
+ const customTypesExist = customTypes !== '';
147
+
148
+ const replaceContent = function (template: string): string {
149
+ return template
150
+ .replace(/::_MODULE_CUSTPM_TYPES_::/g, customTypes)
151
+ .replace(/::_MODULE_CUSTPM_TYPES_REFLECTION_::/g, customReflection)
152
+ .replace(/::_MODULE_MEMBERS_TUPLES_::/g, tuples.substring(1))
153
+ .replace(/::_MODULE_MEMBERS_CHECKS_::/g, checks.substring(1))
154
+ .replace(/::_MODULE_MEMBERS_ERRORS_::/g, errors)
155
+ .replace(/::_MODULE_NAME_::/g, preferredModuleName)
332
156
  .replace(
333
- /::_MODULE_PROPERTIES_SPEC_ERRORS_::/g,
334
- traversedProperties,
157
+ /::_TYPE_DEFINITION_INCLUDE_::/g,
158
+ customTypesExist
159
+ ? `// #include "Native${preferredModuleName}DataTypes.g.h" before this file to use the generated type definition`
160
+ : '',
335
161
  )
336
- .replace(/::_MODULE_NAME_::/g, name)
337
- .replace(/::_NAMESPACE_::/g, namespace),
338
- );
339
- });
162
+ .replace(/::_NAMESPACE_::/g, namespace);
163
+ };
164
+
165
+ if (separateDataTypes) {
166
+ if (customTypesExist) {
167
+ files.set(
168
+ `Native${preferredModuleName}DataTypes.g.h`,
169
+ replaceContent(typeOnlyTemplate),
170
+ );
171
+ }
172
+ files.set(
173
+ `Native${preferredModuleName}Spec.g.h`,
174
+ replaceContent(moduleOnlyTemplate),
175
+ );
176
+ } else {
177
+ files.set(
178
+ `Native${preferredModuleName}Spec.g.h`,
179
+ replaceContent(allInOneTemplate),
180
+ );
181
+ }
182
+ }
340
183
  }
341
184
 
342
185
  return files;