@react-native-windows/codegen 0.66.0 → 0.68.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.
- package/CHANGELOG.md +121 -8
- package/bin.js +0 -0
- package/lib-commonjs/Cli.d.ts +7 -0
- package/lib-commonjs/Cli.js +199 -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 +11 -0
- package/lib-commonjs/generators/GenerateNM2.js +94 -0
- package/lib-commonjs/generators/GenerateNM2.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/package.json +24 -13
- package/src/Cli.ts +41 -33
- package/src/generators/AliasGen.ts +105 -0
- package/src/generators/AliasManaging.ts +75 -0
- package/src/generators/GenerateNM2.ts +38 -122
- package/src/generators/ObjectTypes.ts +24 -39
- package/src/generators/ParamTypes.ts +146 -41
- package/src/generators/ReturnTypes.ts +29 -10
- package/src/generators/ValidateConstants.ts +50 -0
- package/src/generators/ValidateMethods.ts +135 -0
- package/.eslintrc.js +0 -4
- package/.vscode/launch.json +0 -23
- package/CHANGELOG.json +0 -688
- package/jest.config.js +0 -1
- package/tsconfig.json +0 -5
package/src/Cli.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
import yargs from 'yargs';
|
|
9
9
|
import path from 'path';
|
|
10
|
-
import fs from 'fs';
|
|
10
|
+
import fs from '@react-native-windows/fs';
|
|
11
11
|
import globby from 'globby';
|
|
12
12
|
import {createNM2Generator} from './generators/GenerateNM2';
|
|
13
13
|
// @ts-ignore
|
|
@@ -98,9 +98,11 @@ function checkFilesForChanges(
|
|
|
98
98
|
|
|
99
99
|
const allExistingFiles = globby
|
|
100
100
|
.sync(`${outputDir}/**`)
|
|
101
|
-
.map(_ => path.normalize(_))
|
|
101
|
+
.map((_) => path.normalize(_))
|
|
102
|
+
.sort();
|
|
103
|
+
const allGeneratedFiles = [...map.keys()]
|
|
104
|
+
.map((_) => path.normalize(_))
|
|
102
105
|
.sort();
|
|
103
|
-
const allGeneratedFiles = [...map.keys()].map(_ => path.normalize(_)).sort();
|
|
104
106
|
|
|
105
107
|
if (
|
|
106
108
|
allExistingFiles.length !== allGeneratedFiles.length ||
|
|
@@ -130,7 +132,7 @@ function writeMapToFiles(map: Map<string, string>, outputDir: string) {
|
|
|
130
132
|
|
|
131
133
|
// This ensures that we delete any generated files from modules that have been deleted
|
|
132
134
|
const allExistingFiles = globby.sync(`${outputDir}/**`);
|
|
133
|
-
allExistingFiles.forEach(existingFile => {
|
|
135
|
+
allExistingFiles.forEach((existingFile) => {
|
|
134
136
|
if (!map.has(path.normalize(existingFile))) {
|
|
135
137
|
fs.unlinkSync(existingFile);
|
|
136
138
|
}
|
|
@@ -207,18 +209,18 @@ function generate(
|
|
|
207
209
|
);
|
|
208
210
|
|
|
209
211
|
const generateNM2 = createNM2Generator({namespace: argv.namespace});
|
|
210
|
-
const generatorPropsH =
|
|
211
|
-
.generate;
|
|
212
|
-
const generatorPropsCPP =
|
|
213
|
-
.generate;
|
|
214
|
-
const generatorShadowNodeH =
|
|
215
|
-
.generate;
|
|
216
|
-
const generatorShadowNodeCPP =
|
|
217
|
-
.generate;
|
|
218
|
-
const generatorComponentDescriptorH =
|
|
219
|
-
.generate;
|
|
220
|
-
const generatorEventEmitterH =
|
|
221
|
-
.generate;
|
|
212
|
+
const generatorPropsH =
|
|
213
|
+
require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsH').generate;
|
|
214
|
+
const generatorPropsCPP =
|
|
215
|
+
require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsCPP').generate;
|
|
216
|
+
const generatorShadowNodeH =
|
|
217
|
+
require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeH').generate;
|
|
218
|
+
const generatorShadowNodeCPP =
|
|
219
|
+
require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeCPP').generate;
|
|
220
|
+
const generatorComponentDescriptorH =
|
|
221
|
+
require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateComponentDescriptorH').generate;
|
|
222
|
+
const generatorEventEmitterH =
|
|
223
|
+
require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterH').generate;
|
|
222
224
|
|
|
223
225
|
normalizeFileMap(
|
|
224
226
|
generateNM2(libraryName, schema, moduleSpecName),
|
|
@@ -226,23 +228,29 @@ function generate(
|
|
|
226
228
|
generatedFiles,
|
|
227
229
|
);
|
|
228
230
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
)
|
|
244
|
-
|
|
245
|
-
|
|
231
|
+
if (
|
|
232
|
+
Object.keys(schema.modules).some(
|
|
233
|
+
(moduleName) => schema.modules[moduleName].type === 'Component',
|
|
234
|
+
)
|
|
235
|
+
) {
|
|
236
|
+
const componentGenerators = [
|
|
237
|
+
generatorPropsH,
|
|
238
|
+
generatorPropsCPP,
|
|
239
|
+
generatorShadowNodeH,
|
|
240
|
+
generatorShadowNodeCPP,
|
|
241
|
+
generatorComponentDescriptorH,
|
|
242
|
+
generatorEventEmitterH,
|
|
243
|
+
];
|
|
244
|
+
|
|
245
|
+
componentGenerators.forEach((generator) => {
|
|
246
|
+
const generated: Map<string, string> = generator(
|
|
247
|
+
libraryName,
|
|
248
|
+
schema,
|
|
249
|
+
moduleSpecName,
|
|
250
|
+
);
|
|
251
|
+
normalizeFileMap(generated, componentOutputdir, generatedFiles);
|
|
252
|
+
});
|
|
253
|
+
}
|
|
246
254
|
|
|
247
255
|
if (test === true) {
|
|
248
256
|
return checkFilesForChanges(generatedFiles, outputDirectory);
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
NativeModuleBaseTypeAnnotation,
|
|
11
|
+
NativeModuleObjectTypeAnnotation,
|
|
12
|
+
NamedShape,
|
|
13
|
+
Nullable,
|
|
14
|
+
} from 'react-native-tscodegen';
|
|
15
|
+
import {AliasMap, getAliasCppName} from './AliasManaging';
|
|
16
|
+
import {translateField} from './ObjectTypes';
|
|
17
|
+
|
|
18
|
+
function translateObjectBody(
|
|
19
|
+
type: NativeModuleObjectTypeAnnotation,
|
|
20
|
+
aliases: AliasMap,
|
|
21
|
+
baseAliasName: string,
|
|
22
|
+
prefix: string,
|
|
23
|
+
) {
|
|
24
|
+
return type.properties
|
|
25
|
+
.map((prop: NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>) => {
|
|
26
|
+
let propType = prop.typeAnnotation;
|
|
27
|
+
if (prop.optional && propType.type !== 'NullableTypeAnnotation') {
|
|
28
|
+
propType = {type: 'NullableTypeAnnotation', typeAnnotation: propType};
|
|
29
|
+
}
|
|
30
|
+
const first = `${prefix}REACT_FIELD(${prop.name})`;
|
|
31
|
+
const second = `${prefix}${translateField(
|
|
32
|
+
propType,
|
|
33
|
+
aliases,
|
|
34
|
+
`${baseAliasName}_${prop.name}`,
|
|
35
|
+
)} ${prop.name};`;
|
|
36
|
+
return `${first}\n${second}`;
|
|
37
|
+
})
|
|
38
|
+
.join('\n');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function createAliasMap(nativeModuleAliases: {
|
|
42
|
+
[name: string]: NativeModuleObjectTypeAnnotation;
|
|
43
|
+
}): AliasMap {
|
|
44
|
+
const aliases: AliasMap = {types: {}, jobs: Object.keys(nativeModuleAliases)};
|
|
45
|
+
for (const aliasName of aliases.jobs) {
|
|
46
|
+
aliases.types[aliasName] = nativeModuleAliases[aliasName];
|
|
47
|
+
}
|
|
48
|
+
return aliases;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface AliasCodeMap {
|
|
52
|
+
[name: string]: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function generateSingleAlias(
|
|
56
|
+
aliases: AliasMap,
|
|
57
|
+
aliasName: string,
|
|
58
|
+
aliasCode: AliasCodeMap,
|
|
59
|
+
): void {
|
|
60
|
+
const aliasType = <NativeModuleObjectTypeAnnotation>aliases.types[aliasName];
|
|
61
|
+
aliasCode[aliasName] = `
|
|
62
|
+
REACT_STRUCT(${getAliasCppName(aliasName)})
|
|
63
|
+
struct ${getAliasCppName(aliasName)} {
|
|
64
|
+
${translateObjectBody(aliasType, aliases, aliasName, ' ')}
|
|
65
|
+
};
|
|
66
|
+
`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function generateNestedAliasesInCorrectOrder(
|
|
70
|
+
aliases: AliasMap,
|
|
71
|
+
aliasCode: AliasCodeMap,
|
|
72
|
+
aliasOrder: string[],
|
|
73
|
+
): void {
|
|
74
|
+
// retrieve and clean all ungenerated aliases
|
|
75
|
+
const jobs = aliases.jobs;
|
|
76
|
+
aliases.jobs = [];
|
|
77
|
+
|
|
78
|
+
// generate each one in its found order
|
|
79
|
+
for (const aliasName of jobs) {
|
|
80
|
+
// generate a new struct and all fields will be examined
|
|
81
|
+
// new anonymous objects could be found
|
|
82
|
+
// they will be stored in aliases.jobs
|
|
83
|
+
generateSingleAlias(aliases, aliasName, aliasCode);
|
|
84
|
+
// nested C++ structs must be put before the current C++ struct
|
|
85
|
+
// as they will be used in the current C++ struct
|
|
86
|
+
// the order will be perfectly and easily ensured by doing this recursively
|
|
87
|
+
generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder);
|
|
88
|
+
// all referenced C++ structs are generated
|
|
89
|
+
// put the current one following them
|
|
90
|
+
aliasOrder.push(aliasName);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function generateAliases(aliases: AliasMap): string {
|
|
95
|
+
const aliasCode: AliasCodeMap = {};
|
|
96
|
+
const aliasOrder: string[] = [];
|
|
97
|
+
generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder);
|
|
98
|
+
|
|
99
|
+
// aliasOrder now has the correct order of C++ struct code
|
|
100
|
+
let traversedAliasedStructs = '';
|
|
101
|
+
for (const aliasName of aliasOrder) {
|
|
102
|
+
traversedAliasedStructs = `${traversedAliasedStructs}${aliasCode[aliasName]}`;
|
|
103
|
+
}
|
|
104
|
+
return traversedAliasedStructs;
|
|
105
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
import {NativeModuleObjectTypeAnnotation} from 'react-native-tscodegen';
|
|
10
|
+
|
|
11
|
+
let preferredModuleName: string = '';
|
|
12
|
+
|
|
13
|
+
export function setPreferredModuleName(moduleName: string): void {
|
|
14
|
+
preferredModuleName = moduleName;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function getAliasCppName(typeName: string): string {
|
|
18
|
+
return `${preferredModuleName}Spec_${typeName}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface AliasMap {
|
|
22
|
+
types: {[name: string]: NativeModuleObjectTypeAnnotation | undefined};
|
|
23
|
+
jobs: string[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const ExtendedObjectKey = '$RNW-TURBOMODULE-ALIAS';
|
|
27
|
+
interface ExtendedObject extends NativeModuleObjectTypeAnnotation {
|
|
28
|
+
'$RNW-TURBOMODULE-ALIAS'?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function recordAnonymouseAlias(
|
|
32
|
+
aliases: AliasMap,
|
|
33
|
+
baseAliasName: string,
|
|
34
|
+
extended: ExtendedObject,
|
|
35
|
+
): string {
|
|
36
|
+
extended[ExtendedObjectKey] = baseAliasName;
|
|
37
|
+
aliases.types[baseAliasName] = extended;
|
|
38
|
+
aliases.jobs.push(baseAliasName);
|
|
39
|
+
return baseAliasName;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function getAnonymousAliasCppName(
|
|
43
|
+
aliases: AliasMap,
|
|
44
|
+
baseAliasName: string,
|
|
45
|
+
objectType: NativeModuleObjectTypeAnnotation,
|
|
46
|
+
): string {
|
|
47
|
+
// someone found an anonymous object literal type
|
|
48
|
+
// if the ExtendedObjectKey flag has been set
|
|
49
|
+
// then it is a known one
|
|
50
|
+
// this happens because method signatures are generate twice in spec and error messages
|
|
51
|
+
const extended = <ExtendedObject>objectType;
|
|
52
|
+
const key = extended[ExtendedObjectKey];
|
|
53
|
+
if (key !== undefined) {
|
|
54
|
+
return getAliasCppName(key);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// if the ExtendedObjectKey flag has not been set
|
|
58
|
+
// it means it is a unknown one
|
|
59
|
+
// associate the name with this object literal type and return
|
|
60
|
+
if (aliases.types[baseAliasName] === undefined) {
|
|
61
|
+
return getAliasCppName(
|
|
62
|
+
recordAnonymouseAlias(aliases, baseAliasName, extended),
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// sometimes names could be anonymous
|
|
67
|
+
let index = 2;
|
|
68
|
+
while (aliases.types[`${baseAliasName}${index}`] !== undefined) {
|
|
69
|
+
index++;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return getAliasCppName(
|
|
73
|
+
recordAnonymouseAlias(aliases, `${baseAliasName}${index}`, extended),
|
|
74
|
+
);
|
|
75
|
+
}
|
|
@@ -6,18 +6,11 @@
|
|
|
6
6
|
|
|
7
7
|
'use strict';
|
|
8
8
|
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
} from '
|
|
14
|
-
import {
|
|
15
|
-
getAliasCppName,
|
|
16
|
-
setPreferredModuleName,
|
|
17
|
-
translateObjectBody,
|
|
18
|
-
} from './ObjectTypes';
|
|
19
|
-
import {translateArgs, translateSpecArgs} from './ParamTypes';
|
|
20
|
-
import {translateImplReturnType, translateSpecReturnType} from './ReturnTypes';
|
|
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';
|
|
21
14
|
|
|
22
15
|
type FilesOutput = Map<string, string>;
|
|
23
16
|
|
|
@@ -37,109 +30,19 @@ const moduleTemplate = `
|
|
|
37
30
|
namespace ::_NAMESPACE_:: {
|
|
38
31
|
::_MODULE_ALIASED_STRUCTS_::
|
|
39
32
|
struct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec {
|
|
40
|
-
|
|
41
|
-
::_MODULE_PROPERTIES_TUPLE_::
|
|
42
|
-
};
|
|
33
|
+
::_MODULE_MEMBERS_TUPLES_::
|
|
43
34
|
|
|
44
35
|
template <class TModule>
|
|
45
36
|
static constexpr void ValidateModule() noexcept {
|
|
46
|
-
|
|
37
|
+
::_MODULE_MEMBERS_CHECKS_::
|
|
47
38
|
|
|
48
|
-
::
|
|
39
|
+
::_MODULE_MEMBERS_ERRORS_::
|
|
49
40
|
}
|
|
50
41
|
};
|
|
51
42
|
|
|
52
43
|
} // namespace ::_NAMESPACE_::
|
|
53
44
|
`;
|
|
54
45
|
|
|
55
|
-
function isMethodSync(funcType: NativeModuleFunctionTypeAnnotation) {
|
|
56
|
-
return (
|
|
57
|
-
funcType.returnTypeAnnotation.type !== 'VoidTypeAnnotation' &&
|
|
58
|
-
funcType.returnTypeAnnotation.type !== 'PromiseTypeAnnotation'
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function isMethodReturnPromise(funcType: NativeModuleFunctionTypeAnnotation) {
|
|
63
|
-
return funcType.returnTypeAnnotation.type === 'PromiseTypeAnnotation';
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function getPossibleMethodSignatures(
|
|
67
|
-
prop: NativeModulePropertyShape,
|
|
68
|
-
funcType: NativeModuleFunctionTypeAnnotation,
|
|
69
|
-
): string[] {
|
|
70
|
-
const args = translateArgs(funcType.params);
|
|
71
|
-
if (isMethodReturnPromise(funcType)) {
|
|
72
|
-
// Sadly, currently, the schema doesn't currently provide us information on the type of the promise.
|
|
73
|
-
args.push('React::ReactPromise<React::JSValue> &&result');
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// TODO: be much more exhastive on the possible method signatures that can be used..
|
|
77
|
-
const sig = `REACT_${isMethodSync(funcType) ? 'SYNC_' : ''}METHOD(${
|
|
78
|
-
prop.name
|
|
79
|
-
}) ${translateImplReturnType(funcType.returnTypeAnnotation)} ${
|
|
80
|
-
prop.name
|
|
81
|
-
}(${args.join(', ')}) noexcept { /* implementation */ }}`;
|
|
82
|
-
|
|
83
|
-
const staticsig = `REACT_${isMethodSync(funcType) ? 'SYNC_' : ''}METHOD(${
|
|
84
|
-
prop.name
|
|
85
|
-
}) static ${translateImplReturnType(funcType.returnTypeAnnotation)} ${
|
|
86
|
-
prop.name
|
|
87
|
-
}(${args.join(', ')}) noexcept { /* implementation */ }}`;
|
|
88
|
-
|
|
89
|
-
return [sig, staticsig];
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function translatePossibleMethodSignatures(
|
|
93
|
-
prop: NativeModulePropertyShape,
|
|
94
|
-
funcType: NativeModuleFunctionTypeAnnotation,
|
|
95
|
-
): string {
|
|
96
|
-
return getPossibleMethodSignatures(prop, funcType)
|
|
97
|
-
.map(sig => `" ${sig}\\n"`)
|
|
98
|
-
.join('\n ');
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function renderProperties(
|
|
102
|
-
properties: ReadonlyArray<NativeModulePropertyShape>,
|
|
103
|
-
tuple: boolean,
|
|
104
|
-
): string {
|
|
105
|
-
// We skip the constants for now, since we dont have Spec file validation of them.
|
|
106
|
-
return properties
|
|
107
|
-
.filter(prop => prop.name !== 'getConstants')
|
|
108
|
-
.map((prop, index) => {
|
|
109
|
-
// TODO: prop.optional === true
|
|
110
|
-
// TODO: prop.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
111
|
-
const funcType =
|
|
112
|
-
prop.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
113
|
-
? prop.typeAnnotation.typeAnnotation
|
|
114
|
-
: prop.typeAnnotation;
|
|
115
|
-
|
|
116
|
-
const traversedArgs = translateSpecArgs(funcType.params);
|
|
117
|
-
|
|
118
|
-
const translatedReturnParam = translateSpecReturnType(
|
|
119
|
-
funcType.returnTypeAnnotation,
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
if (isMethodReturnPromise(funcType)) {
|
|
123
|
-
// Sadly, currently, the schema doesn't currently provide us information on the type of the promise.
|
|
124
|
-
traversedArgs.push('Promise<React::JSValue>');
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (tuple) {
|
|
128
|
-
return ` ${
|
|
129
|
-
isMethodSync(funcType) ? 'Sync' : ''
|
|
130
|
-
}Method<${translatedReturnParam}(${traversedArgs.join(
|
|
131
|
-
', ',
|
|
132
|
-
)}) noexcept>{${index}, L"${prop.name}"},`;
|
|
133
|
-
} else {
|
|
134
|
-
return ` REACT_SHOW_METHOD_SPEC_ERRORS(
|
|
135
|
-
${index},
|
|
136
|
-
"${prop.name}",
|
|
137
|
-
${translatePossibleMethodSignatures(prop, funcType)});`;
|
|
138
|
-
}
|
|
139
|
-
})
|
|
140
|
-
.join('\n');
|
|
141
|
-
}
|
|
142
|
-
|
|
143
46
|
export function createNM2Generator({namespace}: {namespace: string}) {
|
|
144
47
|
return (
|
|
145
48
|
_libraryName: string,
|
|
@@ -161,30 +64,43 @@ export function createNM2Generator({namespace}: {namespace: string}) {
|
|
|
161
64
|
if (nativeModule.type === 'NativeModule') {
|
|
162
65
|
console.log(`Generating Native${preferredModuleName}Spec.g.h`);
|
|
163
66
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
|
|
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}`;
|
|
173
92
|
}
|
|
174
93
|
|
|
175
|
-
|
|
176
|
-
const
|
|
177
|
-
const traversedPropertyTuples = renderProperties(properties, true);
|
|
94
|
+
// generate code for structs
|
|
95
|
+
const traversedAliasedStructs = generateAliases(aliases);
|
|
178
96
|
|
|
179
97
|
files.set(
|
|
180
98
|
`Native${preferredModuleName}Spec.g.h`,
|
|
181
99
|
moduleTemplate
|
|
182
100
|
.replace(/::_MODULE_ALIASED_STRUCTS_::/g, traversedAliasedStructs)
|
|
183
|
-
.replace(/::
|
|
184
|
-
.replace(
|
|
185
|
-
|
|
186
|
-
traversedProperties,
|
|
187
|
-
)
|
|
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)
|
|
188
104
|
.replace(/::_MODULE_NAME_::/g, preferredModuleName)
|
|
189
105
|
.replace(/::_NAMESPACE_::/g, namespace),
|
|
190
106
|
);
|
|
@@ -6,25 +6,17 @@
|
|
|
6
6
|
|
|
7
7
|
'use strict';
|
|
8
8
|
|
|
9
|
+
import {NativeModuleBaseTypeAnnotation, Nullable} from 'react-native-tscodegen';
|
|
9
10
|
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
} from 'react-native-tscodegen';
|
|
11
|
+
AliasMap,
|
|
12
|
+
getAliasCppName,
|
|
13
|
+
getAnonymousAliasCppName,
|
|
14
|
+
} from './AliasManaging';
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
export function setPreferredModuleName(moduleName: string): void {
|
|
19
|
-
preferredModuleName = moduleName;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function getAliasCppName(typeName: string): string {
|
|
23
|
-
return `${preferredModuleName}Spec_${typeName}`;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function translateField(
|
|
16
|
+
export function translateField(
|
|
27
17
|
type: Nullable<NativeModuleBaseTypeAnnotation>,
|
|
18
|
+
aliases: AliasMap,
|
|
19
|
+
baseAliasName: string,
|
|
28
20
|
): string {
|
|
29
21
|
// avoid: Property 'type' does not exist on type 'never'
|
|
30
22
|
const returnType = type.type;
|
|
@@ -40,13 +32,19 @@ function translateField(
|
|
|
40
32
|
case 'BooleanTypeAnnotation':
|
|
41
33
|
return 'bool';
|
|
42
34
|
case 'ArrayTypeAnnotation':
|
|
43
|
-
|
|
44
|
-
|
|
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
|
+
}
|
|
45
44
|
case 'GenericObjectTypeAnnotation':
|
|
46
|
-
return 'React::
|
|
45
|
+
return '::React::JSValue';
|
|
47
46
|
case 'ObjectTypeAnnotation':
|
|
48
|
-
|
|
49
|
-
return 'React::JSValueObject';
|
|
47
|
+
return getAnonymousAliasCppName(aliases, baseAliasName, type);
|
|
50
48
|
case 'ReservedTypeAnnotation': {
|
|
51
49
|
// avoid: Property 'name' does not exist on type 'never'
|
|
52
50
|
const name = type.name;
|
|
@@ -61,25 +59,12 @@ function translateField(
|
|
|
61
59
|
case 'TypeAliasTypeAnnotation':
|
|
62
60
|
return getAliasCppName(type.name);
|
|
63
61
|
case 'NullableTypeAnnotation':
|
|
64
|
-
return `std::optional<${translateField(
|
|
62
|
+
return `std::optional<${translateField(
|
|
63
|
+
type.typeAnnotation,
|
|
64
|
+
aliases,
|
|
65
|
+
baseAliasName,
|
|
66
|
+
)}>`;
|
|
65
67
|
default:
|
|
66
68
|
throw new Error(`Unhandled type in translateReturnType: ${returnType}`);
|
|
67
69
|
}
|
|
68
70
|
}
|
|
69
|
-
|
|
70
|
-
export function translateObjectBody(
|
|
71
|
-
type: NativeModuleObjectTypeAnnotation,
|
|
72
|
-
prefix: string,
|
|
73
|
-
) {
|
|
74
|
-
return type.properties
|
|
75
|
-
.map((prop: NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>) => {
|
|
76
|
-
let propType = prop.typeAnnotation;
|
|
77
|
-
if (prop.optional && propType.type !== 'NullableTypeAnnotation') {
|
|
78
|
-
propType = {type: 'NullableTypeAnnotation', typeAnnotation: propType};
|
|
79
|
-
}
|
|
80
|
-
const first = `${prefix}REACT_FIELD(${prop.name})`;
|
|
81
|
-
const second = `${prefix}${translateField(propType)} ${prop.name};`;
|
|
82
|
-
return `${first}\n${second}`;
|
|
83
|
-
})
|
|
84
|
-
.join('\n');
|
|
85
|
-
}
|