@react-native-windows/codegen 0.0.0-canary.6 → 0.0.0-canary.60
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 +530 -9
- package/README.md +1 -1
- package/bin.js +0 -0
- package/lib-commonjs/Cli.d.ts +7 -0
- package/lib-commonjs/Cli.js +74 -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 +9 -0
- package/lib-commonjs/generators/ObjectTypes.js +73 -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 +135 -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 +29 -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 +75 -0
- package/lib-commonjs/generators/ValidateMethods.js.map +1 -0
- package/lib-commonjs/index.d.ts +39 -0
- package/lib-commonjs/index.js +220 -0
- package/lib-commonjs/index.js.map +1 -0
- package/package.json +37 -20
- package/src/Cli.ts +37 -192
- package/src/generators/AliasGen.ts +105 -0
- package/src/generators/AliasManaging.ts +75 -0
- package/src/generators/GenerateNM2.ts +65 -291
- package/src/generators/GenerateTypeScript.ts +247 -0
- package/src/generators/ObjectTypes.ts +110 -0
- package/src/generators/ParamTypes.ts +262 -0
- package/src/generators/ReturnTypes.ts +55 -0
- package/src/generators/ValidateConstants.ts +50 -0
- package/src/generators/ValidateMethods.ts +149 -0
- package/src/index.ts +384 -0
- package/.eslintrc.js +0 -4
- package/.vscode/launch.json +0 -23
- package/CHANGELOG.json +0 -506
- package/jest.config.js +0 -1
- package/tsconfig.json +0 -5
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
* @format
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
NativeModuleEnumDeclaration,
|
|
11
|
+
NativeModuleBaseTypeAnnotation,
|
|
12
|
+
NativeModuleUnionTypeAnnotation,
|
|
13
|
+
Nullable,
|
|
14
|
+
} from '@react-native/codegen/lib/CodegenSchema';
|
|
15
|
+
import {
|
|
16
|
+
AliasMap,
|
|
17
|
+
getAliasCppName,
|
|
18
|
+
getAnonymousAliasCppName,
|
|
19
|
+
} from './AliasManaging';
|
|
20
|
+
|
|
21
|
+
function translateUnionReturnType(
|
|
22
|
+
type: NativeModuleEnumDeclaration | NativeModuleUnionTypeAnnotation,
|
|
23
|
+
): string {
|
|
24
|
+
const memberType = type.memberType;
|
|
25
|
+
switch (type.memberType) {
|
|
26
|
+
case 'StringTypeAnnotation':
|
|
27
|
+
return 'std::string';
|
|
28
|
+
case 'NumberTypeAnnotation':
|
|
29
|
+
return 'double';
|
|
30
|
+
case 'ObjectTypeAnnotation':
|
|
31
|
+
return '::React::JSValue';
|
|
32
|
+
default:
|
|
33
|
+
throw new Error(
|
|
34
|
+
`Unknown enum/union member type in translateReturnType: ${memberType}`,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function translateFieldOrReturnType(
|
|
40
|
+
type: Nullable<NativeModuleBaseTypeAnnotation>,
|
|
41
|
+
aliases: AliasMap,
|
|
42
|
+
baseAliasName: string,
|
|
43
|
+
callerName: 'translateField' | 'translateReturnType',
|
|
44
|
+
): string {
|
|
45
|
+
// avoid: Property 'type' does not exist on type 'never'
|
|
46
|
+
const returnType = type.type;
|
|
47
|
+
switch (type.type) {
|
|
48
|
+
case 'StringTypeAnnotation':
|
|
49
|
+
return 'std::string';
|
|
50
|
+
case 'NumberTypeAnnotation':
|
|
51
|
+
case 'FloatTypeAnnotation':
|
|
52
|
+
case 'DoubleTypeAnnotation':
|
|
53
|
+
return 'double';
|
|
54
|
+
case 'Int32TypeAnnotation':
|
|
55
|
+
return 'int';
|
|
56
|
+
case 'BooleanTypeAnnotation':
|
|
57
|
+
return 'bool';
|
|
58
|
+
case 'ArrayTypeAnnotation':
|
|
59
|
+
if (type.elementType) {
|
|
60
|
+
return `std::vector<${translateFieldOrReturnType(
|
|
61
|
+
type.elementType,
|
|
62
|
+
aliases,
|
|
63
|
+
`${baseAliasName}_element`,
|
|
64
|
+
callerName,
|
|
65
|
+
)}>`;
|
|
66
|
+
} else {
|
|
67
|
+
return `::React::JSValueArray`;
|
|
68
|
+
}
|
|
69
|
+
case 'GenericObjectTypeAnnotation':
|
|
70
|
+
return '::React::JSValue';
|
|
71
|
+
case 'ObjectTypeAnnotation':
|
|
72
|
+
return getAnonymousAliasCppName(aliases, baseAliasName, type);
|
|
73
|
+
case 'ReservedTypeAnnotation': {
|
|
74
|
+
// avoid: Property 'name' does not exist on type 'never'
|
|
75
|
+
const name = type.name;
|
|
76
|
+
// (#6597)
|
|
77
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
78
|
+
if (name !== 'RootTag')
|
|
79
|
+
throw new Error(`Unknown reserved function: ${name} in ${callerName}`);
|
|
80
|
+
return 'double';
|
|
81
|
+
}
|
|
82
|
+
case 'TypeAliasTypeAnnotation':
|
|
83
|
+
return getAliasCppName(type.name);
|
|
84
|
+
case 'NullableTypeAnnotation':
|
|
85
|
+
return `std::optional<${translateFieldOrReturnType(
|
|
86
|
+
type.typeAnnotation,
|
|
87
|
+
aliases,
|
|
88
|
+
baseAliasName,
|
|
89
|
+
callerName,
|
|
90
|
+
)}>`;
|
|
91
|
+
case 'EnumDeclaration':
|
|
92
|
+
case 'UnionTypeAnnotation':
|
|
93
|
+
return translateUnionReturnType(type);
|
|
94
|
+
default:
|
|
95
|
+
throw new Error(`Unhandled type in ${callerName}: ${returnType}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function translateField(
|
|
100
|
+
type: Nullable<NativeModuleBaseTypeAnnotation>,
|
|
101
|
+
aliases: AliasMap,
|
|
102
|
+
baseAliasName: string,
|
|
103
|
+
): string {
|
|
104
|
+
return translateFieldOrReturnType(
|
|
105
|
+
type,
|
|
106
|
+
aliases,
|
|
107
|
+
baseAliasName,
|
|
108
|
+
'translateField',
|
|
109
|
+
);
|
|
110
|
+
}
|
|
@@ -0,0 +1,262 @@
|
|
|
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
|
+
NativeModuleEnumDeclaration,
|
|
14
|
+
NativeModuleFunctionTypeAnnotation,
|
|
15
|
+
NativeModuleParamTypeAnnotation,
|
|
16
|
+
NativeModuleUnionTypeAnnotation,
|
|
17
|
+
Nullable,
|
|
18
|
+
} from '@react-native/codegen/lib/CodegenSchema';
|
|
19
|
+
import {
|
|
20
|
+
AliasMap,
|
|
21
|
+
getAliasCppName,
|
|
22
|
+
getAnonymousAliasCppName,
|
|
23
|
+
} from './AliasManaging';
|
|
24
|
+
|
|
25
|
+
type NativeModuleParamShape = NamedShape<
|
|
26
|
+
Nullable<NativeModuleParamTypeAnnotation>
|
|
27
|
+
>;
|
|
28
|
+
|
|
29
|
+
type ParamTarget = 'spec' | 'template' | 'callback-arg' | 'method-arg';
|
|
30
|
+
|
|
31
|
+
function decorateType(type: string, target: ParamTarget): string {
|
|
32
|
+
switch (target) {
|
|
33
|
+
case 'method-arg':
|
|
34
|
+
return `${type} &&`;
|
|
35
|
+
case 'callback-arg':
|
|
36
|
+
return `${type} const &`;
|
|
37
|
+
default:
|
|
38
|
+
return type;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function translateUnionReturnType(
|
|
43
|
+
type: NativeModuleEnumDeclaration | NativeModuleUnionTypeAnnotation,
|
|
44
|
+
target: ParamTarget,
|
|
45
|
+
): string {
|
|
46
|
+
const memberType = type.memberType;
|
|
47
|
+
switch (type.memberType) {
|
|
48
|
+
case 'StringTypeAnnotation':
|
|
49
|
+
return 'std::string';
|
|
50
|
+
case 'NumberTypeAnnotation':
|
|
51
|
+
return 'double';
|
|
52
|
+
case 'ObjectTypeAnnotation':
|
|
53
|
+
return decorateType('::React::JSValue', target);
|
|
54
|
+
default:
|
|
55
|
+
throw new Error(
|
|
56
|
+
`Unknown enum/union member type in translateReturnType: ${memberType}`,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function translateFunction(
|
|
62
|
+
param: NativeModuleFunctionTypeAnnotation,
|
|
63
|
+
aliases: AliasMap,
|
|
64
|
+
baseAliasName: string,
|
|
65
|
+
target: ParamTarget,
|
|
66
|
+
): string {
|
|
67
|
+
// TODO: type.returnTypeAnnotation
|
|
68
|
+
switch (target) {
|
|
69
|
+
case 'spec':
|
|
70
|
+
return `Callback<${param.params
|
|
71
|
+
.map((p: NativeModuleParamShape) =>
|
|
72
|
+
translateSpecFunctionParam(p, aliases, `${baseAliasName}_${p.name}`),
|
|
73
|
+
)
|
|
74
|
+
.join(', ')}>`;
|
|
75
|
+
case 'template':
|
|
76
|
+
return `std::function<void(${param.params
|
|
77
|
+
.map((p: NativeModuleParamShape) =>
|
|
78
|
+
translateCallbackParam(p, aliases, `${baseAliasName}_${p.name}`),
|
|
79
|
+
)
|
|
80
|
+
.join(', ')})>`;
|
|
81
|
+
default:
|
|
82
|
+
return `std::function<void(${param.params
|
|
83
|
+
.map((p: NativeModuleParamShape) =>
|
|
84
|
+
translateCallbackParam(p, aliases, `${baseAliasName}_${p.name}`),
|
|
85
|
+
)
|
|
86
|
+
.join(', ')})> const &`;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function translateArray(
|
|
91
|
+
param: NativeModuleArrayTypeAnnotation<
|
|
92
|
+
Nullable<NativeModuleBaseTypeAnnotation>
|
|
93
|
+
>,
|
|
94
|
+
aliases: AliasMap,
|
|
95
|
+
baseAliasName: string,
|
|
96
|
+
target: ParamTarget,
|
|
97
|
+
): string {
|
|
98
|
+
if (param.elementType) {
|
|
99
|
+
switch (target) {
|
|
100
|
+
case 'spec':
|
|
101
|
+
case 'template':
|
|
102
|
+
return `std::vector<${translateNullableParamType(
|
|
103
|
+
param.elementType,
|
|
104
|
+
aliases,
|
|
105
|
+
`${baseAliasName}_element`,
|
|
106
|
+
'template',
|
|
107
|
+
'template',
|
|
108
|
+
)}>`;
|
|
109
|
+
default:
|
|
110
|
+
return `std::vector<${translateNullableParamType(
|
|
111
|
+
param.elementType,
|
|
112
|
+
aliases,
|
|
113
|
+
`${baseAliasName}_element`,
|
|
114
|
+
'template',
|
|
115
|
+
'template',
|
|
116
|
+
)}> const &`;
|
|
117
|
+
}
|
|
118
|
+
} else {
|
|
119
|
+
return decorateType('::React::JSValueArray', target);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function translateParam(
|
|
124
|
+
param: NativeModuleParamTypeAnnotation,
|
|
125
|
+
aliases: AliasMap,
|
|
126
|
+
baseAliasName: string,
|
|
127
|
+
target: ParamTarget,
|
|
128
|
+
): string {
|
|
129
|
+
// avoid: Property 'type' does not exist on type 'never'
|
|
130
|
+
const paramType = param.type;
|
|
131
|
+
switch (param.type) {
|
|
132
|
+
case 'StringTypeAnnotation':
|
|
133
|
+
return 'std::string';
|
|
134
|
+
case 'NumberTypeAnnotation':
|
|
135
|
+
case 'FloatTypeAnnotation':
|
|
136
|
+
case 'DoubleTypeAnnotation':
|
|
137
|
+
return 'double';
|
|
138
|
+
case 'Int32TypeAnnotation':
|
|
139
|
+
return 'int';
|
|
140
|
+
case 'BooleanTypeAnnotation':
|
|
141
|
+
return 'bool';
|
|
142
|
+
case 'FunctionTypeAnnotation':
|
|
143
|
+
return translateFunction(param, aliases, baseAliasName, target);
|
|
144
|
+
case 'ArrayTypeAnnotation':
|
|
145
|
+
return translateArray(param, aliases, baseAliasName, target);
|
|
146
|
+
case 'GenericObjectTypeAnnotation':
|
|
147
|
+
return decorateType('::React::JSValue', target);
|
|
148
|
+
case 'ObjectTypeAnnotation':
|
|
149
|
+
return decorateType(
|
|
150
|
+
getAnonymousAliasCppName(aliases, baseAliasName, param),
|
|
151
|
+
target,
|
|
152
|
+
);
|
|
153
|
+
case 'ReservedTypeAnnotation': {
|
|
154
|
+
// avoid: Property 'name' does not exist on type 'never'
|
|
155
|
+
const name = param.name;
|
|
156
|
+
// (#6597)
|
|
157
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
158
|
+
if (name !== 'RootTag')
|
|
159
|
+
throw new Error(`Unknown reserved function: ${name} in translateParam`);
|
|
160
|
+
return 'double';
|
|
161
|
+
}
|
|
162
|
+
case 'TypeAliasTypeAnnotation':
|
|
163
|
+
return decorateType(getAliasCppName(param.name), target);
|
|
164
|
+
case 'EnumDeclaration':
|
|
165
|
+
case 'UnionTypeAnnotation':
|
|
166
|
+
return translateUnionReturnType(param, target);
|
|
167
|
+
default:
|
|
168
|
+
throw new Error(`Unhandled type in translateParam: ${paramType}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function translateNullableParamType(
|
|
173
|
+
paramType: Nullable<NativeModuleParamTypeAnnotation>,
|
|
174
|
+
aliases: AliasMap,
|
|
175
|
+
baseAliasName: string,
|
|
176
|
+
nullableTarget: ParamTarget,
|
|
177
|
+
target: ParamTarget,
|
|
178
|
+
): string {
|
|
179
|
+
switch (paramType.type) {
|
|
180
|
+
case 'NullableTypeAnnotation':
|
|
181
|
+
return `std::optional<${translateParam(
|
|
182
|
+
paramType.typeAnnotation,
|
|
183
|
+
aliases,
|
|
184
|
+
baseAliasName,
|
|
185
|
+
nullableTarget,
|
|
186
|
+
)}>`;
|
|
187
|
+
default:
|
|
188
|
+
return translateParam(paramType, aliases, baseAliasName, target);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function translateSpecFunctionParam(
|
|
193
|
+
param: NativeModuleParamShape,
|
|
194
|
+
aliases: AliasMap,
|
|
195
|
+
baseAliasName: string,
|
|
196
|
+
): string {
|
|
197
|
+
return translateNullableParamType(
|
|
198
|
+
param.typeAnnotation,
|
|
199
|
+
aliases,
|
|
200
|
+
baseAliasName,
|
|
201
|
+
'spec',
|
|
202
|
+
'spec',
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function translateCallbackParam(
|
|
207
|
+
param: NativeModuleParamShape,
|
|
208
|
+
aliases: AliasMap,
|
|
209
|
+
baseAliasName: string,
|
|
210
|
+
): string {
|
|
211
|
+
return translateNullableParamType(
|
|
212
|
+
param.typeAnnotation,
|
|
213
|
+
aliases,
|
|
214
|
+
baseAliasName,
|
|
215
|
+
'template',
|
|
216
|
+
'callback-arg',
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function translateFunctionParam(
|
|
221
|
+
param: NativeModuleParamShape,
|
|
222
|
+
aliases: AliasMap,
|
|
223
|
+
baseAliasName: string,
|
|
224
|
+
): string {
|
|
225
|
+
return translateNullableParamType(
|
|
226
|
+
param.typeAnnotation,
|
|
227
|
+
aliases,
|
|
228
|
+
baseAliasName,
|
|
229
|
+
'template',
|
|
230
|
+
'method-arg',
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export function translateSpecArgs(
|
|
235
|
+
params: ReadonlyArray<NativeModuleParamShape>,
|
|
236
|
+
aliases: AliasMap,
|
|
237
|
+
baseAliasName: string,
|
|
238
|
+
) {
|
|
239
|
+
return params.map(param => {
|
|
240
|
+
const translatedParam = translateSpecFunctionParam(
|
|
241
|
+
param,
|
|
242
|
+
aliases,
|
|
243
|
+
`${baseAliasName}_${param.name}`,
|
|
244
|
+
);
|
|
245
|
+
return `${translatedParam}`;
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export function translateArgs(
|
|
250
|
+
params: ReadonlyArray<NativeModuleParamShape>,
|
|
251
|
+
aliases: AliasMap,
|
|
252
|
+
baseAliasName: string,
|
|
253
|
+
) {
|
|
254
|
+
return params.map(param => {
|
|
255
|
+
const translatedParam = translateFunctionParam(
|
|
256
|
+
param,
|
|
257
|
+
aliases,
|
|
258
|
+
`${baseAliasName}_${param.name}`,
|
|
259
|
+
);
|
|
260
|
+
return `${translatedParam} ${param.name}`;
|
|
261
|
+
});
|
|
262
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
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 {translateFieldOrReturnType} from './ObjectTypes';
|
|
15
|
+
|
|
16
|
+
function translateReturnType(
|
|
17
|
+
type: Nullable<NativeModuleReturnTypeAnnotation>,
|
|
18
|
+
aliases: AliasMap,
|
|
19
|
+
baseAliasName: string,
|
|
20
|
+
): string {
|
|
21
|
+
switch (type.type) {
|
|
22
|
+
case 'VoidTypeAnnotation':
|
|
23
|
+
case 'PromiseTypeAnnotation':
|
|
24
|
+
return 'void';
|
|
25
|
+
case 'NullableTypeAnnotation':
|
|
26
|
+
return `std::optional<${translateReturnType(
|
|
27
|
+
type.typeAnnotation,
|
|
28
|
+
aliases,
|
|
29
|
+
baseAliasName,
|
|
30
|
+
)}>`;
|
|
31
|
+
default:
|
|
32
|
+
return translateFieldOrReturnType(
|
|
33
|
+
type,
|
|
34
|
+
aliases,
|
|
35
|
+
baseAliasName,
|
|
36
|
+
'translateReturnType',
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function translateSpecReturnType(
|
|
42
|
+
type: Nullable<NativeModuleReturnTypeAnnotation>,
|
|
43
|
+
aliases: AliasMap,
|
|
44
|
+
baseAliasName: string,
|
|
45
|
+
) {
|
|
46
|
+
return translateReturnType(type, aliases, `${baseAliasName}_returnType`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function translateImplReturnType(
|
|
50
|
+
type: Nullable<NativeModuleReturnTypeAnnotation>,
|
|
51
|
+
aliases: AliasMap,
|
|
52
|
+
baseAliasName: string,
|
|
53
|
+
) {
|
|
54
|
+
return translateReturnType(type, aliases, `${baseAliasName}_returnType`);
|
|
55
|
+
}
|
|
@@ -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.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,149 @@
|
|
|
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/codegen/lib/CodegenSchema';
|
|
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 getPossibleMethodSignatures(
|
|
26
|
+
prop: NativeModulePropertyShape,
|
|
27
|
+
funcType: NativeModuleFunctionTypeAnnotation,
|
|
28
|
+
aliases: AliasMap,
|
|
29
|
+
baseAliasName: string,
|
|
30
|
+
): string[] {
|
|
31
|
+
const args = translateArgs(funcType.params, aliases, baseAliasName);
|
|
32
|
+
if (funcType.returnTypeAnnotation.type === 'PromiseTypeAnnotation') {
|
|
33
|
+
if (funcType.returnTypeAnnotation.elementType) {
|
|
34
|
+
args.push(
|
|
35
|
+
`::React::ReactPromise<${translateImplReturnType(
|
|
36
|
+
funcType.returnTypeAnnotation.elementType,
|
|
37
|
+
aliases,
|
|
38
|
+
baseAliasName,
|
|
39
|
+
)}> &&result`,
|
|
40
|
+
);
|
|
41
|
+
} else {
|
|
42
|
+
args.push('::React::ReactPromise<::React::JSValue> &&result');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// TODO: be much more exhaustive on the possible method signatures that can be used..
|
|
47
|
+
const sig = `REACT_${isMethodSync(funcType) ? 'SYNC_' : ''}METHOD(${
|
|
48
|
+
prop.name
|
|
49
|
+
}) ${translateImplReturnType(
|
|
50
|
+
funcType.returnTypeAnnotation,
|
|
51
|
+
aliases,
|
|
52
|
+
baseAliasName,
|
|
53
|
+
)} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }`;
|
|
54
|
+
|
|
55
|
+
const staticsig = `REACT_${isMethodSync(funcType) ? 'SYNC_' : ''}METHOD(${
|
|
56
|
+
prop.name
|
|
57
|
+
}) static ${translateImplReturnType(
|
|
58
|
+
funcType.returnTypeAnnotation,
|
|
59
|
+
aliases,
|
|
60
|
+
baseAliasName,
|
|
61
|
+
)} ${prop.name}(${args.join(', ')}) noexcept { /* implementation */ }`;
|
|
62
|
+
|
|
63
|
+
return [sig, staticsig];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function translatePossibleMethodSignatures(
|
|
67
|
+
prop: NativeModulePropertyShape,
|
|
68
|
+
funcType: NativeModuleFunctionTypeAnnotation,
|
|
69
|
+
aliases: AliasMap,
|
|
70
|
+
baseAliasName: string,
|
|
71
|
+
): string {
|
|
72
|
+
return getPossibleMethodSignatures(prop, funcType, aliases, baseAliasName)
|
|
73
|
+
.map(sig => `" ${sig}\\n"`)
|
|
74
|
+
.join('\n ');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function renderProperties(
|
|
78
|
+
properties: ReadonlyArray<NativeModulePropertyShape>,
|
|
79
|
+
aliases: AliasMap,
|
|
80
|
+
tuple: boolean,
|
|
81
|
+
): string {
|
|
82
|
+
// TODO: generate code for constants
|
|
83
|
+
return properties
|
|
84
|
+
.filter(prop => prop.name !== 'getConstants')
|
|
85
|
+
.map((prop, index) => {
|
|
86
|
+
// TODO: prop.optional === true
|
|
87
|
+
// TODO: prop.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
88
|
+
const propAliasName = prop.name;
|
|
89
|
+
const funcType =
|
|
90
|
+
prop.typeAnnotation.type === 'NullableTypeAnnotation'
|
|
91
|
+
? prop.typeAnnotation.typeAnnotation
|
|
92
|
+
: prop.typeAnnotation;
|
|
93
|
+
|
|
94
|
+
const traversedArgs = translateSpecArgs(
|
|
95
|
+
funcType.params,
|
|
96
|
+
aliases,
|
|
97
|
+
propAliasName,
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const translatedReturnParam = translateSpecReturnType(
|
|
101
|
+
funcType.returnTypeAnnotation,
|
|
102
|
+
aliases,
|
|
103
|
+
propAliasName,
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
if (funcType.returnTypeAnnotation.type === 'PromiseTypeAnnotation') {
|
|
107
|
+
if (funcType.returnTypeAnnotation.elementType) {
|
|
108
|
+
traversedArgs.push(
|
|
109
|
+
`Promise<${translateSpecReturnType(
|
|
110
|
+
funcType.returnTypeAnnotation.elementType,
|
|
111
|
+
aliases,
|
|
112
|
+
propAliasName,
|
|
113
|
+
)}>`,
|
|
114
|
+
);
|
|
115
|
+
} else {
|
|
116
|
+
traversedArgs.push('Promise<::React::JSValue>');
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (tuple) {
|
|
121
|
+
return ` ${
|
|
122
|
+
isMethodSync(funcType) ? 'Sync' : ''
|
|
123
|
+
}Method<${translatedReturnParam}(${traversedArgs.join(
|
|
124
|
+
', ',
|
|
125
|
+
)}) noexcept>{${index}, L"${prop.name}"},`;
|
|
126
|
+
} else {
|
|
127
|
+
return ` REACT_SHOW_METHOD_SPEC_ERRORS(
|
|
128
|
+
${index},
|
|
129
|
+
"${prop.name}",
|
|
130
|
+
${translatePossibleMethodSignatures(
|
|
131
|
+
prop,
|
|
132
|
+
funcType,
|
|
133
|
+
aliases,
|
|
134
|
+
propAliasName,
|
|
135
|
+
)});`;
|
|
136
|
+
}
|
|
137
|
+
})
|
|
138
|
+
.join('\n');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function generateValidateMethods(
|
|
142
|
+
nativeModule: NativeModuleSchema,
|
|
143
|
+
aliases: AliasMap,
|
|
144
|
+
): [string, string] {
|
|
145
|
+
const properties = nativeModule.spec.properties;
|
|
146
|
+
const traversedProperties = renderProperties(properties, aliases, false);
|
|
147
|
+
const traversedPropertyTuples = renderProperties(properties, aliases, true);
|
|
148
|
+
return [traversedPropertyTuples, traversedProperties];
|
|
149
|
+
}
|