@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.
- package/CHANGELOG.md +359 -4
- package/bin.js +0 -0
- package/lib-commonjs/Cli.d.ts +7 -0
- package/lib-commonjs/Cli.js +60 -0
- package/lib-commonjs/Cli.js.map +1 -0
- package/lib-commonjs/generators/AliasGen.d.ts +11 -0
- package/lib-commonjs/generators/AliasGen.js +72 -0
- package/lib-commonjs/generators/AliasGen.js.map +1 -0
- package/lib-commonjs/generators/AliasManaging.d.ts +15 -0
- package/lib-commonjs/generators/AliasManaging.js +49 -0
- package/lib-commonjs/generators/AliasManaging.js.map +1 -0
- package/lib-commonjs/generators/GenerateNM2.d.ts +12 -0
- package/lib-commonjs/generators/GenerateNM2.js +94 -0
- package/lib-commonjs/generators/GenerateNM2.js.map +1 -0
- package/lib-commonjs/generators/GenerateTypeScript.d.ts +11 -0
- package/lib-commonjs/generators/GenerateTypeScript.js +166 -0
- package/lib-commonjs/generators/GenerateTypeScript.js.map +1 -0
- package/lib-commonjs/generators/ObjectTypes.d.ts +8 -0
- package/lib-commonjs/generators/ObjectTypes.js +53 -0
- package/lib-commonjs/generators/ObjectTypes.js.map +1 -0
- package/lib-commonjs/generators/ParamTypes.d.ts +11 -0
- package/lib-commonjs/generators/ParamTypes.js +114 -0
- package/lib-commonjs/generators/ParamTypes.js.map +1 -0
- package/lib-commonjs/generators/ReturnTypes.d.ts +9 -0
- package/lib-commonjs/generators/ReturnTypes.js +63 -0
- package/lib-commonjs/generators/ReturnTypes.js.map +1 -0
- package/lib-commonjs/generators/ValidateConstants.d.ts +8 -0
- package/lib-commonjs/generators/ValidateConstants.js +38 -0
- package/lib-commonjs/generators/ValidateConstants.js.map +1 -0
- package/lib-commonjs/generators/ValidateMethods.d.ts +8 -0
- package/lib-commonjs/generators/ValidateMethods.js +70 -0
- package/lib-commonjs/generators/ValidateMethods.js.map +1 -0
- package/lib-commonjs/index.d.ts +35 -0
- package/lib-commonjs/index.js +190 -0
- package/lib-commonjs/index.js.map +1 -0
- package/package.json +32 -18
- package/src/Cli.ts +26 -152
- package/src/generators/AliasGen.ts +105 -0
- package/src/generators/AliasManaging.ts +75 -0
- package/src/generators/GenerateNM2.ts +69 -297
- package/src/generators/GenerateTypeScript.ts +247 -0
- package/src/generators/ObjectTypes.ts +73 -0
- package/src/generators/ParamTypes.ts +220 -0
- package/src/generators/ReturnTypes.ts +92 -0
- package/src/generators/ValidateConstants.ts +50 -0
- package/src/generators/ValidateMethods.ts +135 -0
- package/src/index.ts +321 -0
- package/.eslintrc.js +0 -4
- package/.vscode/launch.json +0 -23
- package/CHANGELOG.json +0 -462
- package/jest.config.js +0 -1
- package/tsconfig.json +0 -5
|
@@ -6,14 +6,11 @@
|
|
|
6
6
|
|
|
7
7
|
'use strict';
|
|
8
8
|
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
FunctionTypeAnnotationParamTypeAnnotation,
|
|
15
|
-
FunctionTypeAnnotationReturn,
|
|
16
|
-
} from 'react-native-tscodegen';
|
|
9
|
+
import type {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,281 +28,28 @@ 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
|
-
|
|
37
|
-
::_MODULE_PROPERTIES_TUPLE_::
|
|
38
|
-
};
|
|
33
|
+
::_MODULE_MEMBERS_TUPLES_::
|
|
39
34
|
|
|
40
35
|
template <class TModule>
|
|
41
36
|
static constexpr void ValidateModule() noexcept {
|
|
42
|
-
|
|
37
|
+
::_MODULE_MEMBERS_CHECKS_::
|
|
43
38
|
|
|
44
|
-
::
|
|
39
|
+
::_MODULE_MEMBERS_ERRORS_::
|
|
45
40
|
}
|
|
46
41
|
};
|
|
47
42
|
|
|
48
43
|
} // namespace ::_NAMESPACE_::
|
|
49
44
|
`;
|
|
50
45
|
|
|
51
|
-
function
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
export function createNM2Generator({namespace}: {namespace: string}) {
|
|
46
|
+
export function createNM2Generator({
|
|
47
|
+
namespace,
|
|
48
|
+
methodonly,
|
|
49
|
+
}: {
|
|
50
|
+
namespace: string;
|
|
51
|
+
methodonly: boolean;
|
|
52
|
+
}) {
|
|
309
53
|
return (
|
|
310
54
|
_libraryName: string,
|
|
311
55
|
schema: SchemaType,
|
|
@@ -313,33 +57,61 @@ export function createNM2Generator({namespace}: {namespace: string}) {
|
|
|
313
57
|
): FilesOutput => {
|
|
314
58
|
const files = new Map<string, string>();
|
|
315
59
|
|
|
316
|
-
const
|
|
317
|
-
.
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
60
|
+
for (const moduleName of Object.keys(schema.modules)) {
|
|
61
|
+
const nativeModule = schema.modules[moduleName];
|
|
62
|
+
// from 0.65 facebook's react-native-codegen
|
|
63
|
+
// the module name has the Native prefix comparing to 0.63
|
|
64
|
+
// when reading files we provided
|
|
65
|
+
const preferredModuleName = moduleName.startsWith('Native')
|
|
66
|
+
? moduleName.substr(6)
|
|
67
|
+
: moduleName;
|
|
68
|
+
setPreferredModuleName(preferredModuleName);
|
|
69
|
+
|
|
70
|
+
if (nativeModule.type === 'NativeModule') {
|
|
71
|
+
console.log(`Generating Native${preferredModuleName}Spec.g.h`);
|
|
72
|
+
|
|
73
|
+
// copy all explicit to a map
|
|
74
|
+
const aliases: AliasMap = createAliasMap(nativeModule.aliases);
|
|
75
|
+
|
|
76
|
+
// prepare methods
|
|
77
|
+
const methods = generateValidateMethods(nativeModule, aliases);
|
|
78
|
+
let tuples = `
|
|
79
|
+
static constexpr auto methods = std::tuple{
|
|
80
|
+
${methods[0]}
|
|
81
|
+
};`;
|
|
82
|
+
let checks = `
|
|
83
|
+
constexpr auto methodCheckResults = CheckMethods<TModule, ::_MODULE_NAME_::Spec>();`;
|
|
84
|
+
let errors = methods[1];
|
|
85
|
+
|
|
86
|
+
// prepare constants
|
|
87
|
+
const constants = generateValidateConstants(nativeModule, aliases);
|
|
88
|
+
if (constants !== undefined && !methodonly) {
|
|
89
|
+
tuples = `
|
|
90
|
+
static constexpr auto constants = std::tuple{
|
|
91
|
+
${constants[0]}
|
|
92
|
+
};${tuples}`;
|
|
93
|
+
checks = `
|
|
94
|
+
constexpr auto constantCheckResults = CheckConstants<TModule, ::_MODULE_NAME_::Spec>();${checks}`;
|
|
95
|
+
errors = `${constants[1]}
|
|
96
|
+
|
|
97
|
+
${errors}`;
|
|
321
98
|
}
|
|
322
99
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
.replace(/::_MODULE_PROPERTIES_SPEC_ERRORS_::/g, traversedProperties)
|
|
339
|
-
.replace(/::_MODULE_NAME_::/g, name)
|
|
340
|
-
.replace(/::_NAMESPACE_::/g, namespace),
|
|
341
|
-
);
|
|
342
|
-
});
|
|
100
|
+
// generate code for structs
|
|
101
|
+
const traversedAliasedStructs = generateAliases(aliases);
|
|
102
|
+
|
|
103
|
+
files.set(
|
|
104
|
+
`Native${preferredModuleName}Spec.g.h`,
|
|
105
|
+
moduleTemplate
|
|
106
|
+
.replace(/::_MODULE_ALIASED_STRUCTS_::/g, traversedAliasedStructs)
|
|
107
|
+
.replace(/::_MODULE_MEMBERS_TUPLES_::/g, tuples.substring(1))
|
|
108
|
+
.replace(/::_MODULE_MEMBERS_CHECKS_::/g, checks.substring(1))
|
|
109
|
+
.replace(/::_MODULE_MEMBERS_ERRORS_::/g, errors)
|
|
110
|
+
.replace(/::_MODULE_NAME_::/g, preferredModuleName)
|
|
111
|
+
.replace(/::_NAMESPACE_::/g, namespace),
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
343
115
|
|
|
344
116
|
return files;
|
|
345
117
|
};
|
|
@@ -0,0 +1,247 @@
|
|
|
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
|
+
NativeModuleBaseTypeAnnotation,
|
|
12
|
+
NativeModuleFunctionTypeAnnotation,
|
|
13
|
+
NativeModuleObjectTypeAnnotation,
|
|
14
|
+
NativeModuleParamTypeAnnotation,
|
|
15
|
+
NativeModuleReturnTypeAnnotation,
|
|
16
|
+
NativeModuleSchema,
|
|
17
|
+
Nullable,
|
|
18
|
+
SchemaType,
|
|
19
|
+
} from 'react-native-tscodegen';
|
|
20
|
+
|
|
21
|
+
interface CodegenNativeModuleSchema extends NativeModuleSchema {
|
|
22
|
+
optionalTurboModule?: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function setOptionalTurboModule(
|
|
26
|
+
schema: NativeModuleSchema,
|
|
27
|
+
optional: boolean,
|
|
28
|
+
): void {
|
|
29
|
+
const cs = <CodegenNativeModuleSchema>schema;
|
|
30
|
+
cs.optionalTurboModule = optional;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function getOptionalTurboModule(schema: NativeModuleSchema): boolean {
|
|
34
|
+
return (<CodegenNativeModuleSchema>schema).optionalTurboModule ?? false;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type ObjectProp = NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>;
|
|
38
|
+
type FunctionParam = NamedShape<Nullable<NativeModuleParamTypeAnnotation>>;
|
|
39
|
+
type FunctionDecl = NamedShape<Nullable<NativeModuleFunctionTypeAnnotation>>;
|
|
40
|
+
type FilesOutput = Map<string, string>;
|
|
41
|
+
|
|
42
|
+
const moduleTemplate = `
|
|
43
|
+
/*
|
|
44
|
+
* This file is auto-generated from a NativeModule spec file in js.
|
|
45
|
+
*
|
|
46
|
+
* This is a TypeScript turbo module definition file.
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
import {TurboModule, TurboModuleRegistry} from 'react-native';
|
|
50
|
+
'use strict';
|
|
51
|
+
::_MODULE_ALIASED_STRUCTS_::
|
|
52
|
+
export interface Spec extends TurboModule {
|
|
53
|
+
::_MODULE_MEMBERS_::
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export default TurboModuleRegistry.::_MODULE_GETTER_::<Spec>('::_MODULE_NAME_::');
|
|
57
|
+
`;
|
|
58
|
+
|
|
59
|
+
function optionalSign<T>(obj: NamedShape<T>): string {
|
|
60
|
+
return obj.optional ? '?' : '';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function translateType(
|
|
64
|
+
type: Nullable<
|
|
65
|
+
| NativeModuleBaseTypeAnnotation
|
|
66
|
+
| NativeModuleParamTypeAnnotation
|
|
67
|
+
| NativeModuleReturnTypeAnnotation
|
|
68
|
+
>,
|
|
69
|
+
): string {
|
|
70
|
+
// avoid: Property 'type' does not exist on type 'never'
|
|
71
|
+
const returnType = type.type;
|
|
72
|
+
switch (type.type) {
|
|
73
|
+
case 'StringTypeAnnotation':
|
|
74
|
+
return 'string';
|
|
75
|
+
case 'NumberTypeAnnotation':
|
|
76
|
+
case 'FloatTypeAnnotation':
|
|
77
|
+
case 'DoubleTypeAnnotation':
|
|
78
|
+
case 'Int32TypeAnnotation':
|
|
79
|
+
return 'number';
|
|
80
|
+
case 'BooleanTypeAnnotation':
|
|
81
|
+
return 'boolean';
|
|
82
|
+
case 'ArrayTypeAnnotation':
|
|
83
|
+
if (type.elementType) {
|
|
84
|
+
return `${translateType(type.elementType)}[]`;
|
|
85
|
+
} else {
|
|
86
|
+
return `Array`;
|
|
87
|
+
}
|
|
88
|
+
case 'GenericObjectTypeAnnotation':
|
|
89
|
+
return 'object';
|
|
90
|
+
case 'ObjectTypeAnnotation':
|
|
91
|
+
return `{${type.properties
|
|
92
|
+
.map((prop: ObjectProp) => {
|
|
93
|
+
return `${prop.name}${optionalSign(prop)}: ${translateType(
|
|
94
|
+
prop.typeAnnotation,
|
|
95
|
+
)}`;
|
|
96
|
+
})
|
|
97
|
+
.join(', ')}}`;
|
|
98
|
+
case 'ReservedTypeAnnotation': {
|
|
99
|
+
// avoid: Property 'name' does not exist on type 'never'
|
|
100
|
+
const name = type.name;
|
|
101
|
+
// (#6597)
|
|
102
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
103
|
+
if (name !== 'RootTag')
|
|
104
|
+
throw new Error(
|
|
105
|
+
`Unknown reserved function: ${name} in translateReturnType`,
|
|
106
|
+
);
|
|
107
|
+
return 'number';
|
|
108
|
+
}
|
|
109
|
+
case 'TypeAliasTypeAnnotation':
|
|
110
|
+
return type.name;
|
|
111
|
+
case 'NullableTypeAnnotation':
|
|
112
|
+
return `(${translateType(type.typeAnnotation)} | null | undefined)`;
|
|
113
|
+
case 'VoidTypeAnnotation':
|
|
114
|
+
return `void`;
|
|
115
|
+
case 'PromiseTypeAnnotation':
|
|
116
|
+
return `Promise`;
|
|
117
|
+
case `FunctionTypeAnnotation`:
|
|
118
|
+
return `((${type.params
|
|
119
|
+
.map((param: FunctionParam) => {
|
|
120
|
+
return `${param.name}${optionalSign(param)}: ${translateType(
|
|
121
|
+
param.typeAnnotation,
|
|
122
|
+
)}`;
|
|
123
|
+
})
|
|
124
|
+
.join(', ')}) => ${translateType(type.returnTypeAnnotation)})`;
|
|
125
|
+
default:
|
|
126
|
+
throw new Error(`Unhandled type in translateReturnType: ${returnType}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function translateAlias(
|
|
131
|
+
name: string,
|
|
132
|
+
type: NativeModuleObjectTypeAnnotation,
|
|
133
|
+
): string {
|
|
134
|
+
return `
|
|
135
|
+
export interface ${name} {
|
|
136
|
+
${type.properties
|
|
137
|
+
.map((prop: ObjectProp) => {
|
|
138
|
+
return ` ${prop.name}${optionalSign(prop)}: ${translateType(
|
|
139
|
+
prop.typeAnnotation,
|
|
140
|
+
)};`;
|
|
141
|
+
})
|
|
142
|
+
.join('\n')}
|
|
143
|
+
}
|
|
144
|
+
`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function tryGetConstantType(
|
|
148
|
+
nativeModule: NativeModuleSchema,
|
|
149
|
+
): NativeModuleObjectTypeAnnotation | undefined {
|
|
150
|
+
const candidates = nativeModule.spec.properties.filter(
|
|
151
|
+
prop => prop.name === 'getConstants',
|
|
152
|
+
);
|
|
153
|
+
if (candidates.length === 0) {
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const getConstant = candidates[0];
|
|
158
|
+
const funcType =
|
|
159
|
+
getConstant.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
160
|
+
? getConstant.typeAnnotation.typeAnnotation
|
|
161
|
+
: getConstant.typeAnnotation;
|
|
162
|
+
if (
|
|
163
|
+
funcType.params.length > 0 ||
|
|
164
|
+
funcType.returnTypeAnnotation.type !== 'ObjectTypeAnnotation'
|
|
165
|
+
) {
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const constantType = funcType.returnTypeAnnotation;
|
|
170
|
+
if (constantType.properties.length === 0) {
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return constantType;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function translateMethod(func: FunctionDecl): string {
|
|
178
|
+
const funcType =
|
|
179
|
+
func.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
180
|
+
? func.typeAnnotation.typeAnnotation
|
|
181
|
+
: func.typeAnnotation;
|
|
182
|
+
|
|
183
|
+
return `
|
|
184
|
+
${func.name}(${funcType.params
|
|
185
|
+
.map((param: FunctionParam) => {
|
|
186
|
+
return `${param.name}${optionalSign(param)}: ${translateType(
|
|
187
|
+
param.typeAnnotation,
|
|
188
|
+
)}`;
|
|
189
|
+
})
|
|
190
|
+
.join(', ')})${optionalSign(func)}: ${translateType(
|
|
191
|
+
funcType.returnTypeAnnotation,
|
|
192
|
+
)}${
|
|
193
|
+
funcType.returnTypeAnnotation.type === 'ObjectTypeAnnotation' ? '' : ';'
|
|
194
|
+
}`;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function generateTypeScript(
|
|
198
|
+
_libraryName: string,
|
|
199
|
+
schema: SchemaType,
|
|
200
|
+
_moduleSpecName: string,
|
|
201
|
+
): FilesOutput {
|
|
202
|
+
const files = new Map<string, string>();
|
|
203
|
+
|
|
204
|
+
for (const moduleName of Object.keys(schema.modules)) {
|
|
205
|
+
const nativeModule = schema.modules[moduleName];
|
|
206
|
+
// from 0.65 facebook's react-native-codegen
|
|
207
|
+
// the module name has the Native prefix comparing to 0.63
|
|
208
|
+
// when reading files we provided
|
|
209
|
+
const nativePrefix = 'Native';
|
|
210
|
+
const preferredModuleName = moduleName.startsWith(nativePrefix)
|
|
211
|
+
? moduleName.substr(nativePrefix.length)
|
|
212
|
+
: moduleName;
|
|
213
|
+
|
|
214
|
+
if (nativeModule.type === 'NativeModule') {
|
|
215
|
+
console.log(`Generating ${preferredModuleName}Spec.g.ts`);
|
|
216
|
+
|
|
217
|
+
const aliasCode = Object.keys(nativeModule.aliases)
|
|
218
|
+
.map(name => translateAlias(name, nativeModule.aliases[name]))
|
|
219
|
+
.join('');
|
|
220
|
+
|
|
221
|
+
const constantType = tryGetConstantType(nativeModule);
|
|
222
|
+
const constantCode =
|
|
223
|
+
constantType === undefined
|
|
224
|
+
? ''
|
|
225
|
+
: ` getConstants(): ${translateType(constantType)}`;
|
|
226
|
+
|
|
227
|
+
const methods = nativeModule.spec.properties.filter(
|
|
228
|
+
prop => prop.name !== 'getConstants',
|
|
229
|
+
);
|
|
230
|
+
const membersCode = methods.map(translateMethod).join('');
|
|
231
|
+
|
|
232
|
+
files.set(
|
|
233
|
+
`${preferredModuleName}Spec.g.ts`,
|
|
234
|
+
moduleTemplate
|
|
235
|
+
.replace(/::_MODULE_ALIASED_STRUCTS_::/g, aliasCode)
|
|
236
|
+
.replace(/::_MODULE_MEMBERS_::/g, constantCode + membersCode)
|
|
237
|
+
.replace(/::_MODULE_NAME_::/g, preferredModuleName)
|
|
238
|
+
.replace(
|
|
239
|
+
/::_MODULE_GETTER_::/g,
|
|
240
|
+
getOptionalTurboModule(nativeModule) ? 'get' : 'getEnforcing',
|
|
241
|
+
),
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return files;
|
|
247
|
+
}
|