@react-native-windows/codegen 0.0.0-canary.10 → 0.0.0-canary.100

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 +876 -4
  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 +113 -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 +76 -0
  21. package/lib-commonjs/generators/ObjectTypes.js.map +1 -0
  22. package/lib-commonjs/generators/ParamTypes.d.ts +13 -0
  23. package/lib-commonjs/generators/ParamTypes.js +179 -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 +14 -0
  32. package/lib-commonjs/generators/ValidateMethods.js +103 -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 +41 -21
  38. package/src/Cli.ts +57 -223
  39. package/src/generators/AliasGen.ts +186 -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 +131 -0
  44. package/src/generators/ParamTypes.ts +382 -0
  45. package/src/generators/ReturnTypes.ts +70 -0
  46. package/src/generators/ValidateConstants.ts +50 -0
  47. package/src/generators/ValidateMethods.ts +258 -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 -613
  52. package/jest.config.js +0 -1
  53. package/tsconfig.json +0 -5
@@ -0,0 +1,382 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ * Licensed under the MIT License.
4
+ * @format
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ import type {
10
+ NamedShape,
11
+ NativeModuleArrayTypeAnnotation,
12
+ NativeModuleBaseTypeAnnotation,
13
+ NativeModuleEventEmitterBaseTypeAnnotation,
14
+ NativeModuleEventEmitterTypeAnnotation,
15
+ NativeModuleEnumDeclaration,
16
+ NativeModuleFunctionTypeAnnotation,
17
+ NativeModuleParamTypeAnnotation,
18
+ NativeModuleUnionTypeAnnotation,
19
+ Nullable,
20
+ } from '@react-native/codegen/lib/CodegenSchema';
21
+ import {
22
+ AliasMap,
23
+ getAliasCppName,
24
+ getAnonymousAliasCppName,
25
+ } from './AliasManaging';
26
+ import type {CppCodegenOptions} from './ObjectTypes';
27
+
28
+ type NativeModuleParamShape = NamedShape<
29
+ Nullable<NativeModuleParamTypeAnnotation>
30
+ >;
31
+
32
+ type ParamTarget = 'spec' | 'template' | 'callback-arg' | 'method-arg';
33
+
34
+ function decorateType(type: string, target: ParamTarget): string {
35
+ switch (target) {
36
+ case 'method-arg':
37
+ return `${type} &&`;
38
+ case 'callback-arg':
39
+ return `${type} const &`;
40
+ default:
41
+ return type;
42
+ }
43
+ }
44
+
45
+ function translateUnionReturnType(
46
+ type: NativeModuleEnumDeclaration | NativeModuleUnionTypeAnnotation,
47
+ target: ParamTarget,
48
+ options: CppCodegenOptions,
49
+ ): string {
50
+ const memberType = type.memberType;
51
+ switch (type.memberType) {
52
+ case 'StringTypeAnnotation':
53
+ return options.cppStringType;
54
+ case 'NumberTypeAnnotation':
55
+ return 'double';
56
+ case 'ObjectTypeAnnotation':
57
+ return decorateType('::React::JSValue', target);
58
+ default:
59
+ throw new Error(
60
+ `Unknown enum/union member type in translateReturnType: ${memberType}`,
61
+ );
62
+ }
63
+ }
64
+
65
+ function translateFunction(
66
+ param: NativeModuleFunctionTypeAnnotation,
67
+ aliases: AliasMap,
68
+ baseAliasName: string,
69
+ target: ParamTarget,
70
+ options: CppCodegenOptions,
71
+ ): string {
72
+ // TODO: type.returnTypeAnnotation
73
+ switch (target) {
74
+ case 'spec':
75
+ return `Callback<${param.params
76
+ .map((p: NativeModuleParamShape) =>
77
+ translateSpecFunctionParam(
78
+ p,
79
+ aliases,
80
+ `${baseAliasName}_${p.name}`,
81
+ options,
82
+ ),
83
+ )
84
+ .join(', ')}>`;
85
+ case 'template':
86
+ return `std::function<void(${param.params
87
+ .map((p: NativeModuleParamShape) =>
88
+ translateCallbackParam(
89
+ p,
90
+ aliases,
91
+ `${baseAliasName}_${p.name}`,
92
+ options,
93
+ ),
94
+ )
95
+ .join(', ')})>`;
96
+ default:
97
+ return `std::function<void(${param.params
98
+ .map((p: NativeModuleParamShape) =>
99
+ translateCallbackParam(
100
+ p,
101
+ aliases,
102
+ `${baseAliasName}_${p.name}`,
103
+ options,
104
+ ),
105
+ )
106
+ .join(', ')})> const &`;
107
+ }
108
+ }
109
+
110
+ function translateArray(
111
+ param: NativeModuleArrayTypeAnnotation<
112
+ Nullable<NativeModuleBaseTypeAnnotation>
113
+ >,
114
+ aliases: AliasMap,
115
+ baseAliasName: string,
116
+ target: ParamTarget,
117
+ options: CppCodegenOptions,
118
+ ): string {
119
+ if (param.elementType) {
120
+ switch (target) {
121
+ case 'spec':
122
+ case 'template':
123
+ return `std::vector<${translateNullableParamType(
124
+ param.elementType,
125
+ aliases,
126
+ `${baseAliasName}_element`,
127
+ 'template',
128
+ 'template',
129
+ options,
130
+ )}>`;
131
+ default:
132
+ return `std::vector<${translateNullableParamType(
133
+ param.elementType,
134
+ aliases,
135
+ `${baseAliasName}_element`,
136
+ 'template',
137
+ 'template',
138
+ options,
139
+ )}> const &`;
140
+ }
141
+ } else {
142
+ return decorateType('::React::JSValueArray', target);
143
+ }
144
+ }
145
+
146
+ // Hopefully eventually NativeModuleEventEmitterTypeAnnotation will align better with NativeModuleParamTypeAnnotation
147
+ // and this method can be merged / replaced with translateArray
148
+ function translateEventEmitterArray(
149
+ param: {
150
+ readonly type: 'ArrayTypeAnnotation';
151
+ readonly elementType: NativeModuleEventEmitterBaseTypeAnnotation | {type: string};
152
+ },
153
+ aliases: AliasMap,
154
+ baseAliasName: string,
155
+ target: ParamTarget,
156
+ options: CppCodegenOptions,
157
+ ): string {
158
+ switch (target) {
159
+ case 'spec':
160
+ case 'template':
161
+ return `std::vector<${translateEventEmitterParam(
162
+ param.elementType as NativeModuleEventEmitterBaseTypeAnnotation,
163
+ aliases,
164
+ `${baseAliasName}_element`,
165
+ 'template',
166
+ options,
167
+ )}>`;
168
+ default:
169
+ return `std::vector<${translateEventEmitterParam(
170
+ param.elementType as NativeModuleEventEmitterBaseTypeAnnotation,
171
+ aliases,
172
+ `${baseAliasName}_element`,
173
+ 'template',
174
+ options,
175
+ )}> const &`;
176
+ }
177
+ }
178
+
179
+ function translateParam(
180
+ param: NativeModuleParamTypeAnnotation,
181
+ aliases: AliasMap,
182
+ baseAliasName: string,
183
+ target: ParamTarget,
184
+ options: CppCodegenOptions,
185
+ ): string {
186
+ // avoid: Property 'type' does not exist on type 'never'
187
+ const paramType = param.type;
188
+ switch (param.type) {
189
+ case 'StringTypeAnnotation':
190
+ return options.cppStringType;
191
+ case 'NumberTypeAnnotation':
192
+ case 'FloatTypeAnnotation':
193
+ case 'DoubleTypeAnnotation':
194
+ return 'double';
195
+ case 'Int32TypeAnnotation':
196
+ return 'int';
197
+ case 'BooleanTypeAnnotation':
198
+ return 'bool';
199
+ case 'FunctionTypeAnnotation':
200
+ return translateFunction(param, aliases, baseAliasName, target, options);
201
+ case 'ArrayTypeAnnotation':
202
+ return translateArray(param, aliases, baseAliasName, target, options);
203
+ case 'GenericObjectTypeAnnotation':
204
+ return decorateType('::React::JSValue', target);
205
+ case 'ObjectTypeAnnotation':
206
+ return decorateType(
207
+ getAnonymousAliasCppName(aliases, baseAliasName, param),
208
+ target,
209
+ );
210
+ case 'ReservedTypeAnnotation': {
211
+ // avoid: Property 'name' does not exist on type 'never'
212
+ const name = param.name;
213
+ // (#6597)
214
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
215
+ if (name !== 'RootTag')
216
+ throw new Error(`Unknown reserved function: ${name} in translateParam`);
217
+ return 'double';
218
+ }
219
+ case 'TypeAliasTypeAnnotation':
220
+ return decorateType(getAliasCppName(param.name), target);
221
+ case 'MixedTypeAnnotation':
222
+ return '';
223
+ case 'EnumDeclaration':
224
+ case 'UnionTypeAnnotation':
225
+ return translateUnionReturnType(param, target, options);
226
+ default:
227
+ throw new Error(`Unhandled type in translateParam: ${paramType}`);
228
+ }
229
+ }
230
+
231
+ // Hopefully eventually NativeModuleEventEmitterTypeAnnotation will align better with NativeModuleParamTypeAnnotation
232
+ // and this method can be merged / replaced with translateParam
233
+ function translateEventEmitterParam(
234
+ param: NativeModuleEventEmitterTypeAnnotation,
235
+ aliases: AliasMap,
236
+ baseAliasName: string,
237
+ target: ParamTarget,
238
+ options: CppCodegenOptions,
239
+ ): string {
240
+ // avoid: Property 'type' does not exist on type 'never'
241
+ const paramType = param.type;
242
+ switch (param.type) {
243
+ case 'StringTypeAnnotation':
244
+ return options.cppStringType;
245
+ case 'NumberTypeAnnotation':
246
+ case 'FloatTypeAnnotation':
247
+ case 'DoubleTypeAnnotation':
248
+ return 'double';
249
+ case 'Int32TypeAnnotation':
250
+ return 'int';
251
+ case 'BooleanTypeAnnotation':
252
+ return 'bool';
253
+ case 'ArrayTypeAnnotation':
254
+ return translateEventEmitterArray(param, aliases, baseAliasName, target, options);
255
+ case 'TypeAliasTypeAnnotation':
256
+ return decorateType(getAliasCppName(param.name), target);
257
+ case 'VoidTypeAnnotation':
258
+ return 'void';
259
+ default:
260
+ throw new Error(`Unhandled type in translateParam: ${paramType}`);
261
+ }
262
+ }
263
+
264
+ function translateNullableParamType(
265
+ paramType: Nullable<NativeModuleParamTypeAnnotation>,
266
+ aliases: AliasMap,
267
+ baseAliasName: string,
268
+ nullableTarget: ParamTarget,
269
+ target: ParamTarget,
270
+ options: CppCodegenOptions,
271
+ ): string {
272
+ switch (paramType.type) {
273
+ case 'NullableTypeAnnotation':
274
+ return `std::optional<${translateParam(
275
+ paramType.typeAnnotation,
276
+ aliases,
277
+ baseAliasName,
278
+ nullableTarget,
279
+ options,
280
+ )}>`;
281
+ default:
282
+ return translateParam(paramType, aliases, baseAliasName, target, options);
283
+ }
284
+ }
285
+
286
+ function translateSpecFunctionParam(
287
+ param: NativeModuleParamShape,
288
+ aliases: AliasMap,
289
+ baseAliasName: string,
290
+ options: CppCodegenOptions,
291
+ ): string {
292
+ return translateNullableParamType(
293
+ param.typeAnnotation,
294
+ aliases,
295
+ baseAliasName,
296
+ 'spec',
297
+ 'spec',
298
+ options,
299
+ );
300
+ }
301
+
302
+ function translateCallbackParam(
303
+ param: NativeModuleParamShape,
304
+ aliases: AliasMap,
305
+ baseAliasName: string,
306
+ options: CppCodegenOptions,
307
+ ): string {
308
+ return translateNullableParamType(
309
+ param.typeAnnotation,
310
+ aliases,
311
+ baseAliasName,
312
+ 'template',
313
+ 'callback-arg',
314
+ options,
315
+ );
316
+ }
317
+
318
+ function translateFunctionParam(
319
+ param: NativeModuleParamShape,
320
+ aliases: AliasMap,
321
+ baseAliasName: string,
322
+ options: CppCodegenOptions,
323
+ ): string {
324
+ return translateNullableParamType(
325
+ param.typeAnnotation,
326
+ aliases,
327
+ baseAliasName,
328
+ 'template',
329
+ 'method-arg',
330
+ options,
331
+ );
332
+ }
333
+
334
+ export function translateSpecArgs(
335
+ params: ReadonlyArray<NativeModuleParamShape>,
336
+ aliases: AliasMap,
337
+ baseAliasName: string,
338
+ options: CppCodegenOptions,
339
+ ) {
340
+ return params.map(param => {
341
+ const translatedParam = translateSpecFunctionParam(
342
+ param,
343
+ aliases,
344
+ `${baseAliasName}_${param.name}`,
345
+ options,
346
+ );
347
+ return `${translatedParam}`;
348
+ });
349
+ }
350
+
351
+ export function translateEventEmitterArgs(
352
+ params: NativeModuleEventEmitterTypeAnnotation,
353
+ aliases: AliasMap,
354
+ baseAliasName: string,
355
+ options: CppCodegenOptions,
356
+ ) {
357
+ const translatedParam = translateEventEmitterParam(
358
+ params,
359
+ aliases,
360
+ baseAliasName,
361
+ 'spec',
362
+ options,
363
+ );
364
+ return `${translatedParam}`;
365
+ }
366
+
367
+ export function translateArgs(
368
+ params: ReadonlyArray<NativeModuleParamShape>,
369
+ aliases: AliasMap,
370
+ baseAliasName: string,
371
+ options: CppCodegenOptions,
372
+ ) {
373
+ return params.map(param => {
374
+ const translatedParam = translateFunctionParam(
375
+ param,
376
+ aliases,
377
+ `${baseAliasName}_${param.name}`,
378
+ options,
379
+ );
380
+ return `${translatedParam} ${param.name}`;
381
+ });
382
+ }
@@ -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 type {
10
+ NativeModuleReturnTypeAnnotation,
11
+ Nullable,
12
+ } from '@react-native/codegen/lib/CodegenSchema';
13
+ import {AliasMap} from './AliasManaging';
14
+ import {CppCodegenOptions, translateFieldOrReturnType} from './ObjectTypes';
15
+
16
+ function translateReturnType(
17
+ type: Nullable<NativeModuleReturnTypeAnnotation>,
18
+ aliases: AliasMap,
19
+ baseAliasName: string,
20
+ options: CppCodegenOptions,
21
+ ): string {
22
+ switch (type.type) {
23
+ case 'VoidTypeAnnotation':
24
+ case 'PromiseTypeAnnotation':
25
+ return 'void';
26
+ case 'NullableTypeAnnotation':
27
+ return `std::optional<${translateReturnType(
28
+ type.typeAnnotation,
29
+ aliases,
30
+ baseAliasName,
31
+ options,
32
+ )}>`;
33
+ default:
34
+ return translateFieldOrReturnType(
35
+ type,
36
+ aliases,
37
+ baseAliasName,
38
+ 'translateReturnType',
39
+ options,
40
+ );
41
+ }
42
+ }
43
+
44
+ export function translateSpecReturnType(
45
+ type: Nullable<NativeModuleReturnTypeAnnotation>,
46
+ aliases: AliasMap,
47
+ baseAliasName: string,
48
+ options: CppCodegenOptions,
49
+ ) {
50
+ return translateReturnType(
51
+ type,
52
+ aliases,
53
+ `${baseAliasName}_returnType`,
54
+ options,
55
+ );
56
+ }
57
+
58
+ export function translateImplReturnType(
59
+ type: Nullable<NativeModuleReturnTypeAnnotation>,
60
+ aliases: AliasMap,
61
+ baseAliasName: string,
62
+ options: CppCodegenOptions,
63
+ ) {
64
+ return translateReturnType(
65
+ type,
66
+ aliases,
67
+ `${baseAliasName}_returnType`,
68
+ options,
69
+ );
70
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ * Licensed under the MIT License.
4
+ * @format
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ import type {NativeModuleSchema} from '@react-native/codegen/lib/CodegenSchema';
10
+ import {AliasMap, getAnonymousAliasCppName} from './AliasManaging';
11
+
12
+ export function generateValidateConstants(
13
+ nativeModule: NativeModuleSchema,
14
+ aliases: AliasMap,
15
+ ): [string, string] | undefined {
16
+ const candidates = nativeModule.spec.methods.filter(
17
+ prop => prop.name === 'getConstants',
18
+ );
19
+ if (candidates.length === 0) {
20
+ return undefined;
21
+ }
22
+
23
+ const getConstant = candidates[0];
24
+ const funcType =
25
+ getConstant.typeAnnotation.type === 'NullableTypeAnnotation'
26
+ ? getConstant.typeAnnotation.typeAnnotation
27
+ : getConstant.typeAnnotation;
28
+ if (
29
+ funcType.params.length > 0 ||
30
+ funcType.returnTypeAnnotation.type !== 'ObjectTypeAnnotation'
31
+ ) {
32
+ return undefined;
33
+ }
34
+
35
+ const constantType = funcType.returnTypeAnnotation;
36
+ if (constantType.properties.length === 0) {
37
+ return undefined;
38
+ }
39
+
40
+ const cppName = getAnonymousAliasCppName(aliases, 'Constants', constantType);
41
+
42
+ return [
43
+ ` TypedConstant<${cppName}>{0},`,
44
+ ` REACT_SHOW_CONSTANT_SPEC_ERRORS(
45
+ 0,
46
+ "${cppName}",
47
+ " REACT_GET_CONSTANTS(GetConstants) ${cppName} GetConstants() noexcept {/*implementation*/}\\n"
48
+ " REACT_GET_CONSTANTS(GetConstants) static ${cppName} GetConstants() noexcept {/*implementation*/}\\n");`,
49
+ ];
50
+ }