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