@react-native-windows/codegen 0.0.0-canary.4 → 0.0.0-canary.41

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 (52) hide show
  1. package/CHANGELOG.md +359 -4
  2. package/bin.js +0 -0
  3. package/lib-commonjs/Cli.d.ts +7 -0
  4. package/lib-commonjs/Cli.js +60 -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 +12 -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/GenerateTypeScript.d.ts +11 -0
  16. package/lib-commonjs/generators/GenerateTypeScript.js +166 -0
  17. package/lib-commonjs/generators/GenerateTypeScript.js.map +1 -0
  18. package/lib-commonjs/generators/ObjectTypes.d.ts +8 -0
  19. package/lib-commonjs/generators/ObjectTypes.js +53 -0
  20. package/lib-commonjs/generators/ObjectTypes.js.map +1 -0
  21. package/lib-commonjs/generators/ParamTypes.d.ts +11 -0
  22. package/lib-commonjs/generators/ParamTypes.js +114 -0
  23. package/lib-commonjs/generators/ParamTypes.js.map +1 -0
  24. package/lib-commonjs/generators/ReturnTypes.d.ts +9 -0
  25. package/lib-commonjs/generators/ReturnTypes.js +63 -0
  26. package/lib-commonjs/generators/ReturnTypes.js.map +1 -0
  27. package/lib-commonjs/generators/ValidateConstants.d.ts +8 -0
  28. package/lib-commonjs/generators/ValidateConstants.js +38 -0
  29. package/lib-commonjs/generators/ValidateConstants.js.map +1 -0
  30. package/lib-commonjs/generators/ValidateMethods.d.ts +8 -0
  31. package/lib-commonjs/generators/ValidateMethods.js +70 -0
  32. package/lib-commonjs/generators/ValidateMethods.js.map +1 -0
  33. package/lib-commonjs/index.d.ts +35 -0
  34. package/lib-commonjs/index.js +190 -0
  35. package/lib-commonjs/index.js.map +1 -0
  36. package/package.json +32 -18
  37. package/src/Cli.ts +26 -152
  38. package/src/generators/AliasGen.ts +105 -0
  39. package/src/generators/AliasManaging.ts +75 -0
  40. package/src/generators/GenerateNM2.ts +69 -297
  41. package/src/generators/GenerateTypeScript.ts +247 -0
  42. package/src/generators/ObjectTypes.ts +73 -0
  43. package/src/generators/ParamTypes.ts +220 -0
  44. package/src/generators/ReturnTypes.ts +92 -0
  45. package/src/generators/ValidateConstants.ts +50 -0
  46. package/src/generators/ValidateMethods.ts +135 -0
  47. package/src/index.ts +321 -0
  48. package/.eslintrc.js +0 -4
  49. package/.vscode/launch.json +0 -23
  50. package/CHANGELOG.json +0 -462
  51. package/jest.config.js +0 -1
  52. package/tsconfig.json +0 -5
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ * Licensed under the MIT License.
4
+ * @format
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ import type {
10
+ NativeModuleBaseTypeAnnotation,
11
+ Nullable,
12
+ } from 'react-native-tscodegen';
13
+ import {
14
+ AliasMap,
15
+ getAliasCppName,
16
+ getAnonymousAliasCppName,
17
+ } from './AliasManaging';
18
+
19
+ export function translateField(
20
+ type: Nullable<NativeModuleBaseTypeAnnotation>,
21
+ aliases: AliasMap,
22
+ baseAliasName: string,
23
+ ): string {
24
+ // avoid: Property 'type' does not exist on type 'never'
25
+ const returnType = type.type;
26
+ switch (type.type) {
27
+ case 'StringTypeAnnotation':
28
+ return 'std::string';
29
+ case 'NumberTypeAnnotation':
30
+ case 'FloatTypeAnnotation':
31
+ case 'DoubleTypeAnnotation':
32
+ return 'double';
33
+ case 'Int32TypeAnnotation':
34
+ return 'int';
35
+ case 'BooleanTypeAnnotation':
36
+ return 'bool';
37
+ case 'ArrayTypeAnnotation':
38
+ if (type.elementType) {
39
+ return `std::vector<${translateField(
40
+ type.elementType,
41
+ aliases,
42
+ `${baseAliasName}_element`,
43
+ )}>`;
44
+ } else {
45
+ return `::React::JSValueArray`;
46
+ }
47
+ case 'GenericObjectTypeAnnotation':
48
+ return '::React::JSValue';
49
+ case 'ObjectTypeAnnotation':
50
+ return getAnonymousAliasCppName(aliases, baseAliasName, type);
51
+ case 'ReservedTypeAnnotation': {
52
+ // avoid: Property 'name' does not exist on type 'never'
53
+ const name = type.name;
54
+ // (#6597)
55
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
56
+ if (name !== 'RootTag')
57
+ throw new Error(
58
+ `Unknown reserved function: ${name} in translateReturnType`,
59
+ );
60
+ return 'double';
61
+ }
62
+ case 'TypeAliasTypeAnnotation':
63
+ return getAliasCppName(type.name);
64
+ case 'NullableTypeAnnotation':
65
+ return `std::optional<${translateField(
66
+ type.typeAnnotation,
67
+ aliases,
68
+ baseAliasName,
69
+ )}>`;
70
+ default:
71
+ throw new Error(`Unhandled type in translateReturnType: ${returnType}`);
72
+ }
73
+ }
@@ -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 type {
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
+ }
@@ -0,0 +1,92 @@
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-tscodegen';
13
+ import {
14
+ AliasMap,
15
+ getAliasCppName,
16
+ getAnonymousAliasCppName,
17
+ } from './AliasManaging';
18
+
19
+ function translateReturnType(
20
+ type: Nullable<NativeModuleReturnTypeAnnotation>,
21
+ aliases: AliasMap,
22
+ baseAliasName: string,
23
+ ): string {
24
+ // avoid: Property 'type' does not exist on type 'never'
25
+ const returnType = type.type;
26
+ switch (type.type) {
27
+ case 'VoidTypeAnnotation':
28
+ case 'PromiseTypeAnnotation':
29
+ return 'void';
30
+ case 'StringTypeAnnotation':
31
+ return 'std::string';
32
+ case 'NumberTypeAnnotation':
33
+ case 'FloatTypeAnnotation':
34
+ case 'DoubleTypeAnnotation':
35
+ return 'double';
36
+ case 'Int32TypeAnnotation':
37
+ return 'int';
38
+ case 'BooleanTypeAnnotation':
39
+ return 'bool';
40
+ case 'ArrayTypeAnnotation':
41
+ if (type.elementType) {
42
+ return `std::vector<${translateReturnType(
43
+ type.elementType,
44
+ aliases,
45
+ `${baseAliasName}_element`,
46
+ )}>`;
47
+ } else {
48
+ return '::React::JSValueArray';
49
+ }
50
+ case 'GenericObjectTypeAnnotation':
51
+ return '::React::JSValue';
52
+ case 'ObjectTypeAnnotation':
53
+ return getAnonymousAliasCppName(aliases, baseAliasName, type);
54
+ case 'ReservedTypeAnnotation': {
55
+ // avoid: Property 'name' does not exist on type 'never'
56
+ const name = type.name;
57
+ // (#6597)
58
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
59
+ if (name !== 'RootTag')
60
+ throw new Error(
61
+ `Unknown reserved function: ${name} in translateReturnType`,
62
+ );
63
+ return 'double';
64
+ }
65
+ case 'TypeAliasTypeAnnotation':
66
+ return getAliasCppName(type.name);
67
+ case 'NullableTypeAnnotation':
68
+ return `std::optional<${translateReturnType(
69
+ type.typeAnnotation,
70
+ aliases,
71
+ baseAliasName,
72
+ )}>`;
73
+ default:
74
+ throw new Error(`Unhandled type in translateReturnType: ${returnType}`);
75
+ }
76
+ }
77
+
78
+ export function translateSpecReturnType(
79
+ type: Nullable<NativeModuleReturnTypeAnnotation>,
80
+ aliases: AliasMap,
81
+ baseAliasName: string,
82
+ ) {
83
+ return translateReturnType(type, aliases, `${baseAliasName}_returnType`);
84
+ }
85
+
86
+ export function translateImplReturnType(
87
+ type: Nullable<NativeModuleReturnTypeAnnotation>,
88
+ aliases: AliasMap,
89
+ baseAliasName: string,
90
+ ) {
91
+ return translateReturnType(type, aliases, `${baseAliasName}_returnType`);
92
+ }
@@ -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-tscodegen';
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.properties.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
+ }
@@ -0,0 +1,135 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ * Licensed under the MIT License.
4
+ * @format
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ import type {
10
+ NativeModuleFunctionTypeAnnotation,
11
+ NativeModulePropertyShape,
12
+ NativeModuleSchema,
13
+ } from 'react-native-tscodegen';
14
+ import {AliasMap} from './AliasManaging';
15
+ import {translateArgs, translateSpecArgs} from './ParamTypes';
16
+ import {translateImplReturnType, translateSpecReturnType} from './ReturnTypes';
17
+
18
+ function isMethodSync(funcType: NativeModuleFunctionTypeAnnotation) {
19
+ return (
20
+ funcType.returnTypeAnnotation.type !== 'VoidTypeAnnotation' &&
21
+ funcType.returnTypeAnnotation.type !== 'PromiseTypeAnnotation'
22
+ );
23
+ }
24
+
25
+ function isMethodReturnPromise(funcType: NativeModuleFunctionTypeAnnotation) {
26
+ return funcType.returnTypeAnnotation.type === 'PromiseTypeAnnotation';
27
+ }
28
+
29
+ function getPossibleMethodSignatures(
30
+ prop: NativeModulePropertyShape,
31
+ funcType: NativeModuleFunctionTypeAnnotation,
32
+ aliases: AliasMap,
33
+ baseAliasName: string,
34
+ ): string[] {
35
+ const args = translateArgs(funcType.params, aliases, baseAliasName);
36
+ if (isMethodReturnPromise(funcType)) {
37
+ // TODO: type of the promise could be provided in the future
38
+ args.push('::React::ReactPromise<::React::JSValue> &&result');
39
+ }
40
+
41
+ // TODO: be much more exhastive on the possible method signatures that can be used..
42
+ const sig = `REACT_${isMethodSync(funcType) ? 'SYNC_' : ''}METHOD(${
43
+ prop.name
44
+ }) ${translateImplReturnType(
45
+ funcType.returnTypeAnnotation,
46
+ aliases,
47
+ baseAliasName,
48
+ )} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }`;
49
+
50
+ const staticsig = `REACT_${isMethodSync(funcType) ? 'SYNC_' : ''}METHOD(${
51
+ prop.name
52
+ }) static ${translateImplReturnType(
53
+ funcType.returnTypeAnnotation,
54
+ aliases,
55
+ baseAliasName,
56
+ )} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }`;
57
+
58
+ return [sig, staticsig];
59
+ }
60
+
61
+ function translatePossibleMethodSignatures(
62
+ prop: NativeModulePropertyShape,
63
+ funcType: NativeModuleFunctionTypeAnnotation,
64
+ aliases: AliasMap,
65
+ baseAliasName: string,
66
+ ): string {
67
+ return getPossibleMethodSignatures(prop, funcType, aliases, baseAliasName)
68
+ .map(sig => `" ${sig}\\n"`)
69
+ .join('\n ');
70
+ }
71
+
72
+ function renderProperties(
73
+ properties: ReadonlyArray<NativeModulePropertyShape>,
74
+ aliases: AliasMap,
75
+ tuple: boolean,
76
+ ): string {
77
+ // TODO: generate code for constants
78
+ return properties
79
+ .filter(prop => prop.name !== 'getConstants')
80
+ .map((prop, index) => {
81
+ // TODO: prop.optional === true
82
+ // TODO: prop.typeAnnotation.type === 'NullableTypeAnnotation'
83
+ const propAliasName = prop.name;
84
+ const funcType =
85
+ prop.typeAnnotation.type === 'NullableTypeAnnotation'
86
+ ? prop.typeAnnotation.typeAnnotation
87
+ : prop.typeAnnotation;
88
+
89
+ const traversedArgs = translateSpecArgs(
90
+ funcType.params,
91
+ aliases,
92
+ propAliasName,
93
+ );
94
+
95
+ const translatedReturnParam = translateSpecReturnType(
96
+ funcType.returnTypeAnnotation,
97
+ aliases,
98
+ propAliasName,
99
+ );
100
+
101
+ if (isMethodReturnPromise(funcType)) {
102
+ // TODO: type of the promise could be provided in the future
103
+ traversedArgs.push('Promise<::React::JSValue>');
104
+ }
105
+
106
+ if (tuple) {
107
+ return ` ${
108
+ isMethodSync(funcType) ? 'Sync' : ''
109
+ }Method<${translatedReturnParam}(${traversedArgs.join(
110
+ ', ',
111
+ )}) noexcept>{${index}, L"${prop.name}"},`;
112
+ } else {
113
+ return ` REACT_SHOW_METHOD_SPEC_ERRORS(
114
+ ${index},
115
+ "${prop.name}",
116
+ ${translatePossibleMethodSignatures(
117
+ prop,
118
+ funcType,
119
+ aliases,
120
+ propAliasName,
121
+ )});`;
122
+ }
123
+ })
124
+ .join('\n');
125
+ }
126
+
127
+ export function generateValidateMethods(
128
+ nativeModule: NativeModuleSchema,
129
+ aliases: AliasMap,
130
+ ): [string, string] {
131
+ const properties = nativeModule.spec.properties;
132
+ const traversedProperties = renderProperties(properties, aliases, false);
133
+ const traversedPropertyTuples = renderProperties(properties, aliases, true);
134
+ return [traversedPropertyTuples, traversedProperties];
135
+ }